Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:43

0001 from __future__ import print_function
0002 import FWCore.ParameterSet.Config as cms
0003 
0004 ##    __  __       _          ____   _  _____   __  __
0005 ##   |  \/  | __ _| | _____  |  _ \ / \|_   _| |  \/  |_   _  ___  _ __  ___
0006 ##   | |\/| |/ _` | |/ / _ \ | |_) / _ \ | |   | |\/| | | | |/ _ \| '_ \/ __|
0007 ##   | |  | | (_| |   <  __/ |  __/ ___ \| |   | |  | | |_| | (_) | | | \__ \
0008 ##   |_|  |_|\__,_|_|\_\___| |_| /_/   \_\_|   |_|  |_|\__,_|\___/|_| |_|___/
0009 ##
0010 ##
0011 ### ==== Make PAT Muons ====
0012 import PhysicsTools.PatAlgos.producersLayer1.muonProducer_cfi
0013 patMuonsWithoutTrigger = PhysicsTools.PatAlgos.producersLayer1.muonProducer_cfi.patMuons.clone(
0014     muonSource = 'muons',
0015     # embed the tracks, so we don't have to carry them around
0016     embedTrack          = True,
0017     embedCombinedMuon   = True,
0018     embedStandAloneMuon = True,
0019     embedPFCandidate    = False,
0020     embedCaloMETMuonCorrs = cms.bool(False),
0021     embedTcMETMuonCorrs   = cms.bool(False),
0022     embedPfEcalEnergy     = cms.bool(False),
0023     # then switch off some features we don't need
0024     #addTeVRefits = False, ## <<--- this doesn't work. PAT bug ??
0025     embedPickyMuon = False,
0026     embedTpfmsMuon = False,
0027     userIsolation = cms.PSet(),   # no extra isolation beyond what's in reco::Muon itself
0028     isoDeposits = cms.PSet(), # no heavy isodeposits
0029     addGenMatch = False,       # no mc: T&P doesn't take it from here anyway.
0030 )
0031 # Reset all these; the default in muonProducer_cfi is not empty, but wrong
0032 patMuonsWithoutTrigger.userData.userInts.src    = []
0033 patMuonsWithoutTrigger.userData.userFloats.src  = []
0034 patMuonsWithoutTrigger.userData.userCands.src   = []
0035 patMuonsWithoutTrigger.userData.userClasses.src = []
0036 
0037 ##    __  __       _       _       ____      ___        __  _     _
0038 ##   |  \/  | __ _| |_ ___| |__   |  _ \    / \ \      / / | |   / |
0039 ##   | |\/| |/ _` | __/ __| '_ \  | |_) |  / _ \ \ /\ / /  | |   | |
0040 ##   | |  | | (_| | || (__| | | | |  _ <  / ___ \ V  V /   | |___| |
0041 ##   |_|  |_|\__,_|\__\___|_| |_| |_| \_\/_/   \_\_/\_/    |_____|_|
0042 ##
0043 ##
0044 from MuonAnalysis.MuonAssociators.muonL1Match_cfi import muonL1Match as muonL1Info
0045 
0046 ## Define a generic function, so that it can be used with existing PAT Muons
0047 def addL1UserData(patMuonProducer, l1ModuleLabel = "muonL1Info"):
0048     "Load variables inside PAT muon, from module <l1ModuleLabel> that you must run before it"
0049     patMuonProducer.userData.userInts.src += [
0050         cms.InputTag(l1ModuleLabel, "quality"), # will be -999 in case of no match
0051     ]
0052     patMuonProducer.userData.userFloats.src += [
0053         cms.InputTag(l1ModuleLabel, "deltaR"),  # will be 999 in case of no match
0054     ]
0055     patMuonProducer.userData.userFloats.src += [
0056         cms.InputTag(l1ModuleLabel, "deltaPhi"),  # will be 999 in case of no match
0057     ]
0058     patMuonProducer.userData.userInts.src += [
0059         cms.InputTag(l1ModuleLabel, "bx"),  # will be -999 in case of no match
0060     ]
0061     patMuonProducer.userData.userCands.src += [
0062         cms.InputTag(l1ModuleLabel)
0063     ]
0064 
0065 ## Do it for this collection of pat Muons
0066 addL1UserData(patMuonsWithoutTrigger, "muonL1Info")
0067 
0068 ##    __  __       _       _       _   _ _   _____
0069 ##   |  \/  | __ _| |_ ___| |__   | | | | | |_   _|
0070 ##   | |\/| |/ _` | __/ __| '_ \  | |_| | |   | |
0071 ##   | |  | | (_| | || (__| | | | |  _  | |___| |
0072 ##   |_|  |_|\__,_|\__\___|_| |_| |_| |_|_____|_|
0073 ##
0074 ##
0075 
0076 ### ==== Unpack trigger, and match ====
0077 from PhysicsTools.PatAlgos.triggerLayer1.triggerProducer_cfi import patTrigger as patTriggerFull
0078 patTriggerFull.onlyStandAlone = True
0079 patTrigger = cms.EDProducer("TriggerObjectFilterByCollection",
0080     src = cms.InputTag("patTriggerFull"),
0081     collections = cms.vstring("hltL1extraParticles", "hltGmtStage2Digis", "hltL2MuonCandidates", "hltIterL3MuonCandidates","hltIterL3FromL2MuonCandidates","hltHighPtTkMuonCands", "hltGlbTrkMuonCands", "hltMuTrackJpsiCtfTrackCands", "hltMuTrackJpsiEffCtfTrackCands", "hltMuTkMuJpsiTrackerMuonCands","hltTracksIter"),
0082 )
0083 #patTrigger = cms.EDFilter("PATTriggerObjectStandAloneSelector",
0084 #    src = cms.InputTag("patTriggerFull"),
0085 #    cut = cms.string('coll("hltL1extraParticles") || coll("hltL2MuonCandidates") || coll("hltIterL3MuonCandidates") || coll("hltGlbTrkMuonCands") || coll("hltMuTrackJpsiCtfTrackCands") || coll("hltMuTrackJpsiEffCtfTrackCands") || coll("hltMuTkMuJpsiTrackerMuonCands")'),
0086 #)
0087 
0088 ### ==== Then perform a match for all HLT triggers of interest
0089 muonTriggerMatchHLT = cms.EDProducer( "PATTriggerMatcherDRDPtLessByR",
0090     src     = cms.InputTag( "patMuonsWithoutTrigger" ),
0091     matched = cms.InputTag( "patTrigger" ),
0092     matchedCuts = cms.string(""),
0093 #    andOr          = cms.bool( False ),
0094 #    filterIdsEnum  = cms.vstring( '*' ),
0095 #    filterIds      = cms.vint32( 0 ),
0096 #    filterLabels   = cms.vstring( '*' ),
0097 #    pathNames      = cms.vstring( '*' ),
0098 #    collectionTags = cms.vstring( '*' ),
0099     maxDPtRel = cms.double( 0.5 ),
0100     maxDeltaR = cms.double( 0.5 ),
0101     resolveAmbiguities    = cms.bool( True ),
0102     resolveByMatchQuality = cms.bool( True ) #change with respect to previous tag
0103 )
0104 
0105 ### == For HLT triggers which are just L1s, we need a different matcher
0106 from MuonAnalysis.MuonAssociators.muonHLTL1Match_cfi import muonHLTL1Match
0107 muonMatchL1 = muonHLTL1Match.clone(
0108     src     = muonTriggerMatchHLT.src,
0109     matched = muonTriggerMatchHLT.matched,
0110 )
0111 
0112 ### Single Mu L1
0113 muonMatchHLTL1 = muonMatchL1.clone(matchedCuts = cms.string('coll("hltL1extraParticles")'))
0114 muonMatchHLTL2 = muonTriggerMatchHLT.clone(matchedCuts = cms.string('coll("hltL2MuonCandidates")'), maxDeltaR = 0.3, maxDPtRel = 10.0)  #maxDeltaR Changed accordingly to Zoltan tuning. It was: 1.2
0115 muonMatchHLTL3 = muonTriggerMatchHLT.clone(matchedCuts = cms.string('coll("hltIterL3MuonCandidates")'), maxDeltaR = 0.1, maxDPtRel = 10.0)  #maxDeltaR Changed accordingly to Zoltan tuning. It was: 0.5
0116 muonMatchHLTL3T = muonTriggerMatchHLT.clone(matchedCuts = cms.string('coll("hltGlbTrkMuonCands")'),  maxDeltaR = 0.1, maxDPtRel = 10.0)  #maxDeltaR Changed accordingly to Zoltan tuning. It was: 0.5
0117 muonMatchHLTL3fromL2 = muonTriggerMatchHLT.clone(matchedCuts = cms.string('coll("hltIterL3FromL2MuonCandidates")'),  maxDeltaR = 0.1, maxDPtRel = 10.0)  #maxDeltaR Changed accordingly to Zoltan tuning. It was: 0.5
0118 muonMatchHLTTkMu =  muonTriggerMatchHLT.clone(matchedCuts = cms.string('coll("hltHighPtTkMuonCands")'),  maxDeltaR = 0.1, maxDPtRel = 10.0)  #maxDeltaR Changed accordingly to Zoltan tuning. It was: 0.5
0119 muonMatchHLTCtfTrack  = muonTriggerMatchHLT.clone(matchedCuts = cms.string('coll("hltMuTrackJpsiCtfTrackCands")'),    maxDeltaR = 0.1, maxDPtRel = 10.0)  #maxDeltaR Changed accordingly to Zoltan tuning.
0120 muonMatchHLTCtfTrack2 = muonTriggerMatchHLT.clone(matchedCuts = cms.string('coll("hltMuTrackJpsiEffCtfTrackCands")'), maxDeltaR = 0.1, maxDPtRel = 10.0)  #maxDeltaR Changed accordingly to Zoltan tuning.
0121 muonMatchHLTTrackMu  = muonTriggerMatchHLT.clone(matchedCuts = cms.string('coll("hltMuTkMuJpsiTrackerMuonCands")'), maxDeltaR = 0.1, maxDPtRel = 10.0) #maxDeltaR Changed accordingly to Zoltan tuning.
0122 muonMatchHLTTrackIt  = muonTriggerMatchHLT.clone(matchedCuts = cms.string('coll("hltTracksIter")'), maxDeltaR = 0.1, maxDPtRel = 1.0) #maxDeltaR Changed accordingly to Zoltan tuning.
0123 
0124 patTriggerMatchers1Mu = cms.Sequence(
0125       #muonMatchHLTL1 +   # keep off by default, since it is slow and usually not needed
0126       muonMatchHLTL2 +
0127       muonMatchHLTL3 +
0128       muonMatchHLTL3T +
0129       muonMatchHLTL3fromL2 +
0130       muonMatchHLTTkMu
0131 )
0132 patTriggerMatchers1MuInputTags = [
0133     #cms.InputTag('muonMatchHLTL1','propagatedReco'), # fake, will match if and only if he muon did propagate to station 2
0134     #cms.InputTag('muonMatchHLTL1'),
0135     cms.InputTag('muonMatchHLTL2'),
0136     cms.InputTag('muonMatchHLTL3'),
0137     cms.InputTag('muonMatchHLTL3T'),
0138     cms.InputTag('muonMatchHLTL3fromL2'),
0139     cms.InputTag('muonMatchHLTTkMu'),
0140 ]
0141 
0142 patTriggerMatchers2Mu = cms.Sequence(
0143     muonMatchHLTCtfTrack  +
0144     muonMatchHLTCtfTrack2 +
0145     muonMatchHLTTrackMu +
0146     muonMatchHLTTrackIt
0147 )
0148 patTriggerMatchers2MuInputTags = [
0149     cms.InputTag('muonMatchHLTCtfTrack'),
0150     cms.InputTag('muonMatchHLTCtfTrack2'),
0151     cms.InputTag('muonMatchHLTTrackMu'),
0152     cms.InputTag('muonMatchHLTTrackIt'),
0153 ]
0154 
0155 ## ==== Embed ====
0156 patMuonsWithTrigger = cms.EDProducer( "PATTriggerMatchMuonEmbedder",
0157     src     = cms.InputTag(  "patMuonsWithoutTrigger" ),
0158     matches = cms.VInputTag()
0159 )
0160 patMuonsWithTrigger.matches += patTriggerMatchers1MuInputTags
0161 patMuonsWithTrigger.matches += patTriggerMatchers2MuInputTags
0162 
0163 
0164 ## ==== Trigger Sequence ====
0165 patTriggerMatching = cms.Sequence(
0166     patTriggerFull * patTrigger *
0167     patTriggerMatchers1Mu *
0168     patTriggerMatchers2Mu *
0169     patMuonsWithTrigger
0170 )
0171 
0172 patMuonsWithTriggerSequence = cms.Sequence(
0173     muonL1Info             *
0174     patMuonsWithoutTrigger *
0175     patTriggerMatching
0176 )
0177 
0178 
0179 
0180 def switchOffAmbiguityResolution(process):
0181     "Switch off ambiguity resolution: allow multiple reco muons to match to the same trigger muon"
0182     process.muonMatchHLTL1.resolveAmbiguities = False
0183     process.muonMatchHLTL2.resolveAmbiguities = False
0184     process.muonMatchHLTL3.resolveAmbiguities = False
0185     process.muonMatchHLTL3fromL2.resolveAmbiguities = False
0186     process.muonMatchHLTTkMu.resolveAmbiguities  = False
0187     process.muonMatchHLTCtfTrack.resolveAmbiguities = False
0188     process.muonMatchHLTTrackMu.resolveAmbiguities  = False
0189     process.muonMatchHLTTrackIt.resolveAmbiguities  = False
0190 
0191 def changeTriggerProcessName(process, triggerProcessName, oldProcessName="HLT"):
0192     "Change the process name under which the trigger was run"
0193     patTriggerFull.processName = triggerProcessName
0194 
0195 def changeRecoMuonInput(process, recoMuonCollectionTag, oldRecoMuonCollectionTag=cms.InputTag("muons")):
0196     "Use a different input collection of reco muons"
0197     from PhysicsTools.PatAlgos.tools.helpers import massSearchReplaceAnyInputTag
0198     massSearchReplaceAnyInputTag(process.patMuonsWithTriggerSequence, oldRecoMuonCollectionTag, recoMuonCollectionTag)
0199 
0200 def useExistingPATMuons(process, newPatMuonTag, addL1Info=False):
0201     "Start from existing pat Muons instead of producing them"
0202     process.patMuonsWithTriggerSequence.remove(process.patMuonsWithoutTrigger)
0203     process.patMuonsWithTriggerSequence.remove(process.muonL1Info)
0204     process.patMuonsWithTrigger.src = newPatMuonTag
0205     from PhysicsTools.PatAlgos.tools.helpers import massSearchReplaceAnyInputTag
0206     massSearchReplaceAnyInputTag(process.patMuonsWithTriggerSequence, cms.InputTag('patMuonsWithoutTrigger'), newPatMuonTag)
0207     if addL1Info:
0208         process.muonL1Info.src = newPatMuonTag.muonSource
0209         addL1UserData(getattr(process,newPatMuonTag.moduleLabel), 'muonL1Info')
0210 
0211 def addPreselection(process, cut):
0212     "Add a preselection cut to the muons before matching (might be relevant, due to ambiguity resolution in trigger matching!"
0213     process.patMuonsWithoutTriggerUnfiltered = process.patMuonsWithoutTrigger.clone()
0214     process.globalReplace('patMuonsWithoutTrigger', cms.EDFilter("PATMuonSelector", src = cms.InputTag('patMuonsWithoutTriggerUnfiltered'), cut = cms.string(cut)))
0215     process.patMuonsWithTriggerSequence.replace(process.patMuonsWithoutTrigger, process.patMuonsWithoutTriggerUnfiltered * process.patMuonsWithoutTrigger)
0216 
0217 def addMCinfo(process):
0218     "Add MC matching information to the muons"
0219     process.load("PhysicsTools.PatAlgos.mcMatchLayer0.muonMatch_cfi")
0220     process.patMuonsWithTriggerSequence.replace(process.patMuonsWithoutTrigger, process.muonMatch + process.patMuonsWithoutTrigger)
0221     process.patMuonsWithoutTrigger.addGenMatch = True
0222     process.patMuonsWithoutTrigger.embedGenMatch = True
0223     process.patMuonsWithoutTrigger.genParticleMatch = 'muonMatch'
0224 
0225 def addDiMuonTriggers(process):
0226     print("[MuonAnalysis.MuonAssociators.patMuonsWithTrigger_cff] Di-muon triggers are already enabled by default")
0227 
0228 def addHLTL1Passthrough(process, embedder="patMuonsWithTrigger"):
0229     process.patMuonsWithTriggerSequence.replace(process.muonMatchHLTL3, process.muonMatchHLTL1 + process.muonMatchHLTL3)
0230     getattr(process,embedder).matches += [ cms.InputTag('muonMatchHLTL1'), cms.InputTag('muonMatchHLTL1','propagatedReco') ]
0231 
0232 def useL1MatchingWindowForSinglets(process):
0233     "Change the L1 trigger matching window to be suitable also for CSC single triggers"
0234     if hasattr(process, 'muonL1Info'):
0235         process.muonL1Info.maxDeltaR     = 0.3 #Changed accordingly to Zoltan tuning. It was: 1.2
0236         process.muonL1Info.maxDeltaEta   = 0.2
0237         process.muonL1Info.fallbackToME1 = True
0238     if hasattr(process, 'muonMatchHLTL1'):
0239         process.muonMatchHLTL1.maxDeltaR     = 0.3 #Changed accordingly to Zoltan tuning. It was: 1.2
0240         process.muonMatchHLTL1.maxDeltaEta   = 0.2
0241         process.muonMatchHLTL1.fallbackToME1 = True
0242 
0243 
0244 def useL1Stage2Candidates(process):
0245     if hasattr(process, 'muonL1Info'):
0246         # l1PhiOffest might need a second look
0247         # barrel seems not to requre it, whereas encaps do
0248         # anyhow the effect is of the order of 0.02
0249         #process.muonL1Info.l1PhiOffset = cms.double()
0250         process.muonL1Info.useMB2InOverlap = cms.bool(True)
0251         process.muonL1Info.useStage2L1 = cms.bool(True)
0252         process.muonL1Info.preselection = cms.string("")
0253         process.muonL1Info.matched = cms.InputTag("gmtStage2Digis:Muon:")
0254 
0255 def appendL1MatchingAlgo(process, algo = "quality"):
0256     if hasattr(process, 'muonL1Info'):
0257         newMuonL1Info = process.muonL1Info.clone(sortBy = cms.string(algo),
0258                                                          sortByQuality  = cms.bool(algo == "quality"),
0259                                                          sortByDeltaPhi = cms.bool(algo == "deltaEta"),
0260                                                          sortByDeltaEta = cms.bool(algo == "deltaPhi"),
0261                                                          sortByPt       = cms.bool(algo == "pt"),
0262                                                          maxDeltaR  = cms.double(0.3))
0263         setattr(process, "muonL1Info" + algo.title(), newMuonL1Info)
0264         process.patMuonsWithTriggerSequence.replace(process.muonL1Info, process.muonL1Info + getattr(process, 'muonL1Info' + algo.title()))
0265         addL1UserData(patMuonsWithoutTrigger, "muonL1Info" + algo.title())