Using minuit to minimize chi2 and estimate errors

I am using Minuit with pyroot. I have learnt how to define a function in python and pass it to the fitting method, using ROOT::Fit::Fitter for example. Now I would like to use all the functionality provided by Minuit to minimize chi2 and estimate the best fit and error bars. I have found all the information about the TMinuit class in

Now I wondering if anyone has a tutorial or a working example I can use as a template for doing such analysis, plotting contours etc. I have googled it and surprisingly I haven’t found much material about minuit in C++.

There is the $(ROOTSYS)/tutorials/fit/lfit.C. Otherwise maybe @moneta could have more information…


This example is for the TMinuit class. This is a low level class and we recommend to use either the ROOT::Math::Fitter class or, if you want something at lower level, the ROOT::Math::Minimiser.
Examples are also in tutorials in $(ROOTSYS)/tutorials/fit/ but in C++, it should be easy to convert them Python.
Direct links are:



I have already performed the fit using ROOT::MATH::Fitter class in python, and it works.

I think the minimizer class is something I may use. I would need specifically to draw contours of the chi2 for arbitrary values of the chi2 (Contours from minos for arbitrary sigma level), and you mentioned that I could use ROOT::Math::MinimizerOptions::SetDefaultErrorDef( value). Now it is not clear to me if I should use then the minimizer class to draw the contours or the TMinuit class. (Do I understand correctly that the classes T<Something> are considered low level?)

@bellenot I cannot find this example in my version 6.16.00

For the contour you can use

as following

TGraph g(n); 
minimizer->Contour(ipar, jpar, n, g.GetX(), g.GetY() ); 

@moneta I have found some sample code Numerical Minimization with ROOT::Math::Functor and I am able to run it. If I change min = ROOT.Math.Factory.CreateMinimizer("GSLMultiMin", "BFGS") to min = ROOT.Math.Factory.CreateMinimizer("Minuit2", "") I get an error:

ReferenceError: attempt to access a null-pointer

but min = ROOT.Math.Factory.CreateMinimizer("Minuit", "") works. Is it obvious? any explanation?

EDIT: I did not install minuit2 , that’s it

@moneta I have written the following code:

minimum.Contour(0,1,npoints,x0, x1)
minimum.Contour(0,1,npoints,x2, x3)    

but the resulting contours lay exactly on top of each other, so it looks like SetDefaultErrorDef is not governing this. any ideas?

I have changed ROOT.Math.MinimizerOptions.SetDefaultErrorDef(chi2_contour2) with minimum.SetErrorDef(chi2_contour2) and it produces two distinct contours (using either chi2_contour2 or chi2_contour3 ). It is not clear to me if SetErrorDef takes as input the same quantity of SetDefaultErrorDef, as I do not find any documentation, I suppose I should look into the source code then. In particular it is not clear if the input should be the value of the chi2 or the probability or some multiplicative factor of sigma

EDIT: from section 3.3.8 and table 7.1 I would understand that ERRDEF is actually the UP parameter that is the difference between the chi2 I am interested in and the minimum chi2

is this correct?

I found this:

According to this, the ErrorDef I guess you need

ErrorDef = 0.5 * N^2

for likelihood fits, and the same without 0.5 for chi^2 fits, and N is the is the number of standard deviations (sigmas) you want to plot the contour at.