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 
123 muonBSConstrain = cms.EDProducer("MuonBeamspotConstraintValueMapProducer",
124  src = cms.InputTag("linkedObjects","muons"),
125 )
126 
127 muonTable = simpleCandidateFlatTableProducer.clone(
128  src = cms.InputTag("linkedObjects","muons"),
129  name = cms.string("Muon"),
130  doc = cms.string("slimmedMuons after basic selection (" + finalMuons.cut.value()+")"),
131  variables = cms.PSet(CandVars,
132  ptErr = Var("bestTrack().ptError()", float, doc = "ptError of the muon track", precision=6),
133  tunepRelPt = Var("tunePMuonBestTrack().pt/pt",float,doc="TuneP relative pt, tunePpt/pt",precision=6),
134  dz = Var("dB('PVDZ')",float,doc="dz (with sign) wrt first PV, in cm",precision=10),
135  dzErr = Var("abs(edB('PVDZ'))",float,doc="dz uncertainty, in cm",precision=6),
136  dxybs = Var("dB('BS2D')",float,doc="dxy (with sign) wrt the beam spot, in cm",precision=10),
137  dxy = Var("dB('PV2D')",float,doc="dxy (with sign) wrt first PV, in cm",precision=10),
138  dxyErr = Var("edB('PV2D')",float,doc="dxy uncertainty, in cm",precision=6),
139  ip3d = Var("abs(dB('PV3D'))",float,doc="3D impact parameter wrt first PV, in cm",precision=10),
140  sip3d = Var("abs(dB('PV3D')/edB('PV3D'))",float,doc="3D impact parameter significance wrt first PV",precision=10),
141  segmentComp = Var("segmentCompatibility()", float, doc = "muon segment compatibility", precision=14), # keep higher precision since people have cuts with 3 digits on this
142  nStations = Var("numberOfMatchedStations", "uint8", doc = "number of matched stations with default arbitration (segment & track)"),
143  nTrackerLayers = Var("?track.isNonnull?innerTrack().hitPattern().trackerLayersWithMeasurement():0", "uint8", doc = "number of layers in the tracker"),
144  highPurity = Var("?track.isNonnull?innerTrack().quality('highPurity'):0", bool, doc = "inner track is high purity"),
145  jetIdx = Var("?hasUserCand('jet')?userCand('jet').key():-1", "int16", doc="index of the associated jet (-1 if none)"),
146  svIdx = Var("?hasUserCand('vertex')?userCand('vertex').key():-1", "int16", doc="index of matching secondary vertex"),
147  tkRelIso = Var("isolationR03().sumPt/tunePMuonBestTrack().pt",float,doc="Tracker-based relative isolation dR=0.3 for highPt, trkIso/tunePpt",precision=6),
148  miniPFRelIso_chg = Var("userFloat('miniIsoChg')/pt",float,doc="mini PF relative isolation, charged component"),
149  miniPFRelIso_all = Var("userFloat('miniIsoAll')/pt",float,doc="mini PF relative isolation, total (with scaled rho*EA PU corrections)"),
150  pfRelIso03_chg = Var("pfIsolationR03().sumChargedHadronPt/pt",float,doc="PF relative isolation dR=0.3, charged component"),
151  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)"),
152  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)"),
153  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),
154  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),
155  tightCharge = Var("?(muonBestTrack().ptError()/muonBestTrack().pt() < 0.2)?2:0", "uint8", doc="Tight charge criterion using pterr/pt of muonBestTrack (0:fail, 2:pass)"),
156  looseId = Var("passed('CutBasedIdLoose')",bool, doc="muon is loose muon"),
157  isPFcand = Var("isPFMuon",bool,doc="muon is PF candidate"),
158  isGlobal = Var("isGlobalMuon",bool,doc="muon is global muon"),
159  isTracker = Var("isTrackerMuon",bool,doc="muon is tracker muon"),
160  isStandalone = Var("isStandAloneMuon",bool,doc="muon is a standalone muon"),
161  mediumId = Var("passed('CutBasedIdMedium')",bool,doc="cut-based ID, medium WP"),
162  mediumPromptId = Var("passed('CutBasedIdMediumPrompt')",bool,doc="cut-based ID, medium prompt WP"),
163  tightId = Var("passed('CutBasedIdTight')",bool,doc="cut-based ID, tight WP"),
164  softId = Var("passed('SoftCutBasedId')",bool,doc="soft cut-based ID"),
165  softMvaId = Var("passed('SoftMvaId')",bool,doc="soft MVA ID"),
166  softMva = Var("softMvaValue()",float,doc="soft MVA ID score",precision=6),
167  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)"),
168  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)"),
169  tkIsoId = Var("?passed('TkIsoTight')?2:passed('TkIsoLoose')","uint8",doc="TkIso ID (1=TkIsoLoose, 2=TkIsoTight)"),
170  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)"),
171  mvaMuID = Var("mvaIDValue()",float,doc="MVA-based ID score ",precision=6),
172  mvaMuID_WP = Var("userFloat('mvaIDMuon_wpMedium') + userFloat('mvaIDMuon_wpTight')","uint8",doc="MVA-based ID selector WPs (1=MVAIDwpMedium,2=MVAIDwpTight)"),
173  multiIsoId = Var("?passed('MultiIsoMedium')?2:passed('MultiIsoLoose')","uint8",doc="MultiIsoId from miniAOD selector (1=MultiIsoLoose, 2=MultiIsoMedium)"),
174  puppiIsoId = Var("passed('PuppiIsoLoose')+passed('PuppiIsoMedium')+passed('PuppiIsoTight')", "uint8", doc="PuppiIsoId from miniAOD selector (1=Loose, 2=Medium, 3=Tight)"),
175  triggerIdLoose = Var("passed('TriggerIdLoose')",bool,doc="TriggerIdLoose ID"),
176  inTimeMuon = Var("passed('InTimeMuon')",bool,doc="inTimeMuon ID"),
177  jetNDauCharged = Var("?userCand('jetForLepJetVar').isNonnull()?userFloat('jetNDauChargedMVASel'):0", "uint8", doc="number of charged daughters of the closest jet"),
178  ),
179  externalVariables = cms.PSet(
180  mvaTTH = ExtVar(cms.InputTag("muonMVATTH"),float, doc="TTH MVA lepton ID score",precision=14),
181  mvaLowPt = ExtVar(cms.InputTag("muonMVALowPt"),float, doc="Low pt muon ID score",precision=14),
182  fsrPhotonIdx = ExtVar(cms.InputTag("leptonFSRphotons:muFsrIndex"), "int16", doc="Index of the lowest-dR/ET2 among associated FSR photons"),
183  bsConstrainedPt = ExtVar(cms.InputTag("muonBSConstrain:muonBSConstrainedPt"),float, doc="pT with beamspot constraint",precision=-1),
184  bsConstrainedPtErr = ExtVar(cms.InputTag("muonBSConstrain:muonBSConstrainedPtErr"),float, doc="pT error with beamspot constraint ",precision=6),
185  bsConstrainedChi2 = ExtVar(cms.InputTag("muonBSConstrain:muonBSConstrainedChi2"),float, doc="chi2 of beamspot constraint",precision=6),
186  ),
187 )
188 
189 # Increase precision of eta and phi
190 muonTable.variables.eta.precision = 16
191 muonTable.variables.phi.precision = 16
192 
193 (run2_nanoAOD_106Xv2 | run3_nanoAOD_122).toModify(muonTable.variables,mvaMuID=None).toModify(
194  muonTable.variables, mvaMuID = Var("userFloat('mvaIDMuon')", float, doc="MVA-based ID score",precision=6))
195 
196 
197 # Revert back to AK4 CHS jets for Run 2
198 run2_nanoAOD_ANY.toModify(
199  ptRatioRelForMu,srcJet="updatedJets"
200 )
201 
202 
203 muonsMCMatchForTable = cms.EDProducer("MCMatcher", # cut on deltaR, deltaPt/Pt; pick best by deltaR
204  src = muonTable.src, # final reco collection
205  matched = cms.InputTag("finalGenParticles"), # final mc-truth particle collection
206  mcPdgId = cms.vint32(13), # one or more PDG ID (13 = mu); absolute values (see below)
207  checkCharge = cms.bool(False), # True = require RECO and MC objects to have the same charge
208  mcStatus = cms.vint32(1), # PYTHIA status code (1 = stable, 2 = shower, 3 = hard scattering)
209  maxDeltaR = cms.double(0.3), # Minimum deltaR for the match
210  maxDPtRel = cms.double(0.5), # Minimum deltaPt/Pt for the match
211  resolveAmbiguities = cms.bool(True), # Forbid two RECO objects to match to the same GEN object
212  resolveByMatchQuality = cms.bool(True), # False = just match input in order; True = pick lowest deltaR pair first
213 )
214 
215 muonMCTable = cms.EDProducer("CandMCMatchTableProducer",
216  src = muonTable.src,
217  mcMap = cms.InputTag("muonsMCMatchForTable"),
218  objName = muonTable.name,
219  objType = muonTable.name, #cms.string("Muon"),
220  branchName = cms.string("genPart"),
221  docString = cms.string("MC matching to status==1 muons"),
222 )
223 
224 muonTask = cms.Task(slimmedMuonsUpdated,isoForMu,ptRatioRelForMu,slimmedMuonsWithUserData,finalMuons,finalLooseMuons )
225 muonMCTask = cms.Task(muonsMCMatchForTable,muonMCTable)
226 muonTablesTask = cms.Task(muonMVATTH,muonMVALowPt,muonBSConstrain,muonTable,muonMVAID)
227 
228 
229 
230 
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