Back to home page

Project CMSSW displayed by LXR

 
 

    


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 from PhysicsTools.HeppyCore.utils.deltar import matchObjectCollection, matchObjectCollection3
0007 import PhysicsTools.HeppyCore.framework.config as cfg
0008         
0009 class TriggerMatchAnalyzer( Analyzer ):
0010     def __init__(self, cfg_ana, cfg_comp, looperName ):
0011         super(TriggerMatchAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
0012         self.processName = getattr(self.cfg_ana,"processName","PAT")
0013         self.fallbackName = getattr(self.cfg_ana,"fallbackProcessName","RECO")
0014         self.unpackPathNames = getattr(self.cfg_ana,"unpackPathNames",True)
0015         self.label = self.cfg_ana.label
0016         self.trgObjSelectors = []
0017         self.trgObjSelectors.extend(getattr(self.cfg_ana,"trgObjSelectors",[]))
0018         self.collToMatch = getattr(self.cfg_ana,"collToMatch",None)
0019         self.collMatchSelectors = []
0020         self.collMatchSelectors.extend(getattr(self.cfg_ana,"collMatchSelectors",[]))
0021         self.collMatchDRCut = getattr(self.cfg_ana,"collMatchDRCut",0.3)
0022         if self.collToMatch and not hasattr(self.cfg_ana,"univoqueMatching"): raise RuntimeError("Please specify if the matching to trigger objects should be 1-to-1 or 1-to-many")
0023         self.match1To1 = getattr(self.cfg_ana,"univoqueMatching",True)
0024 
0025     def declareHandles(self):
0026         super(TriggerMatchAnalyzer, self).declareHandles()
0027         self.handles['TriggerBits'] = AutoHandle( ('TriggerResults','','HLT'), 'edm::TriggerResults' )
0028         fallback = ( 'selectedPatTrigger','', self.fallbackName) if self.fallbackName else None
0029         self.handles['TriggerObjects'] = AutoHandle( ('selectedPatTrigger','',self.processName), 'std::vector<pat::TriggerObjectStandAlone>', fallbackLabel=fallback )
0030 
0031     def beginLoop(self, setup):
0032         super(TriggerMatchAnalyzer,self).beginLoop(setup)
0033 
0034     def process(self, event):
0035         self.readCollections( event.input )
0036         triggerBits = self.handles['TriggerBits'].product()
0037         allTriggerObjects = self.handles['TriggerObjects'].product()
0038         names = event.input.object().triggerNames(triggerBits)
0039         for ob in allTriggerObjects: ob.unpackPathNames(names)
0040         triggerObjects = [ob for ob in allTriggerObjects if False not in [sel(ob) for sel in self.trgObjSelectors]]
0041 
0042         setattr(event,'trgObjects_'+self.label,triggerObjects)
0043 
0044         if self.collToMatch:
0045             tcoll = getattr(event,self.collToMatch)
0046             doubleandselector = lambda lep,ob: False if False in [sel(lep,ob) for sel in self.collMatchSelectors] else True
0047             pairs = matchObjectCollection3(tcoll,triggerObjects,deltaRMax=self.collMatchDRCut,filter=doubleandselector) if self.match1To1 else matchObjectCollection(tcoll,triggerObjects,self.collMatchDRCut,filter=doubleandselector)
0048             for lep in tcoll: setattr(lep,'matchedTrgObj'+self.label,pairs[lep])
0049 
0050         if self.verbose:
0051             print('Verbose debug for triggerMatchAnalyzer %s'%self.label)
0052             for ob in getattr(event,'trgObjects_'+self.label):
0053                 types = ", ".join([str(f) for f in ob.filterIds()])
0054                 filters = ", ".join([str(f) for f in ob.filterLabels()])
0055                 paths = ", ".join([("%s***" if f in set(ob.pathNames(True)) else "%s")%f for f in ob.pathNames()]) # asterisks indicate final paths fired by this object, see pat::TriggerObjectStandAlone class
0056                 print('Trigger object: pt=%.2f, eta=%.2f, phi=%.2f, collection=%s, type_ids=%s, filters=%s, paths=%s'%(ob.pt(),ob.eta(),ob.phi(),ob.collection(),types,filters,paths))
0057             if self.collToMatch:
0058                 for lep in tcoll:
0059                     mstring = 'None'
0060                     ob = getattr(lep,'matchedTrgObj'+self.label)
0061                     if ob: mstring = 'trigger obj with pt=%.2f, eta=%.2f, phi=%.2f, collection=%s'%(ob.pt(),ob.eta(),ob.phi(),ob.collection())
0062                     print('Lepton pt=%.2f, eta=%.2f, phi=%.2f matched to %s'%(lep.pt(),lep.eta(),lep.phi(),mstring))
0063 
0064         return True
0065 
0066 
0067 setattr(TriggerMatchAnalyzer,"defaultConfig",cfg.Analyzer(
0068     TriggerMatchAnalyzer, name="TriggerMatchAnalyzerDefault",
0069     label='DefaultTrigObjSelection',
0070     processName = 'PAT',
0071     fallbackProcessName = 'RECO',
0072     unpackPathNames = True,
0073     trgObjSelectors = [],
0074     collToMatch = None,
0075     collMatchSelectors = [],
0076     collMatchDRCut = 0.3,
0077     univoqueMatching = True,
0078     verbose = False
0079 )
0080 )
0081 
0082