File indexing completed on 2024-04-06 12:23:57
0001 from __future__ import print_function
0002 import FWCore.ParameterSet.Config as cms
0003
0004 from PhysicsTools.PatAlgos.tools.ConfigToolBase import *
0005 from PhysicsTools.PatAlgos.tools.helpers import cloneProcessingSnippet
0006 from RecoTauTag.RecoTau.TauDiscriminatorTools import *
0007 from PhysicsTools.PatAlgos.cleaningLayer1.tauCleaner_cfi import preselection
0008
0009
0010 def applyPostfix(process, label, postfix):
0011 result = None
0012 if hasattr(process, label+postfix):
0013 result = getattr(process, label + postfix)
0014 else:
0015 raise ValueError("Error in <applyPostfix>: No module of name = %s attached to process !!" % (label + postfix))
0016 return result
0017
0018 def _buildIDSourcePSet(tauType, idSources, postfix =""):
0019 """ Build a PSet defining the tau ID sources to embed into the pat::Tau """
0020 output = cms.PSet()
0021 for label, discriminator in idSources:
0022 wp = -99
0023 if ";" in discriminator:
0024 discriminator, wp_str = discriminator.split(";")
0025 wp = int(wp_str)
0026 id = cms.PSet()
0027 id.inputTag = cms.InputTag(discriminator.replace(":", postfix + ":"))
0028 id.provenanceConfigLabel=cms.string("")
0029 id.idLabel=cms.string("")
0030 setattr(output, label, id)
0031 return output
0032
0033 def _switchToPFTau(process,
0034 tauSource,
0035 pfTauType,
0036 idSources,
0037 patTauLabel = "",
0038 postfix = ""):
0039 """internal auxiliary function to switch to **any** PFTau collection"""
0040 print(' switching PAT Tau input to: ', tauSource)
0041
0042 applyPostfix(process, "tauMatch" + patTauLabel, postfix).src = tauSource
0043 applyPostfix(process, "tauGenJetMatch" + patTauLabel, postfix).src = tauSource
0044
0045 applyPostfix(process, "tauIsoDepositPFCandidates" + patTauLabel, postfix).src = tauSource
0046 applyPostfix(process, "tauIsoDepositPFCandidates" + patTauLabel, postfix).ExtractorPSet.tauSource = tauSource
0047 applyPostfix(process, "tauIsoDepositPFChargedHadrons" + patTauLabel, postfix).src = tauSource
0048 applyPostfix(process, "tauIsoDepositPFChargedHadrons" + patTauLabel, postfix).ExtractorPSet.tauSource = tauSource
0049 applyPostfix(process, "tauIsoDepositPFNeutralHadrons" + patTauLabel, postfix).src = tauSource
0050 applyPostfix(process, "tauIsoDepositPFNeutralHadrons" + patTauLabel, postfix).ExtractorPSet.tauSource = tauSource
0051 applyPostfix(process, "tauIsoDepositPFGammas" + patTauLabel, postfix).src = tauSource
0052 applyPostfix(process, "tauIsoDepositPFGammas" + patTauLabel, postfix).ExtractorPSet.tauSource = tauSource
0053
0054 applyPostfix(process, "patTaus" + patTauLabel, postfix).tauSource = tauSource
0055
0056 applyPostfix(process, "patTaus" + patTauLabel, postfix).tauTransverseImpactParameterSource = ""
0057 applyPostfix(process, "patTaus" + patTauLabel, postfix).tauIDSources = _buildIDSourcePSet(pfTauType, idSources, postfix)
0058
0059 if hasattr(process, "cleanPatTaus" + patTauLabel + postfix):
0060 getattr(process, "cleanPatTaus" + patTauLabel + postfix).preselection = preselection
0061
0062
0063 classicTauIDSources = [
0064 ("leadingTrackFinding", "DiscriminationByLeadingTrackFinding"),
0065 ("leadingTrackPtCut", "DiscriminationByLeadingTrackPtCut"),
0066 ("trackIsolation", "DiscriminationByTrackIsolation"),
0067 ("ecalIsolation", "DiscriminationByECALIsolation"),
0068 ("byIsolation", "DiscriminationByIsolation"),
0069 ("againstElectron", "DiscriminationAgainstElectron"),
0070 ("againstMuon", "DiscriminationAgainstMuon")
0071 ]
0072
0073 classicPFTauIDSources = [
0074 ("leadingPionPtCut", "DiscriminationByLeadingPionPtCut"),
0075 ("trackIsolationUsingLeadingPion", "DiscriminationByTrackIsolationUsingLeadingPion"),
0076 ("ecalIsolationUsingLeadingPion", "DiscriminationByECALIsolationUsingLeadingPion"),
0077 ("byIsolationUsingLeadingPion", "DiscriminationByIsolationUsingLeadingPion")
0078 ]
0079
0080
0081 hpsTauIDSources = [
0082 ("decayModeFindingNewDMs", "DiscriminationByDecayModeFindingNewDMs"),
0083 ("decayModeFinding", "DiscriminationByDecayModeFinding"),
0084 ("byLooseCombinedIsolationDeltaBetaCorr3Hits", "DiscriminationByLooseCombinedIsolationDBSumPtCorr3Hits"),
0085 ("byMediumCombinedIsolationDeltaBetaCorr3Hits", "DiscriminationByMediumCombinedIsolationDBSumPtCorr3Hits"),
0086 ("byTightCombinedIsolationDeltaBetaCorr3Hits", "DiscriminationByTightCombinedIsolationDBSumPtCorr3Hits"),
0087 ("byCombinedIsolationDeltaBetaCorrRaw3Hits", "DiscriminationByRawCombinedIsolationDBSumPtCorr3Hits"),
0088 ("byLooseCombinedIsolationDeltaBetaCorr3HitsdR03", "DiscriminationByLooseCombinedIsolationDBSumPtCorr3HitsdR03"),
0089 ("byMediumCombinedIsolationDeltaBetaCorr3HitsdR03", "DiscriminationByMediumCombinedIsolationDBSumPtCorr3HitsdR03"),
0090 ("byTightCombinedIsolationDeltaBetaCorr3HitsdR03", "DiscriminationByTightCombinedIsolationDBSumPtCorr3HitsdR03"),
0091 ("byPhotonPtSumOutsideSignalCone", "DiscriminationByPhotonPtSumOutsideSignalCone"),
0092 ("chargedIsoPtSum", "ChargedIsoPtSum"),
0093 ("neutralIsoPtSum", "NeutralIsoPtSum"),
0094 ("puCorrPtSum", "PUcorrPtSum"),
0095 ("neutralIsoPtSumWeight", "NeutralIsoPtSumWeight"),
0096 ("footprintCorrection", "FootprintCorrection"),
0097 ("photonPtSumOutsideSignalCone", "PhotonPtSumOutsideSignalCone"),
0098 ("againstMuonLoose3", "DiscriminationByLooseMuonRejection3"),
0099 ("againstMuonTight3", "DiscriminationByTightMuonRejection3"),
0100 ("byIsolationMVArun2v1DBoldDMwLTraw", "DiscriminationByIsolationMVArun2v1DBoldDMwLTraw"),
0101 ("byVVLooseIsolationMVArun2v1DBoldDMwLT", "DiscriminationByVVLooseIsolationMVArun2v1DBoldDMwLT"),
0102 ("byVLooseIsolationMVArun2v1DBoldDMwLT", "DiscriminationByVLooseIsolationMVArun2v1DBoldDMwLT"),
0103 ("byLooseIsolationMVArun2v1DBoldDMwLT", "DiscriminationByLooseIsolationMVArun2v1DBoldDMwLT"),
0104 ("byMediumIsolationMVArun2v1DBoldDMwLT", "DiscriminationByMediumIsolationMVArun2v1DBoldDMwLT"),
0105 ("byTightIsolationMVArun2v1DBoldDMwLT", "DiscriminationByTightIsolationMVArun2v1DBoldDMwLT"),
0106 ("byVTightIsolationMVArun2v1DBoldDMwLT", "DiscriminationByVTightIsolationMVArun2v1DBoldDMwLT"),
0107 ("byVVTightIsolationMVArun2v1DBoldDMwLT", "DiscriminationByVVTightIsolationMVArun2v1DBoldDMwLT"),
0108 ("byIsolationMVArun2v1DBnewDMwLTraw", "DiscriminationByIsolationMVArun2v1DBnewDMwLTraw"),
0109 ("byVVLooseIsolationMVArun2v1DBnewDMwLT", "DiscriminationByVVLooseIsolationMVArun2v1DBnewDMwLT"),
0110 ("byVLooseIsolationMVArun2v1DBnewDMwLT", "DiscriminationByVLooseIsolationMVArun2v1DBnewDMwLT"),
0111 ("byLooseIsolationMVArun2v1DBnewDMwLT", "DiscriminationByLooseIsolationMVArun2v1DBnewDMwLT"),
0112 ("byMediumIsolationMVArun2v1DBnewDMwLT", "DiscriminationByMediumIsolationMVArun2v1DBnewDMwLT"),
0113 ("byTightIsolationMVArun2v1DBnewDMwLT", "DiscriminationByTightIsolationMVArun2v1DBnewDMwLT"),
0114 ("byVTightIsolationMVArun2v1DBnewDMwLT", "DiscriminationByVTightIsolationMVArun2v1DBnewDMwLT"),
0115 ("byVVTightIsolationMVArun2v1DBnewDMwLT", "DiscriminationByVVTightIsolationMVArun2v1DBnewDMwLT"),
0116 ("byIsolationMVArun2v1PWoldDMwLTraw", "DiscriminationByIsolationMVArun2v1PWoldDMwLTraw"),
0117 ("byVLooseIsolationMVArun2v1PWoldDMwLT", "DiscriminationByVLooseIsolationMVArun2v1PWoldDMwLT"),
0118 ("byLooseIsolationMVArun2v1PWoldDMwLT", "DiscriminationByLooseIsolationMVArun2v1PWoldDMwLT"),
0119 ("byMediumIsolationMVArun2v1PWoldDMwLT", "DiscriminationByMediumIsolationMVArun2v1PWoldDMwLT"),
0120 ("byTightIsolationMVArun2v1PWoldDMwLT", "DiscriminationByTightIsolationMVArun2v1PWoldDMwLT"),
0121 ("byVTightIsolationMVArun2v1PWoldDMwLT", "DiscriminationByVTightIsolationMVArun2v1PWoldDMwLT"),
0122 ("byVVTightIsolationMVArun2v1PWoldDMwLT", "DiscriminationByVVTightIsolationMVArun2v1PWoldDMwLT"),
0123 ("byIsolationMVArun2v1PWnewDMwLTraw", "DiscriminationByIsolationMVArun2v1PWnewDMwLTraw"),
0124 ("byVLooseIsolationMVArun2v1PWnewDMwLT", "DiscriminationByVLooseIsolationMVArun2v1PWnewDMwLT"),
0125 ("byLooseIsolationMVArun2v1PWnewDMwLT", "DiscriminationByLooseIsolationMVArun2v1PWnewDMwLT"),
0126 ("byMediumIsolationMVArun2v1PWnewDMwLT", "DiscriminationByMediumIsolationMVArun2v1PWnewDMwLT"),
0127 ("byTightIsolationMVArun2v1PWnewDMwLT", "DiscriminationByTightIsolationMVArun2v1PWnewDMwLT"),
0128 ("byVTightIsolationMVArun2v1PWnewDMwLT", "DiscriminationByVTightIsolationMVArun2v1PWnewDMwLT"),
0129 ("byVVTightIsolationMVArun2v1PWnewDMwLT", "DiscriminationByVVTightIsolationMVArun2v1PWnewDMwLT"),
0130 ("chargedIsoPtSumdR03", "ChargedIsoPtSumdR03"),
0131 ("neutralIsoPtSumdR03", "NeutralIsoPtSumdR03"),
0132 ("neutralIsoPtSumWeightdR03", "NeutralIsoPtSumWeightdR03"),
0133 ("footprintCorrectiondR03", "FootprintCorrectiondR03"),
0134 ("photonPtSumOutsideSignalConedR03", "PhotonPtSumOutsideSignalConedR03"),
0135 ("byIsolationMVArun2v1DBdR03oldDMwLTraw", "DiscriminationByIsolationMVArun2v1DBdR03oldDMwLTraw"),
0136 ("byVVLooseIsolationMVArun2v1DBdR03oldDMwLT", "DiscriminationByVVLooseIsolationMVArun2v1DBdR03oldDMwLT"),
0137 ("byVLooseIsolationMVArun2v1DBdR03oldDMwLT", "DiscriminationByVLooseIsolationMVArun2v1DBdR03oldDMwLT"),
0138 ("byLooseIsolationMVArun2v1DBdR03oldDMwLT", "DiscriminationByLooseIsolationMVArun2v1DBdR03oldDMwLT"),
0139 ("byMediumIsolationMVArun2v1DBdR03oldDMwLT", "DiscriminationByMediumIsolationMVArun2v1DBdR03oldDMwLT"),
0140 ("byTightIsolationMVArun2v1DBdR03oldDMwLT", "DiscriminationByTightIsolationMVArun2v1DBdR03oldDMwLT"),
0141 ("byVTightIsolationMVArun2v1DBdR03oldDMwLT", "DiscriminationByVTightIsolationMVArun2v1DBdR03oldDMwLT"),
0142 ("byVVTightIsolationMVArun2v1DBdR03oldDMwLT", "DiscriminationByVVTightIsolationMVArun2v1DBdR03oldDMwLT"),
0143 ("byIsolationMVArun2v1PWdR03oldDMwLTraw", "DiscriminationByIsolationMVArun2v1PWdR03oldDMwLTraw"),
0144 ("byVLooseIsolationMVArun2v1PWdR03oldDMwLT", "DiscriminationByVLooseIsolationMVArun2v1PWdR03oldDMwLT"),
0145 ("byLooseIsolationMVArun2v1PWdR03oldDMwLT", "DiscriminationByLooseIsolationMVArun2v1PWdR03oldDMwLT"),
0146 ("byMediumIsolationMVArun2v1PWdR03oldDMwLT", "DiscriminationByMediumIsolationMVArun2v1PWdR03oldDMwLT"),
0147 ("byTightIsolationMVArun2v1PWdR03oldDMwLT", "DiscriminationByTightIsolationMVArun2v1PWdR03oldDMwLT"),
0148 ("byVTightIsolationMVArun2v1PWdR03oldDMwLT", "DiscriminationByVTightIsolationMVArun2v1PWdR03oldDMwLT"),
0149 ("byVVTightIsolationMVArun2v1PWdR03oldDMwLT", "DiscriminationByVVTightIsolationMVArun2v1PWdR03oldDMwLT"),
0150 ("againstElectronMVA6Raw", "DiscriminationByMVA6rawElectronRejection"),
0151 ("againstElectronMVA6category", "DiscriminationByMVA6rawElectronRejection:category"),
0152 ("againstElectronVLooseMVA6", "DiscriminationByMVA6VLooseElectronRejection"),
0153 ("againstElectronLooseMVA6", "DiscriminationByMVA6LooseElectronRejection"),
0154 ("againstElectronMediumMVA6", "DiscriminationByMVA6MediumElectronRejection"),
0155 ("againstElectronTightMVA6", "DiscriminationByMVA6TightElectronRejection"),
0156 ("againstElectronVTightMVA6", "DiscriminationByMVA6VTightElectronRejection"),
0157 ("againstElectronDeadECAL", "DiscriminationByDeadECALElectronRejection"),
0158 ]
0159
0160
0161 def switchToPFTauFixedCone(process,
0162 tauSource = cms.InputTag('fixedConePFTauProducer'),
0163 patTauLabel = "",
0164 postfix = ""):
0165 fixedConeIDSources = copy.copy(classicTauIDSources)
0166 fixedConeIDSources.extend(classicPFTauIDSources)
0167
0168 _switchToPFTau(process, tauSource, 'fixedConePFTau', fixedConeIDSources,
0169 patTauLabel = patTauLabel, postfix = postfix)
0170
0171
0172 def switchToPFTauShrinkingCone(process,
0173 tauSource = cms.InputTag('shrinkingConePFTauProducer'),
0174 patTauLabel = "",
0175 postfix = ""):
0176 shrinkingIDSources = copy.copy(classicTauIDSources)
0177 shrinkingIDSources.extend(classicPFTauIDSources)
0178
0179 _switchToPFTau(process, tauSource, 'shrinkingConePFTau', shrinkingIDSources,
0180 patTauLabel = patTauLabel, postfix = postfix)
0181
0182
0183 def switchToPFTauHPS(process,
0184 tauSource = cms.InputTag('hpsPFTauProducer'),
0185 patTauLabel = "",
0186 jecLevels = [],
0187 postfix = ""):
0188
0189 _switchToPFTau(process, tauSource, 'hpsPFTau', hpsTauIDSources,
0190 patTauLabel = patTauLabel, postfix = postfix)
0191
0192
0193 applyPostfix(process, "patTaus" + patTauLabel, postfix).tauTransverseImpactParameterSource = tauSource.value().replace("Producer", "TransverseImpactParameters")
0194
0195
0196 if hasattr(process, "cleanPatTaus" + patTauLabel + postfix):
0197 getattr(process, "cleanPatTaus" + patTauLabel + postfix).preselection = preselection
0198
0199
0200 def switchToPFTauByType(process,
0201 pfTauType = None,
0202 tauSource = cms.InputTag('hpsPFTauProducer'),
0203 patTauLabel = "",
0204 postfix = "" ):
0205 mapping = {
0206 'shrinkingConePFTau' : switchToPFTauShrinkingCone,
0207 'fixedConePFTau' : switchToPFTauFixedCone,
0208 'hpsPFTau' : switchToPFTauHPS
0209 }
0210 if not pfTauType in mapping.keys():
0211 raise ValueError("Error in <switchToPFTauByType>: Undefined pfTauType = %s !!" % pfTauType)
0212
0213 mapping[pfTauType](process, tauSource = tauSource,
0214 patTauLabel = patTauLabel, postfix = postfix)
0215
0216 class AddTauCollection(ConfigToolBase):
0217
0218 """ Add a new collection of taus. Takes the configuration from the
0219 already configured standard tau collection as starting point;
0220 replaces before calling addTauCollection will also affect the
0221 new tau collections
0222 """
0223 _label='addTauCollection'
0224 _defaultParameters=dicttypes.SortedKeysDict()
0225 def __init__(self):
0226 ConfigToolBase.__init__(self)
0227 self.addParameter(self._defaultParameters, 'tauCollection',
0228 self._defaultValue, 'Input tau collection', cms.InputTag)
0229 self.addParameter(self._defaultParameters, 'algoLabel',
0230 self._defaultValue, "label to indicate the tau algorithm (e.g.'hps')", str)
0231 self.addParameter(self._defaultParameters, 'typeLabel',
0232 self._defaultValue, "label to indicate the type of constituents (either 'PFTau' or 'Tau')", str)
0233 self.addParameter(self._defaultParameters, 'doPFIsoDeposits',
0234 True, "run sequence for computing particle-flow based IsoDeposits")
0235 self.addParameter(self._defaultParameters, 'standardAlgo',
0236 "hps", "standard algorithm label of the collection from which the clones " \
0237 + "for the new tau collection will be taken from " \
0238 + "(note that this tau collection has to be available in the event before hand)")
0239 self.addParameter(self._defaultParameters, 'standardType',
0240 "PFTau", "standard constituent type label of the collection from which the clones " \
0241 + " for the new tau collection will be taken from "\
0242 + "(note that this tau collection has to be available in the event before hand)")
0243
0244 self._parameters=copy.deepcopy(self._defaultParameters)
0245 self._comment = ""
0246
0247 def getDefaultParameters(self):
0248 return self._defaultParameters
0249
0250 def __call__(self,process,
0251 tauCollection = None,
0252 algoLabel = None,
0253 typeLabel = None,
0254 doPFIsoDeposits = None,
0255 jetCorrLabel = None,
0256 standardAlgo = None,
0257 standardType = None):
0258
0259 if tauCollection is None:
0260 tauCollection = self._defaultParameters['tauCollection'].value
0261 if algoLabel is None:
0262 algoLabel = self._defaultParameters['algoLabel'].value
0263 if typeLabel is None:
0264 typeLabel = self._defaultParameters['typeLabel'].value
0265 if doPFIsoDeposits is None:
0266 doPFIsoDeposits = self._defaultParameters['doPFIsoDeposits'].value
0267 if standardAlgo is None:
0268 standardAlgo = self._defaultParameters['standardAlgo'].value
0269 if standardType is None:
0270 standardType = self._defaultParameters['standardType'].value
0271
0272 self.setParameter('tauCollection', tauCollection)
0273 self.setParameter('algoLabel', algoLabel)
0274 self.setParameter('typeLabel', typeLabel)
0275 self.setParameter('doPFIsoDeposits', doPFIsoDeposits)
0276 self.setParameter('standardAlgo', standardAlgo)
0277 self.setParameter('standardType', standardType)
0278
0279 self.apply(process)
0280
0281 def toolCode(self, process):
0282 tauCollection = self._parameters['tauCollection'].value
0283 algoLabel = self._parameters['algoLabel'].value
0284 typeLabel = self._parameters['typeLabel'].value
0285 doPFIsoDeposits = self._parameters['doPFIsoDeposits'].value
0286 standardAlgo = self._parameters['standardAlgo'].value
0287 standardType = self._parameters['standardType'].value
0288
0289
0290
0291 if typeLabel == 'Tau':
0292 print("NO PF Isolation will be computed for CaloTau (this could be improved later)")
0293 doPFIsoDeposits = False
0294
0295
0296
0297 def oldLabel(prefix = ''):
0298 if prefix == '':
0299 return "patTaus"
0300 else:
0301 return prefix + "PatTaus"
0302
0303
0304
0305
0306 def capitalize(label):
0307 return label[0].capitalize() + label[1:]
0308
0309
0310
0311 def newLabel(oldLabel):
0312 newLabel = oldLabel
0313 if ( oldLabel.find(standardAlgo) >= 0 and oldLabel.find(standardType) >= 0 ):
0314 oldLabel = oldLabel.replace(standardAlgo, algoLabel).replace(standardType, typeLabel)
0315 else:
0316 oldLabel = oldLabel + capitalize(algoLabel + typeLabel)
0317 return oldLabel
0318
0319
0320 def addClone(hook, **replaceStatements):
0321
0322
0323 newModule = getattr(process, hook).clone(**replaceStatements)
0324
0325
0326 def addPFIsoDepositClone(hook, **replaceStatements):
0327 newModule = getattr(process, hook).clone(**replaceStatements)
0328 newModuleIsoDepositExtractor = getattr(newModule, "ExtractorPSet")
0329 setattr(newModuleIsoDepositExtractor, "tauSource", getattr(newModule, "src"))
0330
0331
0332 addClone(oldLabel(), tauSource = tauCollection)
0333
0334
0335 addClone(oldLabel('selected'), src = cms.InputTag(newLabel(oldLabel())))
0336
0337
0338 addClone(oldLabel('clean'), src=cms.InputTag(newLabel(oldLabel('selected'))))
0339
0340
0341 newTaus = getattr(process, newLabel(oldLabel()))
0342
0343
0344 addClone('tauMatch', src = tauCollection)
0345 addClone('tauGenJetMatch', src = tauCollection)
0346
0347
0348 if doPFIsoDeposits:
0349 addPFIsoDepositClone('tauIsoDepositPFCandidates', src = tauCollection)
0350 addPFIsoDepositClone('tauIsoDepositPFChargedHadrons', src = tauCollection)
0351 addPFIsoDepositClone('tauIsoDepositPFNeutralHadrons', src = tauCollection)
0352 addPFIsoDepositClone('tauIsoDepositPFGammas', src = tauCollection)
0353
0354
0355 def fixInputTag(x):
0356 x.setModuleLabel(newLabel(x.moduleLabel))
0357
0358
0359 fixInputTag(newTaus.genParticleMatch)
0360 fixInputTag(newTaus.genJetMatch)
0361 fixInputTag(newTaus.isoDeposits.pfAllParticles)
0362 fixInputTag(newTaus.isoDeposits.pfNeutralHadron)
0363 fixInputTag(newTaus.isoDeposits.pfChargedHadron)
0364 fixInputTag(newTaus.isoDeposits.pfGamma)
0365 fixInputTag(newTaus.userIsolation.pfAllParticles.src)
0366 fixInputTag(newTaus.userIsolation.pfNeutralHadron.src)
0367 fixInputTag(newTaus.userIsolation.pfChargedHadron.src)
0368 fixInputTag(newTaus.userIsolation.pfGamma.src)
0369
0370
0371
0372 oldTaus = getattr(process, oldLabel())
0373 switchToPFTauByType(process, pfTauType = algoLabel + typeLabel,
0374 tauSource = getattr(newTaus, "tauSource"),
0375 patTauLabel = capitalize(algoLabel + typeLabel))
0376
0377 addTauCollection=AddTauCollection()