CMS 3D CMS Logo

muons_cff.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 
5 from PhysicsTools.NanoAOD.simpleCandidateFlatTableProducer_cfi import simpleCandidateFlatTableProducer
6 
8 
9 # this below is used only in some eras
10 slimmedMuonsUpdated = cms.EDProducer("PATMuonUpdater",
11  src = cms.InputTag("slimmedMuons"),
12  vertices = cms.InputTag("offlineSlimmedPrimaryVertices"),
13  computeMiniIso = cms.bool(False),
14  fixDxySign = cms.bool(True),
15  pfCandsForMiniIso = cms.InputTag("packedPFCandidates"),
16  miniIsoParams = PhysicsTools.PatAlgos.producersLayer1.muonProducer_cfi.patMuons.miniIsoParams, # so they're in sync
17  recomputeMuonBasicSelectors = cms.bool(False),
18 )
19 
20 isoForMu = cms.EDProducer("MuonIsoValueMapProducer",
21  src = cms.InputTag("slimmedMuonsUpdated"),
22  relative = cms.bool(False),
23  rho_MiniIso = cms.InputTag("fixedGridRhoFastjetAll"),
24  EAFile_MiniIso = cms.FileInPath("PhysicsTools/NanoAOD/data/effAreaMuons_cone03_pfNeuHadronsAndPhotons_94X.txt"),
25 )
26 
27 ptRatioRelForMu = cms.EDProducer("MuonJetVarProducer",
28  srcJet = cms.InputTag("updatedJetsPuppi"),
29  srcLep = cms.InputTag("slimmedMuonsUpdated"),
30  srcVtx = cms.InputTag("offlineSlimmedPrimaryVertices"),
31 )
32 
33 muonMVAID = cms.EDProducer("EvaluateMuonMVAID",
34  src = cms.InputTag("slimmedMuonsUpdated"),
35  weightFile = cms.FileInPath("RecoMuon/MuonIdentification/data/mvaID.onnx"),
36  isClassifier = cms.bool(False),
37  backend = cms.string('ONNX'),
38  name = cms.string("muonMVAID"),
39  outputTensorName= cms.string("probabilities"),
40  inputTensorName= cms.string("float_input"),
41  outputNames = cms.vstring(["probGOOD", "wpMedium", "wpTight"]),
42  batch_eval =cms.bool(True),
43  outputFormulas = cms.vstring(["at(1)", "? at(1) > 0.14 ? 1 : 0", "? at(1) > 0.35 ? 1 : 0"]),
44  variablesOrder = cms.vstring(["LepGood_global_muon","LepGood_validFraction","Muon_norm_chi2_extended","LepGood_local_chi2","LepGood_kink","LepGood_segmentComp","Muon_n_Valid_hits_extended","LepGood_n_MatchedStations","LepGood_Valid_pixel","LepGood_tracker_layers","LepGood_pt","LepGood_eta"]),
45  variables = cms.PSet(
46  LepGood_global_muon = cms.string("isGlobalMuon"),
47  LepGood_validFraction = cms.string("?innerTrack.isNonnull?innerTrack().validFraction:-99"),
48  LepGood_local_chi2 = cms.string("combinedQuality().chi2LocalPosition"),
49  LepGood_kink = cms.string("combinedQuality().trkKink"),
50  LepGood_segmentComp = cms.string("segmentCompatibility"),
51  LepGood_n_MatchedStations = cms.string("numberOfMatchedStations()"),
52  LepGood_Valid_pixel = cms.string("?innerTrack.isNonnull()?innerTrack().hitPattern().numberOfValidPixelHits():-99"),
53  LepGood_tracker_layers = cms.string("?innerTrack.isNonnull()?innerTrack().hitPattern().trackerLayersWithMeasurement():-99"),
54  LepGood_pt = cms.string("pt"),
55  LepGood_eta = cms.string("eta"),
56  )
57 )
58 
59 slimmedMuonsWithUserData = cms.EDProducer("PATMuonUserDataEmbedder",
60  src = cms.InputTag("slimmedMuonsUpdated"),
61  userFloats = cms.PSet(
62  miniIsoChg = cms.InputTag("isoForMu:miniIsoChg"),
63  miniIsoAll = cms.InputTag("isoForMu:miniIsoAll"),
64  ptRatio = cms.InputTag("ptRatioRelForMu:ptRatio"),
65  ptRel = cms.InputTag("ptRatioRelForMu:ptRel"),
66  jetNDauChargedMVASel = cms.InputTag("ptRatioRelForMu:jetNDauChargedMVASel"),
67  mvaIDMuon_wpMedium = cms.InputTag("muonMVAID:wpMedium"),
68  mvaIDMuon_wpTight = cms.InputTag("muonMVAID:wpTight")
69  ),
70  userCands = cms.PSet(
71  jetForLepJetVar = cms.InputTag("ptRatioRelForMu:jetForLepJetVar") # warning: Ptr is null if no match is found
72  ),
73 )
74 
75 (run2_nanoAOD_106Xv2 | run3_nanoAOD_122 ).toModify(slimmedMuonsWithUserData.userFloats,
76  mvaIDMuon = cms.InputTag("muonMVAID:probGOOD"))
77 
78 
79 finalMuons = cms.EDFilter("PATMuonRefSelector",
80  src = cms.InputTag("slimmedMuonsWithUserData"),
81  cut = cms.string("pt > 15 || (pt > 3 && (passed('CutBasedIdLoose') || passed('SoftCutBasedId') || passed('SoftMvaId') || passed('CutBasedIdGlobalHighPt') || passed('CutBasedIdTrkHighPt')))")
82 )
83 
84 finalLooseMuons = cms.EDFilter("PATMuonRefSelector", # for isotrack cleaning
85  src = cms.InputTag("slimmedMuonsWithUserData"),
86  cut = cms.string("pt > 3 && track.isNonnull && isLooseMuon")
87 )
88 
89 muonMVATTH= cms.EDProducer("MuonBaseMVAValueMapProducer",
90  src = cms.InputTag("linkedObjects","muons"),
91  weightFile = cms.FileInPath("PhysicsTools/NanoAOD/data/mu_BDTG_2017.weights.xml"),
92  name = cms.string("muonMVATTH"),
93  isClassifier = cms.bool(True),
94  variablesOrder = cms.vstring(["LepGood_pt","LepGood_eta","LepGood_jetNDauChargedMVASel","LepGood_miniRelIsoCharged","LepGood_miniRelIsoNeutral","LepGood_jetPtRelv2","LepGood_jetDF","LepGood_jetPtRatio","LepGood_dxy","LepGood_sip3d","LepGood_dz","LepGood_segmentComp"]),
95  variables = cms.PSet(
96  LepGood_pt = cms.string("pt"),
97  LepGood_eta = cms.string("eta"),
98  LepGood_jetNDauChargedMVASel = cms.string("?userCand('jetForLepJetVar').isNonnull()?userFloat('jetNDauChargedMVASel'):0"),
99  LepGood_miniRelIsoCharged = cms.string("userFloat('miniIsoChg')/pt"),
100  LepGood_miniRelIsoNeutral = cms.string("(userFloat('miniIsoAll')-userFloat('miniIsoChg'))/pt"),
101  LepGood_jetPtRelv2 = cms.string("?userCand('jetForLepJetVar').isNonnull()?userFloat('ptRel'):0"),
102  LepGood_jetDF = cms.string("?userCand('jetForLepJetVar').isNonnull()?max(userCand('jetForLepJetVar').bDiscriminator('pfDeepFlavourJetTags:probbb')+userCand('jetForLepJetVar').bDiscriminator('pfDeepFlavourJetTags:probb')+userCand('jetForLepJetVar').bDiscriminator('pfDeepFlavourJetTags:problepb'),0.0):0.0"),
103  LepGood_jetPtRatio = cms.string("?userCand('jetForLepJetVar').isNonnull()?min(userFloat('ptRatio'),1.5):1.0/(1.0+(pfIsolationR04().sumChargedHadronPt + max(pfIsolationR04().sumNeutralHadronEt + pfIsolationR04().sumPhotonEt - pfIsolationR04().sumPUPt/2,0.0))/pt)"),
104  LepGood_dxy = cms.string("log(abs(dB('PV2D')))"),
105  LepGood_sip3d = cms.string("abs(dB('PV3D')/edB('PV3D'))"),
106  LepGood_dz = cms.string("log(abs(dB('PVDZ')))"),
107  LepGood_segmentComp = cms.string("segmentCompatibility"),
108 
109  )
110 )
111 
112 muonMVALowPt = muonMVATTH.clone(
113  weightFile = cms.FileInPath("PhysicsTools/NanoAOD/data/mu_BDTG_lowpt.weights.xml"),
114  name = cms.string("muonMVALowPt"),
115 )
116 
117 run2_muon_2016.toModify(
118  muonMVATTH,
119  weightFile = "PhysicsTools/NanoAOD/data/mu_BDTG_2016.weights.xml",
120 )
121 
122 muonTable = simpleCandidateFlatTableProducer.clone(
123  src = cms.InputTag("linkedObjects","muons"),
124  name = cms.string("Muon"),
125  doc = cms.string("slimmedMuons after basic selection (" + finalMuons.cut.value()+")"),
126  variables = cms.PSet(CandVars,
127  ptErr = Var("bestTrack().ptError()", float, doc = "ptError of the muon track", precision=6),
128  tunepRelPt = Var("tunePMuonBestTrack().pt/pt",float,doc="TuneP relative pt, tunePpt/pt",precision=6),
129  dz = Var("dB('PVDZ')",float,doc="dz (with sign) wrt first PV, in cm",precision=10),
130  dzErr = Var("abs(edB('PVDZ'))",float,doc="dz uncertainty, in cm",precision=6),
131  dxybs = Var("dB('BS2D')",float,doc="dxy (with sign) wrt the beam spot, in cm",precision=10),
132  dxy = Var("dB('PV2D')",float,doc="dxy (with sign) wrt first PV, in cm",precision=10),
133  dxyErr = Var("edB('PV2D')",float,doc="dxy uncertainty, in cm",precision=6),
134  ip3d = Var("abs(dB('PV3D'))",float,doc="3D impact parameter wrt first PV, in cm",precision=10),
135  sip3d = Var("abs(dB('PV3D')/edB('PV3D'))",float,doc="3D impact parameter significance wrt first PV",precision=10),
136  segmentComp = Var("segmentCompatibility()", float, doc = "muon segment compatibility", precision=14), # keep higher precision since people have cuts with 3 digits on this
137  nStations = Var("numberOfMatchedStations", "uint8", doc = "number of matched stations with default arbitration (segment & track)"),
138  nTrackerLayers = Var("?track.isNonnull?innerTrack().hitPattern().trackerLayersWithMeasurement():0", "uint8", doc = "number of layers in the tracker"),
139  highPurity = Var("?track.isNonnull?innerTrack().quality('highPurity'):0", bool, doc = "inner track is high purity"),
140  jetIdx = Var("?hasUserCand('jet')?userCand('jet').key():-1", "int16", doc="index of the associated jet (-1 if none)"),
141  svIdx = Var("?hasUserCand('vertex')?userCand('vertex').key():-1", "int16", doc="index of matching secondary vertex"),
142  tkRelIso = Var("isolationR03().sumPt/tunePMuonBestTrack().pt",float,doc="Tracker-based relative isolation dR=0.3 for highPt, trkIso/tunePpt",precision=6),
143  miniPFRelIso_chg = Var("userFloat('miniIsoChg')/pt",float,doc="mini PF relative isolation, charged component"),
144  miniPFRelIso_all = Var("userFloat('miniIsoAll')/pt",float,doc="mini PF relative isolation, total (with scaled rho*EA PU corrections)"),
145  pfRelIso03_chg = Var("pfIsolationR03().sumChargedHadronPt/pt",float,doc="PF relative isolation dR=0.3, charged component"),
146  pfRelIso03_all = Var("(pfIsolationR03().sumChargedHadronPt + max(pfIsolationR03().sumNeutralHadronEt + pfIsolationR03().sumPhotonEt - pfIsolationR03().sumPUPt/2,0.0))/pt",float,doc="PF relative isolation dR=0.3, total (deltaBeta corrections)"),
147  pfRelIso04_all = Var("(pfIsolationR04().sumChargedHadronPt + max(pfIsolationR04().sumNeutralHadronEt + pfIsolationR04().sumPhotonEt - pfIsolationR04().sumPUPt/2,0.0))/pt",float,doc="PF relative isolation dR=0.4, total (deltaBeta corrections)"),
148  jetRelIso = Var("?userCand('jetForLepJetVar').isNonnull()?(1./userFloat('ptRatio'))-1.:(pfIsolationR04().sumChargedHadronPt + max(pfIsolationR04().sumNeutralHadronEt + pfIsolationR04().sumPhotonEt - pfIsolationR04().sumPUPt/2,0.0))/pt",float,doc="Relative isolation in matched jet (1/ptRatio-1, pfRelIso04_all if no matched jet)",precision=8),
149  jetPtRelv2 = Var("?userCand('jetForLepJetVar').isNonnull()?userFloat('ptRel'):0",float,doc="Relative momentum of the lepton with respect to the closest jet after subtracting the lepton",precision=8),
150  tightCharge = Var("?(muonBestTrack().ptError()/muonBestTrack().pt() < 0.2)?2:0", "uint8", doc="Tight charge criterion using pterr/pt of muonBestTrack (0:fail, 2:pass)"),
151  looseId = Var("passed('CutBasedIdLoose')",bool, doc="muon is loose muon"),
152  isPFcand = Var("isPFMuon",bool,doc="muon is PF candidate"),
153  isGlobal = Var("isGlobalMuon",bool,doc="muon is global muon"),
154  isTracker = Var("isTrackerMuon",bool,doc="muon is tracker muon"),
155  isStandalone = Var("isStandAloneMuon",bool,doc="muon is a standalone muon"),
156  mediumId = Var("passed('CutBasedIdMedium')",bool,doc="cut-based ID, medium WP"),
157  mediumPromptId = Var("passed('CutBasedIdMediumPrompt')",bool,doc="cut-based ID, medium prompt WP"),
158  tightId = Var("passed('CutBasedIdTight')",bool,doc="cut-based ID, tight WP"),
159  softId = Var("passed('SoftCutBasedId')",bool,doc="soft cut-based ID"),
160  softMvaId = Var("passed('SoftMvaId')",bool,doc="soft MVA ID"),
161  softMva = Var("softMvaValue()",float,doc="soft MVA ID score",precision=6),
162  highPtId = Var("?passed('CutBasedIdGlobalHighPt')?2:passed('CutBasedIdTrkHighPt')","uint8",doc="high-pT cut-based ID (1 = tracker high pT, 2 = global high pT, which includes tracker high pT)"),
163  pfIsoId = Var("passed('PFIsoVeryLoose')+passed('PFIsoLoose')+passed('PFIsoMedium')+passed('PFIsoTight')+passed('PFIsoVeryTight')+passed('PFIsoVeryVeryTight')","uint8",doc="PFIso ID from miniAOD selector (1=PFIsoVeryLoose, 2=PFIsoLoose, 3=PFIsoMedium, 4=PFIsoTight, 5=PFIsoVeryTight, 6=PFIsoVeryVeryTight)"),
164  tkIsoId = Var("?passed('TkIsoTight')?2:passed('TkIsoLoose')","uint8",doc="TkIso ID (1=TkIsoLoose, 2=TkIsoTight)"),
165  miniIsoId = Var("passed('MiniIsoLoose')+passed('MiniIsoMedium')+passed('MiniIsoTight')+passed('MiniIsoVeryTight')","uint8",doc="MiniIso ID from miniAOD selector (1=MiniIsoLoose, 2=MiniIsoMedium, 3=MiniIsoTight, 4=MiniIsoVeryTight)"),
166  mvaMuID = Var("mvaIDValue()",float,doc="MVA-based ID score ",precision=6),
167  mvaMuID_WP = Var("userFloat('mvaIDMuon_wpMedium') + userFloat('mvaIDMuon_wpTight')","uint8",doc="MVA-based ID selector WPs (1=MVAIDwpMedium,2=MVAIDwpTight)"),
168  multiIsoId = Var("?passed('MultiIsoMedium')?2:passed('MultiIsoLoose')","uint8",doc="MultiIsoId from miniAOD selector (1=MultiIsoLoose, 2=MultiIsoMedium)"),
169  puppiIsoId = Var("passed('PuppiIsoLoose')+passed('PuppiIsoMedium')+passed('PuppiIsoTight')", "uint8", doc="PuppiIsoId from miniAOD selector (1=Loose, 2=Medium, 3=Tight)"),
170  triggerIdLoose = Var("passed('TriggerIdLoose')",bool,doc="TriggerIdLoose ID"),
171  inTimeMuon = Var("passed('InTimeMuon')",bool,doc="inTimeMuon ID"),
172  jetNDauCharged = Var("?userCand('jetForLepJetVar').isNonnull()?userFloat('jetNDauChargedMVASel'):0", "uint8", doc="number of charged daughters of the closest jet"),
173  ),
174  externalVariables = cms.PSet(
175  mvaTTH = ExtVar(cms.InputTag("muonMVATTH"),float, doc="TTH MVA lepton ID score",precision=14),
176  mvaLowPt = ExtVar(cms.InputTag("muonMVALowPt"),float, doc="Low pt muon ID score",precision=14),
177  fsrPhotonIdx = ExtVar(cms.InputTag("leptonFSRphotons:muFsrIndex"), "int16", doc="Index of the lowest-dR/ET2 among associated FSR photons"),
178  ),
179 )
180 
181 
182 (run2_nanoAOD_106Xv2 | run3_nanoAOD_122).toModify(muonTable.variables,mvaMuID=None).toModify(
183  muonTable.variables, mvaMuID = Var("userFloat('mvaIDMuon')", float, doc="MVA-based ID score",precision=6))
184 
185 
186 # Revert back to AK4 CHS jets for Run 2
187 run2_nanoAOD_ANY.toModify(
188  ptRatioRelForMu,srcJet="updatedJets"
189 )
190 
191 
192 muonsMCMatchForTable = cms.EDProducer("MCMatcher", # cut on deltaR, deltaPt/Pt; pick best by deltaR
193  src = muonTable.src, # final reco collection
194  matched = cms.InputTag("finalGenParticles"), # final mc-truth particle collection
195  mcPdgId = cms.vint32(13), # one or more PDG ID (13 = mu); absolute values (see below)
196  checkCharge = cms.bool(False), # True = require RECO and MC objects to have the same charge
197  mcStatus = cms.vint32(1), # PYTHIA status code (1 = stable, 2 = shower, 3 = hard scattering)
198  maxDeltaR = cms.double(0.3), # Minimum deltaR for the match
199  maxDPtRel = cms.double(0.5), # Minimum deltaPt/Pt for the match
200  resolveAmbiguities = cms.bool(True), # Forbid two RECO objects to match to the same GEN object
201  resolveByMatchQuality = cms.bool(True), # False = just match input in order; True = pick lowest deltaR pair first
202 )
203 
204 muonMCTable = cms.EDProducer("CandMCMatchTableProducer",
205  src = muonTable.src,
206  mcMap = cms.InputTag("muonsMCMatchForTable"),
207  objName = muonTable.name,
208  objType = muonTable.name, #cms.string("Muon"),
209  branchName = cms.string("genPart"),
210  docString = cms.string("MC matching to status==1 muons"),
211 )
212 
213 muonTask = cms.Task(slimmedMuonsUpdated,isoForMu,ptRatioRelForMu,slimmedMuonsWithUserData,finalMuons,finalLooseMuons )
214 muonMCTask = cms.Task(muonsMCMatchForTable,muonMCTable)
215 muonTablesTask = cms.Task(muonMVATTH,muonMVALowPt,muonTable,muonMVAID)
216 
217 
218 
219 
def ExtVar(tag, valtype, doc=None, precision=-1)
Definition: common_cff.py:27
def Var(expr, valtype, doc=None, precision=-1)
Definition: common_cff.py:16