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.
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  TH1D h1("h1", "h1", 100, -5, 5)
root  TH1D h2("h2", "h2", 100, -5, 5)
root  h1.FillRandom("gaus", 1000)
root  h2.FillRandom("gaus", 2000)
root  Double_t h1Integral = h1.Integral()
root  Double_t h2Integral = h2.Integral()
root  Double_t scale = h2Integral / h1Integral
root  h1.Scale(scale)
root  Double_t scale2 = h2.Integral() / h1.Integral() - 1
root  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!