CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/PhysicsTools/UtilAlgos/plugins/ConfigurableAnalysis.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    ConfigurableAnalysis
00004 // Class:      ConfigurableAnalysis
00005 // 
00013 //
00014 // Original Author:  Jean-Roch Vlimant
00015 //         Created:  Mon Apr 14 11:39:51 CEST 2008
00016 // $Id: ConfigurableAnalysis.cc,v 1.10 2010/09/28 09:08:33 srappocc Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <memory>
00023 
00024 // user include files
00025 #include "FWCore/Framework/interface/Frameworkfwd.h"
00026 #include "FWCore/Framework/interface/EDProducer.h"
00027 #include "FWCore/Framework/interface/EDFilter.h"
00028 
00029 #include "FWCore/Framework/interface/Event.h"
00030 #include "FWCore/Framework/interface/EventSetup.h"
00031 #include "FWCore/Framework/interface/MakerMacros.h"
00032 #include "FWCore/ServiceRegistry/interface/Service.h"
00033 
00034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00035 
00036 #include "PhysicsTools/UtilAlgos/interface/Selections.h"
00037 #include "PhysicsTools/UtilAlgos/interface/Plotter.h"
00038 #include "PhysicsTools/UtilAlgos/interface/NTupler.h"
00039 #include "PhysicsTools/UtilAlgos/interface/InputTagDistributor.h"
00040 
00041 //
00042 // class decleration
00043 //
00044 
00045 class ConfigurableAnalysis : public edm::EDFilter {
00046    public:
00047       explicit ConfigurableAnalysis(const edm::ParameterSet&);
00048       ~ConfigurableAnalysis();
00049 
00050    private:
00051       virtual void beginJob();
00052       virtual bool filter(edm::Event&, const edm::EventSetup&);
00053       virtual void endJob() ;
00054 
00055   Selections * selections_;
00056   Plotter * plotter_;
00057   NTupler * ntupler_;
00058 
00059   std::vector<std::string> flows_;
00060   bool workAsASelector_;
00061 };
00062 
00063 //
00064 // constants, enums and typedefs
00065 //
00066 
00067 //
00068 // static data member definitions
00069 //
00070 
00071 //
00072 // constructors and destructor
00073 //
00074 ConfigurableAnalysis::ConfigurableAnalysis(const edm::ParameterSet& iConfig) :
00075   selections_(0), plotter_(0), ntupler_(0)
00076 {
00077 
00078   std::string moduleLabel = iConfig.getParameter<std::string>("@module_label");
00079 
00080   //configure inputag distributor
00081   if (iConfig.exists("InputTags"))
00082     edm::Service<InputTagDistributorService>()->init(moduleLabel,iConfig.getParameter<edm::ParameterSet>("InputTags"));
00083 
00084   //configure the variable helper
00085   edm::Service<VariableHelperService>()->init(moduleLabel,iConfig.getParameter<edm::ParameterSet>("Variables"));
00086 
00087   //list of selections
00088   selections_ = new Selections(iConfig.getParameter<edm::ParameterSet>("Selections"));
00089 
00090   //plotting device
00091   edm::ParameterSet plotPset = iConfig.getParameter<edm::ParameterSet>("Plotter");
00092   if (!plotPset.empty()){
00093     std::string plotterName = plotPset.getParameter<std::string>("ComponentName");
00094     plotter_ = PlotterFactory::get()->create(plotterName, plotPset);
00095   }
00096   else
00097     plotter_ = 0;
00098 
00099   //ntupling device
00100   edm::ParameterSet ntPset = iConfig.getParameter<edm::ParameterSet>("Ntupler");
00101   if (!ntPset.empty()){
00102     std::string ntuplerName=ntPset.getParameter<std::string>("ComponentName");
00103     ntupler_ = NTuplerFactory::get()->create(ntuplerName, ntPset);
00104   }
00105   else ntupler_=0;
00106   
00107   flows_ = iConfig.getParameter<std::vector<std::string> >("flows");
00108   workAsASelector_ = iConfig.getParameter<bool>("workAsASelector");
00109 
00110   //vector of passed selections
00111   produces<std::vector<bool> >();
00112 
00113   //ntupler needs to register its products
00114   if (ntupler_) ntupler_->registerleaves(this);
00115 }
00116 
00117 ConfigurableAnalysis::~ConfigurableAnalysis()
00118 {
00119   delete selections_;
00120 }
00121 
00122 
00123 //
00124 // member functions
00125 //
00126 
00127 // ------------ method called to produce the data  ------------
00128 bool ConfigurableAnalysis::filter(edm::Event& iEvent, const edm::EventSetup& iSetup)
00129 {
00130   using namespace edm;
00131 
00132   //will the filter pass or not.
00133   bool majorGlobalAccept=false;
00134 
00135   std::auto_ptr<std::vector<bool> > passedProduct(new std::vector<bool>(flows_.size(),false));
00136   bool filledOnce=false;  
00137 
00138   // loop the requested selections
00139   for (Selections::iterator selection=selections_->begin(); selection!=selections_->end();++selection){
00140     //was this flow of filter actually asked for
00141     bool skip=true;
00142     unsigned int iFlow=0;
00143     for (;iFlow!=flows_.size();++iFlow){if (flows_[iFlow]==selection->name()){skip=false; break;}}
00144     if (skip) continue;
00145 
00146     //make a specific direction in the plotter
00147     if (plotter_) plotter_->setDir(selection->name());
00148     
00149     // apply individual filters on the event
00150     std::map<std::string, bool> accept=selection->accept(iEvent);
00151     
00152     bool globalAccept=true;
00153     std::string separator="";
00154     std::string cumulative="";
00155     std::string allButOne="allBut_";
00156     std::string fullAccept="fullAccept";
00157     std::string fullContent="fullContent";
00158 
00159     if (selection->makeContentPlots() && plotter_)
00160       plotter_->fill(fullContent,iEvent);
00161 
00162     //loop the filters to make cumulative and allButOne job
00163     for (Selection::iterator filterIt=selection->begin(); filterIt!=selection->end();++filterIt){
00164       Filter & filter=(**filterIt);
00165       //      bool lastCut=((filterIt+1)==selection->end());
00166 
00167       //increment the directory name
00168       cumulative+=separator+filter.name(); separator="_";
00169 
00170       if (accept[filter.name()]){
00171         //      if (globalAccept && selection->makeCumulativePlots() && !lastCut)
00172         if (globalAccept && selection->makeCumulativePlots() && plotter_)
00173           plotter_->fill(cumulative,iEvent);
00174       }
00175       else{
00176         globalAccept=false;
00177         // did all the others filter fire
00178         bool goodForAllButThisOne=true;
00179         for (std::map<std::string,bool>::iterator decision=accept.begin(); decision!=accept.end();++decision){
00180           if (decision->first==filter.name()) continue;
00181           if (!decision->second) {
00182             goodForAllButThisOne=false;
00183             break;}
00184         }
00185         if (goodForAllButThisOne && selection->makeAllButOnePlots() && plotter_){
00186           plotter_->fill(allButOne+filter.name(),iEvent);
00187         }
00188       }
00189       
00190     }// loop over the filters in this selection
00191 
00192     if (globalAccept){
00193       (*passedProduct)[iFlow]=true;
00194       majorGlobalAccept=true;
00195       //make final plots only if no cumulative plots
00196       if (selection->makeFinalPlots() && !selection->makeCumulativePlots() && plotter_)
00197         plotter_->fill(fullAccept,iEvent);
00198 
00199       //make the ntuple and put it in the event
00200       if (selection->ntuplize() && !filledOnce && ntupler_){
00201         ntupler_->fill(iEvent);
00202         filledOnce=true;}
00203     }
00204     
00205   }//loop the different filter order/number: loop the Selections
00206 
00207   iEvent.put(passedProduct);
00208   if (workAsASelector_)
00209     return majorGlobalAccept;
00210   else
00211     return true;
00212 }
00213    
00214 
00215 // ------------ method called once each job just before starting event loop  ------------
00216 void 
00217 ConfigurableAnalysis::beginJob()
00218 {
00219 }
00220 
00221 // ------------ method called once each job just after ending the event loop  ------------
00222 void 
00223 ConfigurableAnalysis::endJob() {
00224   //print summary tables
00225   selections_->print();
00226   if (plotter_) plotter_->complete();
00227 }
00228 
00229 
00230 DEFINE_FWK_MODULE(ConfigurableAnalysis);