TH1::Scale issue (maybe?)

Hello rooters,

I have an issue with TH1::Scale(). I’ve been looking at the code and at previous posts in the forum but I can’t find the solution to this one.
The thing is that I want to use Scale() to, well, scale a TH1 up to the same normalization value as another TH1 that I have. The thing is that after I perform the scaling the integrals of both histograms are still not equal, but slightly different. Here’s a bit of code:

  Double_t h1Integral = h1->Integral();
  Double_t h2Integral = h2->Integral();
  Double_t scale = h2Integral / h1Integral;
  Double scale2 = h2->Integral() / h1->Integral() - 1;

In this case, I was expecting scale2 to be exactly 0, but it isn’t. Am I doing things right?

Thanks once again for your help!


if your TH1 are TH1S or TH1C, the bin contents could overflow when you scale them.

Hi oxon,

Thanks for the fast reply, I’m working with TH1D so hopefully overflowing is not an issue.


I think it would be better to attache a minimum reproducible script. The following example works as you expected.

root [0] TH1D h1("h1", "h1", 100, -5, 5)
root [1] TH1D h2("h2", "h2", 100, -5, 5)
root [2] h1.FillRandom("gaus", 1000)
root [3] h2.FillRandom("gaus", 2000)
root [4] Double_t h1Integral = h1.Integral()
root [5] Double_t h2Integral = h2.Integral()
root [6] Double_t scale = h2Integral / h1Integral
root [7] h1.Scale(scale)
root [8] Double_t scale2 = h2.Integral() / h1.Integral() - 1
root [9] scale2

Mmm, I was wrong, I was using TH1F. The thing is that I have ~ 10^9 entries in the histo, maybe I’m hitting a floating precision problem? I’ll try to regenerate the histos with TH1D and see if that works.

Thanks for your help!