CMS 3D CMS Logo

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