CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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:
63  BaseHandler();
65  }
66  BaseHandler(const edm::ParameterSet& iConfig, triggerExpression::Data & eventCache):
67  m_expression(triggerExpression::parse( iConfig.getParameter<std::string>("triggerSelection")))
68  {
69  // extract list of used paths
70  std::vector<std::string> strs;
71  std::string triggerSelection = iConfig.getParameter<std::string>("triggerSelection");
72  boost::split(strs, triggerSelection, boost::is_any_of("\t ,`!@#$%^&*()~/\\"));
73  for (size_t iToken = 0; iToken < strs.size();++iToken ){
74  if (strs.at(iToken).find("HLT_")==0){
75  m_usedPaths.insert(strs.at(iToken));
76  }
77  }
78 
79  m_eventCache = &eventCache;
80  std::string pathPartialName = iConfig.getParameter<std::string>("partialPathName");
81  m_dirname = iConfig.getUntrackedParameter("mainDQMDirname",std::string("HLT/FSQ/"))+pathPartialName + "/";
82  m_pset = iConfig;
83 
84  };
85  virtual void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup,
87  const trigger::TriggerEvent& trgEvent,
89  const edm::TriggerNames & triggerNames,
90  float weight) = 0;
91  virtual void book(DQMStore::IBooker & booker) = 0;
92  virtual void getAndStoreTokens(edm::ConsumesCollector && iC) = 0;
93 
94  std::unique_ptr<triggerExpression::Evaluator> m_expression;
97  std::map<std::string, MonitorElement*> m_histos;
98  std::set<std::string> m_usedPaths;
100 };
101 //################################################################################################
102 //
103 // Handle objects saved into hlt event by hlt filters
104 //
105 //################################################################################################
107 template <class TInputCandidateType, class TOutputCandidateType, SpecialFilters filter = None>
109  private:
111  std::string m_pathPartialName; //#("HLT_DiPFJetAve30_HFJEC_");
112  std::string m_filterPartialName; //#("ForHFJECBase"); // Calo jet preFilter
113 
115 
119  std::map<std::string, std::shared_ptr<StringObjectFunction<std::vector<TOutputCandidateType> > > > m_plottersCombinedObject;
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  {
138  std::string type = iConfig.getParameter<std::string>("handlerType");
139  if (type != "FromHLT") {
140  m_input = iConfig.getParameter<edm::InputTag>("inputCol");
141  }
142 
143  m_dqmhistolabel = iConfig.getParameter<std::string>("dqmhistolabel");
144  m_filterPartialName = 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 
152  void book(DQMStore::IBooker & booker){
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 > * )0);
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){
171  m_plottersCombinedObject[histoName] = std::shared_ptr<StringObjectFunction<std::vector<TOutputCandidateType> > >(func);
172  } else {
175  m_plottersSingleObject[histoName] = std::shared_ptr<StringObjectFunction<TInputCandidateType> > (func);
176  }
177  }
178  }
179  }
180  }
182  edm::EDGetTokenT<std::vector<TInputCandidateType> > tok = iC.consumes<std::vector<TInputCandidateType> > (m_input);
183  m_tokens[m_input.encode()] = edm::EDGetToken(tok);
184  }
185 
186  //#############################################################################
187  // Count objects. To avoid code duplication we do it in a separate template -
188  // - partial specialization not easy...:
189  // http://stackoverflow.com/questions/21182729/specializing-single-method-in-a-big-template-class
190  //#############################################################################
191  template <class T>
193  int ret = 0;
195  iEvent.getByToken(m_tokens[input.encode()], hIn);
196  if(!hIn.isValid()) {
197  edm::LogError("FSQDiJetAve") << "product not found: "<< input.encode();
198  return -1; // return nonsense value
199  }
200  for (size_t i = 0; i<hIn->size(); ++i) {
201  bool preselection = sel(hIn->at(i));
202  if (preselection){
203  fillSingleObjectPlots(hIn->at(i), weight);
204  ret+=1;
205  }
206  }
207  return ret;
208  }
209 
210  // FIXME (?): code duplication
211  void fillSingleObjectPlots(const TInputCandidateType & cand, float weight){
212  std::map<std::string, MonitorElement*>::iterator it, itE;
213  it = m_histos.begin();
214  itE = m_histos.end();
215  for (;it!=itE;++it){
216  if (m_plotterType[it->first]!=SingleObjectPlotter) 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 *, std::vector<TOutputCandidateType> & cands,
232  const edm::Event& iEvent,
233  const edm::EventSetup& iSetup,
235  const trigger::TriggerEvent& trgEvent,
236  float weight)
237  {
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) continue;
265  pathFullName = hltConfig.triggerName(i);
266  //pathIndex = i;
267  ++numPathMatches;
268  std::vector<std::string > moduleLabels = hltConfig.moduleLabels(i);
269  for (size_t iMod = 0; iMod <moduleLabels.size(); ++iMod){
270  if ("EDFilter" == hltConfig.moduleEDMType(moduleLabels.at(iMod))) {
271  filtersForThisPath.push_back(moduleLabels.at(iMod));
272  if ( moduleLabels.at(iMod).find(m_filterPartialName)!= std::string::npos ){
273  filterFullName = moduleLabels.at(iMod);
274  ++numFilterMatches;
275  }
276  }
277  }
278  }
279 
280  // LogWarning or LogError?
281  if (numPathMatches != 1) {
282  edm::LogInfo("FSQDiJetAve") << "Problem: found " << numPathMatches
283  << " paths matching " << m_pathPartialName << std::endl;
284  return ret;
285  }
286  ret[0] = pathFullName;
287  if (numFilterMatches != 1) {
288  edm::LogError("FSQDiJetAve") << "Problem: found " << numFilterMatches
289  << " filter matching " << m_filterPartialName
290  << " 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, const edm::EventSetup& iSetup,
299  const trigger::TriggerEvent& trgEvent,
301  const edm::TriggerNames & triggerNames,
302  float weight)
303  {
304  size_t found = 0;
305  for (size_t i = 0; i<triggerNames.size(); ++i){
306  std::set<std::string>::iterator 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()) break;
315  }
316  if (found != m_usedPaths.size()){
317  edm::LogInfo("FSQDiJetAve") << "One of requested paths not found, skipping event";
318  return;
319  }
320  if (m_eventCache->configurationUpdated()) {
321  m_expression->init(*m_eventCache);
322  }
323  if (not (*m_expression)(*m_eventCache)) return;
324 
325  /*
326  std::vector<std::string> pathAndFilter = findPathAndFilter(hltConfig);
327 
328  std::string pathFullName = pathAndFilter[0];
329  if (pathFullName == "") {
330  return;
331  }
332  unsigned indexNum = triggerNames.triggerIndex(pathFullName);
333  if(indexNum >= triggerNames.size()){
334  edm::LogError("FSQDiJetAve") << "Problem determining trigger index for " << pathFullName << " " << m_pathPartialName;
335  }
336  if (!triggerResults.accept(indexNum)) return;*/
337 
338  std::vector<TOutputCandidateType> cands;
339  getFilteredCands((TInputCandidateType *)0, cands, iEvent, iSetup, hltConfig, trgEvent, weight);
340 
341  if (cands.size()==0) return;
342 
343  std::vector<TOutputCandidateType> bestCombinationFromCands = getBestCombination(cands);
344  if (bestCombinationFromCands.size()==0) return;
345 
346  // plot
347  std::map<std::string, MonitorElement*>::iterator it, itE;
348  it = m_histos.begin();
349  itE = m_histos.end();
350  for (;it!=itE;++it){
351  if (m_plotterType[it->first]!=CombinedObjectPlotter) continue;
352  float val = (*m_plottersCombinedObject[it->first])(bestCombinationFromCands);
353  it->second->Fill(val, weight);
354  }
355  }
356 
357  std::vector<TOutputCandidateType> getBestCombination(std::vector<TOutputCandidateType> & cands ){
358  int columnSize = cands.size();
359  std::vector<int> currentCombination(m_combinedObjectDimension, 0);
360  std::vector<int> bestCombination(m_combinedObjectDimension, -1);
361 
362  int maxCombinations = 1;
363  int cnt = 0;
364  while (cnt < m_combinedObjectDimension){
365  cnt += 1;
366  maxCombinations *= columnSize;
367  }
368 
369  cnt = 0;
370  float bestCombinedCandVal = -1;
371  while ( cnt < maxCombinations){
372  cnt += 1;
373 
374  // 1. Check if current combination contains duplicates
375  std::vector<int> currentCombinationCopy(currentCombination);
376  std::vector<int>::iterator it;
377  std::sort(currentCombinationCopy.begin(), currentCombinationCopy.end());
378  it = std::unique(currentCombinationCopy.begin(), currentCombinationCopy.end());
379  currentCombinationCopy.resize( std::distance(currentCombinationCopy.begin(),it) );
380  bool duplicatesPresent = currentCombination.size() != currentCombinationCopy.size();
381  // 2. If no duplicates found -
382  // - check if current combination passes the cut
383  // - rank current combination
384  if (!duplicatesPresent) { // no duplicates, we can consider this combined object
385  std::vector<TOutputCandidateType > currentCombinationFromCands;
386  for (int i = 0; i<m_combinedObjectDimension;++i){
387  currentCombinationFromCands.push_back( cands.at(currentCombination.at(i)));
388  }
389  bool isOK = m_combinedObjectSelection(currentCombinationFromCands);
390  if (isOK){
391  float curVal = m_combinedObjectSortFunction(currentCombinationFromCands);
392  // FIXME
393  if (curVal < 0) {
394  edm::LogError("FSQDiJetAve") << "Problem: ranking function returned negative value: " << curVal << std::endl;
395  } else if (curVal > bestCombinedCandVal){
396  //std::cout << curVal << " " << bestCombinedCandVal << std::endl;
397  bestCombinedCandVal = curVal;
398  bestCombination = currentCombination;
399  }
400  }
401  }
402  // 3. Prepare next combination to test
403  // note to future self: less error prone method with modulo
404  currentCombination.at(m_combinedObjectDimension-1)+=1; // increase last number
405  int carry = 0;
406  for (int i = m_combinedObjectDimension-1; i>=0; --i){ // iterate over all numbers, check if we are out of range
407  currentCombination.at(i)+= carry;
408  carry = 0;
409  if (currentCombination.at(i)>=columnSize){
410  carry = 1;
411  currentCombination.at(i) = 0;
412  }
413  }
414  } // combinations loop ends
415 
416  std::vector<TOutputCandidateType > bestCombinationFromCands;
417  if (bestCombination.size()!=0 && bestCombination.at(0)>=0){
418  for (int i = 0; i<m_combinedObjectDimension;++i){
419  bestCombinationFromCands.push_back( cands.at(bestCombination.at(i)));
420  }
421  }
422  return bestCombinationFromCands;
423  }
424 };
425 //#############################################################################
426 // Read any object inheriting from reco::Candidate. Save p4
427 //
428 // problem: for reco::Candidate there is no reflex dictionary, so selector
429 // wont work
430 //#############################################################################
431 template<>
434 {
436  m_tokens[m_input.encode()] = edm::EDGetToken(tok);
437 }
438 template<>
440  reco::Candidate::LorentzVector *, // pass a dummy pointer, makes possible to select correct getFilteredCands
441  std::vector<reco::Candidate::LorentzVector> & cands, // output collection
442  const edm::Event& iEvent,
443  const edm::EventSetup& iSetup,
445  const trigger::TriggerEvent& trgEvent,
446  float weight)
447 {
449  iEvent.getByToken(m_tokens[m_input.encode()], hIn);
450  if(!hIn.isValid()) {
451  edm::LogError("FSQDiJetAve") << "product not found: "<< m_input.encode();
452  return;
453  }
454  for (size_t i = 0; i<hIn->size(); ++i) {
455  bool preselection = m_singleObjectSelection(hIn->at(i).p4());
456  if (preselection){
457  fillSingleObjectPlots(hIn->at(i).p4(), weight);
458  cands.push_back(hIn->at(i).p4());
459  }
460  }
461 }
462 //#############################################################################
463 //
464 // Count any object inheriting from reco::Track. Save into std::vector<int>
465 // note: this is similar to recoCand counter (code duplication is hard to
466 // avoid in this case)
467 //
468 //#############################################################################
469 template<>
471  reco::Track *, // pass a dummy pointer, makes possible to select correct getFilteredCands
472  std::vector<int > & cands, // output collection
473  const edm::Event& iEvent,
474  const edm::EventSetup& iSetup,
476  const trigger::TriggerEvent& trgEvent,
477  float weight)
478 {
479  cands.clear();
480  cands.push_back(count<reco::Track>(iEvent, m_input, m_singleObjectSelection, weight) );
481 }
482 template<>
484  reco::GenParticle *, // pass a dummy pointer, makes possible to select correct getFilteredCands
485  std::vector<int > & cands, // output collection
486  const edm::Event& iEvent, const edm::EventSetup& iSetup,
487  const HLTConfigProvider& hltConfig, const trigger::TriggerEvent& trgEvent, float weight)
488 {
489  cands.clear();
490  cands.push_back(count<reco::GenParticle>(iEvent, m_input, m_singleObjectSelection, weight) );
491 }
492 //#############################################################################
493 //
494 // Count any object inheriting from reco::Track that is not to distant from
495 // selected vertex. Save into std::vector<int>
496 // note: this is similar to recoCand counter (code duplication is hard to
497 // avoid in this case)
498 //
499 //#############################################################################
500 template<>
503 {
504  edm::EDGetTokenT<std::vector<reco::Track> > tok = iC.consumes<std::vector<reco::Track> > (m_input);
505  m_tokens[m_input.encode()] = edm::EDGetToken(tok);
506 
507  edm::InputTag lVerticesTag = m_pset.getParameter<edm::InputTag>("vtxCollection");
508  edm::EDGetTokenT<reco::VertexCollection > tok2 = iC.consumes< reco::VertexCollection > (lVerticesTag);
509  m_tokens[lVerticesTag.encode()] = edm::EDGetToken(tok2);
510 }
511 
512 template<>
514  reco::Track *, // pass a dummy pointer, makes possible to select correct getFilteredCands
515  std::vector<int > & cands, // output collection
516  const edm::Event& iEvent,
517  const edm::EventSetup& iSetup,
519  const trigger::TriggerEvent& trgEvent, float weight)
520 {
521  // this is not elegant, but should be thread safe
522  static const edm::InputTag lVerticesTag = m_pset.getParameter<edm::InputTag>("vtxCollection");
523  static const int lMinNDOF = m_pset.getParameter<int>("minNDOF"); //7
524  static const double lMaxZ = m_pset.getParameter<double>("maxZ"); // 15
525  static const double lMaxDZ = m_pset.getParameter<double>("maxDZ"); // 0.12
526  static const double lMaxDZ2dzsigma = m_pset.getParameter<double>("maxDZ2dzsigma"); // 3
527  static const double lMaxDXY = m_pset.getParameter<double>("maxDXY"); // 0.12
528  static const double lMaxDXY2dxysigma = m_pset.getParameter<double>("maxDXY2dxysigma"); // 3
529 
530  cands.clear();
531  cands.push_back(0);
532 
534  iEvent.getByToken(m_tokens[lVerticesTag.encode()], vertices);
535 
536  //double bestvz=-999.9, bestvx=-999.9, bestvy=-999.9;
537 
538  double dxy, dz, dzsigma, dxysigma;
539  math::XYZPoint vtxPoint(0.0,0.0,0.0);
540  double vzErr =0.0, vxErr=0.0, vyErr=0.0;
541 
542  // take first vertex passing the criteria
543  int bestVtx = -1;
544  for (size_t i = 0; i < vertices->size(); ++i){
545  if (vertices->at(i).ndof()<lMinNDOF) continue;
546  if (fabs(vertices->at(i).z())> lMaxZ) continue;
547 
548  vtxPoint=vertices->at(i).position();
549  vzErr=vertices->at(i).zError();
550  vxErr=vertices->at(i).xError();
551  vyErr=vertices->at(i).yError();
552  bestVtx = i;
553  break;
554  }
555  if (bestVtx < 0) return;
556  // const reco::Vertex & vtx = vertices->at(bestVtx);
557 
559  iEvent.getByToken(m_tokens[m_input.encode()], hIn);
560  if(!hIn.isValid()) {
561  edm::LogError("FSQDiJetAve") << "product not found: "<< m_input.encode();
562  return;
563  }
564 
565  for (size_t i = 0; i<hIn->size(); ++i) {
566  if (!m_singleObjectSelection(hIn->at(i))) continue;
567  dxy=0.0, dz=0.0, dxysigma=0.0, dzsigma=0.0;
568  dxy = -1.*hIn->at(i).dxy(vtxPoint);
569  dz = hIn->at(i).dz(vtxPoint);
570  dxysigma = sqrt(hIn->at(i).dxyError()*hIn->at(i).dxyError()+vxErr*vyErr);
571  dzsigma = sqrt(hIn->at(i).dzError()*hIn->at(i).dzError()+vzErr*vzErr);
572 
573  if(fabs(dz)>lMaxDZ)continue; // TODO...
574  if(fabs(dz/dzsigma)>lMaxDZ2dzsigma)continue;
575  if(fabs(dxy)>lMaxDXY)continue;
576  if(fabs(dxy/dxysigma)>lMaxDXY2dxysigma)continue;
577 
578  cands.at(0)+=1;
579  }//loop over tracks
580 }
581 //#############################################################################
582 //
583 // Apply JEC to PFJets
584 //
585 //#############################################################################
586 template<>
589 {
590  edm::EDGetTokenT<std::vector<reco::PFJet> > tok = iC.consumes<std::vector<reco::PFJet> > (m_input);
591  m_tokens[m_input.encode()] = edm::EDGetToken(tok);
592 
593  edm::InputTag jetCorTag = m_pset.getParameter<edm::InputTag>("PFJetCorLabel");
594  edm::EDGetTokenT<reco::JetCorrector> jetcortoken = iC.consumes<reco::JetCorrector>(jetCorTag);
595  m_tokens[jetCorTag.encode()] = edm::EDGetToken(jetcortoken);
596 
597 }
598 
599 template<>
601  reco::PFJet *, // pass a dummy pointer, makes possible to select correct getFilteredCands
602  std::vector<reco::PFJet > & cands, // output collection
603  const edm::Event& iEvent,
604  const edm::EventSetup& iSetup,
606  const trigger::TriggerEvent& trgEvent, float weight)
607 {
608  cands.clear();
609  static const edm::InputTag jetCorTag = m_pset.getParameter<edm::InputTag>("PFJetCorLabel");
611  iEvent.getByToken(m_tokens[jetCorTag.encode()], pfcorrector);
612 
614  iEvent.getByToken(m_tokens[m_input.encode()], hIn);
615 
616  if(!hIn.isValid()) {
617  edm::LogError("FSQDiJetAve") << "product not found: "<< m_input.encode();
618  return;
619  }
620 
621  for (size_t i = 0; i<hIn->size(); ++i) {
622  double scale = pfcorrector->correction(hIn->at(i));
623  reco::PFJet newPFJet(scale*hIn->at(i).p4(), hIn->at(i).vertex(),
624  hIn->at(i).getSpecific(), hIn->at(i).getJetConstituents());
625 
626  bool preselection = m_singleObjectSelection(newPFJet);
627  if (preselection){
628  fillSingleObjectPlots(newPFJet, weight);
629  cands.push_back(newPFJet);
630  }
631  }
632 }
633 //#############################################################################
634 //
635 // Count any object inheriting from reco::Candidate. Save into std::vector<int>
636 // same problem as for reco::Candidate handler ()
637 //
638 //#############################################################################
639 template<>
642 {
644  m_tokens[m_input.encode()] = edm::EDGetToken(tok);
645 }
646 template<>
648  reco::Candidate::LorentzVector *, // pass a dummy pointer, makes possible to select correct getFilteredCands
649  std::vector<int > & cands, // output collection
650  const edm::Event& iEvent,
651  const edm::EventSetup& iSetup,
653  const trigger::TriggerEvent& trgEvent,
654  float weight)
655 {
656  cands.clear();
657  cands.push_back(0);
658 
660  iEvent.getByToken(m_tokens[m_input.encode()], hIn);
661  if(!hIn.isValid()) {
662  edm::LogError("FSQDiJetAve") << "product not found: "<< m_input.encode();
663  return;
664  }
665  for (size_t i = 0; i<hIn->size(); ++i) {
666  bool preselection = m_singleObjectSelection(hIn->at(i).p4());
667  if (preselection){
668  fillSingleObjectPlots(hIn->at(i).p4(), weight);
669  cands.at(0)+=1;
670  }
671  }
672 }
673 //#############################################################################
674 //
675 // Read and save trigger::TriggerObject from triggerEvent
676 //
677 //#############################################################################
678 template<>
681  std::vector<trigger::TriggerObject> &cands,
682  const edm::Event& iEvent,
683  const edm::EventSetup& iSetup,
685  const trigger::TriggerEvent& trgEvent,
686  float weight)
687 {
688  // 1. Find matching path. Inside matchin path find matching filter
689  std::string filterFullName = findPathAndFilter(hltConfig)[1];
690  if (filterFullName == "") {
691  return;
692  }
693 
694  // 2. Fetch HLT objects saved by selected filter. Save those fullfilling preselection
695  // objects are saved in cands variable
696  std::string process = trgEvent.usedProcessName(); // broken?
697  edm::InputTag hltTag(filterFullName ,"", process);
698 
699  const int hltIndex = trgEvent.filterIndex(hltTag);
700  if ( hltIndex >= trgEvent.sizeFilters() ) {
701  edm::LogInfo("FSQDiJetAve") << "Cannot determine hlt index for |" << filterFullName << "|" << process;
702  return;
703  }
704 
705  const trigger::TriggerObjectCollection & toc(trgEvent.getObjects());
706  const trigger::Keys & khlt = trgEvent.filterKeys(hltIndex);
707 
708  trigger::Keys::const_iterator kj = khlt.begin();
709 
710  for(;kj != khlt.end(); ++kj){
711  bool preselection = m_singleObjectSelection(toc[*kj]);
712  if (preselection){
713  fillSingleObjectPlots(toc[*kj], weight);
714  cands.push_back( toc[*kj]);
715  }
716  }
717 
718 }
719 
721 typedef HandlerTemplate<reco::Candidate::LorentzVector, reco::Candidate::LorentzVector> RecoCandidateHandler;// in fact reco::Candidate, reco::Candidate::LorentzVector
732 }
733 //################################################################################################
734 //
735 // Plugin functions
736 //
737 //################################################################################################
739  m_eventCache(iConfig.getParameterSet("triggerConfiguration") , consumesCollector())
740 {
741  m_useGenWeight = iConfig.getParameter<bool>("useGenWeight");
742  if (m_useGenWeight) {
743  m_genEvInfoToken = consumes <GenEventInfoProduct> (edm::InputTag("generator"));
744  }
745 
746  triggerSummaryLabel_ = iConfig.getParameter<edm::InputTag>("triggerSummaryLabel");
747  triggerResultsLabel_ = iConfig.getParameter<edm::InputTag>("triggerResultsLabel");
748  triggerSummaryToken = consumes <trigger::TriggerEvent> (triggerSummaryLabel_);
749  triggerResultsToken = consumes <edm::TriggerResults> (triggerResultsLabel_);
750 
752  triggerResultsFUToken= consumes <edm::TriggerResults> (edm::InputTag(triggerResultsLabel_.label(),triggerResultsLabel_.instance(),std::string("FU")));
753 
754  std::vector< edm::ParameterSet > todo = iConfig.getParameter< std::vector< edm::ParameterSet > >("todo");
755  for (size_t i = 0; i < todo.size(); ++i) {
756  edm::ParameterSet pset = todo.at(i);
757  std::string type = pset.getParameter<std::string>("handlerType");
758  if (type == "FromHLT") {
759  m_handlers.push_back(std::shared_ptr<FSQ::HLTHandler>(new FSQ::HLTHandler(pset, m_eventCache)));
760  }
761  else if (type == "RecoCandidateCounter") {
762  m_handlers.push_back(std::shared_ptr<FSQ::RecoCandidateCounter>(new FSQ::RecoCandidateCounter(pset, m_eventCache)));
763  }
764  else if (type == "RecoTrackCounter") {
765  m_handlers.push_back(std::shared_ptr<FSQ::RecoTrackCounter>(new FSQ::RecoTrackCounter(pset, m_eventCache)));
766  }
767  else if (type == "RecoTrackCounterWithVertexConstraint") {
768  m_handlers.push_back(std::shared_ptr<FSQ::RecoTrackCounterWithVertexConstraint>
770  }
771  else if (type == "FromRecoCandidate") {
772  m_handlers.push_back(std::shared_ptr<FSQ::RecoCandidateHandler>(new FSQ::RecoCandidateHandler(pset, m_eventCache)));
773  }
774  else if (type == "RecoPFJet") {
775  m_handlers.push_back(std::shared_ptr<FSQ::RecoPFJetHandler>(new FSQ::RecoPFJetHandler(pset, m_eventCache)));
776  }
777  else if (type == "RecoPFJetWithJEC") {
778  m_handlers.push_back(std::shared_ptr<FSQ::RecoPFJetWithJECHandler>(new FSQ::RecoPFJetWithJECHandler(pset, m_eventCache)));
779  }
780  else if (type == "RecoTrack") {
781  m_handlers.push_back(std::shared_ptr<FSQ::RecoTrackHandler>(new FSQ::RecoTrackHandler(pset, m_eventCache)));
782  }
783  else if (type == "RecoPhoton") {
784  m_handlers.push_back(std::shared_ptr<FSQ::RecoPhotonHandler>(new FSQ::RecoPhotonHandler(pset, m_eventCache)));
785  }
786  else if (type == "RecoMuon") {
787  m_handlers.push_back(std::shared_ptr<FSQ::RecoMuonHandler>(new FSQ::RecoMuonHandler(pset, m_eventCache)));
788  }
789  else if (type == "RecoGenParticleCounter") {
790  m_handlers.push_back(std::shared_ptr<FSQ::RecoGenParticleCounter>(new FSQ::RecoGenParticleCounter(pset, m_eventCache)));
791  }
792  else if (type == "RecoGenParticleHandler") {
793  m_handlers.push_back(std::shared_ptr<FSQ::RecoGenParticleHandler>(new FSQ::RecoGenParticleHandler(pset, m_eventCache)));
794  }
795  else {
796  throw cms::Exception("FSQ DQM handler not know: "+ type);
797  }
798  }
799  for (size_t i = 0; i < m_handlers.size(); ++i) {
800  m_handlers.at(i)->getAndStoreTokens(consumesCollector());
801  }
802 
803 }
804 
806 {}
807 
808 void
810 {
811  using namespace edm;
812  if (not m_eventCache.setEvent(iEvent, iSetup)){
813  edm::LogError("FSQDiJetAve") << "Could not setup the filter";
814  }
815 
816  //---------- triggerResults ----------
818  if(!m_triggerResults.isValid()) {
820  if(!m_triggerResults.isValid()) {
821  edm::LogError("FSQDiJetAve") << "TriggerResults not valid, skippng event";
822  return;
823  }
824  }
825 
826  //---------- triggerResults ----------
827  if(m_triggerResults.isValid()) {
829  } else {
830  edm::LogError("FSQDiJetAve") << "TriggerResults not found";
831  return;
832  }
833 
834  //---------- triggerSummary ----------
836  if(!m_trgEvent.isValid()) {
838  if(!m_trgEvent.isValid()) {
839  edm::LogInfo("FSQDiJetAve") << "TriggerEvent not found, ";
840  return;
841  }
842  }
843 
844  float weight = 1.;
845  if (m_useGenWeight){
847  iEvent.getByToken(m_genEvInfoToken, hGW);
848  weight = hGW->weight();
849  }
850 
851  for (size_t i = 0; i < m_handlers.size(); ++i) {
852  m_handlers.at(i)->analyze(iEvent, iSetup, m_hltConfig, *m_trgEvent.product(), *m_triggerResults.product(), m_triggerNames, weight);
853  }
854 
855 }
856 // ------------ method called when starting to processes a run ------------
857 //*
858 void
860 {
861  bool changed(true);
863  if (m_hltConfig.init(run, c, processName, changed)) {
864  LogDebug("FSQDiJetAve") << "HLTConfigProvider failed to initialize.";
865  }
866 
867 }
869  for (size_t i = 0; i < m_handlers.size(); ++i) {
870  m_handlers.at(i)->book(booker);
871  }
872 }
873 //*/
874 // ------------ method called when ending the processing of a run ------------
875 /*
876 void
877 FSQDiJetAve::endRun(edm::Run const&, edm::EventSetup const&)
878 {
879 }
880 */
881 
882 // ------------ method called when starting to processes a luminosity block ------------
883 /*
884 void
885 FSQDiJetAve::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
886 {
887 }
888 */
889 
890 // ------------ method called when ending the processing of a luminosity block ------------
891 /*
892 void
893 FSQDiJetAve::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
894 {}
895 // */
896 
897 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
898 void
900  //The following says we do not know what parameters are allowed so do no validation
901  // Please change this to state exactly what you do use, even if it is no parameters
903  desc.setUnknown();
904  descriptions.addDefault(desc);
905 }
906 
#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:192
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
int i
Definition: DBlmapReader.cc:9
std::string m_filterPartialName
Definition: FSQDiJetAve.cc:112
virtual edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const
Definition: Event.cc:208
triggerExpression::Data m_eventCache
Definition: FSQDiJetAve.h:77
HandlerTemplate< reco::Photon, reco::Photon > RecoPhotonHandler
Definition: FSQDiJetAve.cc:725
SpecialFilters
Definition: FSQDiJetAve.cc:106
std::map< std::string, int > m_plotterType
Definition: FSQDiJetAve.cc:124
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
Evaluator * parse(const T &text)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
void book(DQMStore::IBooker &booker)
Definition: FSQDiJetAve.cc:152
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: FSQDiJetAve.cc:899
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:118
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
FSQDiJetAve(const edm::ParameterSet &)
Definition: FSQDiJetAve.cc:738
edm::EDGetTokenT< GenEventInfoProduct > m_genEvInfoToken
Definition: FSQDiJetAve.h:89
HandlerTemplate< trigger::TriggerObject, trigger::TriggerObject > HLTHandler
Definition: FSQDiJetAve.cc:720
Strings::size_type size() const
Definition: TriggerNames.cc:39
virtual void bookHistograms(DQMStore::IBooker &, edm::Run const &run, edm::EventSetup const &c) override
Definition: FSQDiJetAve.cc:868
Jets made from PFObjects.
Definition: PFJet.h:21
std::string encode() const
Definition: InputTag.cc:164
std::vector< edm::ParameterSet > m_singleObjectDrawables
Definition: FSQDiJetAve.cc:126
edm::InputTag m_input
Definition: FSQDiJetAve.cc:128
std::map< std::string, MonitorElement * > m_histos
Definition: FSQDiJetAve.cc:97
static std::string const input
Definition: EdmProvDump.cc:43
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:729
std::vector< TOutputCandidateType > getBestCombination(std::vector< TOutputCandidateType > &cands)
Definition: FSQDiJetAve.cc:357
std::string m_pathPartialName
Definition: FSQDiJetAve.cc:111
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
std::string m_dqmhistolabel
Definition: FSQDiJetAve.cc:110
HandlerTemplate< reco::GenParticle, reco::GenParticle > RecoGenParticleHandler
Definition: FSQDiJetAve.cc:727
void fillSingleObjectPlots(const TInputCandidateType &cand, float weight)
Definition: FSQDiJetAve.cc:211
StringCutObjectSelector< std::vector< TOutputCandidateType > > m_combinedObjectSelection
Definition: FSQDiJetAve.cc:117
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:98
T sqrt(T t)
Definition: SSEVec.h:48
edm::EDGetTokenT< trigger::TriggerEvent > triggerSummaryFUToken
Definition: FSQDiJetAve.h:88
const std::string & usedProcessName() const
getters
Definition: TriggerEvent.h:95
MonitorElement * book1D(Args &&...args)
Definition: DQMStore.h:115
tuple preselection
PRESELECTION
void getAndStoreTokens(edm::ConsumesCollector &&iC)
Definition: FSQDiJetAve.cc:181
std::vector< std::string > findPathAndFilter(const HLTConfigProvider &hltConfig)
Definition: FSQDiJetAve.cc:255
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::Track, int, BestVertexMatching > RecoTrackCounterWithVertexConstraint
Definition: FSQDiJetAve.cc:730
HandlerTemplate< reco::PFJet, reco::PFJet, ApplyJEC > RecoPFJetWithJECHandler
Definition: FSQDiJetAve.cc:723
bool isValid() const
Definition: HandleBase.h:75
HandlerTemplate< reco::GenParticle, int > RecoGenParticleCounter
Definition: FSQDiJetAve.cc:731
std::map< std::string, std::shared_ptr< StringObjectFunction< TInputCandidateType > > > m_plottersSingleObject
Definition: FSQDiJetAve.cc:120
HandlerTemplate(const edm::ParameterSet &iConfig, triggerExpression::Data &eventCache)
Definition: FSQDiJetAve.cc:132
std::map< std::string, edm::EDGetToken > m_tokens
Definition: FSQDiJetAve.cc:129
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:66
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:94
HandlerTemplate< reco::Candidate::LorentzVector, reco::Candidate::LorentzVector > RecoCandidateHandler
Definition: FSQDiJetAve.cc:721
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:274
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
HandlerTemplate< reco::Candidate::LorentzVector, int > RecoCandidateCounter
Definition: FSQDiJetAve.cc:728
StringCutObjectSelector< TInputCandidateType > m_singleObjectSelection
Definition: FSQDiJetAve.cc:116
HandlerTemplate< reco::Track, reco::Track > RecoTrackHandler
Definition: FSQDiJetAve.cc:724
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)
Definition: FSQDiJetAve.cc:297
bool setEvent(const edm::Event &event, const edm::EventSetup &setup)
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:42
std::string const & process() const
Definition: InputTag.h:46
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
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: FSQDiJetAve.cc:809
HandlerTemplate< reco::Muon, reco::Muon > RecoMuonHandler
Definition: FSQDiJetAve.cc:726
edm::InputTag triggerResultsLabel_
Definition: FSQDiJetAve.h:95
int weight
Definition: histoStyle.py:50
tuple process
Definition: LaserDQM_cfg.py:3
edm::TriggerNames m_triggerNames
Definition: FSQDiJetAve.h:91
edm::ParameterSet m_pset
Definition: FSQDiJetAve.cc:99
std::string const & instance() const
Definition: InputTag.h:43
double split
Definition: MVATrainer.cc:139
std::string m_dirname
Definition: FSQDiJetAve.cc:96
HandlerTemplate< reco::PFJet, reco::PFJet > RecoPFJetHandler
Definition: FSQDiJetAve.cc:722
virtual void dqmBeginRun(edm::Run const &run, edm::EventSetup const &c) override
Definition: FSQDiJetAve.cc:859
Definition: Run.h:41
triggerExpression::Data * m_eventCache
Definition: FSQDiJetAve.cc:95
std::vector< edm::ParameterSet > m_combinedObjectDrawables
Definition: FSQDiJetAve.cc:125
std::map< std::string, std::shared_ptr< StringObjectFunction< std::vector< TOutputCandidateType > > > > m_plottersCombinedObject
Definition: FSQDiJetAve.cc:119