TH1F multiply issue


I’m not sure if I just can’t figure out the right way to do it or if there is a deeper problem, so any suggestion is welcome.

Say I have a MC generation following a -1 power law :

int nbins = 100; float xmin = 10; float xmax = 2000; int nevts = 1e7; TF1* f = new TF1("f","1./x",0,xmax); TH1F* h = new TH1F("h","h",nbins,xmin,xmax); h->FillRandom("f",nevts);

Here I am with my nice histogram. Now, say I want to rescale it so that each bin is at one. I could loop on each bin, but I prefer to use builtin ROOT function, so here is what I do :

h->Scale(f->Integral(xmin,xmax)/float(nevts)*float(nbins)/(xmax-xmin)); // Rescale the histogram so that it follows the actual function TF1* ff = new TF1("ff","x",0,xmax); // Create the inverse function of "f" h->Multiply(ff,1); // Multiply the histogram by the inverse function h->Draw() // Enjoy ?

Following all those steps, I expect to obtain a histogram full of ones (should I ?). This is not the case. It works for the following sets of {nbins,xmin,xmax} :

But in other cases the mean value is systematically shifted :

I suspected a number of bin vs bin size issue, but for instance the set {999,1,1000} (where each bin has a size of 1) doesn’t work either.

Am I doing it wrong or is there a bug somewhere ?


NB : I’m using ROOT 5.20/00, CINT 5.16.29.

Edit : Tried other functions. It seems to work fine if I use f(x)=x or f(x)=x² as the input function, but gets worst if I use f(x)=1./x², so I think the issue comes from ROOT. Any comment ?

When you do "h->Multiply(ff,1); " you multiply the bin content by the value of the function at the centre of the bin. This is wrong when the function has a huge variation between the bin edges. When increasing the number of bins this effect is obviously less visible.

In versions <5.23 TH1::FillRandom was computing the integral of the function by taking
the function value in the centre of the bin. In version >=5.23/02, we have optimized the
algorithm in computing correctly the integral of the function inside each bin. I suggest to move to 5.23/02 to better see the effect. You should do the same in your procedure.


That makes sense, thanks for the explanation. I’ll work it around.