CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DQMOffline/EGamma/src/ElectronDqmAnalyzerBase.cc

Go to the documentation of this file.
00001 
00002 #include "DQMOffline/EGamma/interface/ElectronDqmAnalyzerBase.h"
00003 #include "DQMServices/Core/interface/DQMStore.h"
00004 #include "DQMServices/Core/interface/MonitorElement.h"
00005 #include "FWCore/ServiceRegistry/interface/Service.h"
00006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "TMath.h"
00009 #include "TFile.h"
00010 #include "TH1F.h"
00011 #include "TH1I.h"
00012 #include "TH2F.h"
00013 #include "TProfile.h"
00014 #include "TTree.h"
00015 #include <iostream>
00016 #include <algorithm>
00017 #include <sstream>
00018 
00019 ElectronDqmAnalyzerBase::ElectronDqmAnalyzerBase( const edm::ParameterSet& conf )
00020  : bookPrefix_("ele"), bookIndex_(0), histoNamesReady(false), finalDone_(false)
00021  {
00022   verbosity_ = conf.getUntrackedParameter<int>("Verbosity") ;
00023   finalStep_ = conf.getParameter<std::string>("FinalStep") ;
00024   inputFile_ = conf.getParameter<std::string>("InputFile") ;
00025   outputFile_ = conf.getParameter<std::string>("OutputFile") ;
00026   inputInternalPath_ = conf.getParameter<std::string>("InputFolderName") ;
00027   outputInternalPath_ = conf.getParameter<std::string>("OutputFolderName") ;
00028  }
00029 
00030 ElectronDqmAnalyzerBase::~ElectronDqmAnalyzerBase()
00031  {}
00032 
00033 void ElectronDqmAnalyzerBase::setBookPrefix( const std::string & prefix )
00034  { bookPrefix_ = prefix ; }
00035 
00036 void ElectronDqmAnalyzerBase::setBookIndex( short index )
00037  { bookIndex_ = index ; }
00038 
00039 std::string ElectronDqmAnalyzerBase::newName( const std::string & name )
00040  {
00041   if (bookPrefix_.empty())
00042    { return name ; }
00043   std::ostringstream oss ;
00044   oss<<bookPrefix_ ;
00045   if (bookIndex_>=0)
00046    { oss<<bookIndex_++ ; }
00047   oss<<"_"<<name ;
00048   return oss.str() ;
00049  }
00050 
00051 const std::string * ElectronDqmAnalyzerBase::find( const std::string & name )
00052  {
00053   typedef std::vector<std::string> HistoNames ;
00054   typedef HistoNames::iterator HistoNamesItr ;
00055   if (!histoNamesReady)
00056    { histoNamesReady = true ; histoNames_ = store_->getMEs() ; }
00057   HistoNamesItr histoName ;
00058   std::vector<HistoNamesItr> res ;
00059   for ( histoName = histoNames_.begin() ; histoName != histoNames_.end() ; ++histoName )
00060    {
00061     std::size_t nsize = name.size(), lsize = histoName->size() ;
00062 //    if ( (histoName->find(bookPrefix_)==0) &&
00063 //         (lsize>=nsize) &&
00064 //         (histoName->find(name)==(lsize-nsize)) )
00065 //     { res.push_back(histoName) ; }
00066     if ( (lsize>=nsize) &&
00067          (histoName->find(name)==(lsize-nsize)) )
00068      { res.push_back(histoName) ; }
00069    }
00070   if (res.size()==0)
00071    {
00072     std::ostringstream oss ;
00073     oss<<"Histogram "<<name<<" not found in "<<outputInternalPath_ ;
00074     char sep = ':' ;
00075     for ( histoName = histoNames_.begin() ; histoName != histoNames_.end() ; ++histoName )
00076      { oss<<sep<<' '<<*histoName ; sep = ',' ; }
00077     oss<<'.' ;
00078     edm::LogWarning("ElectronDqmAnalyzerBase::find")<<oss.str() ;
00079     return 0 ;
00080    }
00081   else if (res.size()>1)
00082    {
00083     std::ostringstream oss ;
00084     oss<<"Ambiguous histograms for "<<name<<" in "<<outputInternalPath_ ;
00085     char sep = ':' ;
00086     std::vector<HistoNamesItr>::iterator resItr ;
00087     for ( resItr = res.begin() ; resItr != res.end() ; ++resItr )
00088      { oss<<sep<<' '<<(**resItr) ; sep = ',' ; }
00089     oss<<'.' ;
00090     edm::LogWarning("ElectronDqmAnalyzerBase::find")<<oss.str() ;
00091     return 0 ;
00092    }
00093   else
00094    {
00095     return &*res[0] ;
00096    }
00097  }
00098 
00099 void ElectronDqmAnalyzerBase::beginJob()
00100  {
00101   store_ = edm::Service<DQMStore>().operator->() ;
00102   if (!store_)
00103    { edm::LogError("ElectronDqmAnalyzerBase::prepareStore")<<"No DQMStore found !" ; }
00104   store_->setVerbose(verbosity_) ;
00105   if (inputFile_!="")
00106    { store_->open(inputFile_) ; }
00107   store_->setCurrentFolder(outputInternalPath_) ;
00108   book() ;
00109  }
00110 
00111 void ElectronDqmAnalyzerBase::endRun( edm::Run const &, edm::EventSetup const & )
00112  {
00113   if (finalStep_=="AtRunEnd")
00114    {
00115     if (finalDone_)
00116      { edm::LogWarning("ElectronDqmAnalyzerBase::endRun")<<"finalize() already called" ; }
00117     store_->setCurrentFolder(outputInternalPath_) ;
00118     finalize() ;
00119     finalDone_ = true ;
00120    }
00121  }
00122 void ElectronDqmAnalyzerBase::endLuminosityBlock( edm::LuminosityBlock const &, edm::EventSetup const & )
00123  {
00124   if (finalStep_=="AtLumiEnd")
00125    {
00126     if (finalDone_)
00127      { edm::LogWarning("ElectronDqmAnalyzerBase::endLuminosityBlock")<<"finalize() already called" ; }
00128     store_->setCurrentFolder(outputInternalPath_) ;
00129     finalize() ;
00130     finalDone_ = true ;
00131    }
00132  }
00133 
00134 void ElectronDqmAnalyzerBase::endJob()
00135  {
00136   if (finalStep_=="AtJobEnd")
00137    {
00138     if (finalDone_)
00139      { edm::LogWarning("ElectronDqmAnalyzerBase::endJob")<<"finalize() already called" ; }
00140     store_->setCurrentFolder(outputInternalPath_) ;
00141     finalize() ;
00142     finalDone_ = true ;
00143    }
00144   if (outputFile_!="")
00145    { store_->save(outputFile_) ; }
00146  }
00147 
00148 MonitorElement * ElectronDqmAnalyzerBase::get( const std::string & name )
00149  {
00150   const std::string * fullName = find(name) ;
00151   if (fullName)
00152    { return store_->get(inputInternalPath_+"/"+*fullName) ; }
00153   else
00154   { return 0 ; }
00155  }
00156 
00157 void ElectronDqmAnalyzerBase::remove( const std::string & name )
00158  {
00159   const std::string * fullName = find(name) ;
00160   if (fullName)
00161    {
00162     store_->setCurrentFolder(inputInternalPath_) ;
00163     store_->removeElement(*fullName) ;
00164    }
00165  }
00166 
00167 void ElectronDqmAnalyzerBase::remove_other_dirs()
00168  {
00169   std::string currentPath = store_->pwd() ;
00170   store_->cd() ;
00171 
00172   std::string currentFolder ;
00173   std::vector<std::string> subDirs ;
00174   std::vector<std::string>::iterator subDir ;
00175   std::string delimiter = "/" ;
00176 
00177   std::string::size_type lastPos = currentPath.find_first_not_of(delimiter,0) ;
00178   std::string::size_type pos = currentPath.find_first_of(delimiter,lastPos) ;
00179   while (std::string::npos != pos || std::string::npos != lastPos)
00180    {
00181     if (currentFolder.empty())
00182      { currentFolder = currentPath.substr(lastPos, pos - lastPos) ; }
00183     else
00184      {
00185       currentFolder += "/" ;
00186       currentFolder += currentPath.substr(lastPos, pos - lastPos) ;
00187      }
00188     lastPos = currentPath.find_first_not_of(delimiter, pos);
00189     pos = currentPath.find_first_of(delimiter, lastPos);
00190 
00191     subDirs = store_->getSubdirs() ;
00192     for ( subDir = subDirs.begin() ; subDir != subDirs.end() ; subDir++ )
00193      {
00194       if (currentFolder!=(*subDir))
00195        { store_->rmdir(*subDir) ; }
00196      }
00197     store_->cd(currentFolder) ;
00198    }
00199  }
00200 
00201 MonitorElement * ElectronDqmAnalyzerBase::bookH1andDivide
00202  ( const std::string & name, const std::string & num, const std::string & denom,
00203    const std::string & titleX, const std::string & titleY,
00204    const std::string & title )
00205  { return bookH1andDivide(name,get(num),get(denom),titleX,titleY,title) ;  }
00206 
00207 MonitorElement * ElectronDqmAnalyzerBase::bookH2andDivide
00208  ( const std::string & name, const std::string & num, const std::string & denom,
00209    const std::string & titleX, const std::string & titleY,
00210    const std::string & title )
00211  { return bookH2andDivide(name,get(num),get(denom),titleX,titleY,title) ; }
00212 
00213 MonitorElement * ElectronDqmAnalyzerBase::cloneH1
00214  ( const std::string & clone, const std::string & original,
00215    const std::string & title )
00216  { return cloneH1(clone,get(original),title) ; }
00217 
00218 MonitorElement * ElectronDqmAnalyzerBase::profileX
00219  ( const std::string & me2d, const std::string & title, const std::string & titleX, const std::string & titleY,
00220    Double_t minimum, Double_t maximum )
00221  { return profileX(get(me2d),title,titleX,titleY,minimum,maximum) ; }
00222 
00223 MonitorElement * ElectronDqmAnalyzerBase::profileY
00224  ( const std::string & me2d,
00225    const std::string & title, const std::string & titleX, const std::string & titleY,
00226    Double_t minimum, Double_t maximum )
00227  { return profileY(get(me2d),title,titleX,titleY,minimum,maximum) ; }
00228 
00229 MonitorElement * ElectronDqmAnalyzerBase::bookH1
00230  ( const std::string & name, const std::string & title,
00231    int nchX, double lowX, double highX,
00232    const std::string & titleX, const std::string & titleY,
00233    Option_t * option )
00234  {
00235   MonitorElement * me = store_->book1D(newName(name),title,nchX,lowX,highX) ;
00236   if (titleX!="") { me->getTH1F()->GetXaxis()->SetTitle(titleX.c_str()) ; }
00237   if (titleY!="") { me->getTH1F()->GetYaxis()->SetTitle(titleY.c_str()) ; }
00238   if (TString(option)!="") { me->getTH1F()->SetOption(option) ; }
00239   return me ;
00240  }
00241 
00242 MonitorElement * ElectronDqmAnalyzerBase::bookH1withSumw2
00243  ( const std::string & name, const std::string & title,
00244    int nchX, double lowX, double highX,
00245    const std::string & titleX, const std::string & titleY,
00246    Option_t * option )
00247  {
00248   MonitorElement * me = store_->book1D(newName(name),title,nchX,lowX,highX) ;
00249   me->getTH1F()->Sumw2() ;
00250   if (titleX!="") { me->getTH1F()->GetXaxis()->SetTitle(titleX.c_str()) ; }
00251   if (titleY!="") { me->getTH1F()->GetYaxis()->SetTitle(titleY.c_str()) ; }
00252   if (TString(option)!="") { me->getTH1F()->SetOption(option) ; }
00253   return me ;
00254  }
00255 
00256 MonitorElement * ElectronDqmAnalyzerBase::bookH2
00257  ( const std::string & name, const std::string & title,
00258    int nchX, double lowX, double highX,
00259    int nchY, double lowY, double highY,
00260    const std::string & titleX, const std::string & titleY,
00261    Option_t * option )
00262  {
00263   MonitorElement * me = store_->book2D(newName(name),title,nchX,lowX,highX,nchY,lowY,highY) ;
00264   if (titleX!="") { me->getTH2F()->GetXaxis()->SetTitle(titleX.c_str()) ; }
00265   if (titleY!="") { me->getTH2F()->GetYaxis()->SetTitle(titleY.c_str()) ; }
00266   if (TString(option)!="") { me->getTH2F()->SetOption(option) ; }
00267   return me ;
00268  }
00269 
00270 MonitorElement * ElectronDqmAnalyzerBase::bookH2withSumw2
00271  ( const std::string & name, const std::string & title,
00272    int nchX, double lowX, double highX,
00273    int nchY, double lowY, double highY,
00274    const std::string & titleX, const std::string & titleY,
00275    Option_t * option )
00276  {
00277   MonitorElement * me = store_->book2D(newName(name),title,nchX,lowX,highX,nchY,lowY,highY) ;
00278   me->getTH2F()->Sumw2() ;
00279   if (titleX!="") { me->getTH2F()->GetXaxis()->SetTitle(titleX.c_str()) ; }
00280   if (titleY!="") { me->getTH2F()->GetYaxis()->SetTitle(titleY.c_str()) ; }
00281   if (TString(option)!="") { me->getTH2F()->SetOption(option) ; }
00282   return me ;
00283  }
00284 
00285 MonitorElement * ElectronDqmAnalyzerBase::bookP1
00286  ( const std::string & name, const std::string & title,
00287    int nchX, double lowX, double highX,
00288              double lowY, double highY,
00289    const std::string & titleX, const std::string & titleY,
00290    Option_t * option )
00291  {
00292   MonitorElement * me = store_->bookProfile(newName(name),title,nchX,lowX,highX,lowY,highY," ") ;
00293   if (titleX!="") { me->getTProfile()->GetXaxis()->SetTitle(titleX.c_str()) ; }
00294   if (titleY!="") { me->getTProfile()->GetYaxis()->SetTitle(titleY.c_str()) ; }
00295   if (TString(option)!="") { me->getTProfile()->SetOption(option) ; }
00296   return me ;
00297  }
00298 
00299 MonitorElement * ElectronDqmAnalyzerBase::bookH1andDivide
00300  ( const std::string & name, MonitorElement * num, MonitorElement * denom,
00301    const std::string & titleX, const std::string & titleY,
00302    const std::string & title )
00303  {
00304   std::string name2 = newName(name) ;
00305   TH1F * h_temp = (TH1F *)num->getTH1F()->Clone(name2.c_str()) ;
00306   h_temp->Reset() ;
00307   h_temp->Divide(num->getTH1(),denom->getTH1(),1,1,"b") ;
00308   h_temp->GetXaxis()->SetTitle(titleX.c_str()) ;
00309   h_temp->GetYaxis()->SetTitle(titleY.c_str()) ;
00310   if (title!="") { h_temp->SetTitle(title.c_str()) ; }
00311   if (verbosity_>0) { h_temp->Print() ; }
00312   MonitorElement * me = store_->book1D(name2,h_temp) ;
00313   delete h_temp ;
00314   return me ;
00315  }
00316 
00317 MonitorElement * ElectronDqmAnalyzerBase::bookH2andDivide
00318  ( const std::string & name, MonitorElement * num, MonitorElement * denom,
00319    const std::string & titleX, const std::string & titleY,
00320    const std::string & title )
00321  {
00322   std::string name2 = newName(name) ;
00323   TH2F * h_temp = (TH2F *)num->getTH2F()->Clone(name2.c_str()) ;
00324   h_temp->Reset() ;
00325   h_temp->Divide(num->getTH1(),denom->getTH1(),1,1,"b") ;
00326   h_temp->GetXaxis()->SetTitle(titleX.c_str()) ;
00327   h_temp->GetYaxis()->SetTitle(titleY.c_str()) ;
00328   if (title!="") { h_temp->SetTitle(title.c_str()) ; }
00329   if (verbosity_>0) { h_temp->Print() ; }
00330   MonitorElement * me = store_->book2D(name2,h_temp) ;
00331   delete h_temp ;
00332   return me ;
00333  }
00334 
00335 MonitorElement * ElectronDqmAnalyzerBase::cloneH1
00336  ( const std::string & name, MonitorElement * original,
00337    const std::string & title )
00338  {
00339   std::string name2 = newName(name) ;
00340   TH1F * h_temp = (TH1F *)original->getTH1F()->Clone(name2.c_str()) ;
00341   h_temp->Reset() ;
00342   if (title!="") { h_temp->SetTitle(title.c_str()) ; }
00343   MonitorElement * me = store_->book1D(name2,h_temp) ;
00344   delete h_temp ;
00345   return me ;
00346  }
00347 
00348 MonitorElement * ElectronDqmAnalyzerBase::profileX
00349  ( MonitorElement * me2d,
00350    const std::string & title, const std::string & titleX, const std::string & titleY,
00351    Double_t minimum, Double_t maximum )
00352  {
00353   std::string name2 = me2d->getName()+"_pfx" ;
00354   TProfile * p1_temp = me2d->getTH2F()->ProfileX() ;
00355   if (title!="") { p1_temp->SetTitle(title.c_str()) ; }
00356   if (titleX!="") { p1_temp->GetXaxis()->SetTitle(titleX.c_str()) ; }
00357   if (titleY!="") { p1_temp->GetYaxis()->SetTitle(titleY.c_str()) ; }
00358   if (minimum!=-1111) { p1_temp->SetMinimum(minimum) ; }
00359   if (maximum!=-1111) { p1_temp->SetMaximum(maximum) ; }
00360   MonitorElement * me = store_->bookProfile(name2,p1_temp) ;
00361   delete p1_temp ;
00362   return me ;
00363  }
00364 
00365 MonitorElement * ElectronDqmAnalyzerBase::profileY
00366  ( MonitorElement * me2d,
00367    const std::string & title, const std::string & titleX, const std::string & titleY,
00368    Double_t minimum, Double_t maximum )
00369  {
00370   std::string name2 = me2d->getName()+"_pfy" ;
00371   TProfile * p1_temp = me2d->getTH2F()->ProfileY() ;
00372   if (title!="") { p1_temp->SetTitle(title.c_str()) ; }
00373   if (titleX!="") { p1_temp->GetXaxis()->SetTitle(titleX.c_str()) ; }
00374   if (titleY!="") { p1_temp->GetYaxis()->SetTitle(titleY.c_str()) ; }
00375   if (minimum!=-1111) { p1_temp->SetMinimum(minimum) ; }
00376   if (maximum!=-1111) { p1_temp->SetMaximum(maximum) ; }
00377   MonitorElement * me = store_->bookProfile(name2,p1_temp) ;
00378   delete p1_temp ;
00379   return me ;
00380  }
00381