File indexing completed on 2023-03-17 11:17:01
0001 import re
0002 import FWCore.ParameterSet.Config as cms
0003
0004 training_vars = {
0005 'muonMultiplicity': {'default': -1, 'type': 'i'},
0006 'trackPPar': {'default': -1, 'max_idx': 3, 'type': 'f'},
0007 'flightDistance3dSig': {'default': -1, 'max_idx': 1, 'type': 'f'},
0008 'trackSip2dVal': {'default': -1, 'max_idx': 3, 'type': 'f'},
0009 'vertexBoostOverSqrtJetPt': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
0010 'trackEtaRel': {'default': -1, 'max_idx': 3, 'type': 'f'},
0011 'vertexMass': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
0012 'trackDecayLenVal': {'default': -0.1, 'max_idx': 3, 'type': 'f'},
0013 'trackJetPt': {'default': -1, 'type': 'f'},
0014 'neutralHadronMultiplicity': {'default': -1, 'type': 'i'},
0015 'flightDistance3dVal': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
0016 'trackJetDist': {'default': -0.1, 'max_idx': 3, 'type': 'f'},
0017 'leptonSip3d': {'default': -10000, 'max_idx': 3, 'type': 'f'},
0018 'neutralHadronEnergyFraction': {'default': -0.1, 'type': 'f'},
0019 'trackPtRatio': {'default': -0.1, 'max_idx': 3, 'type': 'f'},
0020 'hadronMultiplicity': {'default': -1, 'type': 'i'},
0021 'trackSumJetEtRatio': {'default': -0.1, 'type': 'f'},
0022 'vertexJetDeltaR': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
0023 'leptonRatioRel': {'default': -1, 'max_idx': 3, 'type': 'f'},
0024 'chargedHadronMultiplicity': {'default': -1, 'type': 'i'},
0025 'jetNTracks': {'default': -0.1, 'type': 'i'},
0026 'trackDeltaR': {'default': -0.1, 'max_idx': 3, 'type': 'f'},
0027 'vertexFitProb': {'default': -1, 'max_idx': 1, 'type': 'f'},
0028 'trackSip3dValAboveCharm': {'default': -1, 'max_idx': 1, 'type': 'f'},
0029 'jetEta': {'default': -3, 'type': 'f'},
0030 'leptonDeltaR': {'default': -1, 'max_idx': 3, 'type': 'f'},
0031 'hadronPhotonMultiplicity': {'default': -1, 'type': 'i'},
0032 'leptonPtRel': {'default': -1, 'max_idx': 3, 'type': 'f'},
0033 'flightDistance2dVal': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
0034 'trackSumJetDeltaR': {'default': -0.1, 'type': 'f'},
0035 'photonMultiplicity': {'default': -1, 'type': 'i'},
0036 'chargedHadronEnergyFraction': {'default': -0.1, 'type': 'f'},
0037 'trackSip3dSigAboveQuarterCharm': {'default': -999, 'max_idx': 1, 'type': 'f'},
0038 'vertexLeptonCategory': {'default': -1, 'type': 'i'},
0039 'massVertexEnergyFraction': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
0040 'trackSip2dSig': {'default': -100, 'max_idx': 3, 'type': 'f'},
0041 'flightDistance2dSig': {'default': -1, 'max_idx': 1, 'type': 'f'},
0042 'jetPt': {'default': -1, 'type': 'f'},
0043 'totalMultiplicity': {'default': -1, 'type': 'i'},
0044 'trackSip2dValAboveCharm': {'default': -1, 'max_idx': 1, 'type': 'f'},
0045 'electronEnergyFraction': {'default': -0.1, 'type': 'f'},
0046 'jetNSecondaryVertices': {'default': 0, 'type': 'i'},
0047 'trackSip2dSigAboveCharm': {'default': -999, 'max_idx': 1, 'type': 'f'},
0048 'vertexCategory': {'default': -1, 'type': 'i'},
0049 'vertexEnergyRatio': {'default': -10, 'max_idx': 1, 'type': 'f'},
0050 'photonEnergyFraction': {'default': -0.1, 'type': 'f'},
0051 'flavour': {'default': -1, 'type': 'i'},
0052 'muonEnergyFraction': {'default': -0.1, 'type': 'f'},
0053 'vertexNTracks': {'default': 0, 'max_idx': 1, 'type': 'i'},
0054 'trackSip2dSigAboveQuarterCharm': {'default': -999, 'max_idx': 1, 'type': 'f'},
0055 'trackSip3dVal': {'default': -1, 'max_idx': 3, 'type': 'f'},
0056 'leptonRatio': {'default': -1, 'max_idx': 3, 'type': 'f'},
0057 'trackPtRel': {'default': -1, 'max_idx': 3, 'type': 'f'},
0058 'leptonEtaRel': {'default': -1, 'max_idx': 3, 'type': 'f'},
0059 'trackPParRatio': {'default': 1.1, 'max_idx': 3, 'type': 'f'},
0060 'trackSip3dSig': {'default': -100, 'max_idx': 3, 'type': 'f'},
0061 'trackSip3dSigAboveCharm': {'default': -999, 'max_idx': 1, 'type': 'f'},
0062 'electronMultiplicity': {'default': -1, 'type': 'i'}
0063 }
0064
0065
0066
0067
0068
0069 varname_regex_=re.compile(r'(?P<name>[a-zA-Z0-9]+)(:?_(?P<idx>\d+))?$')
0070 def var_match(varname):
0071 '''matches the name used in the MVA training to
0072 get the TaggingVariableName and index'''
0073 match = varname_regex_.match(varname)
0074 if not match:
0075 raise ValueError(
0076 'Variable name {0} does not match '
0077 'the default regular expression'.format(varname)
0078 )
0079 return match
0080
0081 def get_var_name(varname):
0082 'returns the TaggingVariableName of a MVA Name'
0083 match = var_match(varname)
0084 name = match.group('name')
0085 if name not in training_vars:
0086 raise ValueError(
0087 'Variable name {0}, matched to name {1}, '
0088 'is not among the known trainig variables.'.format(
0089 varname, name)
0090 )
0091 return name
0092
0093 def get_var_default(varname):
0094 'returns the default value used in the traing'
0095 name = get_var_name(varname)
0096 return training_vars[name]['default']
0097
0098 def get_var_idx(varname):
0099 'returns the index in case of vectorial TaggingVariableName'
0100 match = var_match(varname)
0101 idx = match.group('idx')
0102 return int(idx) if idx else None
0103
0104 def get_var_pset(mvaname):
0105 'returns the cms.PSet to be used by CharmTaggerESProducer'
0106 pset = cms.PSet(
0107 name = cms.string(mvaname),
0108 taggingVarName = cms.string(get_var_name(mvaname)),
0109 default = cms.double(get_var_default(mvaname))
0110 )
0111 idx = get_var_idx(mvaname)
0112 if idx is not None:
0113 pset.idx = cms.int32(idx)
0114 return pset
0115
0116 if __name__ == '__main__':
0117 assert(varname_regex_.match('leptonEtaRel_10').groupdict() == {'name': 'leptonEtaRel', 'idx': '10'})
0118 assert(varname_regex_.match('leptonEtaRel_1').groupdict() == {'name': 'leptonEtaRel', 'idx': '1'})
0119 assert(varname_regex_.match('leptonEtaRel').groupdict() == {'name': 'leptonEtaRel', 'idx': None})
0120 assert(varname_regex_.match('lepton_EtaRel') == None)
0121
0122 assert(get_var_default('leptonEtaRel_10') == training_vars['leptonEtaRel']['default'])
0123 assert(get_var_default('electronMultiplicity') == training_vars['electronMultiplicity']['default'])
0124 assert(get_var_idx('leptonEtaRel_10') == 10)
0125 assert(get_var_idx('leptonEtaRel_3') == 3)
0126 assert(get_var_idx('FOOBAR') == None)
0127