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