Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-12 23:04:03

0001 #
0002 # Common definition of time-life variables for pat-leptons produced
0003 # with {Electron,Muon,Tau}TimeLifeInfoTableProducer
0004 #
0005 import FWCore.ParameterSet.Config as cms
0006 from PhysicsTools.NanoAOD.common_cff import *
0007 from PhysicsTools.NanoAOD.nano_eras_cff import *
0008 from PhysicsTools.PatAlgos.patRefitVertexProducer_cfi import patRefitVertexProducer
0009 from PhysicsTools.NanoAOD.simpleVertexFlatTableProducer_cfi import simpleVertexFlatTableProducer
0010 from PhysicsTools.PatAlgos.patElectronTimeLifeInfoProducer_cfi import patElectronTimeLifeInfoProducer
0011 from PhysicsTools.PatAlgos.patMuonTimeLifeInfoProducer_cfi import patMuonTimeLifeInfoProducer
0012 from PhysicsTools.PatAlgos.patTauTimeLifeInfoProducer_cfi import patTauTimeLifeInfoProducer
0013 from PhysicsTools.NanoAOD.simpleCandidate2TrackTimeLifeInfoFlatTableProducer_cfi import simpleCandidate2TrackTimeLifeInfoFlatTableProducer
0014 from TrackingTools.TransientTrack.TransientTrackBuilder_cfi import *
0015 from PhysicsTools.NanoAOD.nanoDQM_tools_cff import *
0016 
0017 # common settings of lepton life-time info producer
0018 prod_common = cms.PSet(
0019     pvSource = cms.InputTag("offlineSlimmedPrimaryVerticesWithBS"),
0020     pvChoice = cms.int32(0) #0: PV[0], 1: smallest dz
0021 )
0022 
0023 # impact parameter
0024 ipVars = cms.PSet(
0025     #ipLength = Var("ipLength().value()", float, doc="lenght of impact parameter (3d)", precision=10),#MB: redundant
0026     ipLengthSig = Var("ipLength().significance()", float, doc="significance of impact parameter", precision=10),
0027     IPx = Var("ipVector().x()", float, doc="x coordinate of impact parameter vector", precision=10),
0028     IPy = Var("ipVector().y()", float, doc="y coordinate of impact parameter vector", precision=10),
0029     IPz = Var("ipVector().z()", float, doc="z coordinate of impact parameter vector", precision=10)
0030 )
0031 
0032 # track parameters and covariance at ref. point
0033 trackVars = cms.PSet(
0034     track_qoverp = Var("?hasTrack()?track().parameter(0):0", float, doc="track q/p", precision=10),
0035     track_lambda = Var("?hasTrack()?track().parameter(1):0", float, doc="track lambda", precision=10),
0036     track_phi = Var("?hasTrack()?track().parameter(2):0", float, doc="track phi", precision=10),
0037     #track_deltaPhi = Var("?hasTrack()?deltaPhi(track().parameter(2), phi):0", float, doc="track phi minus lepton phi", precision=10),
0038     track_dxy = Var("?hasTrack()?track().parameter(3):0", float, doc="track dxy", precision=10),
0039     track_dsz = Var("?hasTrack()?track().parameter(4):0", float, doc="track dsz", precision=10),
0040     bField_z = Var("?hasTrack()?bField_z:0", float, doc="z coordinate of magnetic field at track ref. point", precision=10),
0041 )
0042 # track covariance elements (adding to trackVars)
0043 for i in range(0,5):
0044     for j in range(i,5):
0045         jistr = str(j)+str(i)
0046         setattr(trackVars, 'track_cov'+jistr, Var("?hasTrack()?track().covariance("+str(j)+","+str(i)+"):0", float, doc="track covariance element ("+str(j)+","+str(i)+")", precision=10))
0047 
0048 # secondary vertex
0049 svVars = cms.PSet(
0050     # SV
0051     hasRefitSV = Var("hasSV()", bool, doc="has SV refit using miniAOD quantities"),
0052     refitSVx = Var("?hasSV()?sv().x():0", float, doc="x coordinate of SV", precision=10),
0053     refitSVy = Var("?hasSV()?sv().y():0", float, doc="y coordinate of SV", precision=10),
0054     refitSVz = Var("?hasSV()?sv().z():0", float, doc="z coordinate of SV", precision=10),
0055     refitSVchi2 = Var("?hasSV()?sv().normalizedChi2():0", float, doc="reduced chi2, i.e. chi2/ndof, of SV fit", precision=8),
0056     #refitSVndof = Var("?hasSV()?sv().ndof():0", float, doc="ndof of SV fit", precision=8),#MB: not important
0057     # flight-length
0058     #refitFlightLength = Var("?hasSV()?flightLength().value():0", float, doc="flight-length,i.e. the PV to SV distance", precision=10),
0059     #refitFlightLengthSig = Var("?hasSV()?flightLength().significance():0", float, doc="Significance of flight-length", precision=10)
0060 )
0061 # secondary vertex covariance elements (adding to svVars)
0062 for i in range(0,3):
0063     for j in range(i,3):
0064         jistr = str(j)+str(i)
0065         setattr(svVars, 'refitSVcov'+jistr, Var("?hasSV()?sv().covariance("+str(j)+","+str(i)+"):0", float, doc="Covariance of SV ("+str(j)+","+str(i)+")", precision=10))
0066 
0067 # primary vertex covariance elements
0068 pvCovVars = cms.PSet()
0069 for i in range(0,3):
0070     for j in range(i,3):
0071         jistr = str(j)+str(i)
0072         setattr(pvCovVars, 'cov'+jistr, Var("covariance("+str(j)+","+str(i)+")", float, doc="vertex covariance ("+str(j)+","+str(i)+")", precision=10))
0073 
0074 # Module to refit PV with beam-spot constraint that is not present in Run-2 samples
0075 refittedPV = patRefitVertexProducer.clone(
0076     srcVertices = "offlineSlimmedPrimaryVertices",
0077 )
0078 run2_nanoAOD_ANY.toModify(
0079     prod_common, pvSource = "refittedPV")
0080 
0081 # Definition of DQM plots
0082 ipVarsPlots = cms.VPSet(
0083     #Plot1D('ipLength', 'ipLength', 25, -0.25, 0.25, 'signed lenght of impact parameter (3d)'),
0084     Plot1D('ipLengthSig', 'ipLengthSig', 60, -5, 10, 'signed significance of impact parameter'),
0085     Plot1D('IPx', 'IPx', 40, -0.02, 0.02, 'x coordinate of impact parameter vector'),
0086     Plot1D('IPy', 'IPy', 40, -0.02, 0.02, 'y coordinate of impact parameter vector'),
0087     Plot1D('IPz', 'IPz', 40, -0.02, 0.02, 'z coordinate of impact parameter vector')
0088 )
0089 trackVarsPlots = cms.VPSet(
0090     Plot1D('track_qoverp', 'track_qoverp', 40, -0.2, 0.2, 'track q/p'),
0091     Plot1D('track_lambda', 'track_lambda', 30, -1.5, 1.5, 'track lambda'),
0092     Plot1D('track_phi', 'track_phi', 20, -3.14159, 3.14159, 'track phi'),
0093     Plot1D('track_dxy', 'track_dxy', 20, -0.1, 0.1, 'track dxy'),
0094     Plot1D('track_dsz', 'track_dsz', 20, -10, 10, 'track dsz'),
0095     NoPlot('bField_z')
0096 )
0097 #no plots for track covariance elements, but store placeholders
0098 for i in range(0,5):
0099     for j in range(i,5):
0100         trackVarsPlots.append(NoPlot('track_cov'+str(j)+str(i)))
0101 svVarsPlots = cms.VPSet(
0102     Plot1D('hasRefitSV', 'hasRefitSV', 2, 0, 2, 'has SV refit using miniAOD quantities'),
0103     Plot1D('refitSVx', 'refitSVx', 20, -0.1, 0.1, 'x coordinate of refitted SV'),
0104     Plot1D('refitSVy', 'refitSVy', 20, -0.1, 0.1, 'y coordinate of refitted SV'),
0105     Plot1D('refitSVz', 'refitSVz', 20, -20, 20, 'z coordinate of refitted SV'),
0106     Plot1D('refitSVchi2', 'refitSVchi2', 20, 0, 40, 'reduced chi2 of SV fit'),
0107     #Plot1D('refitSVndof', 'refitSVndof', 10, 0, 10, 'ndof of SV fit')
0108 )
0109 #no plots for SV covariance elements, but store placeholders
0110 for i in range(0,3):
0111     for j in range(i,3):
0112         svVarsPlots.append(NoPlot('refitSVcov'+str(j)+str(i)))
0113 
0114 #
0115 # Customization sequences and functions
0116 #
0117 # electrons
0118 def addElectronTimeLifeInfoTask(process):
0119     process.electronTimeLifeInfos = patElectronTimeLifeInfoProducer.clone(
0120         src = process.electronTable.src,
0121         selection = 'pt > 15',
0122         pvSource = prod_common.pvSource,
0123         pvChoice = prod_common.pvChoice
0124     )
0125     process.electronTimeLifeInfoTable = simpleCandidate2TrackTimeLifeInfoFlatTableProducer.clone(
0126         name = process.electronTable.name,
0127         src = process.electronTable.src,
0128         doc = cms.string("Additional time-life info for non-prompt electrons"),
0129         extension = True,
0130         externalTypedVariables = cms.PSet()
0131     )
0132     process.electronTimeLifeInfoTask = cms.Task(
0133         process.electronTimeLifeInfos,
0134         process.electronTimeLifeInfoTable
0135     )
0136     # refit PV with beam-spot constraint that is not present in Run-2 samples
0137     if not hasattr(process,'refittedPV'):
0138         setattr(process,'refittedPV',refittedPV)
0139     _electronTimeLifeInfoTaskRun2 = process.electronTimeLifeInfoTask.copy()
0140     _electronTimeLifeInfoTaskRun2.add(process.refittedPV)
0141     run2_nanoAOD_ANY.toReplaceWith(process.electronTimeLifeInfoTask,
0142                                    _electronTimeLifeInfoTaskRun2)
0143     process.electronTablesTask.add(process.electronTimeLifeInfoTask)
0144     return process
0145 #base vars
0146 electronVars = cms.PSet(
0147     ipVars
0148 )
0149 for var in electronVars.parameters_():
0150     setattr(getattr(electronVars, var), "src", cms.InputTag("electronTimeLifeInfos"))
0151 def addTimeLifeInfoToElectrons(process):
0152     if not hasattr(process,'electronTimeLifeInfoTask'):
0153         process = addElectronTimeLifeInfoTask(process)
0154     electronExtVars = cms.PSet(
0155         process.electronTimeLifeInfoTable.externalTypedVariables,
0156         electronVars
0157     )
0158     process.electronTimeLifeInfoTable.externalTypedVariables = electronExtVars
0159     # add DQM plots if needed
0160     if hasattr(process,'nanoDQM'):
0161         process.nanoDQM.vplots.Electron.plots.extend(ipVarsPlots)
0162     return process
0163 #track vars
0164 electronTrackVars = cms.PSet(
0165     trackVars
0166 )
0167 for var in electronTrackVars.parameters_():
0168     setattr(getattr(electronTrackVars, var), "src", cms.InputTag("electronTimeLifeInfos"))
0169 def addElectronTrackVarsToTimeLifeInfo(process):
0170     if not hasattr(process,'electronTimeLifeInfoTask'):
0171         process = addElectronTimeLifeInfoTask(process)
0172     electronExtVars = cms.PSet(
0173         process.electronTimeLifeInfoTable.externalTypedVariables,
0174         electronTrackVars
0175     )
0176     process.electronTimeLifeInfoTable.externalTypedVariables = electronExtVars
0177     # add DQM plots if needed
0178     if hasattr(process,'nanoDQM'):
0179         process.nanoDQM.vplots.Electron.plots.extend(trackVarsPlots)
0180     return process
0181 
0182 # muons
0183 def addMuonTimeLifeInfoTask(process):
0184     process.muonTimeLifeInfos = patMuonTimeLifeInfoProducer.clone(
0185         src = process.muonTable.src,
0186         selection = 'pt > 15',
0187         pvSource = prod_common.pvSource,
0188         pvChoice = prod_common.pvChoice
0189     )
0190     process.muonTimeLifeInfoTable = simpleCandidate2TrackTimeLifeInfoFlatTableProducer.clone(
0191         name = process.muonTable.name,
0192         src = process.muonTable.src,
0193         doc = cms.string("Additional time-life info for non-prompt muon"),
0194         extension = True,
0195         externalTypedVariables = cms.PSet()
0196     )
0197     process.muonTimeLifeInfoTask = cms.Task(
0198         process.muonTimeLifeInfos,
0199         process.muonTimeLifeInfoTable
0200     )
0201     # refit PV with beam-spot constraint that is not present in Run-2 samples
0202     if not hasattr(process,'refittedPV'):
0203         setattr(process,'refittedPV',refittedPV)
0204     _muonTimeLifeInfoTaskRun2 = process.muonTimeLifeInfoTask.copy()
0205     _muonTimeLifeInfoTaskRun2.add(process.refittedPV)
0206     run2_nanoAOD_ANY.toReplaceWith(process.muonTimeLifeInfoTask,
0207                                    _muonTimeLifeInfoTaskRun2)
0208     process.muonTablesTask.add(process.muonTimeLifeInfoTask)
0209     return process
0210 #base vars
0211 muonVars = cms.PSet(
0212     ipVars
0213 )
0214 for var in muonVars.parameters_():
0215     setattr(getattr(muonVars, var), "src", cms.InputTag("muonTimeLifeInfos"))
0216 def addTimeLifeInfoToMuons(process):
0217     if not hasattr(process,'muonTimeLifeInfoTask'):
0218         process = addMuonTimeLifeInfoTask(process)
0219     muonExtVars = cms.PSet(
0220         process.muonTimeLifeInfoTable.externalTypedVariables,
0221         muonVars
0222     )
0223     process.muonTimeLifeInfoTable.externalTypedVariables = muonExtVars
0224     # add DQM plots if needed
0225     if hasattr(process,'nanoDQM'):
0226         process.nanoDQM.vplots.Muon.plots.extend(ipVarsPlots)
0227     return process
0228 #track vars
0229 muonTrackVars = cms.PSet(
0230     trackVars
0231 )
0232 for var in muonTrackVars.parameters_():
0233     setattr(getattr(muonTrackVars, var), "src", cms.InputTag("muonTimeLifeInfos"))
0234 def addMuonTrackVarsToTimeLifeInfo(process):
0235     if not hasattr(process,'muonTimeLifeInfoTask'):
0236         process = addMuonTimeLifeInfoTask(process)
0237     muonExtVars = cms.PSet(
0238         process.muonTimeLifeInfoTable.externalTypedVariables,
0239         muonTrackVars
0240     )
0241     process.muonTimeLifeInfoTable.externalTypedVariables = muonExtVars
0242     # add DQM plots if needed
0243     if hasattr(process,'nanoDQM'):
0244         process.nanoDQM.vplots.Muon.plots.extend(trackVarsPlots)
0245     return process
0246 
0247 # taus
0248 def addTauTimeLifeInfoTask(process):
0249     process.tauTimeLifeInfos = patTauTimeLifeInfoProducer.clone(
0250         src = process.tauTable.src,
0251         pvSource = prod_common.pvSource,
0252         pvChoice = prod_common.pvChoice
0253     )
0254     process.tauTimeLifeInfoTable = simpleCandidate2TrackTimeLifeInfoFlatTableProducer.clone(
0255         name = process.tauTable.name,
0256         src = process.tauTable.src,
0257         doc = cms.string("Additional tau time-life info"),
0258         extension = True,
0259         externalTypedVariables = cms.PSet()
0260     )
0261     process.tauTimeLifeInfoTask = cms.Task(
0262         process.tauTimeLifeInfos,
0263         process.tauTimeLifeInfoTable
0264     )
0265     # refit PV with beam-spot constraint that is not present in Run-2 samples
0266     if not hasattr(process,'refittedPV'):
0267         setattr(process,'refittedPV',refittedPV)
0268     _tauTimeLifeInfoTaskRun2 = process.tauTimeLifeInfoTask.copy()
0269     _tauTimeLifeInfoTaskRun2.add(process.refittedPV)
0270     run2_nanoAOD_ANY.toReplaceWith(process.tauTimeLifeInfoTask,
0271                                    _tauTimeLifeInfoTaskRun2)
0272     process.tauTablesTask.add(process.tauTimeLifeInfoTask)
0273     return process
0274 # base vars
0275 tauVars = cms.PSet(
0276     svVars,
0277     ipVars
0278 )
0279 for var in tauVars.parameters_():
0280     setattr(getattr(tauVars, var), "src", cms.InputTag("tauTimeLifeInfos"))
0281 def addTimeLifeInfoToTaus(process):
0282     if not hasattr(process,'tauTimeLifeInfoTask'):
0283         process = addTauTimeLifeInfoTask(process)
0284     tauExtVars = cms.PSet(
0285         process.tauTimeLifeInfoTable.externalTypedVariables,
0286         tauVars
0287     )
0288     process.tauTimeLifeInfoTable.externalTypedVariables = tauExtVars
0289     # add DQM plots if needed
0290     if hasattr(process,'nanoDQM'):
0291         process.nanoDQM.vplots.Tau.plots.extend(ipVarsPlots)
0292         process.nanoDQM.vplots.Tau.plots.extend(svVarsPlots)
0293     return process
0294 #track vars
0295 tauTrackVars = cms.PSet(
0296     trackVars
0297 )
0298 for var in tauTrackVars.parameters_():
0299     setattr(getattr(tauTrackVars, var), "src", cms.InputTag("tauTimeLifeInfos"))
0300 def addTauTrackVarsToTimeLifeInfo(process):
0301     if not hasattr(process,'tauTimeLifeInfoTask'):
0302         process = addTauTimeLifeInfoTask(process)
0303     tauExtVars = cms.PSet(
0304         process.tauTimeLifeInfoTable.externalTypedVariables,
0305         tauTrackVars
0306     )
0307     process.tauTimeLifeInfoTable.externalTypedVariables = tauExtVars
0308     # add DQM plots if needed
0309     if hasattr(process,'nanoDQM'):
0310         process.nanoDQM.vplots.Tau.plots.extend(trackVarsPlots)
0311     return process
0312 
0313 # Vertices
0314 def addExtendVertexInfo(process):
0315     process.pvbsTable = simpleVertexFlatTableProducer.clone(
0316         src = prod_common.pvSource,
0317         name = "PVBS",
0318         doc = "main primary vertex with beam-spot",
0319         maxLen = 1,
0320         variables = cms.PSet(
0321             pvCovVars,
0322             x = Var("position().x()", float, doc = "position x coordinate, in cm", precision = 10),
0323             y = Var("position().y()", float, doc = "position y coordinate, in cm", precision = 10),
0324             z = Var("position().z()", float, doc = "position z coordinate, in cm", precision = 16),
0325             #ndof = Var("ndof()", float, doc = "number of degrees of freedom", precision = 8),#MB: not important
0326             chi2 = Var("normalizedChi2()", float, doc = "reduced chi2, i.e. chi2/ndof", precision = 8),
0327         ),
0328     )
0329     process.pvbsTableTask = cms.Task(process.pvbsTable)
0330     # refit PV with beam-spot constraint that is not present in Run-2 samples
0331     if not hasattr(process,'refittedPV'):
0332         setattr(process,'refittedPV',refittedPV)
0333     _pvbsTableTaskRun2 = process.pvbsTableTask.copy()
0334     _pvbsTableTaskRun2.add(process.refittedPV)
0335     run2_nanoAOD_ANY.toReplaceWith(process.pvbsTableTask,
0336                                    _pvbsTableTaskRun2)
0337     process.vertexTablesTask.add(process.pvbsTableTask)
0338     return process
0339 
0340 # Time-life info without track parameters
0341 def addTimeLifeInfoBase(process):
0342     addTimeLifeInfoToElectrons(process)
0343     addTimeLifeInfoToMuons(process)
0344     addTimeLifeInfoToTaus(process)
0345     addExtendVertexInfo(process)
0346     return process
0347 # Add track parameters to time-life info
0348 def addTrackVarsToTimeLifeInfo(process):
0349     addElectronTrackVarsToTimeLifeInfo(process)
0350     addMuonTrackVarsToTimeLifeInfo(process)
0351     addTauTrackVarsToTimeLifeInfo(process)
0352     return process
0353 # Full
0354 def addTimeLifeInfo(process):
0355     addTimeLifeInfoBase(process)
0356     addTrackVarsToTimeLifeInfo(process)
0357     return process