Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-23 02:25:52

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