# Error Function

Hello,
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
Valentina

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.

Best Regards

Lorenzo

Dear Lorenzo, thanks a lot for you suggestions.
But I donâ€™t use to fit my data
h1->Fit(â€¦)
I have my FCN function here my piece of code:

TFitter *gMinuit = new TFitter(9);
gMinuit->SetFCN(fcn);

//Background from the fit in the entire mass range
gMinuit->SetParameter(0.,â€śslope1â€ť,-2.30544e+03,1e-01,-100000.,10000.);
gMinuit->SetParameter(1.,â€śslope2â€ť,1.17373e+03,0.001,-1000000.,80000.);
gMinuit->SetParameter(2.,â€śslope3â€ť,-1.29492e+02,0.001,-300.,50.);

gMinuit->SetParameter(3.,â€śmvâ€ť,4.24318e+00,1e-03,4.2,4.3);
gMinuit->SetParameter(4.,â€śgvâ€ť, 1.21355e-01,1e-03,0.02,0.22);
gMinuit->SetParameter(5.,â€śNsâ€ť,9.45499e+00,1e-03,0.001,100.);

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(â€śHESSEâ€ť, 0,0);
gMinuit->ExecuteCommand(â€śMINOSâ€ť, 0,0);

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

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
TFitter::SetUserFunc

Lorenzo

Hi,

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
one?

I tried:
TFitter *minuit = new TFitter(1);
minuit->SetUserFunc(myTF1);
minuit->SetFCN(myFCN); //(1 parameter)
minuit->SetParameter(0, â€śalphaâ€ť, 0.10, 0.01, 0.01, 1.0);
minuit->ExecuteCommand(â€śMINOSâ€ť, 0,0);

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

Hi,

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, root.cern.ch/root/html/tutorials â€¦ 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

Lorenzo
exampleFitUser.C (2.36 KB)

1 Like

Hi,

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
working.

Cheers,
Hermes
TestFitsChiSquare.C (3.57 KB)
Fit_Histos.root (4.29 KB)

Hi,

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);``

with

``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
Lorenzo

Hi,

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.

Hermes
TestFitsChiSquare_2.C (4.97 KB)

Hi,

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.

Cheers,
Hermes