TH1::GetSumOfWeights() Problem?

Hi,
When I use TH1::GetSumOfWeights(), I would expect for it to return the sum over all bins of the error^2 for that bin (ie the weight for the bin). Instead of that, it seems to be returning the integral, which is only correct if I haven’t done any weighting to the histogram.

When I run the following macro:

TFile infile("simuhist.root");
TH1F* mInvm = (TH1F*)infile.Get("mInvm");
float integral=0,sumweight=0;
for(int i = 1; i <= mInvm->GetNbinsX(); i++){
  integral+=mInvm->GetBinContent(i);
  sumweight+=mInvm->GetBinError(i)*mInvm->GetBinError(i);
}
cout<<integral<<" "<<mInvm->Integral()<<" "<<sumweight<<" "<<mInvm->GetSumOfWeights()<<endl;

I get:

2.02611e-05 2.02611e-05 6.55266e-13 2.02611e-05

When I go to the source file, sure enough, it’s calculating the Integral:

Double_t TH1::GetSumOfWeights() const
{
   //   -*-*-*-*-*-*Return the sum of weights excluding under/overflows*-*-*-*-*
   //               ===================================================
   Int_t bin,binx,biny,binz;
   Double_t sum =0;
   for(binz=1; binz<=fZaxis.GetNbins(); binz++) {
      for(biny=1; biny<=fYaxis.GetNbins(); biny++) {
         for(binx=1; binx<=fXaxis.GetNbins(); binx++) {
            bin = GetBin(binx,biny,binz);
            sum += GetBinContent(bin);
         }
      }
   }
   return sum;
}

I would expect it to look like:

Double_t TH1::GetSumOfWeights() const
{
   //   -*-*-*-*-*-*Return the sum of weights excluding under/overflows*-*-*-*-*
   //               ===================================================
   Int_t bin,binx,biny,binz;
   Double_t sum =0;
   for(binz=1; binz<=fZaxis.GetNbins(); binz++) {
      for(biny=1; biny<=fYaxis.GetNbins(); biny++) {
         for(binx=1; binx<=fXaxis.GetNbins(); binx++) {
            bin = GetBin(binx,biny,binz);
            sum += GetBinError(bin) * GetBinError(bin);
         }
      }
   }
   return sum;
}

If I’m misunderstanding what it should be doing, it would be nice to have a function that does what I was expecting, that is, returning the error on the integral of the histogram.

GetSumOfWeigths return the sum of weigths, not the sum of squares of weigths.
We do not have a direct function doing that, but easy to achieve with

myhist.GetSumw2()->GetSum()
Rene

Hello,

i make a small correction (in my opinion) to the reply of Rene.

you may instead of doing hist->GetSumw2()->GetSum()

do a loop "by hand"
hist->GetSumw2()
and
to make the total

because

GetSum() would take as well the underflow and overflow.

So GetSum() would not allow to obtain the appropriate error on your total sum of weight. Especially because GetSumOfWeight does not take into account underflow and overflow.

Thanks