00001 #include "Validation/RecoParticleFlow/interface/TH2Analyzer.h"
00002
00003 #include "TH2D.h"
00004 #include "TH1D.h"
00005 #include "TF1.h"
00006 #include "TPad.h"
00007
00008 #include <string>
00009 #include <iostream>
00010 #include <sstream>
00011 #include <algorithm>
00012
00013 using namespace std;
00014
00015
00016 void TH2Analyzer::Eval( int rebinFactor ) {
00017
00018
00019 Reset();
00020
00021 const string bname = hist2D_->GetName();
00022 const string rebinName = bname + "_rebin";
00023 rebinnedHist2D_ = (TH2D*) hist2D_ -> Clone( rebinName.c_str() );
00024 rebinnedHist2D_->RebinX( rebinFactor );
00025
00026 const string averageName = bname + "_average";
00027 average_ = new TH1D( averageName.c_str(),"arithmetic average",
00028 rebinnedHist2D_->GetNbinsX(),
00029 rebinnedHist2D_->GetXaxis()->GetXmin(),
00030 rebinnedHist2D_->GetXaxis()->GetXmax() );
00031
00032 const string rmsName = bname + "_RMS";
00033 RMS_ = new TH1D( rmsName.c_str(), "RMS",
00034 rebinnedHist2D_->GetNbinsX(),
00035 rebinnedHist2D_->GetXaxis()->GetXmin(),
00036 rebinnedHist2D_->GetXaxis()->GetXmax() );
00037
00038 const string sigmaGaussName = bname + "_sigmaGauss";
00039 sigmaGauss_ = new TH1D(sigmaGaussName.c_str(), "sigmaGauss",
00040 rebinnedHist2D_->GetNbinsX(),
00041 rebinnedHist2D_->GetXaxis()->GetXmin(),
00042 rebinnedHist2D_->GetXaxis()->GetXmax() );
00043
00044 const string meanXName = bname + "_meanX";
00045 meanXslice_ = new TH1D(meanXName.c_str(), "meanX",
00046 rebinnedHist2D_->GetNbinsX(),
00047 rebinnedHist2D_->GetXaxis()->GetXmin(),
00048 rebinnedHist2D_->GetXaxis()->GetXmax() );
00049
00050 ProcessSlices( rebinnedHist2D_ );
00051 }
00052
00053 void TH2Analyzer::Reset() {
00054 if ( rebinnedHist2D_ ) delete rebinnedHist2D_;
00055 if ( average_ ) delete average_;
00056 if ( RMS_ ) delete RMS_;
00057 if ( sigmaGauss_ ) delete sigmaGauss_;
00058 if ( meanXslice_ ) delete meanXslice_;
00059
00060
00061
00062
00063
00064
00065 }
00066
00067 void TH2Analyzer::Eval(const int rebinFactor, const int binxmin,
00068 const int binxmax, const bool cst_binning)
00069 {
00070 Reset();
00071 const string bname = hist2D_->GetName();
00072 const string rebinName = bname + "_rebin";
00073
00074 if (binxmax>hist2D_->GetNbinsX())
00075 {
00076 std::cout << "Error: TH2Analyzer.cc: binxmax>hist2D_->GetNbinsX()" << std::endl;
00077 return;
00078 }
00079
00080 if (cst_binning)
00081 {
00082
00083
00084
00085
00086
00087 rebinnedHist2D_ = new TH2D(rebinName.c_str(),"rebinned histo",
00088 binxmax-binxmin+1,
00089 hist2D_->GetXaxis()->GetBinLowEdge(binxmin),
00090 hist2D_->GetXaxis()->GetBinUpEdge(binxmax),
00091 hist2D_->GetNbinsY(),
00092 hist2D_->GetYaxis()->GetXmin(),
00093 hist2D_->GetYaxis()->GetXmax() );
00094 for (int binyc=1;binyc<hist2D_->GetNbinsY()+1;++binyc)
00095 {
00096 for (int binxc=binxmin;binxc<binxmax+1;++binxc)
00097 {
00098
00099
00100
00101 rebinnedHist2D_->SetBinContent(binxc-binxmin+1,binyc,hist2D_->GetBinContent(binxc,binyc));
00102 rebinnedHist2D_->SetBinError(binxc-binxmin+1,binyc,hist2D_->GetBinError(binxc,binyc));
00103 }
00104 }
00105 rebinnedHist2D_->RebinX( rebinFactor );
00106
00107 const string averageName = bname + "_average";
00108 average_ = new TH1D( averageName.c_str(),"arithmetic average",
00109 rebinnedHist2D_->GetNbinsX(),
00110 rebinnedHist2D_->GetXaxis()->GetXmin(),
00111 rebinnedHist2D_->GetXaxis()->GetXmax() );
00112
00113 const string rmsName = bname + "_RMS";
00114 RMS_ = new TH1D( rmsName.c_str(), "RMS",
00115 rebinnedHist2D_->GetNbinsX(),
00116 rebinnedHist2D_->GetXaxis()->GetXmin(),
00117 rebinnedHist2D_->GetXaxis()->GetXmax() );
00118
00119 const string sigmaGaussName = bname + "_sigmaGauss";
00120 sigmaGauss_ = new TH1D(sigmaGaussName.c_str(), "sigmaGauss",
00121 rebinnedHist2D_->GetNbinsX(),
00122 rebinnedHist2D_->GetXaxis()->GetXmin(),
00123 rebinnedHist2D_->GetXaxis()->GetXmax() );
00124
00125 const string meanXName = bname + "_meanX";
00126 meanXslice_ = new TH1D(meanXName.c_str(), "meanX",
00127 rebinnedHist2D_->GetNbinsX(),
00128 rebinnedHist2D_->GetXaxis()->GetXmin(),
00129 rebinnedHist2D_->GetXaxis()->GetXmax() );
00130 }
00131 else
00132 {
00133
00134
00135
00136
00137
00138
00139
00140
00141 unsigned int nbin=0;
00142 if (((binxmax-binxmin+1)%rebinFactor)!=0.0)
00143 {
00144 nbin=std::abs((binxmax-binxmin+1)/rebinFactor)+1;
00145 }
00146 else nbin=(binxmax-binxmin+1)/rebinFactor;
00147
00148 double *xlow = new double[nbin+1];
00149 int *binlow = new int[nbin+1];
00150
00151 TH1D* h0_slice1 = hist2D_->ProjectionY("h0_slice1", binxmin, binxmax, "");
00152 const unsigned int totalNumberOfEvents=static_cast<unsigned int>(h0_slice1->GetEntries());
00153
00154 delete h0_slice1;
00155
00156 unsigned int neventsc=0;
00157 unsigned int binXmaxc=binxmin+1;
00158 xlow[0]=hist2D_->GetXaxis()->GetBinLowEdge(binxmin);
00159 binlow[0]=binxmin;
00160 for (unsigned int binc=1;binc<nbin;++binc)
00161 {
00162 while (neventsc<binc*totalNumberOfEvents/nbin)
00163 {
00164 TH1D* h0_slice1c = hist2D_->ProjectionY("h0_slice1",binxmin, binXmaxc, "");
00165 neventsc=static_cast<unsigned int>(h0_slice1c->GetEntries());
00166
00167
00168 ++binXmaxc;
00169 delete h0_slice1c;
00170 }
00171
00172 binlow[binc]=binXmaxc-1;
00173 xlow[binc]=hist2D_->GetXaxis()->GetBinLowEdge(binXmaxc-1);
00174 }
00175 xlow[nbin]=hist2D_->GetXaxis()->GetBinUpEdge(binxmax);
00176 binlow[nbin]=binxmax;
00177
00178
00179
00180
00181
00182
00183 rebinnedHist2D_ = new TH2D(rebinName.c_str(),"rebinned histo",
00184 nbin, xlow, hist2D_->GetNbinsY(),
00185 hist2D_->GetYaxis()->GetXmin(),
00186 hist2D_->GetYaxis()->GetXmax() );
00187 for (int binyc=1;binyc<hist2D_->GetNbinsY()+1;++binyc)
00188 {
00189 for (unsigned int binxc=1;binxc<nbin+1;++binxc)
00190 {
00191 double sum=0.0;
00192 for (int binxh2c=binlow[binxc-1];binxh2c<binlow[binxc];++binxh2c)
00193 {
00194 sum+=hist2D_->GetBinContent(binxh2c,binyc);
00195 }
00196 rebinnedHist2D_->SetBinContent(binxc,binyc,sum);
00197 }
00198 }
00199
00200 const string averageName = bname + "_average";
00201 average_ = new TH1D( averageName.c_str(),"arithmetic average", nbin, xlow);
00202
00203 const string rmsName = bname + "_RMS";
00204 RMS_ = new TH1D( rmsName.c_str(), "RMS", nbin, xlow);
00205
00206 const string sigmaGaussName = bname + "_sigmaGauss";
00207 sigmaGauss_ = new TH1D(sigmaGaussName.c_str(), "sigmaGauss", nbin, xlow);
00208
00209 const string meanXName = bname + "_meanX";
00210 meanXslice_ = new TH1D(meanXName.c_str(), "meanX", nbin, xlow);
00211 delete [] xlow;
00212 delete [] binlow;
00213 }
00214 ProcessSlices( rebinnedHist2D_ );
00215 }
00216
00217 void TH2Analyzer::ProcessSlices( const TH2D* histo) {
00218
00219
00220
00221 TH1::AddDirectory(0);
00222
00223 for( int i=1; i<=histo->GetNbinsX(); ++i) {
00224 TH1D* proj = histo->ProjectionY("toto", i, i);
00225 const double mean = proj->GetMean();
00226 const double rms = proj->GetRMS();
00227
00228
00229 average_->SetBinContent( i, mean);
00230 average_->SetBinError( i, proj->GetMeanError());
00231 RMS_->SetBinContent(i, rms);
00232 RMS_->SetBinError(i, proj->GetRMSError());
00233
00234 const double error=(histo->GetXaxis()->GetBinUpEdge(i)-histo->GetXaxis()->GetBinLowEdge(i))/2.0;
00235
00236 meanXslice_->SetBinContent(i, histo->GetXaxis()->GetBinLowEdge(i)+error);
00237 meanXslice_->SetBinError(i, error);
00238
00239
00240
00241
00242 ProcessSlice(i, proj );
00243 delete proj;
00244 }
00245
00246 TH1::AddDirectory(1);
00247 }
00248
00249
00250 void TH2Analyzer::ProcessSlice(const int i, TH1D* proj ) const {
00251
00252
00253 const double rms = proj->GetRMS();
00254
00255
00256
00257 if (rms!=0.0)
00258 {
00259 const double fitmin=proj->GetMean()-proj->GetRMS();
00260 const double fitmax=proj->GetMean()+proj->GetRMS();
00261
00262
00263
00264
00265
00266
00267 TF1* f1= new TF1("f1", "gaus", fitmin, fitmax);
00268 f1->SetParameters(proj->GetRMS(),proj->GetMean(),proj->GetBinContent(proj->GetMaximumBin()));
00269 proj->Fit("f1","R", "", proj->GetXaxis()->GetXmin(), proj->GetXaxis()->GetXmax());
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 sigmaGauss_->SetBinContent(i, f1->GetParameter(2));
00280 sigmaGauss_->SetBinError(i, f1->GetParError(2));
00281 delete f1;
00282 }
00283 else
00284 {
00285 sigmaGauss_->SetBinContent(i, 0.0);
00286 sigmaGauss_->SetBinError(i, 0.0);
00287 }
00288 }