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;
  h1->Scale(scale);
  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!

Marcos

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.

Marcos

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
(Double_t)0.00000000000000000e+00

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!

Marcos

Picking up this old post, to ask about my issue: After scaling here, the number of entries in h1 didn’t change, which is strange given the description of Scale() here. Can someone please explain?

I am using root 6.32.02

Hi,
scaling the histogram does not change the number of entries in it. Why would you think it should?

It is scaling the bin contents. So, I wondered why does it not reflect in the entries. I wanted to make a plot where I was also displaying the stats. But, since stats did not reflect the updated bin contents, I was puzzled about the Scale().

Maybe a simple example can help see what’s going on; try this, check the stats box and think about it:

TH1D h1("h1", "h1", 10, -5, 5);
h1.Fill(3, 3.2);
h1.SetBinContent(2, 5.1);
h1.Draw("hist");

Yes, the entries depend on how many times I use SetBinContent()/Fill(). That is what you wanted to convey, right?

So, this makes me feel like a TH1F’s entries should display something like the area, shouldn’t it? That would seem like a more appropriate description of the diagram?