musinsky
8/7/2013 - 1:21 PM

Simplified basic functions of ROOT TH1 class

Simplified basic functions of ROOT TH1 class

void TH1::Sumw2(Bool_t flag) // default flag = KTRUE
{
  if (!flag) {
    if (fSumw2.fN > 0 ) fSumw2.Set(0);
    return;
  }
  if (fSumw2.fN == fNcells) return;

  fSumw2.Set(fNcells);
  if (fEntries > 0)
    for (Int_t bin = 0; bin < fNcells; bin++) {
      fSumw2.fArray[bin] = TMath::Abs(GetBinContent(bin));
    }
}
void TH1::SetBinError(Int_t bin, Double_t error)
{
  if (!fSumw2.fN) Sumw2();
  fSumw2.fArray[bin] = error*error;
}
void TH1D::SetBinContent(Int_t bin, Double_t content)
{
  // AbstractMethod, overridden by TH1D
  fEntries++;
  fTsumw = 0;   // stats[0]
  fArray[bin] = content;
}
void TH1::ResetStats()
{
  Double_t stats[kNstat] = {0}; // kNstat = 13
  fTsumw = 0;
  fEntries = 1; // to force re-calculation of the statistics in GetStats
  GetStats(stats);
  PutStats(stats);
  fEntries = TMath::Abs(fTsumw);
  // use effective entries for weighted histograms: (sum_w) ^2 / sum_w2
  if (fSumw2.fN > 0 && fTsumw > 0 && stats[1] > 0 )
    fEntries = stats[0]*stats[0]/stats[1];
}
void TH1::PutStats(Double_t *stats)
{
  fTsumw   = stats[0];
  fTsumw2  = stats[1];
  fTsumwx  = stats[2];
  fTsumwx2 = stats[3];
}
void TH1::GetStats(Double_t *stats) const
{
  if ((fTsumw == 0 && fEntries > 0) || fXaxis.TestBit(TAxis::kAxisRange)) {
    for (bin = 0; bin < 4; bin++) stats[bin] = 0;
    // in original code include underflow/overflow if fgStatOverflows
    for (binx = firstBinX; binx <= lastBinX; binx++) {
      x   = fXaxis.GetBinCenter(binx);
      w   = GetBinContent(binx);
      err = TMath::Abs(GetBinError(binx));
      stats[0] += w;
      stats[1] += err*err;
      stats[2] += w*x;
      stats[3] += w*x*x;
    }
    // if (stats[0] < 0) do something ?!
  } else {
    stats[0] = fTsumw;
    stats[1] = fTsumw2;
    stats[2] = fTsumwx;
    stats[3] = fTsumwx2;
  }
}
Double_t TH1::GetEffectiveEntries() const
{
  Stat_t s[kNstat];
  this->GetStats(s); // s[0] sum of weights, s[1] sum of squares of weights
  return (s[1] ? s[0]*s[0]/s[1] : TMath::Abs(s[0]));
}
Double_t TH1::GetBinError(Int_t bin) const
{
  if (fSumw2.fN) return TMath::Sqrt(fSumw2.fArray[bin]);
  return TMath::Sqrt(TMath::Abs(GetBinContent(bin)));
}
Double_t TH1D::GetBinContent(Int_t bin) const
{
  // AbstractMethod, overridden by TH1D
  return Double_t (fArray[bin]);
}
Int_t TH1::Fill(Double_t x, Double_t w)
{
  Int_t bin = fXaxis.FindBin(x);
  fEntries++;
  AddBinContent(bin, w);
  if (fSumw2.fN) fSumw2.fArray[bin] += w*w;
  fTsumw   += w;      // stats[0]
  fTsumw2  += w*w;    // stats[1]
  fTsumwx  += w*x;    // stats[2]
  fTsumwx2 += w*x*x;  // stats[3]
  return bin;
}
Int_t TH1::Fill(Double_t x)
{
  Int_t bin = fXaxis.FindBin(x);
  fEntries++;
  AddBinContent(bin);
  if (fSumw2.fN) ++fSumw2.fArray[bin];
  ++fTsumw;           // stats[0]
  ++fTsumw2;          // stats[1]
  fTsumwx  += x;      // stats[2]
  fTsumwx2 += x*x;    // stats[3]
  return bin;
}
void TH1D::AddBinContent(Int_t bin, Double_t w) {
  // AbstractMethod, overridden by TH1D (TH1.h)
  fArray[bin] += Double_t (w);
}
void TH1D::AddBinContent(Int_t bin) {
  // AbstractMethod, overridden by TH1D (TH1.h)
  ++fArray[bin];
}