Error Function

I have a TF1 function with 9 free parameter I know that you can
obtain the function value in one point simply asking

func->Eval(x) where x is the point where I want to know the function value
is there anyway to obtain the error on that function in that point.

I know that I can do that manually using the covariance matrix and the derivative of the function
respect to my parameters but I have some problem and since my function has 9 parameters I would be very happy if there is a simpler way

Hello Valentina,

you need to get the TFitResult class from your fit (using option “S”)

TFitResultPtr r = h1->Fit( myFunction, "S");

and then do as following (n is the number of points you want to evaluate the error in the function. If it is just one point use n =1.

double x[n]= {value1, value2,....};
double ci[n];
double cl = 0.683;  // for 1 sigma error
r->ConfidenceIntervals(n,1,1,ci,  cl);

c1[i] will contain afterwards the symmetric error for the i-point.
See also … eIntervals

Best Regards


Dear Lorenzo, thanks a lot for you suggestions.
But I don’t use to fit my data
I have my FCN function here my piece of code:

TFitter *gMinuit = new TFitter(9);

//Background from the fit in the entire mass range

gMinuit->SetParameter(4.,“gv”, 1.21355e-01,1e-03,0.02,0.22);

gMinuit->SetParameter(6.,“beta”,20.44,0.1,10.,50.); //Very good result
gMinuit->SetParameter(7.,“alpha”, 4.,0.1,1.,10.); //Very good result
gMinuit->SetParameter(8.,“c2”,10.,0.1,1.,1000.);//Very good result

gMinuit->ExecuteCommand(“MIGRAD”, 0,0);
gMinuit->ExecuteCommand(“HESSE”, 0,0);
gMinuit->ExecuteCommand(“MINOS”, 0,0);

Is still possible in this way using the TFitResultPtr?
Thanks again

Hi Valentina,

in principle with TFitter you should be able to use TFitter::GetConfidenceInterval.
However, fitting as you are doing you don’t have a model function defined and specified in the TFitter class,
so it will not work. You need to provide first the model function to the fitter (not the fcn), by calling



Then, which is the proper way of fitting a histogram using using a custom fitting objective function?
I have tried to find one post with a working example unsuccessfully. Does anybody have

I tried:
TFitter *minuit = new TFitter(1);
minuit->SetFCN(myFCN); //(1 parameter)
minuit->SetParameter(0, “alpha”, 0.10, 0.01, 0.01, 1.0);
minuit->ExecuteCommand(“MIGRAD”, 0,0);
minuit->ExecuteCommand(“MINOS”, 0,0);

but is not working. Thanks in advance for any help.


for fitting a user objective function (e.g a user defined likelihood or least square ) you have two possibility:

  1. create your objective function and use the Minimizer class to minimize this function. One example is
    this tutorial, … ion.C.html

  2. Use TH1::Fit with the option “U”. In this case you need to create the objective function as a specific interface required by the TVirtualFitter and you need to call TVirtualFitter::SetFCN before fitting.
    I attach an example for this case

Best Regards

exampleFitUser.C (2.36 KB)

1 Like


Thanks a lot for your answer. I tried to implement my own chi-square
fitting using your example, and compare it with using root’s chi-square output.
However it is still not working.
I made a reduced version of my code to reproduce the problem.
Hope you can have a look at it and help me understand why is not

TestFitsChiSquare.C (3.57 KB)
Fit_Histos.root (4.29 KB)


your macro does not work in CINT but works finr when you compile with ACLIC (after fixing for some missing includes).
The problem with CINT is that you cannot use TF1::EvalPar(x,p) for an interpreted function if you don’t call before TF1::InitArgs(x,p).
You can also call TF1::operator()(x,p) which calls InitArgs inside, thus replace in your chi2 function

double theory = func->Eval(x,p);


double theory = (*func)(x,p);

By the way, your chi2 function is exactly the same used inside ROOT and you will get identical results
Best Regards


Thanks a lot for your help. Now the macro
runs with ACLiC. However the chi2 is really
bad, and I have no clue what I am doing wrong.
My input is two histograms, that are scaled inside
my TF1 and the addition of these two histos is
fitted to a third histogram.
Also I noticed that the parameter limits are not
respected, with a printout I noticed that
even if I restrict a parameter from 0.1 to 1.0
the TF1 is called with a parameter 0. Is there
any other way to restrict the parameter than parlimits?

Hope you can give me some advice on what I am doing
wrong during the fitting.

Many thanks in advance,
TestFitsChiSquare_2.C (4.97 KB)


Sorry for spamming. I found my mistake, it is in the definition of
my TF1, now it is working. Thanks a lot for the help.