16 #include "TH1Helper.h"
21 void TH1Helper::SetCanRebin(TH1 *h,
int axis)
23 #if ROOT_VERSION_CODE < ROOT_VERSION(6,0,0)
24 h->SetBit(TH1::kCanRebin);
26 if (!axis) axis = TH1::kAllAxes;
27 h->SetCanExtend(axis);
32 TH1Helper::TH1Helper(
const TH1 *h,
int binMin,
int binMax)
39 TH1Helper::TH1Helper(
const TH1 *h,
double xMin,
double xMax)
44 void TH1Helper::Set(
const TH1 *h,
double xMin,
double xMax)
54 void TH1Helper::Set(
const TH1 *h,
int binMin,
int binMax)
64 TH1Helper::~TH1Helper()
68 void TH1Helper::Build()
70 const TAxis *ax = fH1->GetXaxis();
71 int nbins = ax->GetNbins();
73 if (fBMin < 1) fBMin = 1;
74 if (fBMax > nbins) fBMax = nbins;
75 fXMin = ax->GetBinLowEdge(fBMin);
76 fXMax = ax->GetBinUpEdge (fBMax);
79 fBMin = ax->FindFixBin(fXMin);
80 if (fBMin < 1) fBMin = 1;
81 fBMax = ax->FindFixBin(fXMax);
82 if (fBMax > nbins) fBMax = nbins;
83 if (fXMin < ax->GetBinLowEdge(fBMin)) fXMin = ax->GetBinLowEdge(fBMin);
84 if (fXMax > ax->GetBinUpEdge (fBMax)) fXMax = ax->GetBinUpEdge (fBMax);
86 memset(fMom,0,
sizeof(fMom));
88 for (
int i=fBMin; i<=fBMax; i++)
if(fH1->GetBinContent(i)) fNonZeros++;
95 void TH1Helper::Aver()
97 if (!fNonZeros)
return;
99 const TAxis *ax = fH1->GetXaxis();
101 double h,error,content,part,center,low,upp;
104 error = fH1->GetBinError (fBMin);
105 content = fH1->GetBinContent(fBMin);
106 h = ax ->GetBinWidth (fBMin);
108 upp = ax->GetBinUpEdge(fBMin);
109 if (upp>fXMax) upp = fXMax;
110 ovl[0][0] = 0.5*(low + upp);
111 ovl[0][1] = (upp-low);
113 ovl[0][2] = content*part;
114 ovl[0][3] = error*TMath::Sqrt(part);
120 error = fH1->GetBinError (fBMax);
121 content = fH1->GetBinContent(fBMax);
122 h = ax ->GetBinWidth (fBMax);
123 low = ax->GetBinLowEdge (fBMax);
124 if (low<ovl[0][5]) low = ovl[0][5];
127 ovl[1][0] = 0.5*(low+upp);
128 ovl[1][1] = (upp-low);
129 ovl[1][2] = content*part;
130 ovl[1][3] = error*TMath::Sqrt(part);
133 double wtot=0,wt,fun;
134 for (
int iter=0;iter <=2;iter++) {
135 for (
int ibin = fBMin; ibin <= fBMax; ibin++) {
137 if (ibin == fBMin) jk=0;
138 if (ibin == fBMax) jk=1;
143 content = ovl[jk][2];
148 center = ax->GetBinCenter(ibin);
149 h = ax ->GetBinWidth(ibin);
150 content = fH1->GetBinContent(ibin);
151 error = fH1->GetBinError (ibin);
152 low = ax->GetBinLowEdge (ibin);
153 upp = ax->GetBinUpEdge (ibin);
156 if (!content)
continue;
162 fMom[1] += fun*content;
166 fun = h*h/12 + TMath::Power(center-fMom[1],2);
167 fMom[2] += fun*content;
172 for (
int i=0;i<3;i++){fun += TMath::Power(TMath::Power(low-fMom[1]+i*h/2,2)-fMom[2],2);}
174 fMom[4] += fun*content;
179 if (!fNonZeros) fMom[0] = 9.e-99;
180 fMom[(1<<iter)] /=fMom[0];
184 double TH1Helper::GetMean () {Aver();
return fMom[1]; }
185 double TH1Helper::GetMeanErr () {Aver();
return TMath::Sqrt(fMom[2]/fMom[0]);}
186 double TH1Helper::GetRMS () {Aver();
return TMath::Sqrt(fMom[2]); }
187 double TH1Helper::GetRMSErr () {Aver();
return TMath::Sqrt(fMom[4]/fMom[0]);}
188 int TH1Helper::GetNonZeros()
const {
return fNonZeros; }
189 double TH1Helper::GetIntegral() {Aver();
return fMom[0]; }
190 double TH1Helper::GetIntegErr() {Aver();
return TMath::Sqrt(fMom[0]); }