Calling Sumw2() fails after filling with negative weights

To illustrate this problem, I edited one of the tutorial macros, which is provide below. When I fill a histogram with negative weights, then call total->Sumw2(), I get the following error:

root [0] .x test.C
Warning in <TCanvas::ResizePad>: Inf/NaN propagated to the pad. Check drawn objects.
Warning in <TCanvas::ResizePad>: c1 height changed from 0 to 10

root [1] Warning in <TCanvas::ResizePad>: Inf/NaN propagated to the pad. Check drawn objects.
Warning in <TCanvas::ResizePad>: c1 height changed from 0 to 10

This error does not occur if Sumw2() is called before the histogram is filled.

One may ask, “Why not just call Sumw2() before filling the histogram?” It is, unfortunately, not always possible to do this. An example is cases when you would like to save the output of TTree::Draw() to a histogram. It seems that initializing the histogram before calling Draw() has no effect.

If anyone has a solution aside from looping through the tree or the bins of the histogram and calculating the errors manually, I would appreciate it.

{
  gROOT->Reset();

  TCanvas* c1 = new TCanvas("c1","The HSUM example",200,10,600,400);
  c1->SetGrid();

  gBenchmark->Start("hsum");

// Create some histograms.
  TH1F* total  = new TH1F("total","This is the total distribution",100,-4,4);
  //total->Sumw2();   // this makes sure that the sum of squares of weights will be stored

// Fill histograms randomly
  gRandom->SetSeed();
  Float_t xmain;
  for ( Int_t i=0; i<10000; i++) {
     xmain = gRandom->Gaus(-1,1.5);
     total->Fill(xmain,-1);
  }
  total->Sumw2();
  total->Draw("ep");
  c1->Modified();
}

When you cann TTree::Draw, you can use the option “e” to force the computation of the errors (ie Sumw2 is called before filling).

mytree.Draw("var","select","e"); or you can also do

TH1F *h = new TH1F("h","title",100,0,10; h->Sumw2(); mytree.Draw("var>>h");
Rene