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:
- 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[0] = [0] action = 140 action param = 0
fExpr[1] = 0.5 action = 141 action param = 0
fExpr[2] = x action = 144 action param = 0
fExpr[3] = [1] action = 140 action param = 1
fExpr[4] = - action = 2 action param = 0
fExpr[5] = [2] action = 140 action param = 2
fExpr[6] = / action = 4 action param = 0
fExpr[7] = sq action = 21 action param = 0
fExpr[8] = * action = 3 action param = 0
fExpr[9] = - action = 44 action param = 0
fExpr[10] = Double_t TMath::Exp(Double_t x) action = 145 action param = 1
fExpr[11] = * action = 3 action param = 0
fExpr[12] = [3] action = 140 action param = 3
fExpr[13] = 0.5 action = 141 action param = 0
fExpr[14] = x action = 144 action param = 0
fExpr[15] = [1] action = 140 action param = 1
fExpr[16] = - action = 2 action param = 0
fExpr[17] = [5] action = 140 action param = 5
fExpr[18] = / action = 4 action param = 0
fExpr[19] = sq action = 21 action param = 0
fExpr[20] = * action = 3 action param = 0
fExpr[21] = - action = 44 action param = 0
fExpr[22] = Double_t TMath::Exp(Double_t x) action = 145 action param = 1001
fExpr[23] = * action = 3 action param = 0
fExpr[24] = + action = 1 action param = 0
fExpr[25] = pol3(6) action = 130 action param = 307
fExpr[26] = + action = 1 action param = 0
fExpr[27] = expo(10) action = 100 action param = 10
fExpr[28] = + action = 1 action param = 0
Optimized expression
fExpr[0] = [0] action = 146 action param = 0
fExpr[1] = 0.5 action = 146 action param = 0
fExpr[2] = x,[1],- action = 148 action param = 0
fExpr[3] = [2] action = 146 action param = 2
fExpr[4] = / action = 4 action param = 0
fExpr[5] = sq action = 161 action param = 0
fExpr[6] = * action = 3 action param = 0
fExpr[7] = - action = 44 action param = 0
fExpr[8] = Double_t TMath::Exp(Double_t x) action = 161 action param = 0
fExpr[9] = * action = 3 action param = 0
fExpr[10] = [3] action = 146 action param = 3
fExpr[11] = 0.5 action = 146 action param = 0
fExpr[12] = x,[1],- action = 148 action param = 0
fExpr[13] = [5] action = 146 action param = 5
fExpr[14] = / action = 4 action param = 0
fExpr[15] = sq action = 161 action param = 0
fExpr[16] = * action = 3 action param = 0
fExpr[17] = - action = 44 action param = 0
fExpr[18] = Double_t TMath::Exp(Double_t x) action = 161 action param = 0
fExpr[19] = * action = 3 action param = 0
fExpr[20] = + action = 1 action param = 0
fExpr[21] = pol3(6) action = 130 action param = 307
fExpr[22] = + action = 1 action param = 0
fExpr[23] = expo(10) action = 100 action param = 10
fExpr[24] = + 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.
- Now, I am calling Chisquare function for histogram:
hist->Chisquare(fitfun, "R");
and the result is [quote]767769.823388[/quote]
- 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:
- 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.
- Now I would like to print all params from function, here they are in order from par0 to par11:
1341.742906 1115.233235 1.993469 531.194042 1115.000000 4.999995 -25701.189785 25.174267 0.018706 -0.000019 -22.135460 0.023835
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.
- 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
- Calling Chisquare() like in point 2 or 4 gives:
which is completely different like in point 4.
- Now I do fit which results in Chisquare() from fit function (like step 3)
- Chisquare from comparing histogram to fitfunction (like step 4) is consistent with step 8:
- 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.