CMS 3D CMS Logo

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