TH1 Errors and negative weights

Dear ROOTers,

I ran across a strange behavior with the Sumw2() function. When running ROOT 5.26/00 on a SL5 x86_64, the following code #1 works fine:

TH1F * h = new TH1F( "h", "H", 5, -2, 2 );
h->Fill( 1, 4 );
h->Sumw2();
h->GetBinContent(4)
(const Double_t)4.00000000000000000e+00
h->GetBinError(4)
(const Double_t)2.00000000000000000e+00 // OK!

The following code #2 produces NaN errors:

TH1F * h = new TH1F( "h", "H", 5, -2, 2 );
h->Fill( 1, -4 );
h->GetBinContent(4)
(const Double_t)(-4.00000000000000000e+00)
h->Sumw2()
h->GetBinContent(4)
(const Double_t)(-4.00000000000000000e+00) // OK
h->GetBinError(4)
(const Double_t)nan

With the following code #3 (Fill() and Sumw2() are swapped) the error is even wrong!

TH1F * h = new TH1F( "h", "H", 5, -2, 2 );
h->Sumw2()
h->Fill( 1, -4 ); 
h->GetBinError(4)
(const Double_t)4.00000000000000000e+00 // should be 2.00000000000000000e+00

I noticed that code #2 works fine running ROOT 5.30/00 on my laptop MacOsX Lion 10.7.3, while code #3 has the same behavior.

Kind regards,
Riccardo

Hi,

The error is correct in the third case, it is computed as the square root of the sum of the weight square, so it is 4 in example 3. In example 1, the histogram does not store the square of the weights (Sumw2) because you have called the function after filling it. So, the value 4 is interpreted as 4 different calls with weight 1, which results in an error of 2.
Note that the support for negative weights has ben introduced only from 5.30, for this reason it did not work before

Best Regards,

Lorenzo