00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <assert.h>
00015
00016 #include "TH1Helper.h"
00017 #include "TMath.h"
00018
00019
00020
00021 TH1Helper::TH1Helper(const TH1 *h, int binMin, int binMax)
00022 {
00023 fNonZeros=0;
00024 if(h==0) return;
00025 Set(h,binMin,binMax);
00026 }
00027
00028 TH1Helper::TH1Helper(const TH1 *h, double xMin, double xMax)
00029 {
00030 Set(h,xMin,xMax);
00031 }
00032
00033 void TH1Helper::Set(const TH1 *h, double xMin, double xMax)
00034 {
00035 fH1 = h;
00036 fBMin = 0;
00037 fBMax = 0;
00038 fXMin = xMin;
00039 fXMax = xMax;
00040 Build();
00041 }
00042
00043 void TH1Helper::Set(const TH1 *h, int binMin, int binMax)
00044 {
00045 fH1 = h;
00046 fBMin = binMin;
00047 fBMax = binMax;
00048 fXMin = 0;
00049 fXMax = 0;
00050 Build();
00051 }
00052
00053 TH1Helper::~TH1Helper()
00054 {
00055 }
00056
00057 void TH1Helper::Build()
00058 {
00059 TAxis *ax = fH1->GetXaxis();
00060 int nbins = ax->GetNbins();
00061 if (fBMax) {
00062 if (fBMin < 1) fBMin = 1;
00063 if (fBMax > nbins) fBMax = nbins;
00064 fXMin = ax->GetBinLowEdge(fBMin);
00065 fXMax = ax->GetBinUpEdge (fBMax);
00066 } else {
00067
00068 fBMin = ax->FindFixBin(fXMin);
00069 if (fBMin < 1) fBMin = 1;
00070 fBMax = ax->FindFixBin(fXMax);
00071 if (fBMax > nbins) fBMax = nbins;
00072 if (fXMin < ax->GetBinLowEdge(fBMin)) fXMin = ax->GetBinLowEdge(fBMin);
00073 if (fXMax > ax->GetBinUpEdge (fBMax)) fXMax = ax->GetBinUpEdge (fBMax);
00074 }
00075 memset(fMom,0,sizeof(fMom));
00076 fNonZeros = 0;
00077 for (int i=fBMin; i<=fBMax; i++) if(fH1->GetBinContent(i)) fNonZeros++;
00078
00079 fMom[0] = 9.e-99;
00080
00081 }
00082
00083
00084 void TH1Helper::Aver()
00085 {
00086 if (!fNonZeros) return;
00087 if (fMom[2]) return;
00088 TAxis *ax = fH1->GetXaxis();
00089 double ovl[2][6];
00090 double h,error,content,part,center,low,upp;
00091
00092
00093 error = fH1->GetBinError (fBMin);
00094 content = fH1->GetBinContent(fBMin);
00095 h = ax ->GetBinWidth (fBMin);
00096 low = fXMin;
00097 upp = ax->GetBinUpEdge(fBMin);
00098 if (upp>fXMax) upp = fXMax;
00099 ovl[0][0] = 0.5*(low + upp);
00100 ovl[0][1] = (upp-low);
00101 part = ovl[0][1]/h;
00102 ovl[0][2] = content*part;
00103 ovl[0][3] = error*TMath::Sqrt(part);
00104 ovl[0][4] = low;
00105 ovl[0][5] = upp;
00106
00107
00108
00109 error = fH1->GetBinError (fBMax);
00110 content = fH1->GetBinContent(fBMax);
00111 h = ax ->GetBinWidth (fBMax);
00112 low = ax->GetBinLowEdge (fBMax);
00113 if (low<ovl[0][5]) low = ovl[0][5];
00114 upp = fXMax;
00115 part = (upp-low)/h;
00116 ovl[1][0] = 0.5*(low+upp);
00117 ovl[1][1] = (upp-low);
00118 ovl[1][2] = content*part;
00119 ovl[1][3] = error*TMath::Sqrt(part);
00120 ovl[1][4] = low;
00121 ovl[1][5] = upp;
00122 double wtot=0,wt,fun;
00123 for (int iter=0;iter <=2;iter++) {
00124 for (int ibin = fBMin; ibin <= fBMax; ibin++) {
00125 int jk = -1;
00126 if (ibin == fBMin) jk=0;
00127 if (ibin == fBMax) jk=1;
00128
00129 if (jk>=0) {
00130 center = ovl[jk][0];
00131 h = ovl[jk][1];
00132 content = ovl[jk][2];
00133 error = ovl[jk][3];
00134 low = ovl[jk][4];
00135 upp = ovl[jk][5];
00136 } else {
00137 center = ax->GetBinCenter(ibin);
00138 h = ax ->GetBinWidth(ibin);
00139 content = fH1->GetBinContent(ibin);
00140 error = fH1->GetBinError (ibin);
00141 low = ax->GetBinLowEdge (ibin);
00142 upp = ax->GetBinUpEdge (ibin);
00143
00144 }
00145 if (!content) continue;
00146 wt = content;
00147 if ( iter==0 ) {
00148 wtot +=wt;
00149 fMom[0] += content;
00150 fun = center;
00151 fMom[1] += fun*content;
00152 continue;
00153 }
00154 if ( iter==1 ) {
00155 fun = h*h/12 + TMath::Power(center-fMom[1],2);
00156 fMom[2] += fun*content;
00157 continue;
00158 }
00159 if ( iter==2 ) {
00160 fun = 0;
00161 for (int i=0;i<3;i++){fun += TMath::Power(TMath::Power(low-fMom[1]+i*h/2,2)-fMom[2],2);}
00162 fun /=3;
00163 fMom[4] += fun*content;
00164 continue;
00165 }
00166
00167 }
00168 if (!fNonZeros) fMom[0] = 9.e-99;
00169 fMom[(1<<iter)] /=fMom[0];
00170 }
00171
00172 }
00173 double TH1Helper::GetMean () {Aver();return fMom[1]; }
00174 double TH1Helper::GetMeanErr () {Aver();return TMath::Sqrt(fMom[2]/fMom[0]);}
00175 double TH1Helper::GetRMS () {Aver();return TMath::Sqrt(fMom[2]); }
00176 double TH1Helper::GetRMSErr () {Aver();return TMath::Sqrt(fMom[4]/fMom[0]);}
00177 int TH1Helper::GetNonZeros() const { return fNonZeros; }
00178 double TH1Helper::GetIntegral() {Aver();return fMom[0]; }
00179 double TH1Helper::GetIntegErr() {Aver();return TMath::Sqrt(fMom[0]); }