File indexing completed on 2024-11-25 02:29:49
0001 import ROOT
0002
0003 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
0004 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
0005 from PhysicsTools.Heppy.analyzers.core.AutoFillTreeProducer import NTupleVariable
0006 import PhysicsTools.HeppyCore.framework.config as cfg
0007
0008 class TriggerBitAnalyzer( Analyzer ):
0009 def __init__(self, cfg_ana, cfg_comp, looperName ):
0010 super(TriggerBitAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
0011 self.processName = getattr(self.cfg_ana,"processName","HLT")
0012 self.prescaleProcessName = getattr(self.cfg_ana,"prescaleProcessName","PAT")
0013 self.fallbackName = getattr(self.cfg_ana,"fallbackProcessName",None)
0014 self.prescaleFallbackName = getattr(self.cfg_ana,"prescaleFallbackProcessName","RECO")
0015 self.outprefix = getattr(self.cfg_ana,"outprefix", self.processName)
0016 self.unrollbits = ( hasattr(self.cfg_ana,"unrollbits") and self.cfg_ana.unrollbits )
0017 self.saveIsUnprescaled = getattr(self.cfg_ana,"saveIsUnprescaled",False)
0018 self.checkL1prescale = getattr(self.cfg_ana,"checkL1prescale",False)
0019 self.force1prescale = False
0020 if self.cfg_comp.isMC and self.saveIsUnprescaled:
0021 print('Cannot save prescale information in MC: will put everything to unprescaled')
0022 self.saveIsUnprescaled = False
0023 self.checkL1prescale = False
0024 self.force1prescale = True
0025
0026 def declareHandles(self):
0027 super(TriggerBitAnalyzer, self).declareHandles()
0028 fallback = ('TriggerResults','',self.fallbackName) if self.fallbackName else None
0029 prescaleFallback = ('patTrigger','',self.prescaleFallbackName) if self.prescaleFallbackName else None
0030 self.handles['TriggerResults'] = AutoHandle( ('TriggerResults','',self.processName), 'edm::TriggerResults', fallbackLabel=fallback )
0031 if self.saveIsUnprescaled:
0032 self.handles["TriggerPrescales"] = AutoHandle( ('patTrigger','',self.prescaleProcessName), 'pat::PackedTriggerPrescales', fallbackLabel=prescaleFallback )
0033 if self.checkL1prescale:
0034 self.handles["TriggerPrescales_l1min"] = AutoHandle( ('patTrigger','l1min',self.prescaleProcessName), 'pat::PackedTriggerPrescales', fallbackLabel=(('patTrigger','l1min',self.prescaleFallbackName) if self.prescaleFallbackName else None) )
0035 self.handles["TriggerPrescales_l1max"] = AutoHandle( ('patTrigger','l1max',self.prescaleProcessName), 'pat::PackedTriggerPrescales', fallbackLabel=(('patTrigger','l1max',self.prescaleFallbackName) if self.prescaleFallbackName else None) )
0036
0037 def beginLoop(self, setup):
0038 super(TriggerBitAnalyzer,self).beginLoop(setup)
0039 self.triggerBitCheckers = []
0040 if self.unrollbits :
0041 self.allPaths = set()
0042 self.triggerBitCheckersSingleBits = []
0043
0044 for T, TL in self.cfg_ana.triggerBits.items():
0045 trigVec = ROOT.vector(ROOT.string)()
0046 for TP in TL:
0047 trigVec.push_back(TP)
0048 if self.unrollbits :
0049 if TP not in self.allPaths :
0050 self.allPaths.update([TP])
0051 trigVecBit = ROOT.vector(ROOT.string)()
0052 trigVecBit.push_back(TP)
0053 outname="%s_BIT_%s"%(self.outprefix,TP)
0054 if not hasattr(setup ,"globalVariables") :
0055 setup.globalVariables = []
0056 if outname[-1] == '*' :
0057 outname=outname[0:-1]
0058 setup.globalVariables.append( NTupleVariable(outname, eval("lambda ev: ev.%s" % outname), int, help="Trigger bit %s"%TP) )
0059 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) )
0060 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) )
0061 self.triggerBitCheckersSingleBits.append( (TP, ROOT.heppy.TriggerBitChecker(trigVecBit)) )
0062
0063 outname="%s_%s"%(self.outprefix,T)
0064 if not hasattr(setup ,"globalVariables") :
0065 setup.globalVariables = []
0066 setup.globalVariables.append( NTupleVariable(outname, eval("lambda ev: ev.%s" % outname), int, help="OR of %s"%TL) )
0067 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) )
0068 self.triggerBitCheckers.append( (T, ROOT.heppy.TriggerBitChecker(trigVec)) )
0069
0070
0071 def process(self, event):
0072 self.readCollections( event.input )
0073 triggerResults = self.handles['TriggerResults'].product()
0074 if self.saveIsUnprescaled:
0075 triggerPrescales = self.handles["TriggerPrescales"].product()
0076 if self.checkL1prescale:
0077 triggerPrescales_min = self.handles["TriggerPrescales_l1min"].product()
0078 triggerPrescales_max = self.handles["TriggerPrescales_l1max"].product()
0079 for T,TC in self.triggerBitCheckers:
0080 outname="%s_%s"%(self.outprefix,T)
0081 setattr(event,outname, TC.check(event.input.object(), triggerResults))
0082 if self.saveIsUnprescaled:
0083 unpr = TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales)
0084 if self.checkL1prescale:
0085 unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_min)
0086 unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_max)
0087 setattr(event,outname+'_isUnprescaled', unpr)
0088 if self.force1prescale: setattr(event,outname+'_isUnprescaled', True)
0089 if self.unrollbits :
0090 for TP,TC in self.triggerBitCheckersSingleBits:
0091 outname="%s_BIT_%s"%(self.outprefix,TP)
0092 if outname[-1] == '*' :
0093 outname=outname[0:-1]
0094 setattr(event,outname, TC.check(event.input.object(), triggerResults))
0095 if self.saveIsUnprescaled:
0096 unpr = TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales)
0097 getpr = TC.getprescale(event.input.object(), triggerResults, triggerPrescales)
0098 if self.checkL1prescale:
0099 unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_min)
0100 unpr = unpr and TC.check_unprescaled(event.input.object(), triggerResults, triggerPrescales_max)
0101 getprl1min = TC.getprescale(event.input.object(), triggerResults, triggerPrescales_min)
0102 getprl1max = TC.getprescale(event.input.object(), triggerResults, triggerPrescales_max)
0103 if (getprl1min != getprl1max):
0104 getpr = -999
0105 else:
0106 getpr = getprl1min*getpr
0107 setattr(event,outname+'_isUnprescaled', unpr)
0108 setattr(event,outname+'_Prescale', getpr)
0109 if self.force1prescale:
0110 setattr(event,outname+'_isUnprescaled', True)
0111 setattr(event,outname+'_Prescale', 1)
0112
0113 return True
0114
0115
0116 setattr(TriggerBitAnalyzer,"defaultConfig",cfg.Analyzer(
0117 TriggerBitAnalyzer, name="TriggerFlags",
0118 processName = 'HLT',
0119 prescaleProcessName = 'PAT',
0120 prescaleFallbackProcessName = 'RECO',
0121 unrollbits = False,
0122 saveIsUnprescaled = False,
0123 checkL1prescale = False,
0124 triggerBits = {
0125
0126 }
0127 )
0128 )
0129 setattr(TriggerBitAnalyzer,"defaultEventFlagsConfig",cfg.Analyzer(
0130 TriggerBitAnalyzer, name="EventFlags",
0131 processName = 'PAT',
0132 fallbackProcessName = 'RECO',
0133 unrollbits = False,
0134 saveIsUnprescaled = False,
0135 outprefix = 'Flag',
0136 triggerBits = {
0137 "HBHENoiseFilter" : [ "Flag_HBHENoiseFilter" ],
0138 "HBHENoiseIsoFilter" : [ "Flag_HBHENoiseIsoFilter" ],
0139 "CSCTightHaloFilter" : [ "Flag_CSCTightHaloFilter" ],
0140 "CSCTightHalo2015Filter" : [ "Flag_CSCTightHalo2015Filter" ],
0141 "hcalLaserEventFilter" : [ "Flag_hcalLaserEventFilter" ],
0142 "EcalDeadCellTriggerPrimitiveFilter" : [ "Flag_EcalDeadCellTriggerPrimitiveFilter" ],
0143 "goodVertices" : [ "Flag_goodVertices" ],
0144 "trackingFailureFilter" : [ "Flag_trackingFailureFilter" ],
0145 "eeBadScFilter" : [ "Flag_eeBadScFilter" ],
0146 "ecalLaserCorrFilter" : [ "Flag_ecalLaserCorrFilter" ],
0147 "trkPOGFilters" : [ "Flag_trkPOGFilters" ],
0148 "trkPOG_manystripclus53X" : [ "Flag_trkPOG_manystripclus53X" ],
0149 "trkPOG_toomanystripclus53X" : [ "Flag_trkPOG_toomanystripclus53X" ],
0150 "trkPOG_logErrorTooManyClusters" : [ "Flag_trkPOG_logErrorTooManyClusters" ],
0151 "METFilters" : [ "Flag_METFilters" ],
0152 }
0153 )
0154 )