Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:21:56

0001 from __future__ import print_function
0002 import FWCore.ParameterSet.Config as cms
0003 from RecoTauTag.RecoTau.TauDiscriminatorTools import noPrediscriminants
0004 from RecoTauTag.RecoTau.PATTauDiscriminationByMVAIsolationRun2_cff import patDiscriminationByIsolationMVArun2v1raw, patDiscriminationByIsolationMVArun2v1
0005 from RecoTauTag.RecoTau.DeepTau_cfi import DeepTau
0006 from RecoTauTag.RecoTau.tauIdWPsDefs import WORKING_POINTS_v2p1, WORKING_POINTS_v2p5, WORKING_POINTS_PHASEII_v2p5
0007 
0008 import os
0009 import re
0010 
0011 class TauIDEmbedder(object):
0012     """class to rerun the tau seq and acces trainings from the database"""
0013     availableDiscriminators = [
0014         "2017v1", "2017v2", "newDM2017v2", "dR0p32017v2", "2016v1", "newDM2016v1",
0015         "deepTau2017v2", "deepTau2017v2p1", "deepTau2018v2p5", "deepTau2026v2p5",
0016         "againstEle2018",
0017         "newDMPhase2v1",
0018         "againstElePhase2v1"
0019     ]
0020 
0021     def __init__(self, process, debug = False,
0022                  originalTauName = "slimmedTaus",
0023                  updatedTauName = "slimmedTausNewID",
0024                  postfix = "",
0025                  toKeep =  ["deepTau2017v2p1", "deepTau2018v2p5", "deepTau2026v2p5"],
0026                  tauIdDiscrMVA_trainings_run2_2017 = { 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", },
0027                  tauIdDiscrMVA_WPs_run2_2017 = {
0028                     'tauIdMVAIsoDBoldDMwLT2017' : {
0029                         'Eff95' : "DBoldDMwLTEff95",
0030                         'Eff90' : "DBoldDMwLTEff90",
0031                         'Eff80' : "DBoldDMwLTEff80",
0032                         'Eff70' : "DBoldDMwLTEff70",
0033                         'Eff60' : "DBoldDMwLTEff60",
0034                         'Eff50' : "DBoldDMwLTEff50",
0035                         'Eff40' : "DBoldDMwLTEff40"
0036                     }
0037                  },
0038                  tauIdDiscrMVA_2017_version = "v1",
0039                  conditionDB = "" # preparational DB: 'frontier://FrontierPrep/CMS_CONDITIONS'
0040                  ):
0041         super(TauIDEmbedder, self).__init__()
0042         self.process = process
0043         self.debug = debug
0044         self.originalTauName = originalTauName
0045         self.updatedTauName = updatedTauName
0046         self.postfix = postfix
0047         self.process.load('RecoTauTag.Configuration.loadRecoTauTagMVAsFromPrepDB_cfi')
0048         if len(conditionDB) != 0:
0049             self.process.CondDBTauConnection.connect = cms.string(conditionDB)
0050             self.process.loadRecoTauTagMVAsFromPrepDB.connect = cms.string(conditionDB)
0051             # if debug:
0052             #   print self.process.CondDBTauConnection.connect
0053             #   print dir(self.process.loadRecoTauTagMVAsFromPrepDB)
0054             #   print self.process.loadRecoTauTagMVAsFromPrepDB.parameterNames_
0055 
0056         self.tauIdDiscrMVA_trainings_run2_2017 = tauIdDiscrMVA_trainings_run2_2017
0057         self.tauIdDiscrMVA_WPs_run2_2017 = tauIdDiscrMVA_WPs_run2_2017
0058         self.tauIdDiscrMVA_2017_version = tauIdDiscrMVA_2017_version
0059         for discr in toKeep:
0060             if discr not in TauIDEmbedder.availableDiscriminators:
0061                 raise RuntimeError('TauIDEmbedder: discriminator "{}" is not supported'.format(discr))
0062         self.toKeep = toKeep
0063     
0064     @staticmethod
0065     def get_cmssw_version(debug = False):
0066         """returns 'CMSSW_X_Y_Z'"""
0067         cmssw_version = os.environ["CMSSW_VERSION"]
0068         if debug: print ("get_cmssw_version:", cmssw_version)
0069         return cmssw_version
0070 
0071     @classmethod
0072     def get_cmssw_version_number(klass, debug = False):
0073         """returns '(release, subversion, patch)' (without 'CMSSW_')"""
0074         v = klass.get_cmssw_version().split("CMSSW_")[1].split("_")[0:3]
0075         if debug: print ("get_cmssw_version_number:", v)
0076         try:
0077             patch = int(v[2])
0078         except:
0079             patch = -1
0080         return int(v[0]), int(v[1]), patch
0081 
0082     @staticmethod
0083     def versionToInt(release=9, subversion=4, patch=0, debug = False):
0084         version = release * 10000 + subversion * 100 + patch + 1 # shifted by one to account for pre-releases.
0085         if debug: print ("versionToInt:", version)
0086         return version
0087 
0088 
0089     @classmethod
0090     def is_above_cmssw_version(klass, release=9, subversion=4, patch=0, debug = False):
0091         split_cmssw_version = klass.get_cmssw_version_number()
0092         if klass.versionToInt(release, subversion, patch) > klass.versionToInt(split_cmssw_version[0], split_cmssw_version[1], split_cmssw_version[2]):
0093             if debug: print ("is_above_cmssw_version:", False)
0094             return False
0095         else:
0096             if debug: print ("is_above_cmssw_version:", True)
0097             return True
0098 
0099     def tauIDMVAinputs(self, module, wp):
0100         return cms.PSet(inputTag = cms.InputTag(module), workingPointIndex = cms.int32(-1 if wp=="raw" else -2 if wp=="category" else getattr(self.process, module).workingPoints.index(wp)))
0101 
0102     def loadMVA_WPs_run2_2017(self):
0103         if self.debug: print ("loadMVA_WPs_run2_2017: performed")
0104         global cms
0105         for training, gbrForestName in self.tauIdDiscrMVA_trainings_run2_2017.items():
0106 
0107             self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append(
0108                 cms.PSet(
0109                     record = cms.string('GBRWrapperRcd'),
0110                     tag = cms.string("RecoTauTag_%s%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version)),
0111                     label = cms.untracked.string("RecoTauTag_%s%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version))
0112                 )
0113             )
0114 
0115             for WP in self.tauIdDiscrMVA_WPs_run2_2017[training].keys():
0116                 self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append(
0117                     cms.PSet(
0118                         record = cms.string('PhysicsTGraphPayloadRcd'),
0119                         tag = cms.string("RecoTauTag_%s%s_WP%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version, WP)),
0120                         label = cms.untracked.string("RecoTauTag_%s%s_WP%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version, WP))
0121                     )
0122                 )
0123 
0124             self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append(
0125                 cms.PSet(
0126                     record = cms.string('PhysicsTFormulaPayloadRcd'),
0127                     tag = cms.string("RecoTauTag_%s%s_mvaOutput_normalization" % (gbrForestName, self.tauIdDiscrMVA_2017_version)),
0128                     label = cms.untracked.string("RecoTauTag_%s%s_mvaOutput_normalization" % (gbrForestName, self.tauIdDiscrMVA_2017_version))
0129                 )
0130             )
0131 
0132     def runTauID(self):
0133         _rerunMvaIsolationTask = cms.Task()
0134         _rerunMvaIsolationSequence = cms.Sequence()
0135         tauIDSources = cms.PSet()
0136 
0137         # rerun the seq to obtain the 2017 nom training with 0.5 iso cone, old DM, ptph>1, trained on 2017MCv1
0138         if "2017v1" in self.toKeep:
0139             self.tauIdDiscrMVA_2017_version = "v1"
0140             self.tauIdDiscrMVA_trainings_run2_2017 = {
0141                 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017",
0142             }
0143             self.tauIdDiscrMVA_WPs_run2_2017 = {
0144                 'tauIdMVAIsoDBoldDMwLT2017' : {
0145                     'Eff95' : "DBoldDMwLTEff95",
0146                     'Eff90' : "DBoldDMwLTEff90",
0147                     'Eff80' : "DBoldDMwLTEff80",
0148                     'Eff70' : "DBoldDMwLTEff70",
0149                     'Eff60' : "DBoldDMwLTEff60",
0150                     'Eff50' : "DBoldDMwLTEff50",
0151                     'Eff40' : "DBoldDMwLTEff40"
0152                 }
0153             }
0154             # update the list of available in DB samples
0155             if not self.is_above_cmssw_version(9, 4, 4, self.debug):
0156                 if self.debug: print ("runTauID: not is_above_cmssw_version(9, 4, 4). Will update the list of available in DB samples to access 2017v1")
0157                 self.loadMVA_WPs_run2_2017()
0158 
0159             _byIsolationOldDMMVArun2017v1raw = "rerunDiscriminationByIsolationOldDMMVArun2017v1raw"+self.postfix
0160             setattr(self.process,_byIsolationOldDMMVArun2017v1raw,patDiscriminationByIsolationMVArun2v1raw.clone(
0161                 PATTauProducer = self.originalTauName,
0162                 Prediscriminants = noPrediscriminants,
0163                 loadMVAfromDB = cms.bool(True),
0164                 mvaName = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1"),
0165                 mvaOpt = cms.string("DBoldDMwLTwGJ"),
0166                 verbosity = cms.int32(0)
0167             ))
0168 
0169             _byIsolationOldDMMVArun2017v1 = "rerunDiscriminationByIsolationOldDMMVArun2017v1"+self.postfix
0170             setattr(self.process,_byIsolationOldDMMVArun2017v1,patDiscriminationByIsolationMVArun2v1.clone(
0171                 PATTauProducer = self.originalTauName,
0172                 Prediscriminants = noPrediscriminants,
0173                 toMultiplex = _byIsolationOldDMMVArun2017v1raw,
0174                 loadMVAfromDB = cms.bool(True),
0175                 mvaOutput_normalization = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations
0176                 mapping = cms.VPSet(
0177                     cms.PSet(
0178                         category = cms.uint32(0),
0179                         cut = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1"), #writeTauIdDiscrWPs
0180                         variable = cms.string("pt"),
0181                     )
0182                 ),
0183                 workingPoints = cms.vstring(
0184                     "_WPEff95",
0185                     "_WPEff90",
0186                     "_WPEff80",
0187                     "_WPEff70",
0188                     "_WPEff60",
0189                     "_WPEff50",
0190                     "_WPEff40"
0191                 )
0192             ))
0193 
0194             _rerunIsolationOldDMMVArun2017v1Task =  cms.Task(
0195                 getattr(self.process,_byIsolationOldDMMVArun2017v1raw),
0196                 getattr(self.process,_byIsolationOldDMMVArun2017v1)
0197             )
0198             _rerunMvaIsolationTask.add(_rerunIsolationOldDMMVArun2017v1Task)
0199             _rerunMvaIsolationSequence += cms.Sequence(_rerunIsolationOldDMMVArun2017v1Task)
0200 
0201             tauIDSources.byIsolationMVArun2017v1DBoldDMwLTraw2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v1, "raw")
0202             tauIDSources.byVVLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v1, "_WPEff95")
0203             tauIDSources.byVLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v1, "_WPEff90")
0204             tauIDSources.byLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v1, "_WPEff80")
0205             tauIDSources.byMediumIsolationMVArun2017v1DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v1, "_WPEff70")
0206             tauIDSources.byTightIsolationMVArun2017v1DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v1, "_WPEff60")
0207             tauIDSources.byVTightIsolationMVArun2017v1DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v1, "_WPEff50")
0208             tauIDSources.byVVTightIsolationMVArun2017v1DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v1, "_WPEff40")
0209 
0210 
0211         if "2017v2" in self.toKeep:
0212             self.tauIdDiscrMVA_2017_version = "v2"
0213             self.tauIdDiscrMVA_trainings_run2_2017 = {
0214                 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017",
0215             }
0216             self.tauIdDiscrMVA_WPs_run2_2017 = {
0217                 'tauIdMVAIsoDBoldDMwLT2017' : {
0218                     'Eff95' : "DBoldDMwLTEff95",
0219                     'Eff90' : "DBoldDMwLTEff90",
0220                     'Eff80' : "DBoldDMwLTEff80",
0221                     'Eff70' : "DBoldDMwLTEff70",
0222                     'Eff60' : "DBoldDMwLTEff60",
0223                     'Eff50' : "DBoldDMwLTEff50",
0224                     'Eff40' : "DBoldDMwLTEff40"
0225                 }
0226             }
0227 
0228             if not self.is_above_cmssw_version(9, 4, 5, self.debug):
0229                 if self.debug: print ("runTauID: not is_above_cmssw_version(9, 4, 5). Will update the list of available in DB samples to access 2017v2")
0230                 self.loadMVA_WPs_run2_2017()
0231 
0232             _byIsolationOldDMMVArun2017v2raw = "rerunDiscriminationByIsolationOldDMMVArun2017v2raw"+self.postfix
0233             setattr(self.process,_byIsolationOldDMMVArun2017v2raw,patDiscriminationByIsolationMVArun2v1raw.clone(
0234                 PATTauProducer = self.originalTauName,
0235                 Prediscriminants = noPrediscriminants,
0236                 loadMVAfromDB = cms.bool(True),
0237                 mvaName = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2"),
0238                 mvaOpt = cms.string("DBoldDMwLTwGJ"),
0239                 verbosity = cms.int32(0)
0240             ))
0241 
0242             _byIsolationOldDMMVArun2017v2 = "rerunDiscriminationByIsolationOldDMMVArun2017v2"+self.postfix
0243             setattr(self.process,_byIsolationOldDMMVArun2017v2,patDiscriminationByIsolationMVArun2v1.clone(
0244                 PATTauProducer = self.originalTauName,
0245                 Prediscriminants = noPrediscriminants,
0246                 toMultiplex = _byIsolationOldDMMVArun2017v2raw,
0247                 loadMVAfromDB = cms.bool(True),
0248                 mvaOutput_normalization = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations
0249                 mapping = cms.VPSet(
0250                     cms.PSet(
0251                         category = cms.uint32(0),
0252                         cut = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2"), #writeTauIdDiscrWPs
0253                         variable = cms.string("pt"),
0254                     )
0255                 ),
0256                 workingPoints = cms.vstring(
0257                     "_WPEff95",
0258                     "_WPEff90",
0259                     "_WPEff80",
0260                     "_WPEff70",
0261                     "_WPEff60",
0262                     "_WPEff50",
0263                     "_WPEff40"
0264                 ),
0265                 verbosity = cms.int32(0)
0266             ))
0267 
0268             _rerunIsolationOldDMMVArun2017v2Task = cms.Task(
0269                 getattr(self.process,_byIsolationOldDMMVArun2017v2raw),
0270                 getattr(self.process,_byIsolationOldDMMVArun2017v2)
0271             )
0272             _rerunMvaIsolationTask.add(_rerunIsolationOldDMMVArun2017v2Task)
0273             _rerunMvaIsolationSequence += cms.Sequence(_rerunIsolationOldDMMVArun2017v2Task)
0274 
0275             tauIDSources.byIsolationMVArun2017v2DBoldDMwLTraw2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v2, "raw")
0276             tauIDSources.byVVLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v2, "_WPEff95")
0277             tauIDSources.byVLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v2, "_WPEff90")
0278             tauIDSources.byLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v2, "_WPEff80")
0279             tauIDSources.byMediumIsolationMVArun2017v2DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v2, "_WPEff70")
0280             tauIDSources.byTightIsolationMVArun2017v2DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v2, "_WPEff60")
0281             tauIDSources.byVTightIsolationMVArun2017v2DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v2, "_WPEff50")
0282             tauIDSources.byVVTightIsolationMVArun2017v2DBoldDMwLT2017 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2017v2, "_WPEff40")
0283 
0284         if "newDM2017v2" in self.toKeep:
0285             self.tauIdDiscrMVA_2017_version = "v2"
0286             self.tauIdDiscrMVA_trainings_run2_2017 = {
0287                 'tauIdMVAIsoDBnewDMwLT2017' : "tauIdMVAIsoDBnewDMwLT2017",
0288             }
0289             self.tauIdDiscrMVA_WPs_run2_2017 = {
0290                 'tauIdMVAIsoDBnewDMwLT2017' : {
0291                     'Eff95' : "DBnewDMwLTEff95",
0292                     'Eff90' : "DBnewDMwLTEff90",
0293                     'Eff80' : "DBnewDMwLTEff80",
0294                     'Eff70' : "DBnewDMwLTEff70",
0295                     'Eff60' : "DBnewDMwLTEff60",
0296                     'Eff50' : "DBnewDMwLTEff50",
0297                     'Eff40' : "DBnewDMwLTEff40"
0298                 }
0299             }
0300 
0301             if not self.is_above_cmssw_version(9, 4, 5, self.debug):
0302                 if self.debug: print ("runTauID: not is_above_cmssw_version(9, 4, 5). Will update the list of available in DB samples to access newDM2017v2")
0303                 self.loadMVA_WPs_run2_2017()
0304 
0305             _byIsolationNewDMMVArun2017v2raw = "rerunDiscriminationByIsolationNewDMMVArun2017v2raw"+self.postfix
0306             setattr(self.process,_byIsolationNewDMMVArun2017v2raw,patDiscriminationByIsolationMVArun2v1raw.clone(
0307                 PATTauProducer = self.originalTauName,
0308                 Prediscriminants = noPrediscriminants,
0309                 loadMVAfromDB = cms.bool(True),
0310                 mvaName = cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2"),
0311                 mvaOpt = cms.string("DBnewDMwLTwGJ"),
0312                 verbosity = cms.int32(0)
0313             ))
0314 
0315             _byIsolationNewDMMVArun2017v2 = "rerunDiscriminationByIsolationNewDMMVArun2017v2"+self.postfix
0316             setattr(self.process,_byIsolationNewDMMVArun2017v2,patDiscriminationByIsolationMVArun2v1.clone(
0317                 PATTauProducer = self.originalTauName,
0318                 Prediscriminants = noPrediscriminants,
0319                 toMultiplex = _byIsolationNewDMMVArun2017v2raw,
0320                 loadMVAfromDB = cms.bool(True),
0321                 mvaOutput_normalization = cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations
0322                 mapping = cms.VPSet(
0323                     cms.PSet(
0324                         category = cms.uint32(0),
0325                         cut = cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2"), #writeTauIdDiscrWPs
0326                         variable = cms.string("pt"),
0327                     )
0328                 ),
0329                 workingPoints = cms.vstring(
0330                     "_WPEff95",
0331                     "_WPEff90",
0332                     "_WPEff80",
0333                     "_WPEff70",
0334                     "_WPEff60",
0335                     "_WPEff50",
0336                     "_WPEff40"
0337                 ),
0338                 verbosity = cms.int32(0)
0339             ))
0340 
0341             _rerunIsolationNewDMMVArun2017v2Task = cms.Task(
0342                 getattr(self.process,_byIsolationNewDMMVArun2017v2raw),
0343                 getattr(self.process,_byIsolationNewDMMVArun2017v2)
0344             )
0345             _rerunMvaIsolationTask.add(_rerunIsolationNewDMMVArun2017v2Task)
0346             _rerunMvaIsolationSequence += cms.Sequence(_rerunIsolationNewDMMVArun2017v2Task)
0347 
0348             tauIDSources.byIsolationMVArun2017v2DBnewDMwLTraw2017 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2017v2, "raw")
0349             tauIDSources.byVVLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2017v2, "_WPEff95")
0350             tauIDSources.byVLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2017v2, "_WPEff90")
0351             tauIDSources.byLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2017v2, "_WPEff80")
0352             tauIDSources.byMediumIsolationMVArun2017v2DBnewDMwLT2017 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2017v2, "_WPEff70")
0353             tauIDSources.byTightIsolationMVArun2017v2DBnewDMwLT2017 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2017v2, "_WPEff60")
0354             tauIDSources.byVTightIsolationMVArun2017v2DBnewDMwLT2017 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2017v2, "_WPEff50")
0355             tauIDSources.byVVTightIsolationMVArun2017v2DBnewDMwLT2017 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2017v2, "_WPEff40")
0356 
0357         if "dR0p32017v2" in self.toKeep:
0358             self.tauIdDiscrMVA_2017_version = "v2"
0359             self.tauIdDiscrMVA_trainings_run2_2017 = {
0360                 'tauIdMVAIsoDBoldDMdR0p3wLT2017' : "tauIdMVAIsoDBoldDMdR0p3wLT2017",
0361             }
0362             self.tauIdDiscrMVA_WPs_run2_2017 = {
0363                 'tauIdMVAIsoDBoldDMdR0p3wLT2017' : {
0364                     'Eff95' : "DBoldDMdR0p3wLTEff95",
0365                     'Eff90' : "DBoldDMdR0p3wLTEff90",
0366                     'Eff80' : "DBoldDMdR0p3wLTEff80",
0367                     'Eff70' : "DBoldDMdR0p3wLTEff70",
0368                     'Eff60' : "DBoldDMdR0p3wLTEff60",
0369                     'Eff50' : "DBoldDMdR0p3wLTEff50",
0370                     'Eff40' : "DBoldDMdR0p3wLTEff40"
0371                 }
0372             }
0373 
0374             if not self.is_above_cmssw_version(9, 4, 5, self.debug):
0375                 if self.debug: print ("runTauID: not is_above_cmssw_version(9, 4, 5). Will update the list of available in DB samples to access dR0p32017v2")
0376                 self.loadMVA_WPs_run2_2017()
0377 
0378             _byIsolationOldDMdR0p3MVArun2017v2raw = "rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw"+self.postfix
0379             setattr(self.process,_byIsolationOldDMdR0p3MVArun2017v2raw,patDiscriminationByIsolationMVArun2v1raw.clone(
0380                 PATTauProducer = self.originalTauName,
0381                 Prediscriminants = noPrediscriminants,
0382                 loadMVAfromDB = cms.bool(True),
0383                 mvaName = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2"),
0384                 mvaOpt = cms.string("DBoldDMwLTwGJ"),
0385                 srcChargedIsoPtSum = cms.string('chargedIsoPtSumdR03'),
0386                 srcFootprintCorrection = cms.string('footprintCorrectiondR03'),
0387                 srcNeutralIsoPtSum = cms.string('neutralIsoPtSumdR03'),
0388                 srcPhotonPtSumOutsideSignalCone = cms.string('photonPtSumOutsideSignalConedR03'),
0389                 verbosity = cms.int32(0)
0390             ))
0391 
0392             _byIsolationOldDMdR0p3MVArun2017v2 = "rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2"+self.postfix
0393             setattr(self.process,_byIsolationOldDMdR0p3MVArun2017v2,patDiscriminationByIsolationMVArun2v1.clone(
0394                 PATTauProducer = self.originalTauName,
0395                 Prediscriminants = noPrediscriminants,
0396                 toMultiplex = _byIsolationOldDMdR0p3MVArun2017v2raw,
0397                 loadMVAfromDB = cms.bool(True),
0398                 mvaOutput_normalization = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations
0399                 mapping = cms.VPSet(
0400                     cms.PSet(
0401                         category = cms.uint32(0),
0402                         cut = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2"), #writeTauIdDiscrWPs
0403                         variable = cms.string("pt"),
0404                     )
0405                 ),
0406                 workingPoints = cms.vstring(
0407                     "_WPEff95",
0408                     "_WPEff90",
0409                     "_WPEff80",
0410                     "_WPEff70",
0411                     "_WPEff60",
0412                     "_WPEff50",
0413                     "_WPEff40"
0414                 ),
0415                 verbosity = cms.int32(0)
0416             ))
0417 
0418             _rerunIsolationOldDMdR0p3MVArun2017v2Task = cms.Task(
0419                 getattr(self.process,_byIsolationOldDMdR0p3MVArun2017v2raw),
0420                 getattr(self.process,_byIsolationOldDMdR0p3MVArun2017v2)
0421             )
0422             _rerunMvaIsolationTask.add(_rerunIsolationOldDMdR0p3MVArun2017v2Task)
0423             _rerunMvaIsolationSequence += cms.Sequence(_rerunIsolationOldDMdR0p3MVArun2017v2Task)
0424 
0425             tauIDSources.byIsolationMVArun2017v2DBoldDMdR0p3wLTraw2017 = self.tauIDMVAinputs(_byIsolationOldDMdR0p3MVArun2017v2, "raw")
0426             tauIDSources.byVVLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.tauIDMVAinputs(_byIsolationOldDMdR0p3MVArun2017v2, "_WPEff95")
0427             tauIDSources.byVLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.tauIDMVAinputs(_byIsolationOldDMdR0p3MVArun2017v2, "_WPEff90")
0428             tauIDSources.byLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.tauIDMVAinputs(_byIsolationOldDMdR0p3MVArun2017v2, "_WPEff80")
0429             tauIDSources.byMediumIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.tauIDMVAinputs(_byIsolationOldDMdR0p3MVArun2017v2, "_WPEff70")
0430             tauIDSources.byTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.tauIDMVAinputs(_byIsolationOldDMdR0p3MVArun2017v2, "_WPEff60")
0431             tauIDSources.byVTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.tauIDMVAinputs(_byIsolationOldDMdR0p3MVArun2017v2, "_WPEff50")
0432             tauIDSources.byVVTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.tauIDMVAinputs(_byIsolationOldDMdR0p3MVArun2017v2, "_WPEff40")
0433 
0434         # 2016 training strategy(v2) - essentially the same as 2017 training strategy (v1), trained on 2016MC, old DM - currently not implemented in the tau sequence of any release
0435         # self.process.rerunDiscriminationByIsolationOldDMMVArun2v2raw = patDiscriminationByIsolationMVArun2v1raw.clone(
0436         #     PATTauProducer = self.originalTauName,
0437         #     Prediscriminants = noPrediscriminants,
0438         #     loadMVAfromDB = cms.bool(True),
0439         #     mvaName = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2"),
0440         #     mvaOpt = cms.string("DBoldDMwLTwGJ"),
0441         #     verbosity = cms.int32(0)
0442         # )
0443         # #
0444         # self.process.rerunDiscriminationByIsolationOldDMMVArun2v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone(
0445         #     PATTauProducer = self.originalTauName,
0446         #     Prediscriminants = noPrediscriminants,
0447         #     toMultiplex = 'rerunDiscriminationByIsolationOldDMMVArun2v2raw',
0448         #     key = cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v2raw:category'),#?
0449         #     loadMVAfromDB = cms.bool(True),
0450         #     mvaOutput_normalization = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations
0451         #     mapping = cms.VPSet(
0452         #         cms.PSet(
0453         #             category = cms.uint32(0),
0454         #             cut = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2_WPEff90"), #writeTauIdDiscrWPs
0455         #             variable = cms.string("pt"),
0456         #         )
0457         #     )
0458         # )
0459 
0460         # 2016 training strategy(v1), trained on 2016MC, old DM
0461         if "2016v1" in self.toKeep:
0462             _byIsolationOldDMMVArun2016v1raw = "rerunDiscriminationByIsolationOldDMMVArun2v1raw"+self.postfix
0463             setattr(self.process,_byIsolationOldDMMVArun2016v1raw,patDiscriminationByIsolationMVArun2v1raw.clone(
0464                 PATTauProducer = self.originalTauName,
0465                 Prediscriminants = noPrediscriminants,
0466                 loadMVAfromDB = cms.bool(True),
0467                 mvaName = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1"),
0468                 mvaOpt = cms.string("DBoldDMwLT"),
0469                 verbosity = cms.int32(0)
0470             ))
0471 
0472             _byIsolationOldDMMVArun2016v1 = "rerunDiscriminationByIsolationOldDMMVArun2v1"+self.postfix
0473             setattr(self.process,_byIsolationOldDMMVArun2016v1,patDiscriminationByIsolationMVArun2v1.clone(
0474                     PATTauProducer = self.originalTauName,
0475                     Prediscriminants = noPrediscriminants,
0476                     toMultiplex = _byIsolationOldDMMVArun2016v1raw,
0477                     loadMVAfromDB = cms.bool(True),
0478                     mvaOutput_normalization = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_mvaOutput_normalization"),
0479                     mapping = cms.VPSet(
0480                         cms.PSet(
0481                             category = cms.uint32(0),
0482                             cut = cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1"),
0483                             variable = cms.string("pt"),
0484                         )
0485                     ),
0486                     workingPoints = cms.vstring(
0487                         "_WPEff90",
0488                         "_WPEff80",
0489                         "_WPEff70",
0490                         "_WPEff60",
0491                         "_WPEff50",
0492                         "_WPEff40"
0493                     )
0494                 ))
0495 
0496             _rerunIsolationOldDMMVArun2016v1Task = cms.Task(
0497                 getattr(self.process,_byIsolationOldDMMVArun2016v1raw),
0498                 getattr(self.process,_byIsolationOldDMMVArun2016v1)
0499             )
0500             _rerunMvaIsolationTask.add(_rerunIsolationOldDMMVArun2016v1Task)
0501             _rerunMvaIsolationSequence += cms.Sequence(_rerunIsolationOldDMMVArun2016v1Task)
0502 
0503             tauIDSources.byIsolationMVArun2v1DBoldDMwLTraw2016 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2016v1, "raw")
0504             tauIDSources.byVLooseIsolationMVArun2v1DBoldDMwLT2016 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2016v1, "_WPEff90")
0505             tauIDSources.byLooseIsolationMVArun2v1DBoldDMwLT2016 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2016v1, "_WPEff80")
0506             tauIDSources.byMediumIsolationMVArun2v1DBoldDMwLT2016 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2016v1, "_WPEff70")
0507             tauIDSources.byTightIsolationMVArun2v1DBoldDMwLT2016 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2016v1, "_WPEff60")
0508             tauIDSources.byVTightIsolationMVArun2v1DBoldDMwLT2016 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2016v1, "_WPEff50")
0509             tauIDSources.byVVTightIsolationMVArun2v1DBoldDMwLT2016 = self.tauIDMVAinputs(_byIsolationOldDMMVArun2016v1, "_WPEff40")
0510 
0511         # 2016 training strategy(v1), trained on 2016MC, new DM
0512         if "newDM2016v1" in self.toKeep:
0513             _byIsolationNewDMMVArun2016v1raw = "rerunDiscriminationByIsolationNewDMMVArun2v1raw"+self.postfix
0514             setattr(self.process,_byIsolationNewDMMVArun2016v1raw,patDiscriminationByIsolationMVArun2v1raw.clone(
0515                 PATTauProducer = self.originalTauName,
0516                 Prediscriminants = noPrediscriminants,
0517                 loadMVAfromDB = cms.bool(True),
0518                 mvaName = cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1"),
0519                 mvaOpt = cms.string("DBnewDMwLT"),
0520                 verbosity = cms.int32(0)
0521             ))
0522 
0523             _byIsolationNewDMMVArun2016v1 = "rerunDiscriminationByIsolationNewDMMVArun2v1"+self.postfix
0524             setattr(self.process,_byIsolationNewDMMVArun2016v1,patDiscriminationByIsolationMVArun2v1.clone(
0525                 PATTauProducer = self.originalTauName,
0526                 Prediscriminants = noPrediscriminants,
0527                 toMultiplex = _byIsolationNewDMMVArun2016v1raw,
0528                 loadMVAfromDB = cms.bool(True),
0529                 mvaOutput_normalization = cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_mvaOutput_normalization"),
0530                 mapping = cms.VPSet(
0531                     cms.PSet(
0532                         category = cms.uint32(0),
0533                         cut = cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff90"),
0534                         variable = cms.string("pt"),
0535                     )
0536                 ),
0537                 workingPoints = cms.vstring(
0538                     "_WPEff90",
0539                     "_WPEff80",
0540                     "_WPEff70",
0541                     "_WPEff60",
0542                     "_WPEff50",
0543                     "_WPEff40"
0544                 )
0545             ))
0546 
0547             _rerunIsolationNewDMMVArun2016v1Task = cms.Task(
0548                 getattr(self.process,_byIsolationNewDMMVArun2016v1raw),
0549                 getattr(self.process,_byIsolationNewDMMVArun2016v1)
0550             )
0551             _rerunMvaIsolationTask.add(_rerunIsolationNewDMMVArun2016v1Task)
0552             _rerunMvaIsolationSequence += cms.Sequence(_rerunIsolationNewDMMVArun2016v1Task)
0553 
0554             tauIDSources.byIsolationMVArun2v1DBnewDMwLTraw2016 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2016v1, "raw")
0555             tauIDSources.byVLooseIsolationMVArun2v1DBnewDMwLT2016 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2016v1, "_WPEff90")
0556             tauIDSources.byLooseIsolationMVArun2v1DBnewDMwLT2016 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2016v1, "_WPEff80")
0557             tauIDSources.byMediumIsolationMVArun2v1DBnewDMwLT2016 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2016v1, "_WPEff70")
0558             tauIDSources.byTightIsolationMVArun2v1DBnewDMwLT2016 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2016v1, "_WPEff60")
0559             tauIDSources.byVTightIsolationMVArun2v1DBnewDMwLT2016 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2016v1, "_WPEff50")
0560             tauIDSources.byVVTightIsolationMVArun2v1DBnewDMwLT2016 = self.tauIDMVAinputs(_byIsolationNewDMMVArun2016v1, "_WPEff40")
0561 
0562         if "deepTau2017v2" in self.toKeep:
0563             if self.debug: print ("Adding DeepTau v2 IDs")
0564 
0565             _deepTauName = "deepTau2017v2"
0566             workingPoints_ = WORKING_POINTS_v2p1
0567 
0568             file_names = [
0569                 'core:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v2p6_e6_core.pb',
0570                 'inner:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v2p6_e6_inner.pb',
0571                 'outer:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v2p6_e6_outer.pb',
0572             ]
0573             full_version = self.getDeepTauVersion(file_names[0])
0574             setattr(self.process,_deepTauName+self.postfix,DeepTau.clone(
0575                 Prediscriminants = noPrediscriminants,
0576                 taus             = self.originalTauName,
0577                 graph_file       = file_names,
0578                 year             = full_version[0],
0579                 version          = full_version[1],
0580                 sub_version      = 1 #MB: subversion cannot be properly deduced from file names; it should be 1 also for v2
0581             ))
0582 
0583             self.processDeepProducer(_deepTauName, tauIDSources, workingPoints_)
0584 
0585             _deepTauProducer = getattr(self.process,_deepTauName+self.postfix)
0586             _rerunMvaIsolationTask.add(_deepTauProducer)
0587             _rerunMvaIsolationSequence += _deepTauProducer
0588 
0589 
0590         if "deepTau2017v2p1" in self.toKeep:
0591             if self.debug: print ("Adding DeepTau v2p1 IDs")
0592 
0593             _deepTauName = "deepTau2017v2p1"
0594             workingPoints_ = WORKING_POINTS_v2p1
0595 
0596             file_names = [
0597                 'core:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v2p6_e6_core.pb',
0598                 'inner:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v2p6_e6_inner.pb',
0599                 'outer:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v2p6_e6_outer.pb',
0600             ]
0601             full_version = self.getDeepTauVersion(file_names[0])
0602             setattr(self.process,_deepTauName+self.postfix,DeepTau.clone(
0603                 Prediscriminants = noPrediscriminants,
0604                 taus             = self.originalTauName,
0605                 graph_file       = file_names,
0606                 year             = full_version[0],
0607                 version          = full_version[1],
0608                 sub_version      = 1, #MB: subversion cannot be properly deduced from file names
0609                 disable_dxy_pca  = True
0610             ))
0611 
0612             self.processDeepProducer(_deepTauName, tauIDSources, workingPoints_)
0613 
0614             _deepTauProducer = getattr(self.process,_deepTauName+self.postfix)
0615             _rerunMvaIsolationTask.add(_deepTauProducer)
0616             _rerunMvaIsolationSequence += _deepTauProducer
0617 
0618         if "deepTau2018v2p5" in self.toKeep:
0619             if self.debug: print ("Adding DeepTau v2p5 IDs")
0620 
0621             _deepTauName = "deepTau2018v2p5"
0622             workingPoints_ = WORKING_POINTS_v2p5
0623 
0624             file_names = [
0625                 'core:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2018v2p5_core.pb',
0626                 'inner:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2018v2p5_inner.pb',
0627                 'outer:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2018v2p5_outer.pb',
0628             ]
0629             full_version = self.getDeepTauVersion(file_names[0])
0630             setattr(self.process,_deepTauName+self.postfix,DeepTau.clone(
0631                 Prediscriminants                = noPrediscriminants,
0632                 taus                            = self.originalTauName,
0633                 graph_file                      = file_names,
0634                 year                            = full_version[0],
0635                 version                         = full_version[1],
0636                 sub_version                     = full_version[2],
0637                 disable_dxy_pca                 = True,
0638                 disable_hcalFraction_workaround = True,
0639                 disable_CellIndex_workaround    = True
0640             ))
0641 
0642             self.processDeepProducer(_deepTauName, tauIDSources, workingPoints_)
0643 
0644             _deepTauProducer = getattr(self.process,_deepTauName+self.postfix)
0645             _rerunMvaIsolationTask.add(_deepTauProducer)
0646             _rerunMvaIsolationSequence += _deepTauProducer
0647 
0648         if "deepTau2026v2p5" in self.toKeep:
0649             if self.debug: print ("Adding Phase2 DeepTau v2p5 IDs")
0650 
0651             _deepTauName = "deepTau2026v2p5"
0652             workingPoints_ = WORKING_POINTS_PHASEII_v2p5
0653 
0654             file_names = [
0655                 'core:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2026v2p5_core.pb',
0656                 'inner:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2026v2p5_inner.pb',
0657                 'outer:RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2026v2p5_outer.pb',
0658             ]
0659             full_version = self.getDeepTauVersion(file_names[0])
0660             setattr(self.process,_deepTauName+self.postfix,DeepTau.clone(
0661                 Prediscriminants                = noPrediscriminants,
0662                 taus                            = self.originalTauName,
0663                 graph_file                      = file_names,
0664                 year                            = full_version[0],
0665                 version                         = full_version[1],
0666                 sub_version                     = full_version[2],
0667                 disable_dxy_pca                 = True,
0668                 disable_hcalFraction_workaround = True,
0669                 disable_CellIndex_workaround    = True
0670             ))
0671 
0672             from RecoTauTag.RecoTau.mergedPhase2SlimmedElectronsForTauId_cff import mergedSlimmedElectronsForTauId
0673             if not hasattr(self.process,"mergedSlimmedElectronsForTauId"):
0674                 self.process.mergedSlimmedElectronsForTauId = mergedSlimmedElectronsForTauId
0675             setattr(getattr(self.process, _deepTauName+self.postfix), "electrons", cms.InputTag("mergedSlimmedElectronsForTauId"))
0676             setattr(getattr(self.process, _deepTauName+self.postfix), "vertices", cms.InputTag("offlineSlimmedPrimaryVertices4D"))
0677 
0678             self.processDeepProducer(_deepTauName, tauIDSources, workingPoints_)
0679 
0680             _deepTauProducer = getattr(self.process,_deepTauName+self.postfix)
0681             _rerunMvaIsolationTask.add(self.process.mergedSlimmedElectronsForTauId)
0682             _rerunMvaIsolationTask.add(_deepTauProducer)
0683             _rerunMvaIsolationSequence += self.process.mergedSlimmedElectronsForTauId
0684             _rerunMvaIsolationSequence += _deepTauProducer
0685 
0686         if "againstEle2018" in self.toKeep:
0687             antiElectronDiscrMVA6_version = "MVA6v3_noeveto"
0688             ### Define new anti-e discriminants
0689             ## Raw
0690             from RecoTauTag.RecoTau.patTauDiscriminationAgainstElectronMVA6_cfi import patTauDiscriminationAgainstElectronMVA6
0691             _byElectronRejectionMVA62018Raw = "patTauDiscriminationByElectronRejectionMVA62018Raw"+self.postfix
0692             setattr(self.process,_byElectronRejectionMVA62018Raw,patTauDiscriminationAgainstElectronMVA6.clone(
0693                 PATTauProducer = self.originalTauName,
0694                 Prediscriminants = noPrediscriminants, #already selected for MiniAOD
0695                 srcElectrons = cms.InputTag('slimmedElectrons'),
0696                 vetoEcalCracks = cms.bool(False), #keep taus in EB-EE cracks
0697                 mvaName_NoEleMatch_wGwoGSF_BL = 'RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_NoEleMatch_wGwoGSF_BL',
0698                 mvaName_NoEleMatch_wGwoGSF_EC = 'RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_NoEleMatch_wGwoGSF_EC',
0699                 mvaName_NoEleMatch_woGwoGSF_BL = 'RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_NoEleMatch_woGwoGSF_BL',
0700                 mvaName_NoEleMatch_woGwoGSF_EC = 'RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_NoEleMatch_woGwoGSF_EC',
0701                 mvaName_wGwGSF_BL = 'RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_wGwGSF_BL',
0702                 mvaName_wGwGSF_EC = 'RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_wGwGSF_EC',
0703                 mvaName_woGwGSF_BL = 'RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_woGwGSF_BL',
0704                 mvaName_woGwGSF_EC = 'RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_woGwGSF_EC'
0705             ))
0706             ## WPs
0707             from RecoTauTag.RecoTau.PATTauDiscriminantCutMultiplexer_cfi import patTauDiscriminantCutMultiplexer
0708             _byElectronRejectionMVA62018 = "patTauDiscriminationByElectronRejectionMVA62018"+self.postfix
0709             setattr(self.process,"patTauDiscriminationByElectronRejectionMVA62018"+self.postfix,patTauDiscriminantCutMultiplexer.clone(
0710                 PATTauProducer = self.originalTauName,
0711                 Prediscriminants = noPrediscriminants,
0712                 toMultiplex = _byElectronRejectionMVA62018Raw,
0713                 mapping = cms.VPSet(
0714                     cms.PSet(
0715                         category = cms.uint32(0),
0716                         cut = cms.string('RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_NoEleMatch_woGwoGSF_BL'),
0717                         variable = cms.string('pt')
0718                     ),
0719                     cms.PSet(
0720                         category = cms.uint32(2),
0721                         cut = cms.string('RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_NoEleMatch_wGwoGSF_BL'),
0722                         variable = cms.string('pt')
0723                     ),
0724                     cms.PSet(
0725                         category = cms.uint32(5),
0726                         cut = cms.string('RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_woGwGSF_BL'),
0727                         variable = cms.string('pt')
0728                     ),
0729                     cms.PSet(
0730                         category = cms.uint32(7),
0731                         cut = cms.string('RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_wGwGSF_BL'),
0732                         variable = cms.string('pt')
0733                     ),
0734                     cms.PSet(
0735                         category = cms.uint32(8),
0736                         cut = cms.string('RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_NoEleMatch_woGwoGSF_EC'),
0737                         variable = cms.string('pt')
0738                     ),
0739                     cms.PSet(
0740                         category = cms.uint32(10),
0741                         cut = cms.string('RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_NoEleMatch_wGwoGSF_EC'),
0742                         variable = cms.string('pt')
0743                     ),
0744                     cms.PSet(
0745                         category = cms.uint32(13),
0746                         cut = cms.string('RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_woGwGSF_EC'),
0747                         variable = cms.string('pt')
0748                     ),
0749                     cms.PSet(
0750                         category = cms.uint32(15),
0751                         cut = cms.string('RecoTauTag_antiElectron'+antiElectronDiscrMVA6_version+'_gbr_wGwGSF_EC'),
0752                         variable = cms.string('pt')
0753                     )
0754                 ),
0755                 workingPoints = cms.vstring(
0756                     "_WPeff98",
0757                     "_WPeff90",
0758                     "_WPeff80",
0759                     "_WPeff70",
0760                     "_WPeff60"
0761                 )
0762             ))
0763             ### Put all new anti-e discrminats to a sequence
0764             _patTauDiscriminationByElectronRejectionMVA62018Task = cms.Task(
0765                 getattr(self.process,_byElectronRejectionMVA62018Raw),
0766                 getattr(self.process,_byElectronRejectionMVA62018)
0767             )
0768             _rerunMvaIsolationTask.add(_patTauDiscriminationByElectronRejectionMVA62018Task)
0769             _rerunMvaIsolationSequence += cms.Sequence(_patTauDiscriminationByElectronRejectionMVA62018Task)
0770 
0771             _againstElectronTauIDSources = cms.PSet(
0772                 againstElectronMVA6Raw2018 = self.tauIDMVAinputs(_byElectronRejectionMVA62018, "raw"),
0773                 againstElectronMVA6category2018 = self.tauIDMVAinputs(_byElectronRejectionMVA62018, "category"),
0774                 againstElectronVLooseMVA62018 = self.tauIDMVAinputs(_byElectronRejectionMVA62018, "_WPeff98"),
0775                 againstElectronLooseMVA62018 = self.tauIDMVAinputs(_byElectronRejectionMVA62018, "_WPeff90"),
0776                 againstElectronMediumMVA62018 = self.tauIDMVAinputs(_byElectronRejectionMVA62018, "_WPeff80"),
0777                 againstElectronTightMVA62018 = self.tauIDMVAinputs(_byElectronRejectionMVA62018, "_WPeff70"),
0778                 againstElectronVTightMVA62018 = self.tauIDMVAinputs(_byElectronRejectionMVA62018, "_WPeff60")
0779             )
0780             _tauIDSourcesWithAgainistEle = cms.PSet(
0781                 tauIDSources.clone(),
0782                 _againstElectronTauIDSources
0783             )
0784             tauIDSources =_tauIDSourcesWithAgainistEle.clone()
0785 
0786         if "newDMPhase2v1" in self.toKeep:
0787             if self.debug: print ("Adding newDMPhase2v1 ID")
0788             def tauIDMVAinputs(module, wp):
0789                 return cms.PSet(inputTag = cms.InputTag(module), workingPointIndex = cms.int32(-1 if wp=="raw" else -2 if wp=="category" else getattr(self.process, module).workingPoints.index(wp)))
0790             _byIsolationNewDMMVAPhase2raw = "rerunDiscriminationByIsolationMVADBnewDMwLTPhase2raw"+self.postfix
0791             setattr(self.process,_byIsolationNewDMMVAPhase2raw,patDiscriminationByIsolationMVArun2v1raw.clone(
0792                 PATTauProducer = self.originalTauName,
0793                 Prediscriminants = noPrediscriminants,
0794                 loadMVAfromDB = True,
0795                 mvaName = 'RecoTauTag_tauIdMVAIsoPhase2',
0796                 mvaOpt = 'DBnewDMwLTwGJPhase2',
0797                 verbosity = 0
0798             ))
0799 
0800             _byIsolationNewDMMVAPhase2 = "rerunDiscriminationByIsolationMVADBnewDMwLTPhase2"+self.postfix
0801             setattr(self.process,_byIsolationNewDMMVAPhase2,patDiscriminationByIsolationMVArun2v1.clone(
0802                 PATTauProducer = self.originalTauName,
0803                 Prediscriminants = noPrediscriminants,
0804                 toMultiplex = _byIsolationNewDMMVAPhase2raw,
0805                 loadMVAfromDB = True,
0806                 mvaOutput_normalization = 'RecoTauTag_tauIdMVAIsoPhase2_mvaOutput_normalization',
0807                 mapping = cms.VPSet(
0808                     cms.PSet(
0809                         category = cms.uint32(0),
0810                         cut = cms.string("RecoTauTag_tauIdMVAIsoPhase2"),
0811                         variable = cms.string("pt"),
0812                     )
0813                 ),
0814                 workingPoints = cms.vstring(
0815                     "_VVLoose",
0816                     "_VLoose",
0817                     "_Loose",
0818                     "_Medium",
0819                     "_Tight",
0820                     "_VTight",
0821                     "_VVTight"
0822                 )
0823             ))
0824             _rerunIsolationMVADBnewDMwLTPhase2Task = cms.Task(
0825                 getattr(self.process,_byIsolationNewDMMVAPhase2raw),
0826                 getattr(self.process,_byIsolationNewDMMVAPhase2)
0827             )
0828             _rerunMvaIsolationTask.add(_rerunIsolationMVADBnewDMwLTPhase2Task)
0829             _rerunMvaIsolationSequence += cms.Sequence(_rerunIsolationMVADBnewDMwLTPhase2Task)
0830 
0831             tauIDSources.byIsolationMVADBnewDMwLTPhase2raw = tauIDMVAinputs(_byIsolationNewDMMVAPhase2, "raw")
0832             tauIDSources.byVVLooseIsolationMVADBnewDMwLTPhase2 = tauIDMVAinputs(_byIsolationNewDMMVAPhase2, "_VVLoose")
0833             tauIDSources.byVLooseIsolationMVADBnewDMwLTPhase2 = tauIDMVAinputs(_byIsolationNewDMMVAPhase2, "_VLoose")
0834             tauIDSources.byLooseIsolationMVADBnewDMwLTPhase2 = tauIDMVAinputs(_byIsolationNewDMMVAPhase2, "_Loose")
0835             tauIDSources.byMediumIsolationMVADBnewDMwLTPhase2 = tauIDMVAinputs(_byIsolationNewDMMVAPhase2, "_Medium")
0836             tauIDSources.byTightIsolationMVADBnewDMwLTPhase2 = tauIDMVAinputs(_byIsolationNewDMMVAPhase2, "_Tight")
0837             tauIDSources.byVTightIsolationMVADBnewDMwLTPhase2 = tauIDMVAinputs(_byIsolationNewDMMVAPhase2, "_VTight")
0838             tauIDSources.byVVTightIsolationMVADBnewDMwLTPhase2 = tauIDMVAinputs(_byIsolationNewDMMVAPhase2, "_VVTight")
0839 
0840         if "againstElePhase2v1" in self.toKeep:
0841             if self.debug: print ("Adding anti-e Phase2v1 ID")
0842             ### Define new anti-e discriminants for Phase2
0843             ## Raw
0844             from RecoTauTag.RecoTau.PATTauDiscriminationAgainstElectronMVA6Phase2_cff import patTauDiscriminationAgainstElectronMVA6Phase2Raw, patTauDiscriminationAgainstElectronMVA6Phase2, mergedSlimmedElectronsForTauId
0845             _byElectronRejectionMVA6Phase2v1Raw = "patTauDiscriminationByElectronRejectionMVA6Phase2v1Raw"+self.postfix
0846             setattr(self.process,_byElectronRejectionMVA6Phase2v1Raw,patTauDiscriminationAgainstElectronMVA6Phase2Raw.clone(
0847                 PATTauProducer = self.originalTauName,
0848                 Prediscriminants = noPrediscriminants #already selected for MiniAOD
0849             ))
0850             ## WPs
0851             _byElectronRejectionMVA6Phase2v1 = "patTauDiscriminationByElectronRejectionMVA6Phase2v1"+self.postfix
0852             setattr(self.process,_byElectronRejectionMVA6Phase2v1,patTauDiscriminationAgainstElectronMVA6Phase2.clone(
0853                 PATTauProducer = self.originalTauName,
0854                 Prediscriminants = noPrediscriminants,
0855                 toMultiplex = _byElectronRejectionMVA6Phase2v1Raw
0856             ))
0857             ### Put all new phase2 anti-e discrminats to a sequence
0858             if not hasattr(self.process,"mergedSlimmedElectronsForTauId"):
0859                 self.process.mergedSlimmedElectronsForTauId = mergedSlimmedElectronsForTauId
0860             _patTauDiscriminationByElectronRejectionMVA6Phase2v1Task = cms.Task(
0861                 self.process.mergedSlimmedElectronsForTauId,
0862                 getattr(self.process,_byElectronRejectionMVA6Phase2v1Raw),
0863                 getattr(self.process,_byElectronRejectionMVA6Phase2v1)
0864             )
0865             _rerunMvaIsolationTask.add(_patTauDiscriminationByElectronRejectionMVA6Phase2v1Task)
0866             _rerunMvaIsolationSequence += cms.Sequence(_patTauDiscriminationByElectronRejectionMVA6Phase2v1Task)
0867 
0868             _againstElectronTauIDPhase2v1Sources = cms.PSet(
0869                 againstElectronMVA6RawPhase2v1 = self.tauIDMVAinputs(_byElectronRejectionMVA6Phase2v1, "raw"),
0870                 againstElectronMVA6categoryPhase2v1 = self.tauIDMVAinputs(_byElectronRejectionMVA6Phase2v1, "category"),
0871                 againstElectronVLooseMVA6Phase2v1 = self.tauIDMVAinputs(_byElectronRejectionMVA6Phase2v1, "_VLoose"),
0872                 againstElectronLooseMVA6Phase2v1 = self.tauIDMVAinputs(_byElectronRejectionMVA6Phase2v1, "_Loose"),
0873                 againstElectronMediumMVA6Phase2v1 = self.tauIDMVAinputs(_byElectronRejectionMVA6Phase2v1, "_Medium"),
0874                 againstElectronTightMVA6Phase2v1 = self.tauIDMVAinputs(_byElectronRejectionMVA6Phase2v1, "_Tight"),
0875                 againstElectronVTightMVA6Phase2v1 = self.tauIDMVAinputs(_byElectronRejectionMVA6Phase2v1, "_VTight")
0876             )
0877             _tauIDSourcesWithAgainistElePhase2v1 = cms.PSet(
0878                 tauIDSources.clone(),
0879                 _againstElectronTauIDPhase2v1Sources
0880             )
0881             tauIDSources =_tauIDSourcesWithAgainistElePhase2v1.clone()
0882         ##
0883         if self.debug: print('Embedding new TauIDs into \"'+self.updatedTauName+'\"')
0884         if not hasattr(self.process, self.updatedTauName):
0885             embedID = cms.EDProducer("PATTauIDEmbedder",
0886                src = cms.InputTag(self.originalTauName),
0887                tauIDSources = tauIDSources
0888             )
0889             setattr(self.process, self.updatedTauName, embedID)
0890         else: #assume same type
0891             tauIDSources = cms.PSet(
0892                 getattr(self.process, self.updatedTauName).tauIDSources,
0893                 tauIDSources)
0894             getattr(self.process, self.updatedTauName).tauIDSources = tauIDSources
0895         if not hasattr(self.process,"rerunMvaIsolationTask"+self.postfix):
0896             setattr(self.process,"rerunMvaIsolationTask"+self.postfix,_rerunMvaIsolationTask)
0897         else:
0898             _updatedRerunMvaIsolationTask = getattr(self.process,"rerunMvaIsolationTask"+self.postfix)
0899             _updatedRerunMvaIsolationTask.add(_rerunMvaIsolationTask)
0900             setattr(self.process,"rerunMvaIsolationTask"+self.postfix,_updatedRerunMvaIsolationTask)
0901         if not hasattr(self.process,"rerunMvaIsolationSequence"+self.postfix):
0902             setattr(self.process,"rerunMvaIsolationSequence"+self.postfix,_rerunMvaIsolationSequence)
0903         else:
0904             _updatedRerunMvaIsolationSequence = getattr(self.process,"rerunMvaIsolationSequence"+self.postfix)
0905             _updatedRerunMvaIsolationSequence += _rerunMvaIsolationSequence
0906             setattr(self.process,"rerunMvaIsolationSequence"+self.postfix,_updatedRerunMvaIsolationSequence)
0907 
0908 
0909     def processDeepProducer(self, producer_name, tauIDSources, workingPoints_):
0910         for target,points in workingPoints_.items():
0911             setattr(tauIDSources, 'by{}VS{}raw'.format(producer_name[0].upper()+producer_name[1:], target),
0912                         cms.PSet(inputTag = cms.InputTag(producer_name+self.postfix, 'VS{}'.format(target)), workingPointIndex = cms.int32(-1)))
0913             
0914             cut_expressions = []
0915             for index, (point,cut) in enumerate(points.items()):
0916                 cut_expressions.append(str(cut))
0917 
0918                 setattr(tauIDSources, 'by{}{}VS{}'.format(point, producer_name[0].upper()+producer_name[1:], target),
0919                         cms.PSet(inputTag = cms.InputTag(producer_name+self.postfix, 'VS{}'.format(target)), workingPointIndex = cms.int32(index)))
0920             if len(cut_expressions) > 0:
0921                 setattr(getattr(self.process, producer_name+self.postfix), 'VS{}WP'.format(target), cms.vstring(*cut_expressions))
0922 
0923 
0924     def getDeepTauVersion(self, file_name):
0925         """returns the DeepTau year, version, subversion. File name should contain a version label with data takig year \
0926         (2011-2, 2015-8), version number (vX) and subversion (pX), e.g. 2017v0p6, in general the following format: \
0927         {year}v{version}p{subversion}"""
0928         version_search = re.search('(20[1,2][125678])v([0-9]+)(p[0-9]+|)[\._]', file_name)
0929         if not version_search:
0930             raise RuntimeError('File "{}" has an invalid name pattern, should be in the format "{year}v{version}p{subversion}". \
0931                                 Unable to extract version number.'.format(file_name))
0932         year = version_search.group(1)
0933         version = version_search.group(2)
0934         subversion = version_search.group(3)
0935         if len(subversion) > 0:
0936             subversion = subversion[1:]
0937         else:
0938             subversion = 0
0939         return int(year), int(version), int(subversion)