CMS 3D CMS Logo

taus_cff.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
3 from PhysicsTools.NanoAOD.nano_eras_cff import run3_nanoAOD_124
4 from PhysicsTools.NanoAOD.simpleCandidateFlatTableProducer_cfi import simpleCandidateFlatTableProducer
5 
6 from PhysicsTools.JetMCAlgos.TauGenJets_cfi import tauGenJets
7 from PhysicsTools.JetMCAlgos.TauGenJetsDecayModeSelectorAllHadrons_cfi import tauGenJetsSelectorAllHadrons
8 
9 
12 
13 
14 
15 # Original DeepTau v2p5 in 12_4_X doesn't include WPs in MINIAOD
16 # Import thresholds here to define WPs manually from raw scores
17 from RecoTauTag.RecoTau.tauIdWPsDefs import WORKING_POINTS_v2p5
18 
19 finalTaus = cms.EDFilter("PATTauRefSelector",
20  src = cms.InputTag("slimmedTaus"),
21  cut = cms.string("pt > 18 && tauID('decayModeFindingNewDMs') && (tauID('byLooseCombinedIsolationDeltaBetaCorr3Hits') || (tauID('chargedIsoPtSumdR03')+max(0.,tauID('neutralIsoPtSumdR03')-0.072*tauID('puCorrPtSum'))<2.5) || tauID('byVVVLooseDeepTau2017v2p1VSjet') || tauID('byVVVLooseDeepTau2018v2p5VSjet'))")
22 )
23 
24 run3_nanoAOD_124.toModify(
25  finalTaus,
26  cut = cms.string("pt > 18 && tauID('decayModeFindingNewDMs') && (tauID('byLooseCombinedIsolationDeltaBetaCorr3Hits') || (tauID('chargedIsoPtSumdR03')+max(0.,tauID('neutralIsoPtSumdR03')-0.072*tauID('puCorrPtSum'))<2.5) || tauID('byVVVLooseDeepTau2017v2p1VSjet') || (tauID('byDeepTau2018v2p5VSjetraw') > {}))".format(WORKING_POINTS_v2p5["jet"]["VVVLoose"]))
27 )
28 
29 
30 def _tauIdWPMask(pattern, choices, doc="", from_raw=False, wp_thrs=None):
31  if from_raw:
32  assert wp_thrs is not None, "wp_thrs argument in _tauIdWPMask() is None, expect it to be dict-like"
33 
34  var_definition = []
35  for wp_name in choices:
36  if not isinstance(wp_thrs[wp_name], float):
37  raise TypeError("Threshold for WP=%s is not a float number." % wp_name)
38  wp_definition = "test_bit(tauID('{}')-{}+1,0)".format(pattern, wp_thrs[wp_name])
39  var_definition.append(wp_definition)
40  var_definition = " + ".join(var_definition)
41  else:
42  var_definition = " + ".join(["tauID('%s')" % (pattern % c) for c in choices])
43 
44  doc = doc + ": "+", ".join(["%d = %s" % (i,c) for (i,c) in enumerate(choices, start=1)])
45  return Var(var_definition, "uint8", doc=doc)
46 
47 
48 tauTable = simpleCandidateFlatTableProducer.clone(
49  src = cms.InputTag("linkedObjects","taus"),
50  name= cms.string("Tau"),
51  doc = cms.string("slimmedTaus after basic selection (" + finalTaus.cut.value()+")")
52 )
53 
54 _tauVarsBase = cms.PSet(P4Vars,
55  charge = Var("charge", int, doc="electric charge"),
56  jetIdx = Var("?hasUserCand('jet')?userCand('jet').key():-1", int, doc="index of the associated jet (-1 if none)"),
57  eleIdx = Var("?overlaps('electrons').size()>0?overlaps('electrons')[0].key():-1", int, doc="index of first matching electron"),
58  muIdx = Var("?overlaps('muons').size()>0?overlaps('muons')[0].key():-1", int, doc="index of first matching muon"),
59  svIdx1 = Var("?overlaps('vertices').size()>0?overlaps('vertices')[0].key():-1", int, doc="index of first matching secondary vertex"),
60  svIdx2 = Var("?overlaps('vertices').size()>1?overlaps('vertices')[1].key():-1", int, doc="index of second matching secondary vertex"),
61  nSVs = Var("?hasOverlaps('vertices')?overlaps('vertices').size():0", int, doc="number of secondary vertices in the tau"),
62  decayMode = Var("decayMode()",int),
63  idDecayModeOldDMs = Var("tauID('decayModeFinding')", bool),
64 
65  leadTkPtOverTauPt = Var("leadChargedHadrCand.pt/pt ",float, doc="pt of the leading track divided by tau pt",precision=10),
66  leadTkDeltaEta = Var("leadChargedHadrCand.eta - eta ",float, doc="eta of the leading track, minus tau eta",precision=8),
67  leadTkDeltaPhi = Var("deltaPhi(leadChargedHadrCand.phi, phi) ",float, doc="phi of the leading track, minus tau phi",precision=8),
68 
69  dxy = Var("leadChargedHadrCand().dxy()",float, doc="d_{xy} of lead track with respect to PV, in cm (with sign)",precision=10),
70  dz = Var("leadChargedHadrCand().dz()",float, doc="d_{z} of lead track with respect to PV, in cm (with sign)",precision=14),
71 
72  # these are too many, we may have to suppress some
73  rawIso = Var( "tauID('byCombinedIsolationDeltaBetaCorrRaw3Hits')", float, doc = "combined isolation (deltaBeta corrections)", precision=10),
74  rawIsodR03 = Var( "(tauID('chargedIsoPtSumdR03')+max(0.,tauID('neutralIsoPtSumdR03')-0.072*tauID('puCorrPtSum')))", float, doc = "combined isolation (deltaBeta corrections, dR=0.3)", precision=10),
75  chargedIso = Var( "tauID('chargedIsoPtSum')", float, doc = "charged isolation", precision=10),
76  neutralIso = Var( "tauID('neutralIsoPtSum')", float, doc = "neutral (photon) isolation", precision=10),
77  puCorr = Var( "tauID('puCorrPtSum')", float, doc = "pileup correction", precision=10),
78  photonsOutsideSignalCone = Var( "tauID('photonPtSumOutsideSignalCone')", float, doc = "sum of photons outside signal cone", precision=10),
79 
80  idAntiMu = _tauIdWPMask("againstMuon%s3", choices=("Loose","Tight"), doc= "Anti-muon discriminator V3: "),
81  idAntiEleDeadECal = Var("tauID('againstElectronDeadECAL')", bool, doc = "Anti-electron dead-ECal discriminator"),
82 
83 )
84 
85 _deepTauVars2017v2p1 = cms.PSet(
86  rawDeepTau2017v2p1VSe = Var("tauID('byDeepTau2017v2p1VSeraw')", float, doc="byDeepTau2017v2p1VSe raw output discriminator (deepTau2017v2p1)", precision=10),
87  rawDeepTau2017v2p1VSmu = Var("tauID('byDeepTau2017v2p1VSmuraw')", float, doc="byDeepTau2017v2p1VSmu raw output discriminator (deepTau2017v2p1)", precision=10),
88  rawDeepTau2017v2p1VSjet = Var("tauID('byDeepTau2017v2p1VSjetraw')", float, doc="byDeepTau2017v2p1VSjet raw output discriminator (deepTau2017v2p1)", precision=10),
89  idDeepTau2017v2p1VSe = _tauIdWPMask("by%sDeepTau2017v2p1VSe",
90  choices=("VVVLoose","VVLoose","VLoose","Loose","Medium","Tight","VTight","VVTight"),
91  doc="byDeepTau2017v2p1VSe ID working points (deepTau2017v2p1)"),
92  idDeepTau2017v2p1VSmu = _tauIdWPMask("by%sDeepTau2017v2p1VSmu",
93  choices=("VLoose", "Loose", "Medium", "Tight"),
94  doc="byDeepTau2017v2p1VSmu ID working points (deepTau2017v2p1)"),
95  idDeepTau2017v2p1VSjet = _tauIdWPMask("by%sDeepTau2017v2p1VSjet",
96  choices=("VVVLoose","VVLoose","VLoose","Loose","Medium","Tight","VTight","VVTight"),
97  doc="byDeepTau2017v2p1VSjet ID working points (deepTau2017v2p1)"),
98 )
99 _deepTauVars2018v2p5 = cms.PSet(
100  rawDeepTau2018v2p5VSe = Var("tauID('byDeepTau2018v2p5VSeraw')", float, doc="byDeepTau2018v2p5VSe raw output discriminator (deepTau2018v2p5)", precision=10),
101  rawDeepTau2018v2p5VSmu = Var("tauID('byDeepTau2018v2p5VSmuraw')", float, doc="byDeepTau2018v2p5VSmu raw output discriminator (deepTau2018v2p5)", precision=10),
102  rawDeepTau2018v2p5VSjet = Var("tauID('byDeepTau2018v2p5VSjetraw')", float, doc="byDeepTau2018v2p5VSjet raw output discriminator (deepTau2018v2p5)", precision=10),
103  idDeepTau2018v2p5VSe = _tauIdWPMask("by%sDeepTau2018v2p5VSe",
104  choices=("VVVLoose","VVLoose","VLoose","Loose","Medium","Tight","VTight","VVTight"),
105  doc="byDeepTau2018v2p5VSe ID working points (deepTau2018v2p5)"),
106  idDeepTau2018v2p5VSmu = _tauIdWPMask("by%sDeepTau2018v2p5VSmu",
107  choices=("VLoose", "Loose", "Medium", "Tight"),
108  doc="byDeepTau2018v2p5VSmu ID working points (deepTau2018v2p5)"),
109  idDeepTau2018v2p5VSjet = _tauIdWPMask("by%sDeepTau2018v2p5VSjet",
110  choices=("VVVLoose","VVLoose","VLoose","Loose","Medium","Tight","VTight","VVTight"),
111  doc="byDeepTau2018v2p5VSjet ID working points (deepTau2018v2p5)"),
112 )
113 
114 _variablesMiniV2 = cms.PSet(
115  _tauVarsBase,
116  _deepTauVars2017v2p1,
117  _deepTauVars2018v2p5
118 )
119 
120 tauTable.variables = _variablesMiniV2
121 
122 run3_nanoAOD_124.toModify(
123  tauTable.variables,
124  idDeepTau2018v2p5VSe = _tauIdWPMask("byDeepTau2018v2p5VSeraw",
125  choices=("VVVLoose","VVLoose","VLoose","Loose","Medium","Tight","VTight","VVTight"),
126  doc="byDeepTau2018v2p5VSe ID working points (deepTau2018v2p5)",
127  from_raw=True, wp_thrs=WORKING_POINTS_v2p5["e"]),
128  idDeepTau2018v2p5VSmu = _tauIdWPMask("byDeepTau2018v2p5VSmuraw",
129  choices=("VLoose", "Loose", "Medium", "Tight"),
130  doc="byDeepTau2018v2p5VSmu ID working points (deepTau2018v2p5)",
131  from_raw=True, wp_thrs=WORKING_POINTS_v2p5["mu"]),
132  idDeepTau2018v2p5VSjet = _tauIdWPMask("byDeepTau2018v2p5VSjetraw",
133  choices=("VVVLoose","VVLoose","VLoose","Loose","Medium","Tight","VTight","VVTight"),
134  doc="byDeepTau2018v2p5VSjet ID working points (deepTau2018v2p5)",
135  from_raw=True, wp_thrs=WORKING_POINTS_v2p5["jet"])
136 )
137 
138 
139 tauGenJetsForNano = tauGenJets.clone(
140  GenParticles = "finalGenParticles",
141  includeNeutrinos = False
142 )
143 
144 tauGenJetsSelectorAllHadronsForNano = tauGenJetsSelectorAllHadrons.clone(
145  src = "tauGenJetsForNano"
146 )
147 
148 genVisTaus = cms.EDProducer("GenVisTauProducer",
149  src = cms.InputTag("tauGenJetsSelectorAllHadronsForNano"),
150  srcGenParticles = cms.InputTag("finalGenParticles")
151 )
152 
153 genVisTauTable = simpleCandidateFlatTableProducer.clone(
154  src = cms.InputTag("genVisTaus"),
155  cut = cms.string("pt > 10."),
156  name = cms.string("GenVisTau"),
157  doc = cms.string("gen hadronic taus "),
158  variables = cms.PSet(
159  pt = Var("pt", float,precision=8),
160  phi = Var("phi", float,precision=8),
161  eta = Var("eta", float,precision=8),
162  mass = Var("mass", float,precision=8),
163  charge = Var("charge", int),
164  status = Var("status", int, doc="Hadronic tau decay mode. 0=OneProng0PiZero, 1=OneProng1PiZero, 2=OneProng2PiZero, 10=ThreeProng0PiZero, 11=ThreeProng1PiZero, 15=Other"),
165  genPartIdxMother = Var("?numberOfMothers>0?motherRef(0).key():-1", int, doc="index of the mother particle"),
166  )
167 )
168 
169 tausMCMatchLepTauForTable = cms.EDProducer("MCMatcher", # cut on deltaR, deltaPt/Pt; pick best by deltaR
170  src = tauTable.src, # final reco collection
171  matched = cms.InputTag("finalGenParticles"), # final mc-truth particle collection
172  mcPdgId = cms.vint32(11,13), # one or more PDG ID (11 = electron, 13 = muon); absolute values (see below)
173  checkCharge = cms.bool(False), # True = require RECO and MC objects to have the same charge
174  mcStatus = cms.vint32(), # PYTHIA status code (1 = stable, 2 = shower, 3 = hard scattering)
175  maxDeltaR = cms.double(0.3), # Minimum deltaR for the match
176  maxDPtRel = cms.double(0.5), # Minimum deltaPt/Pt for the match
177  resolveAmbiguities = cms.bool(True), # Forbid two RECO objects to match to the same GEN object
178  resolveByMatchQuality = cms.bool(True), # False = just match input in order; True = pick lowest deltaR pair first
179 )
180 
181 tausMCMatchHadTauForTable = cms.EDProducer("MCMatcher", # cut on deltaR, deltaPt/Pt; pick best by deltaR
182  src = tauTable.src, # final reco collection
183  matched = cms.InputTag("genVisTaus"), # generator level hadronic tau decays
184  mcPdgId = cms.vint32(15), # one or more PDG ID (15 = tau); absolute values (see below)
185  checkCharge = cms.bool(False), # True = require RECO and MC objects to have the same charge
186  mcStatus = cms.vint32(), # CV: no *not* require certain status code for matching (status code corresponds to decay mode for hadronic tau decays)
187  maxDeltaR = cms.double(0.3), # Maximum deltaR for the match
188  maxDPtRel = cms.double(1.), # Maximum deltaPt/Pt for the match
189  resolveAmbiguities = cms.bool(True), # Forbid two RECO objects to match to the same GEN object
190  resolveByMatchQuality = cms.bool(True), # False = just match input in order; True = pick lowest deltaR pair first
191 )
192 
193 tauMCTable = cms.EDProducer("CandMCMatchTableProducer",
194  src = tauTable.src,
195  mcMap = cms.InputTag("tausMCMatchLepTauForTable"),
196  mcMapVisTau = cms.InputTag("tausMCMatchHadTauForTable"),
197  objName = tauTable.name,
198  objType = tauTable.name, #cms.string("Tau"),
199  branchName = cms.string("genPart"),
200  docString = cms.string("MC matching to status==2 taus"),
201 )
202 
203 
204 tauTask = cms.Task(finalTaus)
205 tauTablesTask = cms.Task(tauTable)
206 
207 genTauTask = cms.Task(tauGenJetsForNano,tauGenJetsSelectorAllHadronsForNano,genVisTaus,genVisTauTable)
208 tauMCTask = cms.Task(genTauTask,tausMCMatchLepTauForTable,tausMCMatchHadTauForTable,tauMCTable)
209 
def Var(expr, valtype, doc=None, precision=-1)
Definition: common_cff.py:16
def _tauIdWPMask(pattern, choices, doc="", from_raw=False, wp_thrs=None)
Tables for final output and docs ##########################.
Definition: taus_cff.py:30
Updated tau collection with MVA-based tau-Ids rerun ####### Used only in some eras.
static std::string join(char **cmd)
Definition: RemoteFile.cc:19