CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ConfigurableAnalysis.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ConfigurableAnalysis
4 // Class: ConfigurableAnalysis
5 //
13 //
14 // Original Author: Jean-Roch Vlimant
15 // Created: Mon Apr 14 11:39:51 CEST 2008
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
26 
32 
34 
39 
40 //
41 // class decleration
42 //
43 
45 public:
46  explicit ConfigurableAnalysis(const edm::ParameterSet&);
47  ~ConfigurableAnalysis() override = default;
48 
49 private:
50  void beginJob() override;
51  bool filter(edm::Event&, const edm::EventSetup&) override;
52  void endJob() override;
53 
54  std::unique_ptr<FilterSelections> selections_;
55  std::unique_ptr<Plotter> plotter_;
56  std::unique_ptr<NTupler> ntupler_;
57 
58  std::vector<std::string> flows_;
60 };
61 
62 //
63 // constants, enums and typedefs
64 //
65 
66 //
67 // static data member definitions
68 //
69 
70 //
71 // constructors and destructor
72 //
74  std::string moduleLabel = iConfig.getParameter<std::string>("@module_label");
75 
76  //configure inputag distributor
77  if (iConfig.exists("InputTags"))
79  moduleLabel, iConfig.getParameter<edm::ParameterSet>("InputTags"), consumesCollector());
80 
81  //configure the variable helper
83  moduleLabel, iConfig.getParameter<edm::ParameterSet>("Variables"), consumesCollector());
84 
85  //list of selections
86  selections_ =
87  std::make_unique<FilterSelections>(iConfig.getParameter<edm::ParameterSet>("Selections"), consumesCollector());
88 
89  //plotting device
91  if (!plotPset.empty()) {
92  std::string plotterName = plotPset.getParameter<std::string>("ComponentName");
93  plotter_ = PlotterFactory::get()->create(plotterName, plotPset);
94  }
95 
96  //ntupling device
97  edm::ParameterSet ntPset = iConfig.getParameter<edm::ParameterSet>("Ntupler");
98  if (!ntPset.empty()) {
99  std::string ntuplerName = ntPset.getParameter<std::string>("ComponentName");
100  ntupler_ = NTuplerFactory::get()->create(ntuplerName, ntPset);
101  }
102 
103  flows_ = iConfig.getParameter<std::vector<std::string>>("flows");
104  workAsASelector_ = iConfig.getParameter<bool>("workAsASelector");
105 
106  //vector of passed selections
107  produces<std::vector<bool>>();
108 
109  //ntupler needs to register its products
110  if (ntupler_)
111  ntupler_->registerleaves(producesCollector());
112 }
113 
114 //
115 // member functions
116 //
117 
118 // ------------ method called to produce the data ------------
120  //will the filter pass or not.
121  bool majorGlobalAccept = false;
122 
123  auto passedProduct = std::make_unique<std::vector<bool>>(flows_.size(), false);
124  bool filledOnce = false;
125 
126  // loop the requested selections
128  //was this flow of filter actually asked for
129  bool skip = true;
130  unsigned int iFlow = 0;
131  for (; iFlow != flows_.size(); ++iFlow) {
132  if (flows_[iFlow] == selection->name()) {
133  skip = false;
134  break;
135  }
136  }
137  if (skip)
138  continue;
139 
140  //make a specific direction in the plotter
141  if (plotter_)
142  plotter_->setDir(selection->name());
143 
144  // apply individual filters on the event
145  std::map<std::string, bool> accept = selection->acceptMap(iEvent);
146 
147  bool globalAccept = true;
148  std::string separator = "";
149  std::string cumulative = "";
150  std::string allButOne = "allBut_";
151  std::string fullAccept = "fullAccept";
152  std::string fullContent = "fullContent";
153 
154  if (selection->makeContentPlots() && plotter_)
155  plotter_->fill(fullContent, iEvent);
156 
157  //loop the filters to make cumulative and allButOne job
158  for (FilterSelection::iterator filterIt = selection->begin(); filterIt != selection->end(); ++filterIt) {
159  SFilter& filter = (*filterIt);
160  // bool lastCut=((filterIt+1)==selection->end());
161 
162  //increment the directory name
163  cumulative += separator;
164  if (filter.inverted())
165  cumulative += "not";
166  cumulative += filter->name();
167  separator = "_";
168 
169  if (accept[filter->name()]) {
170  // if (globalAccept && selection->makeCumulativePlots() && !lastCut)
171  if (globalAccept && selection->makeCumulativePlots() && plotter_)
172  plotter_->fill(cumulative, iEvent);
173  } else {
174  globalAccept = false;
175  // did all the others filter fire
176  bool goodForAllButThisOne = true;
177  for (std::map<std::string, bool>::iterator decision = accept.begin(); decision != accept.end(); ++decision) {
178  if (decision->first == filter->name())
179  continue;
180  if (!decision->second) {
181  goodForAllButThisOne = false;
182  break;
183  }
184  }
185  if (goodForAllButThisOne && selection->makeAllButOnePlots() && plotter_) {
186  plotter_->fill(allButOne + filter->name(), iEvent);
187  }
188  }
189 
190  } // loop over the filters in this selection
191 
192  if (globalAccept) {
193  (*passedProduct)[iFlow] = true;
194  majorGlobalAccept = true;
195  //make final plots only if no cumulative plots
196  if (selection->makeFinalPlots() && !selection->makeCumulativePlots() && plotter_)
197  plotter_->fill(fullAccept, iEvent);
198 
199  //make the ntuple and put it in the event
200  if (selection->ntuplize() && !filledOnce && ntupler_) {
201  ntupler_->fill(iEvent);
202  filledOnce = true;
203  }
204  }
205 
206  } //loop the different filter order/number: loop the Selections
207 
208  iEvent.put(std::move(passedProduct));
209  if (workAsASelector_)
210  return majorGlobalAccept;
211  else
212  return true;
213 }
214 
215 // ------------ method called once each job just before starting event loop ------------
217 
218 // ------------ method called once each job just after ending the event loop ------------
220  //print summary tables
221  selections_->print();
222  if (plotter_)
223  plotter_->complete();
224 }
225 
bool empty() const
Definition: ParameterSet.h:201
~ConfigurableAnalysis() override=default
std::vector< FilterSelection >::iterator iterator
Definition: Selections.h:327
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int init
Definition: HydjetWrapper.h:64
selection
main part
Definition: corrVsCorr.py:100
bool exists(std::string const &parameterName) const
checks if a parameter exists
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:31
std::vector< SFilter >::iterator iterator
Definition: Selections.h:139
int iEvent
Definition: GenABIO.cc:224
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
def move
Definition: eostools.py:511
const std::string & name()
Definition: Selections.h:37
bool filter(edm::Event &, const edm::EventSetup &) override
bool inverted()
Definition: Selections.h:82
ProducesCollector producesCollector()
std::unique_ptr< NTupler > ntupler_
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
ConfigurableAnalysis(const edm::ParameterSet &)
static std::string const separator(":")
#define get
std::unique_ptr< Plotter > plotter_
std::unique_ptr< FilterSelections > selections_
std::vector< std::string > flows_