Histogram and Graph fits, different results!

Dear ROOTers,

Assume a histogram from 0 to 1023 with 1024 bins. Each number from 0 to 1023 has a specific value. Now, consider I draw a graph with the same amounts, with the exception that values for X axis changes to X+0.5 (since histograms only care the center of bins in fits). Now when I fit a line to graph and histogram I get different results:

“first” is a HISTOGRAM.
command: first->Fit(“pol1”,"","",TenPercent_first,SixtyPercent_first);

Minimizer is Linear
Chi2 = 0.0130941
NDf = 11
p0 = -0.410958 +/- 1.7957
p1 = 0.0028866 +/- 0.0118169

“gr” is a GRAPH.
command: gr->Fit(“pol1”,"","",TenPercent_first,SixtyPercent_first);

Minimizer is Linear
Chi2 = 0.000339396
NDf = 11
p0 = -0.344322 +/- 0.0632206
p1 = 0.00245905 +/- 0.000411738

Albeit these two fits do not differ significantly but they do at the end of some complex calculations. Can you tell me why these two fits are different?


first->Fit(“pol1”,“W”,"",TenPercent_first,SixtyPercent_first); // “W” or “WW”

Thanks for your response.

Therefore, TGraph fits the points by setting all the weights to 1 (cost function is the absolute distance of points to the fitted line rather than distance squared.), am I right?

The problem is that I get a better result using histogram than graph, that is:
and I want graph to have the same result as histogram and not the reverse, since I want to use a function at the end which I can only do it with graph and not with histogram. Is there anyway to force the graph-fit to give the same result as the histogram?


Use a TGraphErrors or a TGraphAsymmErrors or a TGraphBentErrors instead of a TGraph.

TGraphErrors results the same as TGraph.

Did you remember to set “errors” (for all points)?
Do remember that for a simple histogram, each bin’s error is just sqrt(bin_contents).

Yes, I did not know that histograms choose this special error by default. Now it is solved. Thank you.