CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TauAnalyzer.py
Go to the documentation of this file.
1 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
2 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
3 from PhysicsTools.Heppy.physicsobjects.Tau import Tau
4 
5 from PhysicsTools.HeppyCore.utils.deltar import deltaR, matchObjectCollection3
6 
8 
9 
10 class TauAnalyzer( Analyzer ):
11 
12 
13  def __init__(self, cfg_ana, cfg_comp, looperName ):
14  super(TauAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
15 
16  #----------------------------------------
17  # DECLARATION OF HANDLES OF LEPTONS STUFF
18  #----------------------------------------
19  def declareHandles(self):
20  super(TauAnalyzer, self).declareHandles()
21  self.handles['taus'] = AutoHandle( ('slimmedTaus',''),'std::vector<pat::Tau>')
22 
23 
24  def beginLoop(self, setup):
25  super(TauAnalyzer,self).beginLoop(setup)
26  self.counters.addCounter('events')
27  count = self.counters.counter('events')
28  count.register('all events')
29  count.register('has >=1 tau at preselection')
30  count.register('has >=1 selected taus')
31  count.register('has >=1 loose taus')
32  count.register('has >=1 inclusive taus')
33 
34  #------------------
35  # MAKE LEPTON LISTS
36  #------------------
37  def makeTaus(self, event):
38  event.selectedTaus = []
39  event.looseTaus = []
40  event.inclusiveTaus = []
41 
42  #get all
43  alltaus = map( Tau, self.handles['taus'].product() )
44 
45  foundTau = False
46  for tau in alltaus:
47  tau.associatedVertex = event.goodVertices[0] if len(event.goodVertices)>0 else event.vertices[0]
48  tau.lepVeto = False
49  tau.idDecayMode = tau.tauID("decayModeFinding")
50  tau.idDecayModeNewDMs = tau.tauID("decayModeFindingNewDMs")
51  if hasattr(self.cfg_ana, 'decayModeID') and self.cfg_ana.decayModeID and not tau.tauID(self.cfg_ana.decayModeID):
52  continue
53 
54  if self.cfg_ana.vetoLeptons:
55  for lep in event.selectedLeptons:
56  if deltaR(lep.eta(), lep.phi(), tau.eta(), tau.phi()) < self.cfg_ana.leptonVetoDR:
57  tau.lepVeto = True
58  if tau.lepVeto: continue
59  if self.cfg_ana.vetoLeptonsPOG:
60  if not tau.tauID(self.cfg_ana.tauAntiMuonID):
61  tau.lepVeto = True
62  if not tau.tauID(self.cfg_ana.tauAntiElectronID):
63  tau.lepVeto = True
64  if tau.lepVeto: continue
65 
66  if tau.pt() < self.cfg_ana.ptMin: continue
67  if abs(tau.eta()) > self.cfg_ana.etaMax: continue
68  if abs(tau.dxy()) > self.cfg_ana.dxyMax or abs(tau.dz()) > self.cfg_ana.dzMax: continue
69 
70  foundTau = True
71  def id3(tau,X):
72  """Create an integer equal to 1-2-3 for (loose,medium,tight)"""
73  return tau.tauID(X%"Loose") + tau.tauID(X%"Medium") + tau.tauID(X%"Tight")
74  def id5(tau,X):
75  """Create an integer equal to 1-2-3-4-5 for (very loose,
76  loose, medium, tight, very tight)"""
77  return id3(tau, X) + tau.tauID(X%"VLoose") + tau.tauID(X%"VTight")
78  def id6(tau,X):
79  """Create an integer equal to 1-2-3-4-5-6 for (very loose,
80  loose, medium, tight, very tight, very very tight)"""
81  return id5(tau, X) + tau.tauID(X%"VVTight")
82 
83  tau.idMVA = id6(tau, "by%sIsolationMVA3oldDMwLT")
84  tau.idMVANewDM = id6(tau, "by%sIsolationMVA3newDMwLT")
85  tau.idCI3hit = id3(tau, "by%sCombinedIsolationDeltaBetaCorr3Hits")
86  tau.idAntiMu = tau.tauID("againstMuonLoose") + tau.tauID("againstMuonTight")
87  tau.idAntiE = id5(tau, "againstElectron%sMVA5")
88  #print "Tau pt %5.1f: idMVA2 %d, idCI3hit %d, %s, %s" % (tau.pt(), tau.idMVA2, tau.idCI3hit, tau.tauID(self.cfg_ana.tauID), tau.tauID(self.cfg_ana.tauLooseID))
89  if tau.tauID(self.cfg_ana.tauID):
90  event.selectedTaus.append(tau)
91  event.inclusiveTaus.append(tau)
92  elif tau.tauID(self.cfg_ana.tauLooseID):
93  event.looseTaus.append(tau)
94  event.inclusiveTaus.append(tau)
95 
96  event.selectedTaus.sort(key = lambda l : l.pt(), reverse = True)
97  event.looseTaus.sort(key = lambda l : l.pt(), reverse = True)
98  self.counters.counter('events').inc('all events')
99  if foundTau: self.counters.counter('events').inc('has >=1 tau at preselection')
100  if len(event.selectedTaus): self.counters.counter('events').inc('has >=1 selected taus')
101  if len(event.looseTaus): self.counters.counter('events').inc('has >=1 loose taus')
102  if len(event.inclusiveTaus): self.counters.counter('events').inc('has >=1 inclusive taus')
103 
104 
105  def matchTaus(self, event):
106  match = matchObjectCollection3(event.inclusiveTaus, event.gentaus, deltaRMax = 0.5)
107  for lep in event.inclusiveTaus:
108  gen = match[lep]
109  lep.mcMatchId = 1 if gen else 0
110 
111  def process(self, event):
112  self.readCollections( event.input )
113 
114  self.makeTaus(event)
115 
116  if not self.cfg_comp.isMC:
117  return True
118 
119  if hasattr(event, 'gentaus'):
120  self.matchTaus(event)
121 
122  return True
123 
124 # Find the definitions of the tau ID strings here:
125 # http://cmslxr.fnal.gov/lxr/source/PhysicsTools/PatAlgos/python/producersLayer1/tauProducer_cfi.py
126 
127 setattr(TauAnalyzer,"defaultConfig",cfg.Analyzer(
128  class_object=TauAnalyzer,
129  ptMin = 20,
130  etaMax = 9999,
131  dxyMax = 1000.,
132  dzMax = 0.2,
133  vetoLeptons = True,
134  leptonVetoDR = 0.4,
135  decayModeID = "decayModeFindingNewDMs", # ignored if not set or ""
136  tauID = "byLooseCombinedIsolationDeltaBetaCorr3Hits",
137  vetoLeptonsPOG = False, # If True, the following two IDs are required
138  tauAntiMuonID = "againstMuonLoose3",
139  tauAntiElectronID = "againstElectronLooseMVA5",
140  tauLooseID = "decayModeFinding",
141  )
142 )
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
def matchObjectCollection3
Definition: deltar.py:38