CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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), 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), help="Trigger bit %s isUnprescaled flag"%TP) )
60  self.triggerBitCheckersSingleBits.append( (TP, ROOT.heppy.TriggerBitChecker(trigVecBit)) )
61 
62  outname="%s_%s"%(self.outprefix,T)
63  if not hasattr(setup ,"globalVariables") :
64  setup.globalVariables = []
65  setup.globalVariables.append( NTupleVariable(outname, eval("lambda ev: ev.%s" % outname), help="OR of %s"%TL) )
66  if self.saveIsUnprescaled or self.force1prescale: setup.globalVariables.append( NTupleVariable(outname+'_isUnprescaled', eval("lambda ev: ev.%s_isUnprescaled" % outname), help="OR of %s is Unprescaled flag"%TL) )
67  self.triggerBitCheckers.append( (T, ROOT.heppy.TriggerBitChecker(trigVec)) )
68 
69 
70  def process(self, event):
71  self.readCollections( event.input )
72  triggerResults = self.handles['TriggerResults'].product()
73  if self.saveIsUnprescaled:
74  triggerPrescales = self.handles["TriggerPrescales"].product()
75  if self.checkL1prescale:
76  triggerPrescales_min = self.handles["TriggerPrescales_l1min"].product()
77  triggerPrescales_max = self.handles["TriggerPrescales_l1max"].product()
78  for T,TC in self.triggerBitCheckers:
79  outname="%s_%s"%(self.outprefix,T)
80  setattr(event,outname, TC.check(event.input.object(), triggerResults))
81  if self.saveIsUnprescaled:
82  unpr = TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales)
83  if self.checkL1prescale:
84  unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_min)
85  unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_max)
86  setattr(event,outname+'_isUnprescaled', unpr)
87  if self.force1prescale: setattr(event,outname+'_isUnprescaled', True)
88  if self.unrollbits :
89  for TP,TC in self.triggerBitCheckersSingleBits:
90  outname="%s_BIT_%s"%(self.outprefix,TP)
91  if outname[-1] == '*' :
92  outname=outname[0:-1]
93  setattr(event,outname, TC.check(event.input.object(), triggerResults))
94  if self.saveIsUnprescaled:
95  unpr = TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales)
96  if self.checkL1prescale:
97  unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_min)
98  unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_max)
99  setattr(event,outname+'_isUnprescaled', unpr)
100  if self.force1prescale: setattr(event,outname+'_isUnprescaled', True)
101 
102  return True
103 
104 
105 setattr(TriggerBitAnalyzer,"defaultConfig",cfg.Analyzer(
106  TriggerBitAnalyzer, name="TriggerFlags",
107  processName = 'HLT',
108  prescaleProcessName = 'PAT',
109  prescaleFallbackProcessName = 'RECO',
110  unrollbits = False,
111  saveIsUnprescaled = False,
112  checkL1prescale = False,
113  triggerBits = {
114  # "<name>" : [ 'HLT_<Something>_v*', 'HLT_<SomethingElse>_v*' ]
115 }
116 )
117 )
118 setattr(TriggerBitAnalyzer,"defaultEventFlagsConfig",cfg.Analyzer(
119  TriggerBitAnalyzer, name="EventFlags",
120  processName = 'PAT',
121  fallbackProcessName = 'RECO',
122  unrollbits = False,
123  saveIsUnprescaled = False,
124  outprefix = 'Flag',
125  triggerBits = {
126  "HBHENoiseFilter" : [ "Flag_HBHENoiseFilter" ],
127  "HBHENoiseIsoFilter" : [ "Flag_HBHENoiseIsoFilter" ],
128  "CSCTightHaloFilter" : [ "Flag_CSCTightHaloFilter" ],
129  "hcalLaserEventFilter" : [ "Flag_hcalLaserEventFilter" ],
130  "EcalDeadCellTriggerPrimitiveFilter" : [ "Flag_EcalDeadCellTriggerPrimitiveFilter" ],
131  "goodVertices" : [ "Flag_goodVertices" ],
132  "trackingFailureFilter" : [ "Flag_trackingFailureFilter" ],
133  "eeBadScFilter" : [ "Flag_eeBadScFilter" ],
134  "ecalLaserCorrFilter" : [ "Flag_ecalLaserCorrFilter" ],
135  "trkPOGFilters" : [ "Flag_trkPOGFilters" ],
136  "trkPOG_manystripclus53X" : [ "Flag_trkPOG_manystripclus53X" ],
137  "trkPOG_toomanystripclus53X" : [ "Flag_trkPOG_toomanystripclus53X" ],
138  "trkPOG_logErrorTooManyClusters" : [ "Flag_trkPOG_logErrorTooManyClusters" ],
139  "METFilters" : [ "Flag_METFilters" ],
140  }
141 )
142 )