test
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 
7 import PhysicsTools.HeppyCore.framework.config as cfg
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  def beginLoop(self, setup):
24  super(TauAnalyzer,self).beginLoop(setup)
25  self.counters.addCounter('events')
26  count = self.counters.counter('events')
27  count.register('all events')
28  count.register('has >=1 tau at preselection')
29  count.register('has >=1 selected taus')
30  count.register('has >=1 other taus')
31 
32  #------------------
33  # MAKE LEPTON LISTS
34  #------------------
35  def makeTaus(self, event):
36  event.inclusiveTaus = []
37  event.selectedTaus = []
38  event.otherTaus = []
39 
40  #get all
41  alltaus = map( Tau, self.handles['taus'].product() )
42 
43  #make inclusive taus
44  for tau in alltaus:
45  tau.associatedVertex = event.goodVertices[0] if len(event.goodVertices)>0 else event.vertices[0]
46  tau.lepVeto = False
47  tau.idDecayMode = tau.tauID("decayModeFinding")
48  tau.idDecayModeNewDMs = tau.tauID("decayModeFindingNewDMs")
49 
50  if hasattr(self.cfg_ana, 'inclusive_decayModeID') and self.cfg_ana.inclusive_decayModeID and not tau.tauID(self.cfg_ana.inclusive_decayModeID):
51  continue
52 
53  tau.inclusive_lepVeto = False
54  if self.cfg_ana.inclusive_vetoLeptons:
55  for lep in event.selectedLeptons:
56  if deltaR(lep.eta(), lep.phi(), tau.eta(), tau.phi()) < self.cfg_ana.inclusive_leptonVetoDR:
57  tau.inclusive_lepVeto = True
58  if tau.inclusive_lepVeto: continue
59  if self.cfg_ana.inclusive_vetoLeptonsPOG:
60  if not tau.tauID(self.cfg_ana.inclusive_tauAntiMuonID):
61  tau.inclusive_lepVeto = True
62  if not tau.tauID(self.cfg_ana.inclusive_tauAntiElectronID):
63  tau.inclusive_lepVeto = True
64  if tau.inclusive_lepVeto: continue
65 
66  if tau.pt() < self.cfg_ana.inclusive_ptMin: continue
67  if abs(tau.eta()) > self.cfg_ana.inclusive_etaMax: continue
68  if abs(tau.dxy()) > self.cfg_ana.inclusive_dxyMax or abs(tau.dz()) > self.cfg_ana.inclusive_dzMax: continue
69 
70  def id3(tau,X):
71  """Create an integer equal to 1-2-3 for (loose,medium,tight)"""
72  return tau.tauID(X%"Loose") + tau.tauID(X%"Medium") + tau.tauID(X%"Tight")
73  def id5(tau,X):
74  """Create an integer equal to 1-2-3-4-5 for (very loose,
75  loose, medium, tight, very tight)"""
76  return id3(tau, X) + tau.tauID(X%"VLoose") + tau.tauID(X%"VTight")
77  def id6(tau,X):
78  """Create an integer equal to 1-2-3-4-5-6 for (very loose,
79  loose, medium, tight, very tight, very very tight)"""
80  return id5(tau, X) + tau.tauID(X%"VVTight")
81 
82  tau.idMVA = id6(tau, "by%sIsolationMVA3oldDMwLT")
83  tau.idMVANewDM = id6(tau, "by%sIsolationMVA3newDMwLT")
84  tau.idCI3hit = id3(tau, "by%sCombinedIsolationDeltaBetaCorr3Hits")
85  tau.idAntiMu = tau.tauID("againstMuonLoose3") + tau.tauID("againstMuonTight3")
86  tau.idAntiE = id5(tau, "againstElectron%sMVA5")
87  #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))
88 
89  if tau.tauID(self.cfg_ana.inclusive_tauID):
90  event.inclusiveTaus.append(tau)
91 
92  for tau in event.inclusiveTaus:
93 
94  tau.loose_lepVeto = False
95  if self.cfg_ana.loose_vetoLeptons:
96  for lep in event.selectedLeptons:
97  if deltaR(lep.eta(), lep.phi(), tau.eta(), tau.phi()) < self.cfg_ana.loose_leptonVetoDR:
98  tau.loose_lepVeto = True
99  if self.cfg_ana.loose_vetoLeptonsPOG:
100  if not tau.tauID(self.cfg_ana.loose_tauAntiMuonID):
101  tau.loose_lepVeto = True
102  if not tau.tauID(self.cfg_ana.loose_tauAntiElectronID):
103  tau.loose_lepVeto = True
104 
105  if tau.tauID(self.cfg_ana.loose_decayModeID) and \
106  tau.pt() > self.cfg_ana.loose_ptMin and abs(tau.eta()) < self.cfg_ana.loose_etaMax and \
107  abs(tau.dxy()) < self.cfg_ana.loose_dxyMax and abs(tau.dz()) < self.cfg_ana.loose_dzMax and \
108  tau.tauID(self.cfg_ana.loose_tauID) and not tau.loose_lepVeto:
109  event.selectedTaus.append(tau)
110  else:
111  event.otherTaus.append(tau)
112 
113  event.inclusiveTaus.sort(key = lambda l : l.pt(), reverse = True)
114  event.selectedTaus.sort(key = lambda l : l.pt(), reverse = True)
115  event.otherTaus.sort(key = lambda l : l.pt(), reverse = True)
116  self.counters.counter('events').inc('all events')
117  if len(event.inclusiveTaus): self.counters.counter('events').inc('has >=1 tau at preselection')
118  if len(event.selectedTaus): self.counters.counter('events').inc('has >=1 selected taus')
119  if len(event.otherTaus): self.counters.counter('events').inc('has >=1 other taus')
120 
121  def matchTaus(self, event):
122  match = matchObjectCollection3(event.inclusiveTaus, event.gentaus, deltaRMax = 0.5)
123  for lep in event.inclusiveTaus:
124  gen = match[lep]
125  lep.mcMatchId = 1 if gen else 0
126  lep.genp = gen
127 
128  def process(self, event):
129  self.readCollections( event.input )
130 
131  self.makeTaus(event)
132 
133  if not self.cfg_comp.isMC:
134  return True
135 
136  if hasattr(event, 'gentaus'):
137  self.matchTaus(event)
138 
139  return True
140 
141 # Find the definitions of the tau ID strings here:
142 # http://cmslxr.fnal.gov/lxr/source/PhysicsTools/PatAlgos/python/producersLayer1/tauProducer_cfi.py
143 
144 setattr(TauAnalyzer,"defaultConfig",cfg.Analyzer(
145  class_object = TauAnalyzer,
146  # inclusive very loose hadronic tau selection
147  inclusive_ptMin = 18,
148  inclusive_etaMax = 9999,
149  inclusive_dxyMax = 1000.,
150  inclusive_dzMax = 0.4,
151  inclusive_vetoLeptons = False,
152  inclusive_leptonVetoDR = 0.4,
153  inclusive_decayModeID = "decayModeFindingNewDMs", # ignored if not set or ""
154  inclusive_tauID = "decayModeFindingNewDMs",
155  inclusive_vetoLeptonsPOG = False, # If True, the following two IDs are required
156  inclusive_tauAntiMuonID = "",
157  inclusive_tauAntiElectronID = "",
158  # loose hadronic tau selection
159  loose_ptMin = 18,
160  loose_etaMax = 9999,
161  loose_dxyMax = 1000.,
162  loose_dzMax = 0.2,
163  loose_vetoLeptons = True,
164  loose_leptonVetoDR = 0.4,
165  loose_decayModeID = "decayModeFindingNewDMs", # ignored if not set or ""
166  loose_tauID = "byLooseCombinedIsolationDeltaBetaCorr3Hits",
167  loose_vetoLeptonsPOG = False, # If True, the following two IDs are required
168  loose_tauAntiMuonID = "againstMuonLoose3",
169  loose_tauAntiElectronID = "againstElectronLooseMVA5"
170  )
171 )
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