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  lep.mcTau = gen
111 
112  def process(self, event):
113  self.readCollections( event.input )
114 
115  self.makeTaus(event)
116 
117  if not self.cfg_comp.isMC:
118  return True
119 
120  if hasattr(event, 'gentaus'):
121  self.matchTaus(event)
122 
123  return True
124 
125 # Find the definitions of the tau ID strings here:
126 # http://cmslxr.fnal.gov/lxr/source/PhysicsTools/PatAlgos/python/producersLayer1/tauProducer_cfi.py
127 
128 setattr(TauAnalyzer,"defaultConfig",cfg.Analyzer(
129  class_object=TauAnalyzer,
130  ptMin = 20,
131  etaMax = 9999,
132  dxyMax = 1000.,
133  dzMax = 0.2,
134  vetoLeptons = True,
135  leptonVetoDR = 0.4,
136  decayModeID = "decayModeFindingNewDMs", # ignored if not set or ""
137  tauID = "byLooseCombinedIsolationDeltaBetaCorr3Hits",
138  vetoLeptonsPOG = False, # If True, the following two IDs are required
139  tauAntiMuonID = "againstMuonLoose3",
140  tauAntiElectronID = "againstElectronLooseMVA5",
141  tauLooseID = "decayModeFinding",
142  )
143 )
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:41