1 from __future__
import print_function
2 import FWCore.ParameterSet.Config
as cms
10 from copy
import deepcopy
13 print(
"WARNING: particle based isolation must be studied")
15 def adaptPFMuons(process,module,postfix="", muonMatchModule=None ):
16 print(
"Adapting PF Muons ")
17 print(
"***************** ")
20 module.useParticleFlow =
True 21 module.pfMuonSource = cms.InputTag(
"pfIsolatedMuonsPFBRECO" + postfix)
22 module.userIsolation = cms.PSet()
38 if muonMatchModule ==
None :
39 applyPostfix(process,
"muonMatch",postfix).src = module.pfMuonSource
41 muonMatchModule.src = module.pfMuonSource
43 print(
" muon source:", module.pfMuonSource)
53 print(
"Adapting PF Electrons ")
54 print(
"********************* ")
57 module.useParticleFlow =
True 58 module.pfElectronSource = cms.InputTag(
"pfIsolatedElectronsPFBRECO" + postfix)
59 module.userIsolation = cms.PSet()
81 print(
" PF electron source:", module.pfElectronSource)
92 raise RuntimeError(
"Photons are not supported yet")
97 tauType=
'shrinkingConePFTau',
98 pf2patSelection=[
"DiscriminationByIsolation",
"DiscriminationByLeadingPionPtCut"],
99 selectionDependsOn=[
"DiscriminationByLeadingTrackFinding"],
100 producerFromType=
lambda producer: producer+
"Producer",
102 print(
"patTaus will be produced from taus of type: %s that pass %s" \
103 % (tauType, pf2patSelection))
108 producerName = producerFromType(tauType)
110 applyPostfix(process,
"pfTaus", postfix).src = producerName+postfix
112 oldTauSansRefs = getattr(process,
'pfTausProducerSansRefs'+postfix)
113 oldTau = getattr(process,
'pfTausProducer'+postfix)
115 newTauSansRefs =
None 116 newTau = getattr(process,producerName+postfix).
clone()
119 if tauType==
'shrinkingConePFTau':
120 newTauSansRefs = getattr(process,producerName+
"SansRefs").
clone()
121 newTauSansRefs.modifiers[1] = cms.PSet(
122 pfTauTagInfoSrc = cms.InputTag(
"pfTauTagInfoProducer"+postfix),
123 name = cms.string(
'pfTauTTIworkaround'+postfix),
124 plugin = cms.string(
'RecoTauTagInfoWorkaroundModifer')
126 newTau.modifiers[1] = newTauSansRefs.modifiers[1]
127 newTauSansRefs.piZeroSrc =
"pfJetsLegacyTaNCPiZeros"+postfix
128 newTau.piZeroSrc = newTauSansRefs.piZeroSrc
129 newTauSansRefs.builders[0].pfCandSrc = oldTauSansRefs.builders[0].pfCandSrc
130 newTauSansRefs.jetRegionSrc = oldTauSansRefs.jetRegionSrc
131 newTauSansRefs.jetSrc = oldTauSansRefs.jetSrc
132 elif tauType==
'fixedConePFTau':
133 newTau.piZeroSrc =
"pfJetsLegacyTaNCPiZeros"+postfix
134 elif tauType==
'hpsPFTau':
135 newTau = getattr(process,
'combinatoricRecoTaus'+postfix).
clone()
136 newTau.piZeroSrc=
"pfJetsLegacyHPSPiZeros"+postfix
137 newTau.modifiers[3] = cms.PSet(
138 pfTauTagInfoSrc = cms.InputTag(
"pfTauTagInfoProducer"+postfix),
139 name = cms.string(
'pfTauTTIworkaround'+postfix),
140 plugin = cms.string(
'RecoTauTagInfoWorkaroundModifer')
144 setattr(process,
'produceHPSPFTaus'+postfix,cms.Sequence(
applyPostfix(process,
'hpsSelectionDiscriminator',postfix)+
applyPostfix(process,
'hpsPFTauProducerSansRefs',postfix)+
applyPostfix(process,
'hpsPFTauProducer',postfix)))
147 cms.InputTag(
"combinatoricRecoTaus"+postfix),
148 cms.InputTag(
"pfTausBase"+postfix) )
151 cms.InputTag(
"combinatoricRecoTaus"+postfix),
152 cms.InputTag(
"pfTausBase"+postfix) )
163 if tauType==
'shrinkingConePFTau':
165 getattr(process,
"pfTausBase"+postfix).src =
"pfTausBaseSansRefs"+postfix
166 baseSequence += getattr(process,
"pfTausBaseSansRefs"+postfix)
169 def producerIsTauTypeMapperWithPostfix(tauProducer):
172 def recoTauTypeMapperWithGroup(tauProducer):
176 for predisc
in selectionDependsOn:
178 originalName = tauType+predisc
179 clonedName =
"pfTausBase"+predisc+postfix
180 clonedDisc = getattr(process, originalName).
clone()
183 tauCollectionToSelect =
None 184 if tauType !=
'hpsPFTau' :
185 tauCollectionToSelect =
"pfTausBase"+postfix
188 tauCollectionToSelect =
"hpsPFTauProducer"+postfix
191 oldTauTypeMapper=recoTauTypeMapperWithGroup,
192 newTauTypeMapper=producerIsTauTypeMapperWithPostfix,
193 preservePFTauProducer=
True)
194 clonedDisc.PFTauProducer = tauCollectionToSelect
197 getattr(process,
"pfTaus" + postfix).discriminators = cms.VPSet()
198 for selection
in pf2patSelection:
200 originalName = tauType+selection
201 clonedName =
"pfTausBase"+selection+postfix
202 clonedDisc = getattr(process, originalName).
clone()
205 tauCollectionToSelect =
None 207 if tauType !=
'hpsPFTau' :
208 tauCollectionToSelect = cms.InputTag(
"pfTausBase"+postfix)
211 tauCollectionToSelect = cms.InputTag(
"hpsPFTauProducer"+postfix)
214 oldTauTypeMapper=recoTauTypeMapperWithGroup,
215 newTauTypeMapper=producerIsTauTypeMapperWithPostfix,
216 preservePFTauProducer=
True)
217 clonedDisc.PFTauProducer = tauCollectionToSelect
220 getattr(process,
"pfTaus" + postfix).discriminators.append(cms.PSet(
221 discriminator=cms.InputTag(clonedName), selectionCut=cms.double(0.5)))
223 if tauType !=
'hpsPFTau':
224 getattr(process,
"pfTaus" + postfix).src =
"pfTausBase"+postfix
228 getattr(process,
"pfTaus" + postfix).src =
"hpsPFTauProducer"+postfix
232 def adaptPFTaus(process,tauType = 'shrinkingConePFTau', postfix = ""):
234 if tauType !=
'hpsPFTau' :
238 [
"DiscriminationByDecayModeFinding"],
239 [
"DiscriminationByDecayModeFinding"],
242 if tauType !=
'hpsPFTau' :
243 getattr(process,
"patTaus" + postfix).tauSource = cms.InputTag(
"pfTausBase"+postfix)
245 getattr(process,
"patTaus" + postfix).tauSource = cms.InputTag(
"hpsPFTauProducer"+postfix)
256 if tauType !=
'hpsPFTau' :
258 patTauLabel=
"pfTausBase"+postfix,
259 tauSource=cms.InputTag(tauType+
'Producer'+postfix),
261 getattr(process,
"patTaus" + postfix).tauSource = cms.InputTag(
"pfTausBase"+postfix)
265 tauSource=cms.InputTag(tauType+
'Producer'+postfix),
267 getattr(process,
"patTaus" + postfix).tauSource = cms.InputTag(
"hpsPFTauProducer"+postfix)
273 process.load(
"CommonTools.ParticleFlow.pfTaus_cff")
274 applyPostfix(process,
"pfTaus",postfix).src = cms.InputTag(tauType+
'Producer'+postfix)
278 from PhysicsTools.PatAlgos.producersLayer1.pfParticleProducer_cfi
import patPFParticles
280 producer = patPFParticles.clone(pfCandidateSource = src)
281 filter = cms.EDFilter(
"PATPFParticleSelector",
282 src = cms.InputTag(
"pat" + patLabel),
283 cut = cms.string(cut))
284 counter = cms.EDFilter(
"PATCandViewCountFilter",
285 minNumber = cms.uint32(0),
286 maxNumber = cms.uint32(999999),
287 src = cms.InputTag(
"pat" + patLabel))
295 applyPostfix(process,
"patCandidateSummary", postfix).candidates.append(cms.InputTag(
'pat' + patLabel))
296 applyPostfix(process,
"selectedPatCandidateSummary", postfix).candidates.append(cms.InputTag(
'selectedPat' + patLabel))
299 def switchToPFMET(process,input=cms.InputTag(
'pfMETPFBRECO'), type1=
False, postfix=
""):
300 print(
'MET: using ', input)
302 oldMETSource =
applyPostfix(process,
"patMETs",postfix).metSource
303 applyPostfix(process,
"patMETs",postfix).metSource = input
304 applyPostfix(process,
"patMETs",postfix).addMuonCorrections =
False 308 print(
'Apply TypeI corrections for MET')
310 jecLabel = getattr(process,
'patJetCorrFactors'+postfix).payload.pythonValue().
replace(
"'",
"")
311 getattr(process,jecLabel+
'Type1CorMet'+postfix).src = input.getModuleLabel()
315 def switchToPFJets(process, input=cms.InputTag(
'pfNoTauClones'), algo=
'AK4', postfix =
"", jetCorrections=(
'AK4PFchs', [
'L1FastJet',
'L2Relative',
'L3Absolute']), type1=
False, outputModules=[
'out']):
317 print(
"Switching to PFJets, ", algo)
318 print(
"************************ ")
319 print(
"input collection: ", input)
322 genJetCollection = cms.InputTag(
'ak4GenJetsNoNu'+postfix)
325 genJetCollection = cms.InputTag(
'ak7GenJetsNoNu'+postfix)
328 print(
'bad jet algorithm:', algo,
'! for now, only AK4 and AK7 are allowed. If you need other algorithms, please contact Colin')
332 from CommonTools.ParticleFlow.Tools.jetTools
import jetAlgo
333 inputCollection = getattr(process,
"pfJetsPFBRECO"+postfix).src
334 setattr(process,
"pfJetsPFBRECO"+postfix,
jetAlgo(algo))
335 getattr(process,
"pfJetsPFBRECO"+postfix).src = inputCollection
336 inputJetCorrLabel=jetCorrections
338 switchJetCollection(process,
342 genJetCollection=genJetCollection,
344 jetTrackAssociation=
True,
345 jetCorrections=inputJetCorrLabel,
346 outputModules = outputModules,
350 applyPostfix(process,
"patJetCorrFactors", postfix).useRho =
False 352 for corr
in inputJetCorrLabel[1]:
353 if corr ==
'L1FastJet':
354 applyPostfix(process,
"patJetCorrFactors", postfix).useRho =
True 355 applyPostfix(process,
"pfJetsPFBRECO", postfix).doAreaFastjet =
True 359 for mod
in process.producerNames().
split(
' '):
361 if mod.startswith(
"kt6")
and mod.endswith(
"Jets"+postfix)
and not 'GenJets' in mod:
362 prefix = mod.replace(postfix,
'')
363 prefix = prefix.replace(
'kt6PFJets',
'')
364 prefix = prefix.replace(
'kt6CaloJets',
'')
365 prefix = getattr(process,
'patJetCorrFactors'+prefix+postfix).payload.pythonValue().
replace(
"'",
"")
366 for essource
in process.es_sources_().
keys():
367 if essource == prefix+
'L1FastJet':
368 setattr(process,essource+postfix,getattr(process,essource).
clone(srcRho=cms.InputTag(mod,
'rho')))
370 getattr(process,prefix+
'CombinedCorrector').
clone(), process, task)
371 getattr(process,prefix+
'CorMet'+postfix).corrector = prefix+
'CombinedCorrector'+postfix
372 for cor
in getattr(process,prefix+
'CombinedCorrector'+postfix).correctors:
374 idx = getattr(process,prefix+
'CombinedCorrector'+postfix).correctors.index(essource);
375 getattr(process,prefix+
'CombinedCorrector'+postfix).correctors[idx] = essource+postfix
377 if hasattr( getattr( process,
"patJets" + postfix),
'embedCaloTowers' ):
378 applyPostfix(process,
"patJets", postfix).embedCaloTowers =
False 379 applyPostfix(process,
"patJets", postfix).embedPFCandidates =
True 386 removeMCMatching(process, names=[
'All'], postfix=postfix, outputModules=outputModules)
389 def adaptPVs(process, pvCollection=cms.InputTag(
'offlinePrimaryVertices'), postfix=
''):
390 print(
"Switching PV collection for PF2PAT:", pvCollection)
391 print(
"***********************************")
394 pvExchange = [
'Vertices',
'vertices',
'pvSrc',
'primaryVertices',
'srcPVs',
'primaryVertex']
399 for m
in (process.producerNames().
split(
' ') + process.filterNames().
split(
' ')):
401 for namePvSrc
in pvExchange:
402 if hasattr(getattr(process,m),namePvSrc):
404 setattr(getattr(process,m),namePvSrc,deepcopy(pvCollection))
407 def usePF2PAT(process,runPF2PAT=True, jetAlgo='AK4', runOnMC=True, postfix="", jetCorrections=(
'AK4PFchs', [
'L1FastJet',
'L2Relative',
'L3Absolute'],
'None'), pvCollection=cms.InputTag(
'goodOfflinePrimaryVerticesPFlow',), typeIMetCorrections=
False, outputModules=[
'out'],excludeFromTopProjection=[
'Tau']):
411 if typeIMetCorrections:
412 jetCorrections = (jetCorrections[0],jetCorrections[1],
'Type-1')
413 """Switch PAT to use PF2PAT instead of AOD sources. if 'runPF2PAT' is true, we'll also add PF2PAT in front of the PAT sequence""" 418 taskLabel = patAlgosToolsTask.label()
420 loadWithPostfix(process,
'PhysicsTools.PatAlgos.patSequences_cff',postfix, loadedProducersAndFilters=taskLabel)
421 loadWithPostfix(process,
"CommonTools.ParticleFlow.PFBRECO_cff",postfix, loadedProducersAndFilters=taskLabel)
423 loadWithPostfix(process,
'PhysicsTools.PatAlgos.patSequences_cff',postfix, loadedProducersAndFilters=taskLabel)
448 switchToPFJets( process, cms.InputTag(
'pfNoTauClonesPFBRECO'+postfix), jetAlgo, postfix=postfix,
449 jetCorrections=jetCorrections, type1=typeIMetCorrections, outputModules=outputModules )
452 if not 'L2L3Residual' in jetCorrections[1]:
454 print(
'#################################################')
455 print(
'WARNING! Not using L2L3Residual but this is data.')
456 print(
'If this is okay with you, disregard this message.')
457 print(
'#################################################')
458 switchToPFJets( process, cms.InputTag(
'pfNoTauClonesPFBRECO'+postfix), jetAlgo, postfix=postfix,
459 jetCorrections=jetCorrections, type1=typeIMetCorrections, outputModules=outputModules )
463 adaptPFTaus( process, tauType=
'hpsPFTau', postfix=postfix )
465 switchToPFMET(process, cms.InputTag(
'pfMETPFBRECO'+postfix), type1=typeIMetCorrections, postfix=postfix)
468 addPFCandidates(process,cms.InputTag(
'pfNoJetClones'+postfix),patLabel=
'PFParticles'+postfix,cut=
"",postfix=postfix)
471 adaptPVs(process, pvCollection=pvCollection, postfix=postfix)
474 runOnData(process,postfix=postfix,outputModules=outputModules)
477 getattr(process,
"pfNoPileUpJME"+postfix).enable =
True 478 getattr(process,
"pfNoMuonJMEPFBRECO"+postfix).enable =
True 479 getattr(process,
"pfNoElectronJMEPFBRECO"+postfix).enable =
True 480 getattr(process,
"pfNoTauPFBRECO"+postfix).enable =
False 481 getattr(process,
"pfNoJetPFBRECO"+postfix).enable =
True 483 for object
in excludeFromTopProjection:
485 if object
in [
'Muon',
'Electron']:
487 getattr(process,
"pfNo"+object+jme+
'PFBRECO'+postfix).enable =
False 488 exclusionList=exclusionList+object+
',' 489 exclusionList=exclusionList.rstrip(
',')
490 print(
"Done: PFBRECO interfaced to PAT, postfix=", postfix,
", Excluded from Top Projection:",exclusionList)
std::vector< std::string_view > split(std::string_view, const char *)
def addToProcessAndTask(label, module, process, task)
def replace(string, replacements)
def massSearchReplaceParam(sequence, paramName, paramOldValue, paramValue, verbose=False)
S & print(S &os, JobReport::InputFile const &f)
def applyPostfix(process, label, postfix)
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
def getPatAlgosToolsTask(process)
def loadWithPostfix(process, moduleName, postfix='', loadedProducersAndFilters=None)