# Can i apply the chi2test over a specific region of my histogram?

**URL:** <https://root-forum.cern.ch/t/can-i-apply-the-chi2test-over-a-specific-region-of-my-histogram/47818>\
**Category:** Newbie\
**Tags:** python\
**Created:** [November 25, 2021, 1:29pm UTC](https://root-forum.cern.ch/t/can-i-apply-the-chi2test-over-a-specific-region-of-my-histogram/47818 "2021-11-25T13:29:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cesar1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@Cesar1](https://root-forum.cern.ch/u/Cesar1)\
**Post date:** [November 25, 2021, 1:29pm UTC](https://root-forum.cern.ch/t/can-i-apply-the-chi2test-over-a-specific-region-of-my-histogram/47818/1 "2021-11-25T13:29:16Z")

</div>

Dear experts,

I am just want to know if there is a way to apply the chi2test in a specific region of my histogram. I am comparing mc-mc histograms, and there is a region that I want to keep outside of my chi2test. There is a way to do it or do I need to fill the histograms just in the area of interest?

Best Regards,  
Cesar.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 25, 2021, 1:36pm UTC](https://root-forum.cern.ch/t/can-i-apply-the-chi2test-over-a-specific-region-of-my-histogram/47818/2 "2021-11-25T13:36:59Z")

</div>

I think @moneta can help you.

---

<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:** [November 25, 2021, 3:03pm UTC](https://root-forum.cern.ch/t/can-i-apply-the-chi2test-over-a-specific-region-of-my-histogram/47818/3 "2021-11-25T15:03:28Z")

</div>

Hi,  
You should be able to do this by calling before `histogram->GetXaxis()->SetRange(bin1, bin2)`

Lorenzo

---

<div class="post-metadata">

**Author:** ![Cesar1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@Cesar1](https://root-forum.cern.ch/u/Cesar1)\
**Post date:** [November 25, 2021, 4:13pm UTC](https://root-forum.cern.ch/t/can-i-apply-the-chi2test-over-a-specific-region-of-my-histogram/47818/4 "2021-11-25T16:13:00Z")

</div>

That worked, thanks! Just another question, i wanted to get the res array, but I created an array here in pyroot and did this:

h1.Chi2Test(h2, “CHI2/NDF WW”, res)

But I got an error saying it could not convert argument 3. How than can I obtaing this array?

---

<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:** [December 3, 2021, 10:51am UTC](https://root-forum.cern.ch/t/can-i-apply-the-chi2test-over-a-specific-region-of-my-histogram/47818/5 "2021-12-03T10:51:56Z")

</div>

Hi,  
You need to provide an array (e.g. a numpy array) to the Chi2Test function.  
Here is an example code:

```auto
import ROOT
import numpy as np

f1 = ROOT.TF1('f1','gaus')
f1.SetParameters(1, 5, 2.5)

h1 = ROOT.TH1D('h1','h1',100,0,10)
h1.FillRandom('f1')

h2 = ROOT.TH1D('h2','h2',100,0,10)
f1.SetParameter(1,5.4)
h2.FillRandom('f1')

res = np.zeros(100)

h1.Chi2Test(h2,'P UU',res)

print(res)

h3 = ROOT.TH1D('h3','rediduals',100,0,10)
for i in range(1,101) :
    h3.SetBinContent(i,res[i-1])
    h3.SetBinError(i,1) # residuals are normalized 

h3.DrawClone()

```
