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