Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:17

0001 // -*- C++ -*-
0002 //
0003 // Package:    ConfigurableAnalysis
0004 // Class:      ConfigurableAnalysis
0005 //
0006 /**\class ConfigurableAnalysis ConfigurableAnalysis.cc CommonTools/UtilAlgos/src/ConfigurableAnalysis.cc
0007 
0008  Description: <one line class summary>
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Jean-Roch Vlimant
0015 //         Created:  Mon Apr 14 11:39:51 CEST 2008
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/one/EDFilter.h"
0025 
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/EventSetup.h"
0028 #include "FWCore/Framework/interface/ConsumesCollector.h"
0029 #include "FWCore/Framework/interface/MakerMacros.h"
0030 #include "FWCore/ServiceRegistry/interface/Service.h"
0031 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0032 
0033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0034 
0035 #include "PhysicsTools/UtilAlgos/interface/Selections.h"
0036 #include "PhysicsTools/UtilAlgos/interface/Plotter.h"
0037 #include "PhysicsTools/UtilAlgos/interface/NTupler.h"
0038 #include "CommonTools/UtilAlgos/interface/InputTagDistributor.h"
0039 
0040 //
0041 // class decleration
0042 //
0043 
0044 class ConfigurableAnalysis : public edm::one::EDFilter<edm::one::SharedResources> {
0045 public:
0046   explicit ConfigurableAnalysis(const edm::ParameterSet&);
0047 
0048 private:
0049   bool filter(edm::Event&, const edm::EventSetup&) override;
0050   void endJob() override;
0051 
0052   std::unique_ptr<FilterSelections> selections_;
0053   std::unique_ptr<Plotter> plotter_;
0054   std::unique_ptr<NTupler> ntupler_;
0055 
0056   std::vector<std::string> flows_;
0057   bool workAsASelector_;
0058 };
0059 
0060 //
0061 // constants, enums and typedefs
0062 //
0063 
0064 //
0065 // static data member definitions
0066 //
0067 
0068 //
0069 // constructors and destructor
0070 //
0071 ConfigurableAnalysis::ConfigurableAnalysis(const edm::ParameterSet& iConfig) {
0072   usesResource(TFileService::kSharedResource);
0073 
0074   std::string moduleLabel = iConfig.getParameter<std::string>("@module_label");
0075 
0076   //configure inputag distributor
0077   if (iConfig.exists("InputTags"))
0078     edm::Service<InputTagDistributorService>()->init(
0079         moduleLabel, iConfig.getParameter<edm::ParameterSet>("InputTags"), consumesCollector());
0080 
0081   //configure the variable helper
0082   edm::Service<VariableHelperService>()->init(
0083       moduleLabel, iConfig.getParameter<edm::ParameterSet>("Variables"), consumesCollector());
0084 
0085   //list of selections
0086   selections_ =
0087       std::make_unique<FilterSelections>(iConfig.getParameter<edm::ParameterSet>("Selections"), consumesCollector());
0088 
0089   //plotting device
0090   edm::ParameterSet plotPset = iConfig.getParameter<edm::ParameterSet>("Plotter");
0091   if (!plotPset.empty()) {
0092     std::string plotterName = plotPset.getParameter<std::string>("ComponentName");
0093     plotter_ = PlotterFactory::get()->create(plotterName, plotPset);
0094   }
0095 
0096   //ntupling device
0097   edm::ParameterSet ntPset = iConfig.getParameter<edm::ParameterSet>("Ntupler");
0098   if (!ntPset.empty()) {
0099     std::string ntuplerName = ntPset.getParameter<std::string>("ComponentName");
0100     ntupler_ = NTuplerFactory::get()->create(ntuplerName, ntPset);
0101   }
0102 
0103   flows_ = iConfig.getParameter<std::vector<std::string>>("flows");
0104   workAsASelector_ = iConfig.getParameter<bool>("workAsASelector");
0105 
0106   //vector of passed selections
0107   produces<std::vector<bool>>();
0108 
0109   //ntupler needs to register its products
0110   if (ntupler_)
0111     ntupler_->registerleaves(producesCollector());
0112 }
0113 
0114 //
0115 // member functions
0116 //
0117 
0118 // ------------ method called to produce the data  ------------
0119 bool ConfigurableAnalysis::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0120   //will the filter pass or not.
0121   bool majorGlobalAccept = false;
0122 
0123   auto passedProduct = std::make_unique<std::vector<bool>>(flows_.size(), false);
0124   bool filledOnce = false;
0125 
0126   // loop the requested selections
0127   for (FilterSelections::iterator selection = selections_->begin(); selection != selections_->end(); ++selection) {
0128     //was this flow of filter actually asked for
0129     bool skip = true;
0130     unsigned int iFlow = 0;
0131     for (; iFlow != flows_.size(); ++iFlow) {
0132       if (flows_[iFlow] == selection->name()) {
0133         skip = false;
0134         break;
0135       }
0136     }
0137     if (skip)
0138       continue;
0139 
0140     //make a specific direction in the plotter
0141     if (plotter_)
0142       plotter_->setDir(selection->name());
0143 
0144     // apply individual filters on the event
0145     std::map<std::string, bool> accept = selection->acceptMap(iEvent);
0146 
0147     bool globalAccept = true;
0148     std::string separator = "";
0149     std::string cumulative = "";
0150     std::string allButOne = "allBut_";
0151     std::string fullAccept = "fullAccept";
0152     std::string fullContent = "fullContent";
0153 
0154     if (selection->makeContentPlots() && plotter_)
0155       plotter_->fill(fullContent, iEvent);
0156 
0157     //loop the filters to make cumulative and allButOne job
0158     for (FilterSelection::iterator filterIt = selection->begin(); filterIt != selection->end(); ++filterIt) {
0159       SFilter& filter = (*filterIt);
0160       //      bool lastCut=((filterIt+1)==selection->end());
0161 
0162       //increment the directory name
0163       cumulative += separator;
0164       if (filter.inverted())
0165         cumulative += "not";
0166       cumulative += filter->name();
0167       separator = "_";
0168 
0169       if (accept[filter->name()]) {
0170         //  if (globalAccept && selection->makeCumulativePlots() && !lastCut)
0171         if (globalAccept && selection->makeCumulativePlots() && plotter_)
0172           plotter_->fill(cumulative, iEvent);
0173       } else {
0174         globalAccept = false;
0175         // did all the others filter fire
0176         bool goodForAllButThisOne = true;
0177         for (std::map<std::string, bool>::iterator decision = accept.begin(); decision != accept.end(); ++decision) {
0178           if (decision->first == filter->name())
0179             continue;
0180           if (!decision->second) {
0181             goodForAllButThisOne = false;
0182             break;
0183           }
0184         }
0185         if (goodForAllButThisOne && selection->makeAllButOnePlots() && plotter_) {
0186           plotter_->fill(allButOne + filter->name(), iEvent);
0187         }
0188       }
0189 
0190     }  // loop over the filters in this selection
0191 
0192     if (globalAccept) {
0193       (*passedProduct)[iFlow] = true;
0194       majorGlobalAccept = true;
0195       //make final plots only if no cumulative plots
0196       if (selection->makeFinalPlots() && !selection->makeCumulativePlots() && plotter_)
0197         plotter_->fill(fullAccept, iEvent);
0198 
0199       //make the ntuple and put it in the event
0200       if (selection->ntuplize() && !filledOnce && ntupler_) {
0201         ntupler_->fill(iEvent);
0202         filledOnce = true;
0203       }
0204     }
0205 
0206   }  //loop the different filter order/number: loop the Selections
0207 
0208   iEvent.put(std::move(passedProduct));
0209   if (workAsASelector_)
0210     return majorGlobalAccept;
0211   else
0212     return true;
0213 }
0214 
0215 // ------------ method called once each job just after ending the event loop  ------------
0216 void ConfigurableAnalysis::endJob() {
0217   //print summary tables
0218   selections_->print();
0219   if (plotter_)
0220     plotter_->complete();
0221 }
0222 
0223 DEFINE_FWK_MODULE(ConfigurableAnalysis);