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