Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-31 22:26:07

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