# Chisquare calculations

Hi,

I have problem with Chi2 calculations for histograms with functions. I don’t know yet whether problem is related to code or to pfilosophy of root, so I will present the idea of my code.

I have a fiiting code which is reading a fit function (background and signal) and all fit parameters from text file. Then this function is fit to histogram and can be writen to the same file, that this parameters can be reused for nex fit (there is many different histograms, if some needs tunning, then I manually change proper entries and rerun code).

I got confused because when I save parameters of the fit function to the text file and then I rerun the code, the calculated Chisquare value is much different from what was before. Here is the example:

1. I am reading the fit function and parameters from the file (ggaus function us my own function defined as sum of two gaussians with the same mean value, it is the same parameters order like gaus(0)+gaus(2) but param 4 is ignored and 2 used instead):

``` f_h_PtYcm_LambdaDiff_X05_Y09 : ggaus+pol3(6)+expo(10) Ndim= 1, Npar= 12, Noper= 29 fExpr =  action = 140 action param = 0 fExpr = 0.5 action = 141 action param = 0 fExpr = x action = 144 action param = 0 fExpr =  action = 140 action param = 1 fExpr = - action = 2 action param = 0 fExpr =  action = 140 action param = 2 fExpr = / action = 4 action param = 0 fExpr = sq action = 21 action param = 0 fExpr = * action = 3 action param = 0 fExpr = - action = 44 action param = 0 fExpr = Double_t TMath::Exp(Double_t x) action = 145 action param = 1 fExpr = * action = 3 action param = 0 fExpr =  action = 140 action param = 3 fExpr = 0.5 action = 141 action param = 0 fExpr = x action = 144 action param = 0 fExpr =  action = 140 action param = 1 fExpr = - action = 2 action param = 0 fExpr =  action = 140 action param = 5 fExpr = / action = 4 action param = 0 fExpr = sq action = 21 action param = 0 fExpr = * action = 3 action param = 0 fExpr = - action = 44 action param = 0 fExpr = Double_t TMath::Exp(Double_t x) action = 145 action param = 1001 fExpr = * action = 3 action param = 0 fExpr = + action = 1 action param = 0 fExpr = pol3(6) action = 130 action param = 307 fExpr = + action = 1 action param = 0 fExpr = expo(10) action = 100 action param = 10 fExpr = + action = 1 action param = 0 Optimized expression fExpr =  action = 146 action param = 0 fExpr = 0.5 action = 146 action param = 0 fExpr = x,,- action = 148 action param = 0 fExpr =  action = 146 action param = 2 fExpr = / action = 4 action param = 0 fExpr = sq action = 161 action param = 0 fExpr = * action = 3 action param = 0 fExpr = - action = 44 action param = 0 fExpr = Double_t TMath::Exp(Double_t x) action = 161 action param = 0 fExpr = * action = 3 action param = 0 fExpr =  action = 146 action param = 3 fExpr = 0.5 action = 146 action param = 0 fExpr = x,,- action = 148 action param = 0 fExpr =  action = 146 action param = 5 fExpr = / action = 4 action param = 0 fExpr = sq action = 161 action param = 0 fExpr = * action = 3 action param = 0 fExpr = - action = 44 action param = 0 fExpr = Double_t TMath::Exp(Double_t x) action = 161 action param = 0 fExpr = * action = 3 action param = 0 fExpr = + action = 1 action param = 0 fExpr = pol3(6) action = 130 action param = 307 fExpr = + action = 1 action param = 0 fExpr = expo(10) action = 100 action param = 10 fExpr = + action = 1 action param = 0 Par 0 p0 = 1341.14 Par 1 p1 = 1115.23 Par 2 p2 = 1.99477 Par 3 p3 = 533.022 Par 4 p4 = 1115 Par 5 p5 = 4.99993 Par 6 p6 = -21507.9 Par 7 p7 = 20.7983 Par 8 p8 = 0.015796 Par 9 p9 = -1.5e-05 Par 10 p10 = -28.1038 Par 11 p11 = 0.028563```
Last 12 lines shows parameters readout from text file. let call my fit function fitfun, and histogram hist.

1. Now, I am calling Chisquare function for histogram:

and the result is [quote]767769.823388[/quote]

1. Then I run fitting code and read out Chi2 value from function:

```hist->Fit(fitfun, "", "", range_min, range_max); fitfun->GetChisquare();```
where range_min and range_max are the same like for function (also read out from text file). Result of chi2 is following:

1. Now I try again to get Chi2 like in step 2 and the result is as expected:

Difference comes from precision I set to float numbers in printf function.

1. Now I would like to print all params from function, here they are in order from par0 to par11:

You can already find that they are very similar to the one from step 1 but Chi2 from step 2 and 4 are dramaticly different.

1. Data are now saved to the text file. In the next iteration I read them again. Function is the same, I will show last lines of fitfun->Print() related to values of parameters I set to function. You can see that they are the same like in point 5.

``` Par 0 p0 = 1341.74 Par 1 p1 = 1115.23 Par 2 p2 = 1.99347 Par 3 p3 = 531.194 Par 4 p4 = 1115 Par 5 p5 = 5 Par 6 p6 = -25701.2 Par 7 p7 = 25.1743 Par 8 p8 = 0.018706 Par 9 p9 = -1.9e-05 Par 10 p10 = -22.1355 Par 11 p11 = 0.023835```

1. Calling Chisquare() like in point 2 or 4 gives:

which is completely different like in point 4.

1. Now I do fit which results in Chisquare() from fit function (like step 3)
1. Chisquare from comparing histogram to fitfunction (like step 4) is consistent with step 8:
1. Data are again saved to file. Repeating procedure will always give comparable results.

My question is why Chisquare behavies like that for pre-fitted function. I would like to have it correct since I am using it to determine whether fit improved the result or not.

HI,

It is possible that a small change in the parameter gives a very large change in the chi2. You can also check the evolution of the chi2 in fitting by using the Verbose option (option “V”) in TH1::Fit. You will see probably that at the first iteration the chi2 value is the same you get with the function TH1::Chi-square

Best Regards

Lorenzo

Hello,
did you check values of TF1 during your steps by call Eval?