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