# Propogating errors in histogram summation

Hi All,

I am curious if it is possible to get uncertainties when getting the sum of histograms.

For example, if I want to sum hhisto from x=a to x=b, is it possible to get the error on this summation? Let’s say the error is defined since we did a hhisto->Sumw2()… I assume the error would just come from quadrature of the error on individual bins.

Any help/suggestions on this would be much appreciated!

Cheers,
Will

Assuming 2 histograms h1 and h2, if you have called Sumw2 for at least one of them before the add operation, then the resulting errors should be correct.

Rene

Hi Rene,

I’m not sure thats exactly what I was thinking of. Perhaps I should try to state my question better.

How would I sum a subrange of a histogram and get a (scalar) value with the appropriate (scalar) error?

I appreciate the help!,
Will

Sorry I was too fast. See

``` Double_t TH1::IntegralAndError(Int_t binx1, Int_t binx2, Double_t & err, Option_t *option="") const; ```

``` //Return integral of bin contents in range [binx1,binx2] and its error // By default the integral is computed as the sum of bin contents in the range. // if option "width" is specified, the integral is the sum of // the bin contents multiplied by the bin width in x. // the error is computed using error propagation from the bin errors assumming that // all the bins are uncorrelated ```
Rene

Rene - That will be perfect. Thank you.

Will

Hi Rene,

I can’t get your solution to work for some reason. I have a TH1F histogram opened from a TFile.

when I do:
hA->Integral()
I get the integral just fine (with a range if I want). But when I try:
hA->IntegralAndError(), hA->DoIntegral, etc, I get an error:

Error: Can’t call TH1F::IntegralAndError() in current scope (tmpfile):1:
Possible candidates are…
(in TH1F)
(in TH1)
*** Interpreter error recovered ***

I’m not sure I understand why this isn’t working since I am working with a TH1F histogram:

root.cern.ch/root/html/TH1.html#TH1:Integral

Many Thanks,
Will

You must be using an old version. Move to version 5.26 or newer

Rene

Hi Rene,

That may be it. I am using 5.24.

I will work on moving to 5.26. In the meantime, are there any simple ways to sum the sqrt of the errors of a histogram?

Thanks again,
Will

Loop on all bins calling hist.GetBinError(bin), bin=1,<=nbins

Rene

Hi Rene

Im also interested in propogating errors in histogram summation. I saw your advice to use

IntegralAndError(Int_t binx1, Int_t binx2, Double_t& err, Option_t* option = “”)

and sorry for the naive question, but, what should I put in the last two slots (err and option) of this function? I have filled my histograms with the Sumw2 option.

Thank you very much for your help!!

Hi,

if you want to have the uncertainty of each bin’s count then you already have it. This is what most people are looking for.

If you want the uncertainty of the histogram’s integral (maybe even on a subrange) then you need to use the function that Rene suggested; err is a variable of type Double_t that you define and that will contain the uncertainty of the integral once the function returns.

Cheers, Axel.

Thank you very much Axel!! It worked perfectly!!

I am interested in finding the significance of the difference between entries in bins between two plots

“X / uncertainty on X”

where X is the difference on the number of entries. In ROOT, I think that I should make something like this

(BinContent_a - BinContent_b) / sqrt((BinError_aBinError_a + BinError_bBinError_b))

and then use SetBinContent on the comparison histogram to overwrite its contents with the significance, instead of the ratio.

I am looking for a way to implement this in my own code which makes a ratio of 2-D histograms. Here is the code in PyROOT:

import ROOT
ROOT.gROOT.SetBatch()

#open root file
rootfile1 = ROOT.TFile.Open(“HIST.07511504._000001.pool.root.1”)

#get histo 1
etaphi_284285 = rootfile1.Get(“run_284285/MuonPhysics/Muons/CBMuons/Muons_CBMuons_eta_phi”)

#open second root file
rootfile2 = ROOT.TFile.Open(“HIST.07363316._000001.pool.root.1”)

#get histo 2
etaphi_283429 = rootfile2.Get(“run_283429/MuonPhysics/Muons/CBMuons/Muons_CBMuons_eta_phi”)

#n bins merge into one bin in both the x and the y directions
rebin_eta = 4
rebin_phi = 4

#begin loop over both histograms
for histogram in [etaphi_284285,
etaphi_283429,
]:

``````print "histogram=", histogram
#rebin
histogram.Rebin2D(rebin_eta, rebin_phi)
print "histogram.Integral=", histogram.Integral()

#normalize to get ratio of the events in bin/total events in each histogram
histogram.Scale(1 / histogram.Integral())
``````

#divide the histograms
ratio = etaphi_284285.Clone()
ratio.Reset()
ratio.SetName(“ratio_284285_283429”)
ratio.Divide(etaphi_284285, etaphi_283429)

(then I create a root file containing this histogram).

Hi,

Could you please start a new thread, preferably in the math section of the forum?

Thanks,
Philippe.