CMS 3D CMS Logo

TriggerBitAnalyzer.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import ROOT
3 
4 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
5 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
6 from PhysicsTools.Heppy.analyzers.core.AutoFillTreeProducer import NTupleVariable
7 import PhysicsTools.HeppyCore.framework.config as cfg
8 
10  def __init__(self, cfg_ana, cfg_comp, looperName ):
11  super(TriggerBitAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
12  self.processName = getattr(self.cfg_ana,"processName","HLT")
13  self.prescaleProcessName = getattr(self.cfg_ana,"prescaleProcessName","PAT")
14  self.fallbackName = getattr(self.cfg_ana,"fallbackProcessName",None)
15  self.prescaleFallbackName = getattr(self.cfg_ana,"prescaleFallbackProcessName","RECO")
16  self.outprefix = getattr(self.cfg_ana,"outprefix", self.processName)
17  self.unrollbits = ( hasattr(self.cfg_ana,"unrollbits") and self.cfg_ana.unrollbits )
18  self.saveIsUnprescaled = getattr(self.cfg_ana,"saveIsUnprescaled",False)
19  self.checkL1prescale = getattr(self.cfg_ana,"checkL1prescale",False)
20  self.force1prescale = False
21  if self.cfg_comp.isMC and self.saveIsUnprescaled:
22  print('Cannot save prescale information in MC: will put everything to unprescaled')
23  self.saveIsUnprescaled = False
24  self.checkL1prescale = False
25  self.force1prescale = True
26 
27  def declareHandles(self):
28  super(TriggerBitAnalyzer, self).declareHandles()
29  fallback = ('TriggerResults','',self.fallbackName) if self.fallbackName else None
30  prescaleFallback = ('patTrigger','',self.prescaleFallbackName) if self.prescaleFallbackName else None
31  self.handles['TriggerResults'] = AutoHandle( ('TriggerResults','',self.processName), 'edm::TriggerResults', fallbackLabel=fallback )
32  if self.saveIsUnprescaled:
33  self.handles["TriggerPrescales"] = AutoHandle( ('patTrigger','',self.prescaleProcessName), 'pat::PackedTriggerPrescales', fallbackLabel=prescaleFallback )
34  if self.checkL1prescale:
35  self.handles["TriggerPrescales_l1min"] = AutoHandle( ('patTrigger','l1min',self.prescaleProcessName), 'pat::PackedTriggerPrescales', fallbackLabel=(('patTrigger','l1min',self.prescaleFallbackName) if self.prescaleFallbackName else None) )
36  self.handles["TriggerPrescales_l1max"] = AutoHandle( ('patTrigger','l1max',self.prescaleProcessName), 'pat::PackedTriggerPrescales', fallbackLabel=(('patTrigger','l1max',self.prescaleFallbackName) if self.prescaleFallbackName else None) )
37 
38  def beginLoop(self, setup):
39  super(TriggerBitAnalyzer,self).beginLoop(setup)
41  if self.unrollbits :
42  self.allPaths = set()
44 
45  for T, TL in self.cfg_ana.triggerBits.items():
46  trigVec = ROOT.vector(ROOT.string)()
47  for TP in TL:
48  trigVec.push_back(TP)
49  if self.unrollbits :
50  if TP not in self.allPaths :
51  self.allPaths.update([TP])
52  trigVecBit = ROOT.vector(ROOT.string)()
53  trigVecBit.push_back(TP)
54  outname="%s_BIT_%s"%(self.outprefix,TP)
55  if not hasattr(setup ,"globalVariables") :
56  setup.globalVariables = []
57  if outname[-1] == '*' :
58  outname=outname[0:-1]
59  setup.globalVariables.append( NTupleVariable(outname, eval("lambda ev: ev.%s" % outname), int, help="Trigger bit %s"%TP) )
60  if self.saveIsUnprescaled or self.force1prescale: setup.globalVariables.append( NTupleVariable(outname+'_isUnprescaled', eval("lambda ev: ev.%s_isUnprescaled" % outname), int, help="Trigger bit %s isUnprescaled flag"%TP) )
61  if self.saveIsUnprescaled or self.force1prescale: setup.globalVariables.append( NTupleVariable(outname+'_Prescale', eval("lambda ev: ev.%s_Prescale" % outname), int, help="get prescale %s "%TP) )
62  self.triggerBitCheckersSingleBits.append( (TP, ROOT.heppy.TriggerBitChecker(trigVecBit)) )
63 
64  outname="%s_%s"%(self.outprefix,T)
65  if not hasattr(setup ,"globalVariables") :
66  setup.globalVariables = []
67  setup.globalVariables.append( NTupleVariable(outname, eval("lambda ev: ev.%s" % outname), int, help="OR of %s"%TL) )
68  if self.saveIsUnprescaled or self.force1prescale: setup.globalVariables.append( NTupleVariable(outname+'_isUnprescaled', eval("lambda ev: ev.%s_isUnprescaled" % outname), int, help="OR of %s is Unprescaled flag"%TL) )
69  self.triggerBitCheckers.append( (T, ROOT.heppy.TriggerBitChecker(trigVec)) )
70 
71 
72  def process(self, event):
73  self.readCollections( event.input )
74  triggerResults = self.handles['TriggerResults'].product()
75  if self.saveIsUnprescaled:
76  triggerPrescales = self.handles["TriggerPrescales"].product()
77  if self.checkL1prescale:
78  triggerPrescales_min = self.handles["TriggerPrescales_l1min"].product()
79  triggerPrescales_max = self.handles["TriggerPrescales_l1max"].product()
80  for T,TC in self.triggerBitCheckers:
81  outname="%s_%s"%(self.outprefix,T)
82  setattr(event,outname, TC.check(event.input.object(), triggerResults))
83  if self.saveIsUnprescaled:
84  unpr = TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales)
85  if self.checkL1prescale:
86  unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_min)
87  unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_max)
88  setattr(event,outname+'_isUnprescaled', unpr)
89  if self.force1prescale: setattr(event,outname+'_isUnprescaled', True)
90  if self.unrollbits :
91  for TP,TC in self.triggerBitCheckersSingleBits:
92  outname="%s_BIT_%s"%(self.outprefix,TP)
93  if outname[-1] == '*' :
94  outname=outname[0:-1]
95  setattr(event,outname, TC.check(event.input.object(), triggerResults))
96  if self.saveIsUnprescaled:
97  unpr = TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales)
98  getpr = TC.getprescale(event.input.object(), triggerResults, triggerPrescales)
99  if self.checkL1prescale:
100  unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_min)
101  unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_max)
102  getprl1min = TC.getprescale(event.input.object(), triggerResults, triggerPrescales_min)
103  getprl1max = TC.getprescale(event.input.object(), triggerResults, triggerPrescales_max)
104  if (getprl1min != getprl1max):
105  getpr = -999
106  else:
107  getpr = getprl1min*getpr
108  setattr(event,outname+'_isUnprescaled', unpr)
109  setattr(event,outname+'_Prescale', getpr)
110  if self.force1prescale:
111  setattr(event,outname+'_isUnprescaled', True)
112  setattr(event,outname+'_Prescale', 1)
113 
114  return True
115 
116 
117 setattr(TriggerBitAnalyzer,"defaultConfig",cfg.Analyzer(
118  TriggerBitAnalyzer, name="TriggerFlags",
119  processName = 'HLT',
120  prescaleProcessName = 'PAT',
121  prescaleFallbackProcessName = 'RECO',
122  unrollbits = False,
123  saveIsUnprescaled = False,
124  checkL1prescale = False,
125  triggerBits = {
126  # "<name>" : [ 'HLT_<Something>_v*', 'HLT_<SomethingElse>_v*' ]
127 }
128 )
129 )
130 setattr(TriggerBitAnalyzer,"defaultEventFlagsConfig",cfg.Analyzer(
131  TriggerBitAnalyzer, name="EventFlags",
132  processName = 'PAT',
133  fallbackProcessName = 'RECO',
134  unrollbits = False,
135  saveIsUnprescaled = False,
136  outprefix = 'Flag',
137  triggerBits = {
138  "HBHENoiseFilter" : [ "Flag_HBHENoiseFilter" ],
139  "HBHENoiseIsoFilter" : [ "Flag_HBHENoiseIsoFilter" ],
140  "CSCTightHaloFilter" : [ "Flag_CSCTightHaloFilter" ],
141  "CSCTightHalo2015Filter" : [ "Flag_CSCTightHalo2015Filter" ],
142  "hcalLaserEventFilter" : [ "Flag_hcalLaserEventFilter" ],
143  "EcalDeadCellTriggerPrimitiveFilter" : [ "Flag_EcalDeadCellTriggerPrimitiveFilter" ],
144  "goodVertices" : [ "Flag_goodVertices" ],
145  "trackingFailureFilter" : [ "Flag_trackingFailureFilter" ],
146  "eeBadScFilter" : [ "Flag_eeBadScFilter" ],
147  "ecalLaserCorrFilter" : [ "Flag_ecalLaserCorrFilter" ],
148  "trkPOGFilters" : [ "Flag_trkPOGFilters" ],
149  "trkPOG_manystripclus53X" : [ "Flag_trkPOG_manystripclus53X" ],
150  "trkPOG_toomanystripclus53X" : [ "Flag_trkPOG_toomanystripclus53X" ],
151  "trkPOG_logErrorTooManyClusters" : [ "Flag_trkPOG_logErrorTooManyClusters" ],
152  "METFilters" : [ "Flag_METFilters" ],
153  }
154 )
155 )
def __init__(self, cfg_ana, cfg_comp, looperName)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
#define update(a, b)