Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:27

0001 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
0002 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
0003 from PhysicsTools.Heppy.physicsobjects.Tau import Tau
0004 
0005 from PhysicsTools.HeppyCore.utils.deltar import deltaR, matchObjectCollection3
0006 
0007 import PhysicsTools.HeppyCore.framework.config as cfg
0008 
0009  
0010 class TauAnalyzer( Analyzer ):
0011 
0012     
0013     def __init__(self, cfg_ana, cfg_comp, looperName ):
0014         super(TauAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
0015 
0016     #----------------------------------------
0017     # DECLARATION OF HANDLES OF LEPTONS STUFF   
0018     #----------------------------------------
0019     def declareHandles(self):
0020         super(TauAnalyzer, self).declareHandles()
0021         self.handles['taus'] = AutoHandle( ('slimmedTaus',''),'std::vector<pat::Tau>')
0022 
0023     def beginLoop(self, setup):
0024         super(TauAnalyzer,self).beginLoop(setup)
0025         self.counters.addCounter('events')
0026         count = self.counters.counter('events')
0027         count.register('all events')
0028         count.register('has >=1 tau at preselection')
0029         count.register('has >=1 selected taus')
0030         count.register('has >=1 other taus')
0031 
0032     #------------------
0033     # MAKE LEPTON LISTS
0034     #------------------
0035     def makeTaus(self, event):
0036         event.inclusiveTaus = []
0037         event.selectedTaus = []
0038         event.otherTaus = []
0039 
0040         #get all
0041         alltaus = map( Tau, self.handles['taus'].product() )
0042 
0043         #make inclusive taus
0044         for tau in alltaus:
0045             tau.associatedVertex = event.goodVertices[0] if len(event.goodVertices)>0 else event.vertices[0]
0046             tau.lepVeto = False
0047             tau.idDecayMode = tau.tauID("decayModeFinding")
0048             tau.idDecayModeNewDMs = tau.tauID("decayModeFindingNewDMs")
0049 
0050             if hasattr(self.cfg_ana, 'inclusive_decayModeID') and self.cfg_ana.inclusive_decayModeID and not tau.tauID(self.cfg_ana.inclusive_decayModeID):
0051                 continue
0052 
0053             tau.inclusive_lepVeto = False
0054             if self.cfg_ana.inclusive_vetoLeptons:
0055                 for lep in event.selectedLeptons:
0056                     if deltaR(lep.eta(), lep.phi(), tau.eta(), tau.phi()) < self.cfg_ana.inclusive_leptonVetoDR:
0057                         tau.inclusive_lepVeto = True
0058                 if tau.inclusive_lepVeto: continue
0059             if self.cfg_ana.inclusive_vetoLeptonsPOG:
0060                 if not tau.tauID(self.cfg_ana.inclusive_tauAntiMuonID):
0061                         tau.inclusive_lepVeto = True
0062                 if not tau.tauID(self.cfg_ana.inclusive_tauAntiElectronID):
0063                         tau.inclusive_lepVeto = True
0064                 if tau.inclusive_lepVeto: continue
0065 
0066             if tau.pt() < self.cfg_ana.inclusive_ptMin: continue
0067             if abs(tau.eta()) > self.cfg_ana.inclusive_etaMax: continue
0068             if abs(tau.dxy()) > self.cfg_ana.inclusive_dxyMax or abs(tau.dz()) > self.cfg_ana.inclusive_dzMax: continue
0069 
0070             def id3(tau,X):
0071                 """Create an integer equal to 1-2-3 for (loose,medium,tight)"""
0072                 return tau.tauID(X%"Loose") + tau.tauID(X%"Medium") + tau.tauID(X%"Tight")
0073             def id5(tau,X):
0074                 """Create an integer equal to 1-2-3-4-5 for (very loose, 
0075                     loose, medium, tight, very tight)"""
0076                 return id3(tau, X) + tau.tauID(X%"VLoose") + tau.tauID(X%"VTight")
0077             def id6(tau,X):
0078                 """Create an integer equal to 1-2-3-4-5-6 for (very loose, 
0079                     loose, medium, tight, very tight, very very tight)"""
0080                 return id5(tau, X) + tau.tauID(X%"VVTight")
0081 
0082             tau.idMVA = id6(tau, "by%sIsolationMVArun2v1DBoldDMwLT")
0083             tau.idMVANewDM = id6(tau, "by%sIsolationMVArun2v1DBnewDMwLT")
0084             tau.idCI3hit = id3(tau, "by%sCombinedIsolationDeltaBetaCorr3Hits")
0085             tau.idAntiMu = tau.tauID("againstMuonLoose3") + tau.tauID("againstMuonTight3")
0086             tau.idAntiE = id5(tau, "againstElectron%sMVA6")
0087             #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))
0088             
0089             if tau.tauID(self.cfg_ana.inclusive_tauID):
0090                 event.inclusiveTaus.append(tau)
0091             
0092         for tau in event.inclusiveTaus:
0093 
0094             tau.loose_lepVeto = False
0095             if self.cfg_ana.loose_vetoLeptons:
0096                 for lep in event.selectedLeptons:
0097                     if deltaR(lep.eta(), lep.phi(), tau.eta(), tau.phi()) < self.cfg_ana.loose_leptonVetoDR:
0098                         tau.loose_lepVeto = True
0099             if self.cfg_ana.loose_vetoLeptonsPOG:
0100                 if not tau.tauID(self.cfg_ana.loose_tauAntiMuonID):
0101                         tau.loose_lepVeto = True
0102                 if not tau.tauID(self.cfg_ana.loose_tauAntiElectronID):
0103                         tau.loose_lepVeto = True
0104 
0105             if tau.tauID(self.cfg_ana.loose_decayModeID) and \
0106                       tau.pt() > self.cfg_ana.loose_ptMin and abs(tau.eta()) < self.cfg_ana.loose_etaMax and \
0107                       abs(tau.dxy()) < self.cfg_ana.loose_dxyMax and abs(tau.dz()) < self.cfg_ana.loose_dzMax and \
0108                       tau.tauID(self.cfg_ana.loose_tauID) and not tau.loose_lepVeto:
0109                 event.selectedTaus.append(tau)
0110             else:
0111                 event.otherTaus.append(tau)
0112 
0113         event.inclusiveTaus.sort(key = lambda l : l.pt(), reverse = True)        
0114         event.selectedTaus.sort(key = lambda l : l.pt(), reverse = True)
0115         event.otherTaus.sort(key = lambda l : l.pt(), reverse = True)
0116         self.counters.counter('events').inc('all events')
0117         if len(event.inclusiveTaus): self.counters.counter('events').inc('has >=1 tau at preselection')
0118         if len(event.selectedTaus): self.counters.counter('events').inc('has >=1 selected taus')
0119         if len(event.otherTaus): self.counters.counter('events').inc('has >=1 other taus')
0120 
0121     def matchTaus(self, event):
0122         match = matchObjectCollection3(event.inclusiveTaus, event.gentaus, deltaRMax = 0.5)
0123         for lep in event.inclusiveTaus:
0124             gen = match[lep]
0125             lep.mcMatchId = 1 if gen else 0
0126             lep.genp = gen
0127 
0128     def process(self, event):
0129         self.readCollections( event.input )
0130 
0131         self.makeTaus(event)
0132 
0133         if not self.cfg_comp.isMC:
0134             return True
0135 
0136         if hasattr(event, 'gentaus'):
0137             self.matchTaus(event)
0138         
0139         return True
0140 
0141 # Find the definitions of the tau ID strings here:
0142 # http://cmslxr.fnal.gov/lxr/source/PhysicsTools/PatAlgos/python/producersLayer1/tauProducer_cfi.py
0143 
0144 setattr(TauAnalyzer,"defaultConfig",cfg.Analyzer(
0145     class_object = TauAnalyzer,
0146     # inclusive very loose hadronic tau selection
0147     inclusive_ptMin = 18,
0148     inclusive_etaMax = 9999,
0149     inclusive_dxyMax = 1000.,
0150     inclusive_dzMax = 0.4,
0151     inclusive_vetoLeptons = False,
0152     inclusive_leptonVetoDR = 0.4,
0153     inclusive_decayModeID = "decayModeFindingNewDMs", # ignored if not set or ""
0154     inclusive_tauID = "decayModeFindingNewDMs",
0155     inclusive_vetoLeptonsPOG = False, # If True, the following two IDs are required
0156     inclusive_tauAntiMuonID = "",
0157     inclusive_tauAntiElectronID = "",
0158     # loose hadronic tau selection
0159     loose_ptMin = 18,
0160     loose_etaMax = 9999,
0161     loose_dxyMax = 1000.,
0162     loose_dzMax = 0.2,
0163     loose_vetoLeptons = True,
0164     loose_leptonVetoDR = 0.4,
0165     loose_decayModeID = "decayModeFindingNewDMs", # ignored if not set or ""
0166     loose_tauID = "byLooseCombinedIsolationDeltaBetaCorr3Hits",
0167     loose_vetoLeptonsPOG = False, # If True, the following two IDs are required
0168     loose_tauAntiMuonID = "againstMuonLoose3",
0169     loose_tauAntiElectronID = "againstElectronLooseMVA5"
0170   )
0171 )