Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:27

0001 from __future__ import print_function
0002 from PhysicsTools.HeppyCore.framework.analyzer import Analyzer
0003 from PhysicsTools.Heppy.analyzers.AutoHandle import AutoHandle
0004 from PhysicsTools.HeppyCore.statistics.counter import Counter
0005 from PhysicsTools.HeppyCore.utils.TriggerList import TriggerList
0006 from PhysicsTools.HeppyCore.utils.TriggerMatching import selTriggerObjects
0007 from PhysicsTools.Heppy.physicsobjects.PhysicsObjects import TriggerObject
0008 
0009 
0010 
0011 class TriggerAnalyzer( Analyzer ):
0012     '''Access to trigger information, and trigger selection'''
0013 
0014     def declareHandles(self):
0015         super(TriggerAnalyzer, self).declareHandles()
0016 
0017         self.handles['cmgTriggerObjectSel'] =  AutoHandle(
0018             'cmgTriggerObjectSel',
0019             'std::vector<cmg::TriggerObject>'
0020             )
0021  
0022         self.handles['cmgTriggerObjectListSel'] =  AutoHandle(
0023             'cmgTriggerObjectListSel',
0024             'std::vector<cmg::TriggerObject>'
0025             )
0026  
0027     def beginLoop(self, setup):
0028         super(TriggerAnalyzer,self).beginLoop(setup)
0029         self.triggerList = TriggerList( self.cfg_comp.triggers )
0030         if hasattr(self.cfg_comp,'vetoTriggers'):
0031             self.vetoTriggerList = TriggerList( self.cfg_comp.vetoTriggers )
0032         else:
0033             self.vetoTriggerList = None
0034             
0035         self.counters.addCounter('Trigger')
0036         self.counters.counter('Trigger').register('All events')
0037         self.counters.counter('Trigger').register('HLT')
0038         
0039 
0040     def process(self, iEvent, event):
0041         self.readCollections( iEvent )
0042         
0043         event.triggerObject = self.handles['cmgTriggerObjectSel'].product()[0]
0044         run = iEvent.eventAuxiliary().id().run()
0045         lumi = iEvent.eventAuxiliary().id().luminosityBlock()
0046         eventId = iEvent.eventAuxiliary().id().event()
0047 
0048         event.run = run
0049         event.lumi = lumi
0050         event.eventId = eventId
0051 
0052 ##        if self.cfg_ana.verbose:
0053 ##            self.printTriggerObject( event.triggerObject )
0054         
0055         self.counters.counter('Trigger').inc('All events')
0056         # import pdb; pdb.set_trace()
0057         usePrescaled = False
0058         if hasattr( self.cfg_ana, 'usePrescaled'):
0059             usePrescaled = self.cfg_ana.usePrescaled
0060 
0061         # import pdb; pdb.set_trace()
0062         passed, hltPath = self.triggerList.triggerPassed(event.triggerObject,
0063                                                          run, lumi, self.cfg_comp.isData,
0064                                                          usePrescaled = usePrescaled)
0065 
0066 
0067 
0068         #Check the veto!
0069         veto=False
0070         if self.vetoTriggerList is not None:
0071             veto,hltVetoPath = self.vetoTriggerList.triggerPassed(event.triggerObject,
0072                                                          run,lumi,self.cfg_comp.isData,
0073                                                          usePrescaled = usePrescaled)
0074 
0075         # Check if events needs to be skipped if no trigger is found (useful for generator level studies)
0076         keepFailingEvents = False
0077         if hasattr( self.cfg_ana, 'keepFailingEvents'):
0078             keepFailingEvents = self.cfg_ana.keepFailingEvents
0079         if not passed or (passed and veto):
0080             event.passedTriggerAnalyzer = False
0081             if not keepFailingEvents:
0082                 return False
0083         else:
0084             event.passedTriggerAnalyzer = True
0085 
0086         event.hltPath = hltPath 
0087 
0088         if hltPath is not None:
0089             trigObjs = map( TriggerObject,
0090                             self.handles['cmgTriggerObjectListSel'].product())
0091             # selecting the trigger objects used in this path
0092             event.triggerObjects = selTriggerObjects( trigObjs, hltPath )
0093             
0094         self.counters.counter('Trigger').inc('HLT')
0095         event.TriggerFired = 1
0096         return True
0097 
0098     def write(self, setup):
0099         print('writing TriggerAnalyzer')
0100         super(TriggerAnalyzer, self).write(setup)
0101         self.triggerList.write( self.dirName )
0102 
0103     def __str__(self):
0104         tmp = super(TriggerAnalyzer,self).__str__()
0105         triglist = str( self.triggerList )
0106         return '\n'.join( [tmp, triglist ] )
0107 
0108 
0109 ##     def printTriggerObject(self, object):
0110 ##         '''FIXME : we need a trigger object class in physicsobjects.'''
0111 ##         print 'trig obj', object.pdgId(), object.pt(), object.charge(), object.eta(), object.phi()
0112 ##         for name in object.getSelectionNames():
0113 ##             hasSel = object.getSelection( name )
0114 ##             if self.cfg_ana.verbose==1 and hasSel:
0115 ##                 print name, hasSel
0116 ##             elif self.cfg_ana.verbose==2:
0117 ##                 print name, hasSel