CMS 3D CMS Logo

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 
20 // system include files
21 #include <memory>
22 
23 // user include files
27 
33 
35 
40 
41 //
42 // class decleration
43 //
44 
46  public:
47  explicit ConfigurableAnalysis(const edm::ParameterSet&);
48  ~ConfigurableAnalysis() override = default;
49 
50  private:
51  void beginJob() override;
52  bool filter(edm::Event&, const edm::EventSetup&) override;
53  void endJob() override ;
54 
55  std::unique_ptr<FilterSelections> selections_;
56  std::unique_ptr<Plotter> plotter_;
57  std::unique_ptr<NTupler> ntupler_;
58 
59  std::vector<std::string> flows_;
61 };
62 
63 //
64 // constants, enums and typedefs
65 //
66 
67 //
68 // static data member definitions
69 //
70 
71 //
72 // constructors and destructor
73 //
75 {
76 
77  std::string moduleLabel = iConfig.getParameter<std::string>("@module_label");
78 
79  //configure inputag distributor
80  if (iConfig.exists("InputTags"))
82 
83  //configure the variable helper
85 
86  //list of selections
87  selections_ = 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_ = std::unique_ptr<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_ = std::unique_ptr<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_) ntupler_->registerleaves(this);
111 }
112 
113 //
114 // member functions
115 //
116 
117 // ------------ method called to produce the data ------------
119 {
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){if (flows_[iFlow]==selection->name()){skip=false; break;}}
132  if (skip) continue;
133 
134  //make a specific direction in the plotter
135  if (plotter_) plotter_->setDir(selection->name());
136 
137  // apply individual filters on the event
138  std::map<std::string, bool> accept=selection->acceptMap(iEvent);
139 
140  bool globalAccept=true;
143  std::string allButOne="allBut_";
144  std::string fullAccept="fullAccept";
145  std::string fullContent="fullContent";
146 
147  if (selection->makeContentPlots() && plotter_)
148  plotter_->fill(fullContent,iEvent);
149 
150  //loop the filters to make cumulative and allButOne job
151  for (FilterSelection::iterator filterIt=selection->begin(); filterIt!=selection->end();++filterIt){
152  SFilter & filter = (*filterIt);
153  // bool lastCut=((filterIt+1)==selection->end());
154 
155  //increment the directory name
156  cumulative+=separator;
157  if (filter.inverted()) cumulative+="not";
158  cumulative+=filter->name();
159  separator="_";
160 
161  if (accept[filter->name()]){
162  // if (globalAccept && selection->makeCumulativePlots() && !lastCut)
163  if (globalAccept && selection->makeCumulativePlots() && plotter_)
164  plotter_->fill(cumulative,iEvent);
165  }
166  else{
167  globalAccept=false;
168  // did all the others filter fire
169  bool goodForAllButThisOne=true;
170  for (std::map<std::string,bool>::iterator decision=accept.begin(); decision!=accept.end();++decision){
171  if (decision->first==filter->name()) continue;
172  if (!decision->second) {
173  goodForAllButThisOne=false;
174  break;}
175  }
176  if (goodForAllButThisOne && selection->makeAllButOnePlots() && plotter_){
177  plotter_->fill(allButOne+filter->name(),iEvent);
178  }
179  }
180 
181  }// loop over the filters in this selection
182 
183  if (globalAccept){
184  (*passedProduct)[iFlow]=true;
185  majorGlobalAccept=true;
186  //make final plots only if no cumulative plots
187  if (selection->makeFinalPlots() && !selection->makeCumulativePlots() && plotter_)
188  plotter_->fill(fullAccept,iEvent);
189 
190  //make the ntuple and put it in the event
191  if (selection->ntuplize() && !filledOnce && ntupler_){
192  ntupler_->fill(iEvent);
193  filledOnce=true;}
194  }
195 
196  }//loop the different filter order/number: loop the Selections
197 
198  iEvent.put(std::move(passedProduct));
199  if (workAsASelector_)
200  return majorGlobalAccept;
201  else
202  return true;
203 }
204 
205 
206 // ------------ method called once each job just before starting event loop ------------
207 void
209 {
210 }
211 
212 // ------------ method called once each job just after ending the event loop ------------
213 void
215  //print summary tables
216  selections_->print();
217  if (plotter_) plotter_->complete();
218 }
219 
220 
T getParameter(std::string const &) const
~ConfigurableAnalysis() override=default
string separator
Definition: mps_merge.py:79
std::vector< FilterSelection >::iterator iterator
Definition: Selections.h:324
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
int init
Definition: HydjetWrapper.h:67
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:30
std::vector< SFilter >::iterator iterator
Definition: Selections.h:143
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
const std::string & name()
Definition: Selections.h:38
bool filter(edm::Event &, const edm::EventSetup &) override
bool inverted()
Definition: Selections.h:84
std::unique_ptr< NTupler > ntupler_
ConfigurableAnalysis(const edm::ParameterSet &)
std::unique_ptr< Plotter > plotter_
std::unique_ptr< FilterSelections > selections_
def move(src, dest)
Definition: eostools.py:511
T get(const Candidate &c)
Definition: component.h:55
std::vector< std::string > flows_