Possible TH1::FillRandom() anomaly?

Dear experts,

Here is something I am unable to explain. I use TH1::FillRandom() to fill two histograms from a base template histogram. Then I perform a chi-square comparison between the two filled histograms.

Over a large number of toys, I naively expect the distribution of this chi-square to be that of a true chi-square distribution with degrees of freedom = number of bins. So the mean should be close to the number of bins.

But I find this to not be the case. I run 100k toys in each run, and results vary from run to run, and in some cases the mean is quite different from the expectation.

I attach code to reproduce what I do, and a plot of the chi2 distribution from one of my runs. Note that the mean should be 20, since there are 20 bins.

It is quite possible that I am misusing the rng’s in some way. So I would appreciate any feedback on this.

I am using ROOT 6.22.06, invoked by source /cvmfs/sft.cern.ch/lcg/app/releases/ROOT/6.22.06/x86_64-centos7-gcc48-opt/bin/thisroot.sh;


troubleshootFillRandom.C (1.2 KB)
chi2_plot.PDF (73.8 KB)

I let @moneta help you.

Hi ,
In your code, running the toys you should add these two lines before filling the histogram:


Otherwise you just incrementing the histogram by filling with more entries. The resulting chi2 values are then not independent and they will not follow a chi2 distribution.
Best regards


1 Like

Hi Lorenzo,

You are right. That does indeed fix the problem. How silly of me.

Thanks a lot,

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.