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