CMS 3D CMS Logo

FSQDiJetAve.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: DQMOffline/FSQDiJetAve
4 // Class: FSQDiJetAve
5 //
12 //
13 // Original Author: Tomasz Fruboes
14 // Created: Tue, 04 Nov 2014 11:36:27 GMT
15 //
16 //
17 // system include files
18 #include <memory>
19 
20 // user include files
26 
28 
31 
33 
42 
43 #include <boost/algorithm/string.hpp>
46 
47 using namespace edm;
48 using namespace std;
49 
50 namespace FSQ {
51  //################################################################################################
52  //
53  // Base Handler class
54  //
55  //################################################################################################
56  class BaseHandler {
57  public:
60 
61  BaseHandler();
62  virtual ~BaseHandler() = default;
64  : m_expression(triggerExpression::parse(iConfig.getParameter<std::string>("triggerSelection"))) {
65  // extract list of used paths
66  std::vector<std::string> strs;
67  std::string triggerSelection = iConfig.getParameter<std::string>("triggerSelection");
68  boost::split(strs, triggerSelection, boost::is_any_of("\t ,`!@#$%^&*()~/\\"));
69  for (auto& str : strs) {
70  if (str.find("HLT_") == 0) {
71  m_usedPaths.insert(str);
72  }
73  }
74 
75  m_eventCache = &eventCache;
76  std::string pathPartialName = iConfig.getParameter<std::string>("partialPathName");
77  m_dirname = iConfig.getUntrackedParameter("mainDQMDirname", std::string("HLT/FSQ/")) + pathPartialName + "/";
78  m_pset = iConfig;
79  };
80  virtual void analyze(const edm::Event& iEvent,
81  const edm::EventSetup& iSetup,
83  const trigger::TriggerEvent& trgEvent,
86  float weight) = 0;
87  virtual void book(DQMStore::IBooker& booker) = 0;
88  virtual void getAndStoreTokens(edm::ConsumesCollector&& iC) = 0;
89 
90  std::unique_ptr<triggerExpression::Evaluator> m_expression;
93  std::map<std::string, MonitorElement*> m_histos;
94  std::set<std::string> m_usedPaths;
96  };
97  //################################################################################################
98  //
99  // Handle objects saved into hlt event by hlt filters
100  //
101  //################################################################################################
103  template <class TInputCandidateType, class TOutputCandidateType, SpecialFilters filter = None>
104  class HandlerTemplate : public BaseHandler {
105  private:
107  std::string m_pathPartialName; //#("HLT_DiPFJetAve30_HFJEC_");
108  std::string m_filterPartialName; //#("ForHFJECBase"); // Calo jet preFilter
109 
111 
115  std::map<std::string, std::shared_ptr<StringObjectFunction<std::vector<TOutputCandidateType> > > >
117  std::map<std::string, std::shared_ptr<StringObjectFunction<TInputCandidateType> > > m_plottersSingleObject;
119  static const int SingleObjectPlotter = 0;
120  static const int CombinedObjectPlotter = 1;
121  std::map<std::string, int> m_plotterType;
122  std::vector<edm::ParameterSet> m_combinedObjectDrawables;
123  std::vector<edm::ParameterSet> m_singleObjectDrawables; // for all single objects passing preselection
124  bool m_isSetup;
126  std::map<std::string, edm::EDGetToken> m_tokens;
127 
128  public:
130  : BaseHandler(iConfig, eventCache),
131  m_singleObjectSelection(iConfig.getParameter<std::string>("singleObjectsPreselection")),
132  m_combinedObjectSelection(iConfig.getParameter<std::string>("combinedObjectSelection")),
133  m_combinedObjectSortFunction(iConfig.getParameter<std::string>("combinedObjectSortCriteria")) {
134  std::string type = iConfig.getParameter<std::string>("handlerType");
135  if (type != "FromHLT") {
136  m_input = iConfig.getParameter<edm::InputTag>("inputCol");
137  }
138 
139  m_dqmhistolabel = iConfig.getParameter<std::string>("dqmhistolabel");
140  m_filterPartialName =
141  iConfig.getParameter<std::string>("partialFilterName"); // std::string find is used to match filter
142  m_pathPartialName = iConfig.getParameter<std::string>("partialPathName");
143  m_combinedObjectDimension = iConfig.getParameter<int>("combinedObjectDimension");
144  m_combinedObjectDrawables = iConfig.getParameter<std::vector<edm::ParameterSet> >("combinedObjectDrawables");
145  m_singleObjectDrawables = iConfig.getParameter<std::vector<edm::ParameterSet> >("singleObjectDrawables");
146  m_isSetup = false;
147  }
148  ~HandlerTemplate() override = default;
149  void book(DQMStore::IBooker& booker) override {
150  if (!m_isSetup) {
151  booker.setCurrentFolder(m_dirname);
152  m_isSetup = true;
153  std::vector<std::vector<edm::ParameterSet>*> todo(2, (std::vector<edm::ParameterSet>*)nullptr);
154  todo[CombinedObjectPlotter] = &m_combinedObjectDrawables;
155  todo[SingleObjectPlotter] = &m_singleObjectDrawables;
156  for (size_t ti = 0; ti < todo.size(); ++ti) {
157  for (size_t i = 0; i < todo[ti]->size(); ++i) {
158  std::string histoName = m_dqmhistolabel + "_" + todo[ti]->at(i).template getParameter<std::string>("name");
159  std::string expression = todo[ti]->at(i).template getParameter<std::string>("expression");
160  int bins = todo[ti]->at(i).template getParameter<int>("bins");
161  double rangeLow = todo[ti]->at(i).template getParameter<double>("min");
162  double rangeHigh = todo[ti]->at(i).template getParameter<double>("max");
163  m_histos[histoName] = booker.book1D(histoName, histoName, bins, rangeLow, rangeHigh);
164  m_plotterType[histoName] = ti;
165  if (ti == CombinedObjectPlotter) {
167  m_plottersCombinedObject[histoName] =
168  std::shared_ptr<StringObjectFunction<std::vector<TOutputCandidateType> > >(func);
169  } else {
170  auto* func = new StringObjectFunction<TInputCandidateType>(expression);
171  m_plottersSingleObject[histoName] = std::shared_ptr<StringObjectFunction<TInputCandidateType> >(func);
172  }
173  }
174  }
175  }
176  }
178  edm::EDGetTokenT<std::vector<TInputCandidateType> > tok = iC.consumes<std::vector<TInputCandidateType> >(m_input);
179  m_tokens[m_input.encode()] = edm::EDGetToken(tok);
180  }
181 
182  //#############################################################################
183  // Count objects. To avoid code duplication we do it in a separate template -
184  // - partial specialization not easy...:
185  // http://stackoverflow.com/questions/21182729/specializing-single-method-in-a-big-template-class
186  //#############################################################################
187  template <class T>
189  int ret = 0;
190  Handle<std::vector<T> > hIn;
191  iEvent.getByToken(m_tokens[input.encode()], hIn);
192  if (!hIn.isValid()) {
193  edm::LogError("FSQDiJetAve") << "product not found: " << input.encode();
194  return -1; // return nonsense value
195  }
196  for (size_t i = 0; i < hIn->size(); ++i) {
197  bool preselection = sel(hIn->at(i));
198  if (preselection) {
199  fillSingleObjectPlots(hIn->at(i), weight);
200  ret += 1;
201  }
202  }
203  return ret;
204  }
205 
206  // FIXME (?): code duplication
207  void fillSingleObjectPlots(const TInputCandidateType& cand, float weight) {
208  std::map<std::string, MonitorElement*>::iterator it, itE;
209  it = m_histos.begin();
210  itE = m_histos.end();
211  for (; it != itE; ++it) {
212  if (m_plotterType[it->first] != SingleObjectPlotter)
213  continue;
214  float val = (*m_plottersSingleObject[it->first])(cand);
215  it->second->Fill(val, weight);
216  }
217  }
218  // Notes:
219  // - this function (and specialized versions) are responsible for calling
220  // fillSingleObjectPlots for all single objects passing the single
221  // object preselection criteria
222  // - FIXME this function should take only event/ event setup (?)
223  // - FIXME responsibility to apply preselection should be elsewhere
224  // hard to fix, since we dont want to copy all objects due to
225  // performance reasons
226  // - note: implementation below working when in/out types are equal
227  // in other cases you must provide specialized version (see below)
228  void getFilteredCands(TInputCandidateType*,
229  std::vector<TOutputCandidateType>& cands,
230  const edm::Event& iEvent,
231  const edm::EventSetup& iSetup,
233  const trigger::TriggerEvent& trgEvent,
234  float weight) {
236  iEvent.getByToken(m_tokens[m_input.encode()], hIn);
237 
238  if (!hIn.isValid()) {
239  edm::LogError("FSQDiJetAve") << "product not found: " << m_input.encode();
240  return;
241  }
242 
243  for (size_t i = 0; i < hIn->size(); ++i) {
244  bool preselection = m_singleObjectSelection(hIn->at(i));
245  if (preselection) {
246  fillSingleObjectPlots(hIn->at(i), weight);
247  cands.push_back(hIn->at(i));
248  }
249  }
250  }
251 
252  std::vector<std::string> findPathAndFilter(const HLTConfigProvider& hltConfig) {
253  std::vector<std::string> ret(2, "");
254  std::string filterFullName = "";
255  std::string pathFullName = "";
256  std::vector<std::string> filtersForThisPath;
257  //int pathIndex = -1;
258  int numPathMatches = 0;
259  int numFilterMatches = 0;
260  for (size_t i = 0; i < hltConfig.size(); ++i) {
261  if (hltConfig.triggerName(i).find(m_pathPartialName) == std::string::npos)
262  continue;
263  pathFullName = hltConfig.triggerName(i);
264  //pathIndex = i;
265  ++numPathMatches;
266  std::vector<std::string> moduleLabels = hltConfig.moduleLabels(i);
267  for (auto& moduleLabel : moduleLabels) {
268  if ("EDFilter" == hltConfig.moduleEDMType(moduleLabel)) {
269  filtersForThisPath.push_back(moduleLabel);
270  if (moduleLabel.find(m_filterPartialName) != std::string::npos) {
271  filterFullName = moduleLabel;
272  ++numFilterMatches;
273  }
274  }
275  }
276  }
277 
278  // LogWarning or LogError?
279  if (numPathMatches != 1) {
280  edm::LogInfo("FSQDiJetAve") << "Problem: found " << numPathMatches << " paths matching " << m_pathPartialName
281  << std::endl;
282  return ret;
283  }
284  ret[0] = pathFullName;
285  if (numFilterMatches != 1) {
286  edm::LogError("FSQDiJetAve") << "Problem: found " << numFilterMatches << " filter matching "
287  << m_filterPartialName << " in path " << m_pathPartialName << std::endl;
288  return ret;
289  }
290  ret[1] = filterFullName;
291  return ret;
292  }
293 
294  void analyze(const edm::Event& iEvent,
295  const edm::EventSetup& iSetup,
297  const trigger::TriggerEvent& trgEvent,
300  float weight) override {
301  size_t found = 0;
302  for (size_t i = 0; i < triggerNames.size(); ++i) {
303  auto itUsedPaths = m_usedPaths.begin();
304  for (; itUsedPaths != m_usedPaths.end(); ++itUsedPaths) {
305  if (triggerNames.triggerName(i).find(*itUsedPaths) != std::string::npos) {
306  ++found;
307  break;
308  }
309  }
310 
311  if (found == m_usedPaths.size())
312  break;
313  }
314  if (found != m_usedPaths.size()) {
315  edm::LogInfo("FSQDiJetAve") << "One of requested paths not found, skipping event";
316  return;
317  }
318  if (m_eventCache->configurationUpdated()) {
319  m_expression->init(*m_eventCache);
320  }
321  if (not(*m_expression)(*m_eventCache))
322  return;
323 
324  /*
325  std::vector<std::string> pathAndFilter = findPathAndFilter(hltConfig);
326 
327  std::string pathFullName = pathAndFilter[0];
328  if (pathFullName == "") {
329  return;
330  }
331  unsigned indexNum = triggerNames.triggerIndex(pathFullName);
332  if(indexNum >= triggerNames.size()){
333  edm::LogError("FSQDiJetAve") << "Problem determining trigger index for " << pathFullName << " " << m_pathPartialName;
334  }
335  if (!triggerResults.accept(indexNum)) return;*/
336 
337  std::vector<TOutputCandidateType> cands;
338  getFilteredCands((TInputCandidateType*)nullptr, cands, iEvent, iSetup, hltConfig, trgEvent, weight);
339 
340  if (cands.empty())
341  return;
342 
343  std::vector<TOutputCandidateType> bestCombinationFromCands = getBestCombination(cands);
344  if (bestCombinationFromCands.empty())
345  return;
346 
347  // plot
348  std::map<std::string, MonitorElement*>::iterator it, itE;
349  it = m_histos.begin();
350  itE = m_histos.end();
351  for (; it != itE; ++it) {
352  if (m_plotterType[it->first] != CombinedObjectPlotter)
353  continue;
354  float val = (*m_plottersCombinedObject[it->first])(bestCombinationFromCands);
355  it->second->Fill(val, weight);
356  }
357  }
358 
359  std::vector<TOutputCandidateType> getBestCombination(std::vector<TOutputCandidateType>& cands) {
360  int columnSize = cands.size();
361  std::vector<int> currentCombination(m_combinedObjectDimension, 0);
362  std::vector<int> bestCombination(m_combinedObjectDimension, -1);
363 
364  int maxCombinations = 1;
365  int cnt = 0;
366  while (cnt < m_combinedObjectDimension) {
367  cnt += 1;
368  maxCombinations *= columnSize;
369  }
370 
371  cnt = 0;
372  float bestCombinedCandVal = -1;
373  while (cnt < maxCombinations) {
374  cnt += 1;
375 
376  // 1. Check if current combination contains duplicates
377  std::vector<int> currentCombinationCopy(currentCombination);
378  std::vector<int>::iterator it;
379  std::sort(currentCombinationCopy.begin(), currentCombinationCopy.end());
380  it = std::unique(currentCombinationCopy.begin(), currentCombinationCopy.end());
381  currentCombinationCopy.resize(std::distance(currentCombinationCopy.begin(), it));
382  bool duplicatesPresent = currentCombination.size() != currentCombinationCopy.size();
383  // 2. If no duplicates found -
384  // - check if current combination passes the cut
385  // - rank current combination
386  if (!duplicatesPresent) { // no duplicates, we can consider this combined object
387  std::vector<TOutputCandidateType> currentCombinationFromCands;
388  currentCombinationFromCands.reserve(m_combinedObjectDimension);
389  for (int i = 0; i < m_combinedObjectDimension; ++i) {
390  currentCombinationFromCands.push_back(cands.at(currentCombination.at(i)));
391  }
392  bool isOK = m_combinedObjectSelection(currentCombinationFromCands);
393  if (isOK) {
394  float curVal = m_combinedObjectSortFunction(currentCombinationFromCands);
395  // FIXME
396  if (curVal < 0) {
397  edm::LogError("FSQDiJetAve")
398  << "Problem: ranking function returned negative value: " << curVal << std::endl;
399  } else if (curVal > bestCombinedCandVal) {
400  //std::cout << curVal << " " << bestCombinedCandVal << std::endl;
401  bestCombinedCandVal = curVal;
402  bestCombination = currentCombination;
403  }
404  }
405  }
406  // 3. Prepare next combination to test
407  // note to future self: less error prone method with modulo
408  currentCombination.at(m_combinedObjectDimension - 1) += 1; // increase last number
409  int carry = 0;
410  for (int i = m_combinedObjectDimension - 1; i >= 0;
411  --i) { // iterate over all numbers, check if we are out of range
412  currentCombination.at(i) += carry;
413  carry = 0;
414  if (currentCombination.at(i) >= columnSize) {
415  carry = 1;
416  currentCombination.at(i) = 0;
417  }
418  }
419  } // combinations loop ends
420 
421  std::vector<TOutputCandidateType> bestCombinationFromCands;
422  if (!bestCombination.empty() && bestCombination.at(0) >= 0) {
423  for (int i = 0; i < m_combinedObjectDimension; ++i) {
424  bestCombinationFromCands.push_back(cands.at(bestCombination.at(i)));
425  }
426  }
427  return bestCombinationFromCands;
428  }
429  };
430  //#############################################################################
431  // Read any object inheriting from reco::Candidate. Save p4
432  //
433  // problem: for reco::Candidate there is no reflex dictionary, so selector
434  // wont work
435  //#############################################################################
436  template <>
438  edm::ConsumesCollector&& iC) {
439  edm::EDGetTokenT<View<reco::Candidate> > tok = iC.consumes<View<reco::Candidate> >(m_input);
440  m_tokens[m_input.encode()] = edm::EDGetToken(tok);
441  }
442  template <>
444  reco::Candidate::LorentzVector*, // pass a dummy pointer, makes possible to select correct getFilteredCands
445  std::vector<reco::Candidate::LorentzVector>& cands, // output collection
446  const edm::Event& iEvent,
447  const edm::EventSetup& iSetup,
449  const trigger::TriggerEvent& trgEvent,
450  float weight) {
452  iEvent.getByToken(m_tokens[m_input.encode()], hIn);
453  if (!hIn.isValid()) {
454  edm::LogError("FSQDiJetAve") << "product not found: " << m_input.encode();
455  return;
456  }
457  for (auto const& i : *hIn) {
458  bool preselection = m_singleObjectSelection(i.p4());
459  if (preselection) {
460  fillSingleObjectPlots(i.p4(), weight);
461  cands.push_back(i.p4());
462  }
463  }
464  }
465  //#############################################################################
466  //
467  // Count any object inheriting from reco::Track. Save into std::vector<int>
468  // note: this is similar to recoCand counter (code duplication is hard to
469  // avoid in this case)
470  //
471  //#############################################################################
472  template <>
474  reco::Track*, // pass a dummy pointer, makes possible to select correct getFilteredCands
475  std::vector<int>& cands, // output collection
476  const edm::Event& iEvent,
477  const edm::EventSetup& iSetup,
479  const trigger::TriggerEvent& trgEvent,
480  float weight) {
481  cands.clear();
482  cands.push_back(count<reco::Track>(iEvent, m_input, m_singleObjectSelection, weight));
483  }
484  template <>
486  reco::GenParticle*, // pass a dummy pointer, makes possible to select correct getFilteredCands
487  std::vector<int>& cands, // output collection
488  const edm::Event& iEvent,
489  const edm::EventSetup& iSetup,
491  const trigger::TriggerEvent& trgEvent,
492  float weight) {
493  cands.clear();
494  cands.push_back(count<reco::GenParticle>(iEvent, m_input, m_singleObjectSelection, weight));
495  }
496  //#############################################################################
497  //
498  // Count any object inheriting from reco::Track that is not to distant from
499  // selected vertex. Save into std::vector<int>
500  // note: this is similar to recoCand counter (code duplication is hard to
501  // avoid in this case)
502  //
503  //#############################################################################
504  template <>
506  edm::EDGetTokenT<std::vector<reco::Track> > tok = iC.consumes<std::vector<reco::Track> >(m_input);
507  m_tokens[m_input.encode()] = edm::EDGetToken(tok);
508 
509  edm::InputTag lVerticesTag = m_pset.getParameter<edm::InputTag>("vtxCollection");
510  edm::EDGetTokenT<reco::VertexCollection> tok2 = iC.consumes<reco::VertexCollection>(lVerticesTag);
511  m_tokens[lVerticesTag.encode()] = edm::EDGetToken(tok2);
512  }
513 
514  template <>
516  reco::Track*, // pass a dummy pointer, makes possible to select correct getFilteredCands
517  std::vector<int>& cands, // output collection
518  const edm::Event& iEvent,
519  const edm::EventSetup& iSetup,
521  const trigger::TriggerEvent& trgEvent,
522  float weight) {
523  // this is not elegant, but should be thread safe
524  static const edm::InputTag lVerticesTag = m_pset.getParameter<edm::InputTag>("vtxCollection");
525  static const int lMinNDOF = m_pset.getParameter<int>("minNDOF"); //7
526  static const double lMaxZ = m_pset.getParameter<double>("maxZ"); // 15
527  static const double lMaxDZ = m_pset.getParameter<double>("maxDZ"); // 0.12
528  static const double lMaxDZ2dzsigma = m_pset.getParameter<double>("maxDZ2dzsigma"); // 3
529  static const double lMaxDXY = m_pset.getParameter<double>("maxDXY"); // 0.12
530  static const double lMaxDXY2dxysigma = m_pset.getParameter<double>("maxDXY2dxysigma"); // 3
531 
532  cands.clear();
533  cands.push_back(0);
534 
536  iEvent.getByToken(m_tokens[lVerticesTag.encode()], vertices);
537 
538  math::XYZPoint vtxPoint(0.0, 0.0, 0.0);
539  double vzErr = 0.0, vxErr = 0.0, vyErr = 0.0;
540 
541  // take first vertex passing the criteria
542  bool vtxfound = false;
543  for (const auto& vertex : *vertices) {
544  if (vertex.ndof() < lMinNDOF)
545  continue;
546  if (fabs(vertex.z()) > lMaxZ)
547  continue;
548 
549  vtxPoint = vertex.position();
550  vzErr = vertex.zError();
551  vxErr = vertex.xError();
552  vyErr = vertex.yError();
553  vtxfound = true;
554  break;
555  }
556  if (!vtxfound)
557  return;
558 
560  iEvent.getByToken(m_tokens[m_input.encode()], hIn);
561  if (!hIn.isValid()) {
562  edm::LogError("FSQDiJetAve") << "product not found: " << m_input.encode();
563  return;
564  }
565 
566  for (auto const& i : *hIn) {
567  if (!m_singleObjectSelection(i))
568  continue;
569 
570  double absdz = fabs(i.dz(vtxPoint));
571  if (absdz > lMaxDZ)
572  continue; // TODO...
573 
574  // double absdxy = fabs(-1. * i.dxy(vtxPoint));
575  double absdxy = fabs(i.dxy(vtxPoint));
576  if (absdxy > lMaxDXY)
577  continue;
578 
579  double dzsigma2 = i.dzError() * i.dzError() + vzErr * vzErr;
580  if (absdz * absdz > lMaxDZ2dzsigma * lMaxDZ2dzsigma * dzsigma2)
581  continue;
582 
583  double dxysigma2 = i.dxyError() * i.dxyError() + vxErr * vyErr;
584  if (absdxy * absdxy > lMaxDXY2dxysigma * lMaxDXY2dxysigma * dxysigma2)
585  continue;
586 
587  cands.at(0) += 1;
588  } //loop over tracks
589  }
590  //#############################################################################
591  //
592  // Apply JEC to PFJets
593  //
594  //#############################################################################
595  template <>
597  edm::EDGetTokenT<std::vector<reco::PFJet> > tok = iC.consumes<std::vector<reco::PFJet> >(m_input);
598  m_tokens[m_input.encode()] = edm::EDGetToken(tok);
599 
600  edm::InputTag jetCorTag = m_pset.getParameter<edm::InputTag>("PFJetCorLabel");
601  edm::EDGetTokenT<reco::JetCorrector> jetcortoken = iC.consumes<reco::JetCorrector>(jetCorTag);
602  m_tokens[jetCorTag.encode()] = edm::EDGetToken(jetcortoken);
603  }
604 
605  template <>
607  reco::PFJet*, // pass a dummy pointer, makes possible to select correct getFilteredCands
608  std::vector<reco::PFJet>& cands, // output collection
609  const edm::Event& iEvent,
610  const edm::EventSetup& iSetup,
612  const trigger::TriggerEvent& trgEvent,
613  float weight) {
614  cands.clear();
615  static const edm::InputTag jetCorTag = m_pset.getParameter<edm::InputTag>("PFJetCorLabel");
617  iEvent.getByToken(m_tokens[jetCorTag.encode()], pfcorrector);
618 
620  iEvent.getByToken(m_tokens[m_input.encode()], hIn);
621 
622  if (!hIn.isValid()) {
623  edm::LogError("FSQDiJetAve") << "product not found: " << m_input.encode();
624  return;
625  }
626 
627  for (auto const& i : *hIn) {
628  double scale = pfcorrector->correction(i);
629  reco::PFJet newPFJet(scale * i.p4(), i.vertex(), i.getSpecific(), i.getJetConstituents());
630 
631  bool preselection = m_singleObjectSelection(newPFJet);
632  if (preselection) {
633  fillSingleObjectPlots(newPFJet, weight);
634  cands.push_back(newPFJet);
635  }
636  }
637  }
638  //#############################################################################
639  //
640  // Count any object inheriting from reco::Candidate. Save into std::vector<int>
641  // same problem as for reco::Candidate handler ()
642  //
643  //#############################################################################
644  template <>
646  edm::EDGetTokenT<View<reco::Candidate> > tok = iC.consumes<View<reco::Candidate> >(m_input);
647  m_tokens[m_input.encode()] = edm::EDGetToken(tok);
648  }
649  template <>
651  reco::Candidate::LorentzVector*, // pass a dummy pointer, makes possible to select correct getFilteredCands
652  std::vector<int>& cands, // output collection
653  const edm::Event& iEvent,
654  const edm::EventSetup& iSetup,
656  const trigger::TriggerEvent& trgEvent,
657  float weight) {
658  cands.clear();
659  cands.push_back(0);
660 
662  iEvent.getByToken(m_tokens[m_input.encode()], hIn);
663  if (!hIn.isValid()) {
664  edm::LogError("FSQDiJetAve") << "product not found: " << m_input.encode();
665  return;
666  }
667  for (auto const& i : *hIn) {
668  bool preselection = m_singleObjectSelection(i.p4());
669  if (preselection) {
670  fillSingleObjectPlots(i.p4(), weight);
671  cands.at(0) += 1;
672  }
673  }
674  }
675  //#############################################################################
676  //
677  // Read and save trigger::TriggerObject from triggerEvent
678  //
679  //#############################################################################
680  template <>
683  std::vector<trigger::TriggerObject>& cands,
684  const edm::Event& iEvent,
685  const edm::EventSetup& iSetup,
687  const trigger::TriggerEvent& trgEvent,
688  float weight) {
689  // 1. Find matching path. Inside matchin path find matching filter
690  std::string filterFullName = findPathAndFilter(hltConfig)[1];
691  if (filterFullName.empty()) {
692  return;
693  }
694 
695  // 2. Fetch HLT objects saved by selected filter. Save those fullfilling preselection
696  // objects are saved in cands variable
697  const std::string& process = trgEvent.usedProcessName(); // broken?
698  edm::InputTag hltTag(filterFullName, "", process);
699 
700  const int hltIndex = trgEvent.filterIndex(hltTag);
701  if (hltIndex >= trgEvent.sizeFilters()) {
702  edm::LogInfo("FSQDiJetAve") << "Cannot determine hlt index for |" << filterFullName << "|" << process;
703  return;
704  }
705 
706  const trigger::TriggerObjectCollection& toc(trgEvent.getObjects());
707  const trigger::Keys& khlt = trgEvent.filterKeys(hltIndex);
708 
709  auto kj = khlt.begin();
710 
711  for (; kj != khlt.end(); ++kj) {
712  bool preselection = m_singleObjectSelection(toc[*kj]);
713  if (preselection) {
714  fillSingleObjectPlots(toc[*kj], weight);
715  cands.push_back(toc[*kj]);
716  }
717  }
718  }
719 
722  RecoCandidateHandler; // in fact reco::Candidate, reco::Candidate::LorentzVector
733 } // namespace FSQ
734 //################################################################################################
735 //
736 // Plugin functions
737 //
738 //################################################################################################
740  : m_eventCache(iConfig.getParameterSet("triggerConfiguration"), consumesCollector()) {
741  m_useGenWeight = iConfig.getParameter<bool>("useGenWeight");
742  if (m_useGenWeight) {
743  m_genEvInfoToken = consumes<GenEventInfoProduct>(edm::InputTag("generator"));
744  }
745 
746  triggerSummaryLabel_ = iConfig.getParameter<edm::InputTag>("triggerSummaryLabel");
747  triggerResultsLabel_ = iConfig.getParameter<edm::InputTag>("triggerResultsLabel");
748  triggerSummaryToken = consumes<trigger::TriggerEvent>(triggerSummaryLabel_);
749  triggerResultsToken = consumes<edm::TriggerResults>(triggerResultsLabel_);
750 
751  triggerSummaryFUToken = consumes<trigger::TriggerEvent>(
753  triggerResultsFUToken = consumes<edm::TriggerResults>(
755 
756  std::vector<edm::ParameterSet> todo = iConfig.getParameter<std::vector<edm::ParameterSet> >("todo");
757  for (const auto& pset : todo) {
758  std::string type = pset.getParameter<std::string>("handlerType");
759  if (type == "FromHLT") {
760  m_handlers.push_back(std::make_shared<FSQ::HLTHandler>(pset, m_eventCache));
761  } else if (type == "RecoCandidateCounter") {
762  m_handlers.push_back(std::make_shared<FSQ::RecoCandidateCounter>(pset, m_eventCache));
763  } else if (type == "RecoTrackCounter") {
764  m_handlers.push_back(std::make_shared<FSQ::RecoTrackCounter>(pset, m_eventCache));
765  } else if (type == "RecoTrackCounterWithVertexConstraint") {
766  m_handlers.push_back(std::make_shared<FSQ::RecoTrackCounterWithVertexConstraint>(pset, m_eventCache));
767  } else if (type == "FromRecoCandidate") {
768  m_handlers.push_back(std::make_shared<FSQ::RecoCandidateHandler>(pset, m_eventCache));
769  } else if (type == "RecoPFJet") {
770  m_handlers.push_back(std::make_shared<FSQ::RecoPFJetHandler>(pset, m_eventCache));
771  } else if (type == "RecoPFJetWithJEC") {
772  m_handlers.push_back(std::make_shared<FSQ::RecoPFJetWithJECHandler>(pset, m_eventCache));
773  } else if (type == "RecoTrack") {
774  m_handlers.push_back(std::make_shared<FSQ::RecoTrackHandler>(pset, m_eventCache));
775  } else if (type == "RecoPhoton") {
776  m_handlers.push_back(std::make_shared<FSQ::RecoPhotonHandler>(pset, m_eventCache));
777  } else if (type == "RecoMuon") {
778  m_handlers.push_back(std::make_shared<FSQ::RecoMuonHandler>(pset, m_eventCache));
779  } else if (type == "RecoGenParticleCounter") {
780  m_handlers.push_back(std::make_shared<FSQ::RecoGenParticleCounter>(pset, m_eventCache));
781  } else if (type == "RecoGenParticleHandler") {
782  m_handlers.push_back(std::make_shared<FSQ::RecoGenParticleHandler>(pset, m_eventCache));
783  } else {
784  throw cms::Exception("FSQ DQM handler not know: " + type);
785  }
786  }
787  for (auto& m_handler : m_handlers) {
788  m_handler->getAndStoreTokens(consumesCollector());
789  }
790 }
791 
792 FSQDiJetAve::~FSQDiJetAve() = default;
793 
795  using namespace edm;
796  if (not m_eventCache.setEvent(iEvent, iSetup)) {
797  edm::LogError("FSQDiJetAve") << "Could not setup the filter";
798  }
799 
800  //---------- triggerResults ----------
802  if (!m_triggerResults.isValid()) {
804  if (!m_triggerResults.isValid()) {
805  edm::LogError("FSQDiJetAve") << "TriggerResults not valid, skippng event";
806  return;
807  }
808  }
809 
810  //---------- triggerResults ----------
811  if (m_triggerResults.isValid()) {
812  m_triggerNames = iEvent.triggerNames(*m_triggerResults);
813  } else {
814  edm::LogError("FSQDiJetAve") << "TriggerResults not found";
815  return;
816  }
817 
818  //---------- triggerSummary ----------
820  if (!m_trgEvent.isValid()) {
822  if (!m_trgEvent.isValid()) {
823  edm::LogInfo("FSQDiJetAve") << "TriggerEvent not found, ";
824  return;
825  }
826  }
827 
828  float weight = 1.;
829  if (m_useGenWeight) {
831  iEvent.getByToken(m_genEvInfoToken, hGW);
832  weight = hGW->weight();
833  }
834 
835  for (auto& m_handler : m_handlers) {
836  m_handler->analyze(
838  }
839 }
840 // ------------ method called when starting to processes a run ------------
841 //*
843  bool changed(true);
845  if (m_hltConfig.init(run, c, processName, changed)) {
846  LogDebug("FSQDiJetAve") << "HLTConfigProvider failed to initialize.";
847  }
848 }
850  for (auto& m_handler : m_handlers) {
851  m_handler->book(booker);
852  }
853 }
854 //*/
855 // ------------ method called when ending the processing of a run ------------
856 /*
857 void
858 FSQDiJetAve::endRun(edm::Run const&, edm::EventSetup const&)
859 {
860 }
861 */
862 
863 // ------------ method called when starting to processes a luminosity block ------------
864 /*
865 void
866 FSQDiJetAve::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
867 {
868 }
869 */
870 
871 // ------------ method called when ending the processing of a luminosity block ------------
872 /*
873 void
874 FSQDiJetAve::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
875 {}
876 // */
877 
878 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
880  //The following says we do not know what parameters are allowed so do no validation
881  // Please change this to state exactly what you do use, even if it is no parameters
883  desc.setUnknown();
884  descriptions.addDefault(desc);
885 }
886 
int count(const edm::Event &iEvent, InputTag &input, StringCutObjectSelector< T > &sel, float weight)
Definition: FSQDiJetAve.cc:188
const Keys & filterKeys(trigger::size_type index) const
Definition: TriggerEvent.h:118
edm::Handle< edm::TriggerResults > m_triggerResults
Definition: FSQDiJetAve.h:88
std::vector< edm::ParameterSet > m_combinedObjectDrawables
Definition: FSQDiJetAve.cc:122
std::string m_filterPartialName
Definition: FSQDiJetAve.cc:108
vector< string > parse(string line, const string &delimiter)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
triggerExpression::Data m_eventCache
Definition: FSQDiJetAve.h:74
HandlerTemplate< reco::Photon, reco::Photon > RecoPhotonHandler
Definition: FSQDiJetAve.cc:726
SpecialFilters
Definition: FSQDiJetAve.cc:102
The single EDProduct to be saved for each event (AOD case)
Definition: TriggerEvent.h:25
std::vector< std::shared_ptr< FSQ::BaseHandler > > m_handlers
Definition: FSQDiJetAve.h:94
std::vector< edm::ParameterSet > m_singleObjectDrawables
Definition: FSQDiJetAve.cc:123
dqm::legacy::MonitorElement MonitorElement
Definition: FSQDiJetAve.cc:58
edm::Handle< trigger::TriggerEvent > m_trgEvent
Definition: FSQDiJetAve.h:89
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:36
std::string encode() const
Definition: InputTag.cc:159
ret
prodAgent to be discontinued
std::string const & instance() const
Definition: InputTag.h:37
edm::EDGetTokenT< GenEventInfoProduct > m_genEvInfoToken
Definition: FSQDiJetAve.h:85
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: FSQDiJetAve.cc:879
edm::EDGetTokenT< edm::TriggerResults > triggerResultsToken
Definition: FSQDiJetAve.h:81
trigger::size_type sizeFilters() const
Definition: TriggerEvent.h:146
T const * product() const
Definition: Handle.h:70
std::vector< std::string > findPathAndFilter(const HLTConfigProvider &hltConfig)
Definition: FSQDiJetAve.cc:252
StringObjectFunction< std::vector< TOutputCandidateType > > m_combinedObjectSortFunction
Definition: FSQDiJetAve.cc:114
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
Definition: weight.py:1
FSQDiJetAve(const edm::ParameterSet &)
Definition: FSQDiJetAve.cc:739
HandlerTemplate< trigger::TriggerObject, trigger::TriggerObject > HLTHandler
Definition: FSQDiJetAve.cc:720
std::string const & label() const
Definition: InputTag.h:36
Log< level::Error, false > LogError
void bookHistograms(DQMStore::IBooker &, edm::Run const &run, edm::EventSetup const &c) override
Definition: FSQDiJetAve.cc:849
Jets made from PFObjects.
Definition: PFJet.h:20
edm::InputTag m_input
Definition: FSQDiJetAve.cc:125
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
std::map< std::string, MonitorElement * > m_histos
Definition: FSQDiJetAve.cc:93
static std::string const input
Definition: EdmProvDump.cc:50
edm::EDGetTokenT< edm::TriggerResults > triggerResultsFUToken
Definition: FSQDiJetAve.h:82
T getUntrackedParameter(std::string const &, T const &) const
Single trigger physics object (e.g., an isolated muon)
Definition: TriggerObject.h:21
std::vector< TOutputCandidateType > getBestCombination(std::vector< TOutputCandidateType > &cands)
Definition: FSQDiJetAve.cc:359
HandlerTemplate< reco::GenParticle, reco::GenParticle > RecoGenParticleHandler
Definition: FSQDiJetAve.cc:728
std::string m_pathPartialName
Definition: FSQDiJetAve.cc:107
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
std::string m_dqmhistolabel
Definition: FSQDiJetAve.cc:106
void fillSingleObjectPlots(const TInputCandidateType &cand, float weight)
Definition: FSQDiJetAve.cc:207
std::map< std::string, int > m_plotterType
Definition: FSQDiJetAve.cc:121
StringCutObjectSelector< std::vector< TOutputCandidateType > > m_combinedObjectSelection
Definition: FSQDiJetAve.cc:113
std::set< std::string > m_usedPaths
Definition: FSQDiJetAve.cc:94
edm::EDGetTokenT< trigger::TriggerEvent > triggerSummaryFUToken
Definition: FSQDiJetAve.h:84
def unique(seq, keepstr=True)
Definition: tier0.py:24
const TriggerObjectCollection & getObjects() const
Definition: TriggerEvent.h:101
void getFilteredCands(TInputCandidateType *, std::vector< TOutputCandidateType > &cands, const edm::Event &iEvent, const edm::EventSetup &iSetup, const HLTConfigProvider &hltConfig, const trigger::TriggerEvent &trgEvent, float weight)
Definition: FSQDiJetAve.cc:228
HandlerTemplate< reco::Candidate::LorentzVector, int > RecoCandidateCounter
Definition: FSQDiJetAve.cc:729
HandlerTemplate< reco::Track, int, BestVertexMatching > RecoTrackCounterWithVertexConstraint
Definition: FSQDiJetAve.cc:731
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
HandlerTemplate< reco::PFJet, reco::PFJet, ApplyJEC > RecoPFJetWithJECHandler
Definition: FSQDiJetAve.cc:724
bool setEvent(const edm::Event &event, const edm::EventSetup &setup)
HandlerTemplate(const edm::ParameterSet &iConfig, triggerExpression::Data &eventCache)
Definition: FSQDiJetAve.cc:129
std::map< std::string, edm::EDGetToken > m_tokens
Definition: FSQDiJetAve.cc:126
BaseHandler(const edm::ParameterSet &iConfig, triggerExpression::Data &eventCache)
Definition: FSQDiJetAve.cc:63
std::vector< TriggerObject > TriggerObjectCollection
collection of trigger physics objects (e.g., all isolated muons)
Definition: TriggerObject.h:75
Log< level::Info, false > LogInfo
std::unique_ptr< triggerExpression::Evaluator > m_expression
Definition: FSQDiJetAve.cc:90
HandlerTemplate< reco::Candidate::LorentzVector, reco::Candidate::LorentzVector > RecoCandidateHandler
Definition: FSQDiJetAve.cc:722
trigger::size_type filterIndex(const edm::InputTag &filterTag) const
find index of filter in data-member vector from filter tag
Definition: TriggerEvent.h:132
HandlerTemplate< reco::Track, int > RecoTrackCounter
Definition: FSQDiJetAve.cc:730
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
std::vector< size_type > Keys
static std::string const triggerResults("TriggerResults")
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
~FSQDiJetAve() override
StringCutObjectSelector< TInputCandidateType > m_singleObjectSelection
Definition: FSQDiJetAve.cc:112
std::map< std::string, std::shared_ptr< StringObjectFunction< TInputCandidateType > > > m_plottersSingleObject
Definition: FSQDiJetAve.cc:117
HandlerTemplate< reco::Track, reco::Track > RecoTrackHandler
Definition: FSQDiJetAve.cc:725
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup, const HLTConfigProvider &hltConfig, const trigger::TriggerEvent &trgEvent, const edm::TriggerResults &triggerResults, const edm::TriggerNames &triggerNames, float weight) override
Definition: FSQDiJetAve.cc:294
dqm::legacy::DQMStore DQMStore
Definition: FSQDiJetAve.cc:59
HLTConfigProvider m_hltConfig
Definition: FSQDiJetAve.h:76
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
bool isValid() const
Definition: HandleBase.h:70
edm::InputTag triggerSummaryLabel_
Definition: FSQDiJetAve.h:90
ParameterSet const & getParameterSet(ParameterSetID const &id)
void book(DQMStore::IBooker &booker) override
Definition: FSQDiJetAve.cc:149
const std::string & usedProcessName() const
getters
Definition: TriggerEvent.h:98
HLT enums.
bool m_useGenWeight
Definition: FSQDiJetAve.h:75
HandlerTemplate< reco::GenParticle, int > RecoGenParticleCounter
Definition: FSQDiJetAve.cc:732
edm::EDGetTokenT< trigger::TriggerEvent > triggerSummaryToken
Definition: FSQDiJetAve.h:83
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: FSQDiJetAve.cc:794
HandlerTemplate< reco::Muon, reco::Muon > RecoMuonHandler
Definition: FSQDiJetAve.cc:727
edm::InputTag triggerResultsLabel_
Definition: FSQDiJetAve.h:91
preselection
PRESELECTION
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
std::string const & process() const
Definition: InputTag.h:40
edm::TriggerNames m_triggerNames
Definition: FSQDiJetAve.h:87
#define str(s)
edm::ParameterSet m_pset
Definition: FSQDiJetAve.cc:95
std::string m_dirname
Definition: FSQDiJetAve.cc:92
HandlerTemplate< reco::PFJet, reco::PFJet > RecoPFJetHandler
Definition: FSQDiJetAve.cc:723
void dqmBeginRun(edm::Run const &run, edm::EventSetup const &c) override
Definition: FSQDiJetAve.cc:842
Definition: Run.h:45
triggerExpression::Data * m_eventCache
Definition: FSQDiJetAve.cc:91
#define LogDebug(id)
void getAndStoreTokens(edm::ConsumesCollector &&iC) override
Definition: FSQDiJetAve.cc:177
std::map< std::string, std::shared_ptr< StringObjectFunction< std::vector< TOutputCandidateType > > > > m_plottersCombinedObject
Definition: FSQDiJetAve.cc:116