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 
2 import operator
3 import itertools
4 import copy
5 import types
6 
7 from ROOT import TLorentzVector
8 
9 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
10 from PhysicsTools.HeppyCore.framework.event import Event
11 from PhysicsTools.HeppyCore.statistics.counter import Counter, Counters
12 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
13 from PhysicsTools.Heppy.physicsobjects.Lepton import Lepton
14 from PhysicsTools.Heppy.physicsobjects.Tau import Tau
15 
16 from PhysicsTools.HeppyCore.utils.deltar import deltaR, deltaPhi, bestMatch , matchObjectCollection3
17 
19 
20 
21 class TauAnalyzer( Analyzer ):
22 
23 
24  def __init__(self, cfg_ana, cfg_comp, looperName ):
25  super(TauAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
26 
27  #----------------------------------------
28  # DECLARATION OF HANDLES OF LEPTONS STUFF
29  #----------------------------------------
30  def declareHandles(self):
31  super(TauAnalyzer, self).declareHandles()
32  self.handles['taus'] = AutoHandle( ('slimmedTaus',''),'std::vector<pat::Tau>')
33 
34 
35  def beginLoop(self, setup):
36  super(TauAnalyzer,self).beginLoop(setup)
37  self.counters.addCounter('events')
38  count = self.counters.counter('events')
39  count.register('all events')
40  count.register('has >=1 tau at preselection')
41  count.register('has >=1 selected taus')
42  count.register('has >=1 loose taus')
43  count.register('has >=1 inclusive taus')
44 
45  #------------------
46  # MAKE LEPTON LISTS
47  #------------------
48  def makeTaus(self, event):
49  event.selectedTaus = []
50  event.looseTaus = []
51  event.inclusiveTaus = []
52 
53  #get all
54  alltaus = map( Tau, self.handles['taus'].product() )
55 
56  foundTau = False
57  for tau in alltaus:
58  tau.associatedVertex = event.goodVertices[0]
59  tau.lepVeto = False
60  if self.cfg_ana.vetoLeptons:
61  for lep in event.selectedLeptons:
62  if deltaR(lep.eta(), lep.phi(), tau.eta(), tau.phi()) < self.cfg_ana.leptonVetoDR:
63  tau.lepVeto = True
64  if tau.lepVeto: continue
65  if self.cfg_ana.vetoLeptonsPOG:
66  if not tau.tauID("againstMuonTight"):
67  tau.lepVeto = True
68  if not tau.tauID("againstElectronLoose"):
69  tau.lepVeto = True
70  if tau.lepVeto: continue
71  if tau.pt() < self.cfg_ana.ptMin: continue
72  if abs(tau.eta()) > self.cfg_ana.etaMax: continue
73 ### tau.dxy and tau.dz are zero
74 ### if abs(tau.dxy()) > self.cfg_ana.dxyMax or abs(tau.dz()) > self.cfg_ana.dzMax: continue
75  foundTau = True
76  def id3(tau,X):
77  """Create an integer equal to 1-2-3 for (loose,medium,tight)"""
78  return tau.tauID(X%"Loose") + tau.tauID(X%"Medium") + tau.tauID(X%"Tight")
79  #tau.idMVA2 = id3(tau, "by%sIsolationMVA2")
80  tau.idCI3hit = id3(tau, "by%sCombinedIsolationDeltaBetaCorr3Hits")
81  #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))
82  if tau.tauID(self.cfg_ana.tauID):
83  event.selectedTaus.append(tau)
84  event.inclusiveTaus.append(tau)
85  elif tau.tauID(self.cfg_ana.tauLooseID):
86  event.looseTaus.append(tau)
87  event.inclusiveTaus.append(tau)
88 
89  event.selectedTaus.sort(key = lambda l : l.pt(), reverse = True)
90  event.looseTaus.sort(key = lambda l : l.pt(), reverse = True)
91  self.counters.counter('events').inc('all events')
92  if foundTau: self.counters.counter('events').inc('has >=1 tau at preselection')
93  if len(event.selectedTaus): self.counters.counter('events').inc('has >=1 selected taus')
94  if len(event.looseTaus): self.counters.counter('events').inc('has >=1 loose taus')
95  if len(event.inclusiveTaus): self.counters.counter('events').inc('has >=1 inclusive taus')
96 
97 
98  def matchTaus(self, event):
99  match = matchObjectCollection3(event.inclusiveTaus, event.gentaus, deltaRMax = 0.5)
100  for lep in event.inclusiveTaus:
101  gen = match[lep]
102  lep.mcMatchId = 1 if gen else 0
103 
104  def process(self, event):
105  self.readCollections( event.input )
106 
107  self.makeTaus(event)
108 
109  if not self.cfg_comp.isMC:
110  return True
111 
112  if hasattr(event, 'gentaus'):
113  self.matchTaus(event)
114 
115  return True
116 
117 
118 setattr(TauAnalyzer,"defaultConfig",cfg.Analyzer(
119  class_object=TauAnalyzer,
120  ptMin = 20,
121  etaMax = 9999,
122  dxyMax = 0.5,
123  dzMax = 1.0,
124  vetoLeptons = True,
125  leptonVetoDR = 0.4,
126  vetoLeptonsPOG = False,
127  tauID = "byLooseCombinedIsolationDeltaBetaCorr3Hits",
128  tauLooseID = "decayModeFinding",
129  )
130 )
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