test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TriggerMatchAnalyzer.py
Go to the documentation of this file.
1 import ROOT
2 
3 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
4 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
5 from PhysicsTools.Heppy.analyzers.core.AutoFillTreeProducer import NTupleVariable
6 from PhysicsTools.HeppyCore.utils.deltar import matchObjectCollection, matchObjectCollection3
7 import PhysicsTools.HeppyCore.framework.config as cfg
8 
9 class TriggerMatchAnalyzer( Analyzer ):
10  def __init__(self, cfg_ana, cfg_comp, looperName ):
11  super(TriggerMatchAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
12  self.processName = getattr(self.cfg_ana,"processName","PAT")
13  self.fallbackName = getattr(self.cfg_ana,"fallbackProcessName","RECO")
14  self.unpackPathNames = getattr(self.cfg_ana,"unpackPathNames",True)
15  self.label = self.cfg_ana.label
16  self.trgObjSelectors = []
17  self.trgObjSelectors.extend(getattr(self.cfg_ana,"trgObjSelectors",[]))
18  self.collToMatch = getattr(self.cfg_ana,"collToMatch",None)
20  self.collMatchSelectors.extend(getattr(self.cfg_ana,"collMatchSelectors",[]))
21  self.collMatchDRCut = getattr(self.cfg_ana,"collMatchDRCut",0.3)
22  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")
23  self.match1To1 = getattr(self.cfg_ana,"univoqueMatching",True)
24 
25  def declareHandles(self):
26  super(TriggerMatchAnalyzer, self).declareHandles()
27  self.handles['TriggerBits'] = AutoHandle( ('TriggerResults','','HLT'), 'edm::TriggerResults' )
28  fallback = ( 'selectedPatTrigger','', self.fallbackName) if self.fallbackName else None
29  self.handles['TriggerObjects'] = AutoHandle( ('selectedPatTrigger','',self.processName), 'std::vector<pat::TriggerObjectStandAlone>', fallbackLabel=fallback )
30 
31  def beginLoop(self, setup):
32  super(TriggerMatchAnalyzer,self).beginLoop(setup)
33 
34  def process(self, event):
35  self.readCollections( event.input )
36  triggerBits = self.handles['TriggerBits'].product()
37  allTriggerObjects = self.handles['TriggerObjects'].product()
38  names = event.input.object().triggerNames(triggerBits)
39  for ob in allTriggerObjects: ob.unpackPathNames(names)
40  triggerObjects = [ob for ob in allTriggerObjects if False not in [sel(ob) for sel in self.trgObjSelectors]]
41 
42  setattr(event,'trgObjects_'+self.label,triggerObjects)
43 
44  if self.collToMatch:
45  tcoll = getattr(event,self.collToMatch)
46  doubleandselector = lambda lep,ob: False if False in [sel(lep,ob) for sel in self.collMatchSelectors] else True
47  pairs = matchObjectCollection3(tcoll,triggerObjects,deltaRMax=self.collMatchDRCut,filter=doubleandselector) if self.match1To1 else matchObjectCollection(tcoll,triggerObjects,self.collMatchDRCut,filter=doubleandselector)
48  for lep in tcoll: setattr(lep,'matchedTrgObj'+self.label,pairs[lep])
49 
50  if self.verbose:
51  print 'Verbose debug for triggerMatchAnalyzer %s'%self.label
52  for ob in getattr(event,'trgObjects_'+self.label):
53  types = ", ".join([str(f) for f in ob.filterIds()])
54  filters = ", ".join([str(f) for f in ob.filterLabels()])
55  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
56  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)
57  if self.collToMatch:
58  for lep in tcoll:
59  mstring = 'None'
60  ob = getattr(lep,'matchedTrgObj'+self.label)
61  if ob: mstring = 'trigger obj with pt=%.2f, eta=%.2f, phi=%.2f, collection=%s'%(ob.pt(),ob.eta(),ob.phi(),ob.collection())
62  print 'Lepton pt=%.2f, eta=%.2f, phi=%.2f matched to %s'%(lep.pt(),lep.eta(),lep.phi(),mstring)
63 
64  return True
65 
66 
67 setattr(TriggerMatchAnalyzer,"defaultConfig",cfg.Analyzer(
68  TriggerMatchAnalyzer, name="TriggerMatchAnalyzerDefault",
69  label='DefaultTrigObjSelection',
70  processName = 'PAT',
71  fallbackProcessName = 'RECO',
72  unpackPathNames = True,
73  trgObjSelectors = [],
74  collToMatch = None,
75  collMatchSelectors = [],
76  collMatchDRCut = 0.3,
77  univoqueMatching = True,
78  verbose = False
79 )
80 )
81 
82 
def matchObjectCollection
Definition: deltar.py:152
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def matchObjectCollection3
Definition: deltar.py:41