# Chi-square test help

**URL:** <https://root-forum.cern.ch/t/chi-square-test-help/51527>\
**Category:** Newbie\
**Tags:** root\
**Created:** [September 10, 2022, 9:46pm UTC](https://root-forum.cern.ch/t/chi-square-test-help/51527 "2022-09-10T21:46:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DTMsurf](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dtmsurf/32/31018_2.png) [@DTMsurf](https://root-forum.cern.ch/u/DTMsurf)\
**Post date:** [September 10, 2022, 9:46pm UTC](https://root-forum.cern.ch/t/chi-square-test-help/51527/1 "2022-09-10T21:46:21Z")

</div>

\_ROOT Version:6.26.04\_1  
\_Platform: macOS Monterey  
\_Compiler: C++

I am working on a macro that applies a chi-square test to many different histograms and then creates a chi-square distribution using the outputted chi square scores. I am able to find the chi-square scores pretty easily using Chi2Test(); however I am unsure how to use these scores to fill another graph as chi2 is not the correct variable name although the class reference guide says " [out] chi2 " which led me to assume that if I wanted to input this in a graph I would just use g-\>Fill(chi2,other value).

```auto
{

  g = new TGraph();

  int i;

  for(int i = 1; i < 26; i++)
    {
      //h1 is the predicted value
      
      TH1F *h1 = new TH1F("h1", "Predict;x;y", 25, 0, 2500);
      
      //h2 represents second histogram (data collected)
      
      TH1F *h2 = new TH1F("h2", "Data;x;y", 25, 0, 2500);
      
      int k;
      

      for(int k = 0; k < i*500; k++)
      {
	h1->Fill( gRandom->Landau(1000, 300) );
	h2->Fill( gRandom->Landau(1000, 300) );
      }

      Double_t res[25], x[25];
      h1->Chi2Test(h2,"UU P", res);

      g->SetPoint(i, chi2, i*500);

      
   }

  g->Draw();
  
 }

```

This is the line of code I have written in which a for loop runs a chi-square test between two identical distributions at a varied number of total event counts.

TLDR; What is the variable name of the output for chi2 using TH1::Chi2Test()

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [September 10, 2022, 11:21pm UTC](https://root-forum.cern.ch/t/chi-square-test-help/51527/2 "2022-09-10T23:21:33Z")

</div>

Hi @DTMsurf, welcome to the ROOT forum!

The [TH1::Chi2Test()](https://root.cern.ch/doc/master/classTH1.html#ab7d63c7c177ccbf879b5dc31f2311b27) method gives you the chi2 value as a return value, which you need to assign to a variable yourself:

```c++
double chi2 = h1->Chi2Test(h2,"UU P");

g->SetPoint(i, chi2, i*500);

```

I hope this helps you to continue for now, let me know if you have follow-up questions!

Jonas

---

<div class="post-metadata">

**Author:** ![DTMsurf](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dtmsurf/32/31018_2.png) [@DTMsurf](https://root-forum.cern.ch/u/DTMsurf)\
**Post date:** [September 30, 2022, 7:22pm UTC](https://root-forum.cern.ch/t/chi-square-test-help/51527/3 "2022-09-30T19:22:10Z")

</div>

This helped greatly and I was able to complete the macro, Thank you!
