Turning off Sumw2

Dear experts,

I wanted to make an example script to demonstrate how Sumw2 affects error bars for weighted histograms. I was expecting that sqrt(bin content) would be drawn if I did not call Sumw2(). However, I cannot seem to avoid the Sumw2 errors, even if I call SetDefaultSumw2(kFALSE). Here is an example:

#include <iostream>
#include "TH1.h"

void ex() {
  TH1F::SetDefaultSumw2(kFALSE);
  TH1F* hi = new TH1F("i", "i", 10, 0, 10);
  hi->Fill(3);
  std::cout << "sqrt(n) error " << hi->GetBinError(4) << std::endl;
  TH1F* hn =  new TH1F("n", "n", 10, 0, 10);
  hn->Fill(3, .5);
  hn->Fill(3, .5);
  std::cout << "without SumW2 " << hn->GetBinError(4) << " uh oh" << std::endl;
  TH1F* hw = new TH1F("w", "w", 10, 0, 10);
  hw->Sumw2();
  hw->Fill(3, .5);
  hw->Fill(3, .5);
  std::cout << "with SumW2 " << hw->GetBinError(4) << std::endl;
}

When I run that example, I get this:

jhakala@JHakBookPro ~  root -l ex.C+
root [0]
Processing ex.C+...
Info in <TMacOSXSystem::ACLiC>: creating shared library /Users/jhakala/./ex_C.so
sqrt(n) error 1
without SumW2 0.707107 uh oh
with SumW2 0.707107

which is not what I was expecting, and does not seem to be what the documentation would indicate.


ROOT Version: 6.23/01
Built for macosx64 on Oct 23 2020, 11:14:00
From heads/master@c6d224cc
Apple clang version 12.0.0 (clang-1200.0.32.21)


Hi,

I modified your code a bit to make it demonstrate the difference properly:

void ex()  {

  TH1F* hi = new TH1F("i", "i", 10, 0, 10);
  hi->Fill(3);
  std::cout << "sqrt(n) error " << hi->GetBinError(4) << std::endl;

  TH1F* hn =  new TH1F("n", "n", 10, 0, 10);
  hn->Fill(3, .5);
  hn->Fill(3, .5);
  hn->Sumw2(kFALSE);
  std::cout << "without SumW2 " << hn->GetBinError(4) << " uh oh" << std::endl;

  TH1F* hw = new TH1F("w", "w", 10, 0, 10);
//  hw->Sumw2(kTRUE); // not needed as Sumw2 is on by default
  hw->Fill(3, .5);
  hw->Fill(3, .5);
  std::cout << "with SumW2 " << hw->GetBinError(4) << std::endl;
}

Hi @yus, OK, that is interesting – to turn off Sumw2, that call to Sumw2(kFALSE) needs to be done after filling the histogram. That is sort of counterintuitive, but OK.

However, as a suggestion to the developers, I would suggest that the behavior of SetDefaultSumw2(kFALSE) be changed. If a user calls that, and then goes on to fill a histogram with weights, then Sumw2 gets silently activated. This should either be changed, or a console message should alert the user that Sumw2 was activated.

The documentation of Sumw2-related things is a bit confusing too – here are some parts of the documentation which seem to give a reader the wrong impression.

  • under the documentation of Sumw2:

This function is automatically called when the histogram is created if the static function TH1::SetDefaultSumw2 has been called before. If flag = false the structure containing the sum of the square of weights is rest and it will be empty, but it is not deleted (i.e. GetSumw2()->fN = 0)

This seems to indicate that Sumw2 should not have happened for hn in my above example, but in fact it does happen.

  • in the “Filling Histograms” section:

If TH1::Sumw2 has been called before filling, the sum of squares of weights is also stored.

This seems to indicate that Sumw2 should be called beforehand if you want Sumw2 errors – but in fact Sumw2 happens by default and if you want it not to happen, the Sumw2(kFALSE) call needs to be called afterword, as in @yus above example.

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