Errors of histos filled by different methods

To figure out how ROOT calculates bin errors when filling histogram bins, I’ve made a script that fills the histogram in different ways with Sumw2() options turned on and off. (attached). Script output is below.

Couple of results are unexpected for me:

A. Fill with weight leads to set error=bin content (cases 1-2), why?
B. Fill with Sumw2(kTRUE) skip error calculations (cases 7-10), why?

Division works as expected - correct value for Sumw2(kTRUE).

root [0] .x sumw2_test.C
Info in <sumw2_test>: I. Fill histo to 100 by different methods:

Info in <sumw2_test>: 1. Sumw2 histo filled with Fill(x,100): 100 +/- 100
Info in <sumw2_test>: 2. None-sumw2 histo filled with Fill(x,100): 100 +/- 100
Info in <sumw2_test>: 3. Sumw2 histo filled 100 times with Fill(x[,1]): 100 +/- 10
Info in <sumw2_test>: 4. None-sumw2 histo filled 100 times with Fill(x[,1]): 100 +/- 10
Info in <sumw2_test>: 5. None-sumw2 histo filled with SetBinContent=100: 100 +/- 10
Info in <sumw2_test>: 6. None-sumw2 histo filled with AddBinContent=100: 100 +/- 10
Info in <sumw2_test>: 7. Sumw2 histo filled with SetBinContent=100 and zero Entries: 100 +/- 0
Info in <sumw2_test>: 8. Sumw2 histo filled with AddBinContent=100 and zero Entries: 100 +/- 0
Info in <sumw2_test>: 9. Sumw2 histo filled with SetBinContent=100 and none-zero Entries(=100): 100 +/- 0
Info in <sumw2_test>: 10.Sumw2 histo filled with AddBinContent=100 and none-zero Entries(=100): 100 +/- 0

Info in <sumw2_test>: II. Divide same histos with errors:

Info in <sumw2_test>: 11. None-sumw2 histo: 100+/-10 divide on 100+/-10: 1 +/- 1
Info in <sumw2_test>: 12. Sumw2 histo: 100+/-10 divide on 100+/-10: 1 +/- 0.141421

sumw2_test.C (4.3 KB)

If you go to the TH1 Documentation you can find the methods you are using (Fill, Add/SetBinContent, GetBinError) and see the code to know what’s happening.
For instance, regarding A, the source code for Fill (Double_t x, Double_t w) says

If the weight is not equal to 1, the storage of the sum of squares of
weights is automatically triggered and the sum of the squares of weights is incremented
by \f$ w^2 \f$ in the bin corresponding to x.

so your line h->Sumw2(kFALSE); before h->Fill in case 2 makes no difference.
For B, check the source code and you’ll find out, but in short, move h->Sumw2(kTRUE); after Add/SetBinContent (these don’t update the errors, only Fill).

When an histogram has errors it is visualized by default with error bars. To visualize it without errors use the option “HIST” together with the required option (eg “hist same c”). The “HIST” option can also be used to plot only the histogram and not the associated function(s).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.