CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/PhysicsTools/UtilAlgos/interface/FWLiteAnalyzerWrapper.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_UtilAlgos_interface_FWLiteAnalyzerWrapper_h
00002 #define PhysicsTools_UtilAlgos_interface_FWLiteAnalyzerWrapper_h
00003 
00004 #include <string>
00005 #include <vector>
00006 #include <iostream>
00007 #include <boost/shared_ptr.hpp>
00008 
00009 #include <TFile.h>
00010 #include <TSystem.h>
00011 
00012 #include "DataFormats/FWLite/interface/ChainEvent.h"
00013 #include "DataFormats/FWLite/interface/InputSource.h"
00014 #include "DataFormats/FWLite/interface/OutputFiles.h"
00015 #include "FWCore/FWLite/interface/AutoLibraryLoader.h"
00016 #include "FWCore/ParameterSet/interface/ProcessDesc.h"
00017 #include "PhysicsTools/FWLite/interface/TFileService.h"
00018 #include "FWCore/PythonParameterSet/interface/PythonProcessDesc.h"
00019 
00101 namespace fwlite {
00102 
00103   template<class T> 
00104   class AnalyzerWrapper {
00105 
00106   public:
00108     AnalyzerWrapper(const edm::ParameterSet& cfg, std::string analyzerName, std::string directory="");
00110     virtual ~AnalyzerWrapper(){};
00112     virtual void beginJob() {  analyzer_->beginJob(); }
00114     virtual void analyze();
00116     virtual void endJob() {  analyzer_->endJob(); }
00117     
00118   protected:
00120     fwlite::InputSource inputHandler_;
00122     fwlite::OutputFiles outputHandler_;
00124     int maxEvents_;
00126     unsigned int reportAfter_;
00128     fwlite::TFileService fileService_;
00130     boost::shared_ptr<T> analyzer_;
00131   };
00132 
00134   template<class T>
00135   AnalyzerWrapper<T>::AnalyzerWrapper(const edm::ParameterSet& cfg, std::string analyzerName, std::string directory): 
00136   inputHandler_( cfg ), outputHandler_( cfg ), maxEvents_(inputHandler_.maxEvents()), 
00137   reportAfter_(inputHandler_.reportAfter()), fileService_( outputHandler_.file() ) 
00138   {
00139     // analysis specific parameters
00140     const edm::ParameterSet& ana = cfg.getParameter<edm::ParameterSet>(analyzerName.c_str());
00141     if(directory.empty()){
00142       // create analysis class of type BasicAnalyzer
00143       analyzer_ = boost::shared_ptr<T>( new T( ana, fileService_) );  
00144     }
00145     else{
00146       // create a directory in the file if directory string is non empty
00147       TFileDirectory dir = fileService_.mkdir(directory.c_str());
00148       analyzer_ = boost::shared_ptr<T>( new T( ana, dir ) );  
00149     }
00150   }
00151     
00153   template<class T> 
00154   void AnalyzerWrapper<T>::analyze(){
00155     int ievt=0;  
00156     std::vector<std::string> const & inputFiles = inputHandler_.files();
00157     // loop the vector of input files
00158     fwlite::ChainEvent event( inputFiles );
00159     for(event.toBegin(); !event.atEnd(); ++event, ++ievt){
00160       // break loop if maximal number of events is reached 
00161       if(maxEvents_>0 ? ievt+1>maxEvents_ : false) break;
00162       // simple event counter
00163       if(reportAfter_!=0 ? (ievt>0 && ievt%reportAfter_==0) : false) 
00164         std::cout << "  processing event: " << ievt << std::endl;
00165       // analyze event
00166       analyzer_->analyze(event);
00167     }
00168   }
00169 }
00170 
00171 #endif