# Reduced chi square test for 2 histogram compared to graph result

**URL:** <https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257>\
**Category:** Newbie\
**Created:** [July 5, 2020, 5:34pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257 "2020-07-05T17:34:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [July 5, 2020, 5:34pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/1 "2020-07-05T17:34:25Z")

</div>

I want to perform a reduced chi-square test for 4 degrees of freedom system for two histograms compared to graph result. I have no idea regarding this even tried to look roofit and other posted questions. The minimal code would work for me.

Thanks

My histogram (histogram-\>h16) and graph root(z.root) files are:  
[Histo16\_1.root](https://root-forum.cern.ch/uploads/short-url/feoT7JqUu0TvgZYYGSP8Qc8P94b.root) (47.4 KB) [Histo16\_2.root](https://root-forum.cern.ch/uploads/short-url/s2MtQlppXybbE3Cq9suIDqlkEY0.root) (47.5 KB) [Z.root](https://root-forum.cern.ch/uploads/short-url/tvc1eK4NlDCyo7BavAnkURbY8rK.root) (8.1 KB)

_ROOT Version:_ 6.14/06  
_Platform:_ Ubuntu  
\_Compiler:

* * *

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [July 6, 2020, 6:39am UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/2 "2020-07-06T06:39:20Z")

</div>

Perhaps @moneta can give a suggestion?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 6, 2020, 12:32pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/3 "2020-07-06T12:32:40Z")

</div>

Hi,

I have not understood exactly which test would you like to do? Test the compatibility of two histograms ? Please explain with more details

Lorenzo

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [July 6, 2020, 1:32pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/4 "2020-07-06T13:32:00Z")

</div>

As much i studied from a research article an author named it reduced chi square test in which the author compared multiple simulated results with the experimental results using root. I’m trying the same, deviations of two simulated results(histogram h16) compared to the experimental (z.root) results. I guess it would help you to understand.  
Thanks

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [July 13, 2020, 1:46pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/5 "2020-07-13T13:46:30Z")

</div>

Can you help if you know how to do that for a particular histogram from root file ?  
Thanks

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 13, 2020, 4:55pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/6 "2020-07-13T16:55:20Z")

</div>

Hi,  
You can do a chi2 test between two histograms, using the `TH1::Chi2Test` function, see  
[https://root.cern.ch/doc/master/classTH1.html#a6c281eebc0c0a848e7a0d620425090a5](https://root.cern.ch/doc/master/classTH1.html#a6c281eebc0c0a848e7a0d620425090a5)

The function can return p-value, chi2 value and ndf. Reduced chi2 is just chi2/ndf.  
Am example is the tutorial `tutorials/math/chi2test.C`, see  
[https://root.cern.ch/doc/master/chi2test\_8C.html](https://root.cern.ch/doc/master/chi2test_8C.html)

Lorenzo

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [July 13, 2020, 5:08pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/7 "2020-07-13T17:08:23Z")

</div>

Thanks, but as I mentioned my both root files contain 20 histograms out of which I want to compare the 16th histogram. So, how should I mention the 16th histogram?  
For example for drawing 16th histogram from the root files on the same canvas I used

```auto
TFile *f1 = TFile::Open("1st.root");
   TH1D* h1 = (TH1D*)f1->Get("h16");
   h1->Draw("hist same");

```

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 13, 2020, 5:12pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/8 "2020-07-13T17:12:36Z")

</div>

You do the same, get the histograms from the file and then compare:

```auto
TFile *f1 = TFile::Open("1st.root");
   TH1D* h1 = (TH1D*)f1->Get("h16");
TFile *f2 = TFile::Open("2nd.root");
    TH1D* h2 = (TH1D*)f2->Get("h16");

h1->Chi2Test(h2,"P");

```

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [July 13, 2020, 5:23pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/9 "2020-07-13T17:23:37Z")

</div>

It works, thanks. I got the message but no graphical outcome or results.

Info in TH1D::Chi2TestX: There is a bin in h1 with less than 10 effective events.ph

Info in TH1D::Chi2TestX: There is a bin in h2 with less than 10 effective events.

Chi2 = 0.000000, Prob = 1, NDF = 9, igood = 3  
root [1]

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 13, 2020, 5:55pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/10 "2020-07-13T17:55:53Z")

</div>

Hi,  
The routine to compute the Chi2 test does not due any graphics. See the tutorial example on how to show some information (e.g. residuals).

I see, however, that you compare histograms that are identical (chi2 value = 0.0)

Lorenzo

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [July 13, 2020, 6:02pm UTC](https://root-forum.cern.ch/t/reduced-chi-square-test-for-2-histogram-compared-to-graph-result/40257/11 "2020-07-13T18:02:50Z")

</div>

Thank you. 🙂
