Hi, what I am trying to do is fit the data in 1 column of a TNtuple to another. However, the behavior of the fitting functions when I try to do this is puzzling me. For example, in the following code snippet, I create data where y is simply the square of x, but the fit algorithm returns y=4.517+.478*x^(2.4) .
root [1] TNtuple t1(“t1”,“t1”,“x:y”)
root [2] for (float i=0;i<5;i=i+.01) t1.Fill(i,ii)
root [3] t1.Draw(“y:x>>hfit”) TCanvas::MakeDefCanvas: created default TCanvas with name c1
root [4] TF2 myfunc=new TF2(“myfunc”,"[0]+[1]*pow(x,[2])-y+1")
root [5] hfit.Fit(“myfunc”)
FCN=244.206 FROM MIGRAD STATUS=CONVERGED 352 CALLS 353 TOTAL
EDM=2.87534e-007 STRATEGY= 1 ERROR MATRIX UNCERTAINTY
3.6 per cent
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 4.51784e+000 6.31813e-001
2 p1 4.77968e-001 1.49865e-001
3 p2 2.40633e+000 1.89383e-001
(Int_t)0
root [6]
Any ideas on what I am doing wrong (Or if this is even the right way to go about this) ?
In your code, you project an ntuple to a 2-d histogram having by default
40 bins by 40 bins, ie the same cell i,j will have many entries and a different number of entries. Then you fit this 2-d histogram. Not a surprise if you get crazy results!
You should increase the number of bins such that you get only one entry per cell filled. See case1 below.
However, sas I say, this is not the solution. What you want in fact is a simple fit of a y=f(x,par) function using the original data point.
See case2 below with your example modified to create a TGraph instead of a TH2. As you will see, even setting the initial parameters far away from the solution, you will see a much better convergence of the fit.
Thanks so much for the detailed solution Rene!
I think I was assuming that the bins were automatically spaced correctly because when you simply call tree.Draw(“x:y”) it draws a smooth curve rather than in cells.