Fitting TH2F

Dear all,
I have a simple (small problem) performing a fit. Here the code I use:

rd51tb.Draw("uno:due>>h521(256,0,255,256,0,255)","");
TF1 *f1 = new TF1("f1","pol1");
h521->Fit("f1");
TF1 *fitinfo=h521->GetFunction("f1");
double element=(fitinfo->GetParameter(1));

The thing is that using pol1 function from the “Fit panel” it works without problem and the fit is nice, but once I do it from the prompt (or from my macro) the fit is different (and very bad): the pol1 doesn’t fit nicely my data, (but from the fit panel yes), my data look like almost a pol1, there is just some small noise somewhere. What is wrong in the above code?

thanks

Hi,

There is nothing wrong with the code above. It is not clear to me if you want to fit 1 2D histogram with 1D function (i.e. a line) or with a 2D one (i.e. a plane).
If you are using the fit panel, “pol1” should work only when you are fitting a 1D histogram. The possibility to fit a 2D histogram with a 1D function is not supported in the case of the fit panel. How did you make the fit from the fit panel ?

Best Regards

Lorenzo

Hi,
I would like to fit the distribution uno:due. With the Draw() I use the fit panel (right click on the Graph) and chosing “pol1” fit, it works and the fit is nice. I need to replicate this by code and then I used what I posted, but the result is different and the fit is totally wrong…

HI,

Although you did not answer my previous question:
Do you want to fit 1 2D histogram with 1D function (i.e. a line) or with a 2D one (i.e. a plane) ?

I assume you want to fit with a line.
In the first case, directly from TTree::Draw you are fitting a TGraph instead of a TH2. This might explains the difference. In the limit the histogram has one entry in each bin they should be the same.
Either increase the TH2 binning or fit directly the TGraph. You can get the TGraph in your macro by doing:

rd51tb.Draw("uno:due");
TGraph *graph = (TGraph*)gPad->GetPrimitive("Graph");
graph->Fit("pol1");

Lorenzo

Yes thanks a lot!!!