CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/Validation/RecoParticleFlow/src/TH2Analyzer.cc

Go to the documentation of this file.
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 // remove? 
00016 void TH2Analyzer::Eval( int rebinFactor ) {
00017   //std::cout << "Eval!" << std::endl;
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   //for( unsigned i=0; i<parameters_.size(); ++i) {
00061   //  delete parameters_[i];
00062   //}
00063   
00064   //parameters_.clear();
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       //std::cout << "hist2D_->GetXaxis()->GetBinLowEdge(" << binxmin << ") = " << hist2D_->GetXaxis()->GetBinLowEdge(binxmin) << std::endl;
00083       //std::cout << "hist2D_->GetXaxis()->GetBinUpEdge(" << binxmax << ") = " << hist2D_->GetXaxis()->GetBinUpEdge(binxmax) << std::endl;
00084       //std::cout << "hist2D_->GetNbinsY() = " << hist2D_->GetNbinsY() << std::endl;
00085       //std::cout << "hist2D_->GetYaxis()->GetXmin() = " << hist2D_->GetYaxis()->GetXmin() << std::endl;
00086       //std::cout << "hist2D_->GetYaxis()->GetXmax() = " << hist2D_->GetYaxis()->GetXmax() << std::endl;
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               //std::cout << "hist2D_->GetBinContent(" << binxc << "," << binyc << ") = " << hist2D_->GetBinContent(binxc,binyc) << std::endl;
00099               //std::cout << "hist2D_->GetBinError(" << binxc << "," << binyc << ") = " << hist2D_->GetBinError(binxc,binyc) << std::endl;
00100               //std::cout << "binxc-binxmin+1 = " << binxc-binxmin+1 << std::endl;
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       // binning is not constant, but made to obtain almost the same number of events in each bin
00134 
00135       //std::cout << "binxmax-binxmin+1 = " << binxmax-binxmin+1 << std::endl;
00136       //std::cout << "rebinFactor = " << rebinFactor << std::endl;
00137       //std::cout << "(binxmax-binxmin+1)/rebinFactor = " << (binxmax-binxmin+1)/rebinFactor << std::endl;
00138       //std::cout << "((binxmax-binxmin+1)%rebinFactor) = " << ((binxmax-binxmin+1)%rebinFactor) << std::endl;
00139       //std::cout << "abs((binxmax-binxmin+1)/rebinFactor) = " << std::abs((binxmax-binxmin+1)/rebinFactor) << std::endl;
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       //std::cout << "totalNumberOfEvents = " << totalNumberOfEvents << std::endl;
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               //        //std::cout << "FL : neventsc = " << neventsc << std::endl;
00167               //        //std::cout << "FL : binXmaxc = " << binXmaxc << std::endl;
00168               ++binXmaxc;
00169               delete h0_slice1c;
00170             }
00171           //std::cout << "binXmaxc-1 = " << binXmaxc-1 << std::endl;
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       //for (unsigned int binc=0;binc<nbin+1;++binc)
00179       //{
00180       //  std::cout << "xlow[" << binc << "] = " << xlow[binc] << std::endl;
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   //std::cout << "ProcessSlices!" << std::endl;
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     //std::cout << "mean = " << mean << std::endl;
00228     //std::cout << "rms = " << rms << std::endl;
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     //std::cout << "error = " << error << std::endl;
00236     meanXslice_->SetBinContent(i, histo->GetXaxis()->GetBinLowEdge(i)+error);
00237     meanXslice_->SetBinError(i, error);
00238     //std::cout << "histo->GetXaxis()->GetBinLowEdge(" << i << ") = "
00239     //          << histo->GetXaxis()->GetBinLowEdge(i) << std::endl;
00240     //std::cout << "meanXslice_->GetBinError(" << i << ") = "
00241     //        << meanXslice_->GetBinError(i) << std::endl;
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   //const double mean = proj->GetMean();
00253   const double rms = proj->GetRMS();
00254   //std::cout << "FL: mean = " << mean << std::endl;
00255   //std::cout << "FL: rms = " << rms << std::endl;
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       //std::cout << "i = " << i << std::endl;
00263       //std::cout << "fitmin = " << fitmin << std::endl;
00264       //std::cout << "fitmax = " << fitmax << std::endl;
00265   
00266       //proj->Draw();
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       //std::ostringstream oss;
00272       //oss << i;
00273       //const std::string plotfitname="Plots/fitbin_"+oss.str()+".eps";
00274       //gPad->SaveAs( plotfitname.c_str() );
00275       //std::cout << "param1 = " << f1->GetParameter(1) << std::endl;
00276       //std::cout << "param2 = " << f1->GetParameter(2) << std::endl;
00277       //std::cout << "paramError2 = " << f1->GetParError(2) << std::endl;
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 }