# Histogram and Graph fits, different results!

**URL:** <https://root-forum.cern.ch/t/histogram-and-graph-fits-different-results/20076>\
**Category:** Roofit and RooStats\
**Created:** [October 24, 2015, 3:18pm UTC](https://root-forum.cern.ch/t/histogram-and-graph-fits-different-results/20076 "2015-10-24T15:18:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhassans](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@mhassans](https://root-forum.cern.ch/u/mhassans)\
**Post date:** [October 24, 2015, 3:18pm UTC](https://root-forum.cern.ch/t/histogram-and-graph-fits-different-results/20076/1 "2015-10-24T15:18:02Z")

</div>

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);  
result:

* * *

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);  
result:

* * *

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?

Thanks

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 24, 2015, 3:23pm UTC](https://root-forum.cern.ch/t/histogram-and-graph-fits-different-results/20076/2 "2015-10-24T15:23:22Z")

</div>

Try:  
first-\>Fit(“pol1”,“W”,"",TenPercent\_first,SixtyPercent\_first); // “W” or “WW”

---

<div class="post-metadata">

**Author:** ![mhassans](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@mhassans](https://root-forum.cern.ch/u/mhassans)\
**Post date:** [October 26, 2015, 12:28pm UTC](https://root-forum.cern.ch/t/histogram-and-graph-fits-different-results/20076/3 "2015-10-26T12:28:47Z")

</div>

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:  
first-\>Fit(“pol1”,"","",TenPercent\_first,SixtyPercent\_first);  
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?

Best,  
Mohammad

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 26, 2015, 2:38pm UTC](https://root-forum.cern.ch/t/histogram-and-graph-fits-different-results/20076/4 "2015-10-26T14:38:29Z")

</div>

Use a [TGraphErrors](https://root.cern.ch/root/html534/TGraphErrors.html) or a [TGraphAsymmErrors](https://root.cern.ch/root/html534/TGraphAsymmErrors.html) or a [TGraphBentErrors](https://root.cern.ch/root/html534/TGraphBentErrors.html) instead of a [TGraph](https://root.cern.ch/root/html534/TGraph.html).

---

<div class="post-metadata">

**Author:** ![mhassans](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@mhassans](https://root-forum.cern.ch/u/mhassans)\
**Post date:** [October 26, 2015, 6:18pm UTC](https://root-forum.cern.ch/t/histogram-and-graph-fits-different-results/20076/5 "2015-10-26T18:18:42Z")

</div>

TGraphErrors results the same as TGraph.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 26, 2015, 6:24pm UTC](https://root-forum.cern.ch/t/histogram-and-graph-fits-different-results/20076/6 "2015-10-26T18:24:43Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![mhassans](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@mhassans](https://root-forum.cern.ch/u/mhassans)\
**Post date:** [October 26, 2015, 8:21pm UTC](https://root-forum.cern.ch/t/histogram-and-graph-fits-different-results/20076/7 "2015-10-26T20:21:43Z")

</div>

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