CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
trainingvars.py
Go to the documentation of this file.
1 import re
2 import FWCore.ParameterSet.Config as cms
3 
4 training_vars = {
5  'muonMultiplicity': {'default': -1, 'type': 'i'},
6  'trackPPar': {'default': -1, 'max_idx': 3, 'type': 'f'},
7  'flightDistance3dSig': {'default': -1, 'max_idx': 1, 'type': 'f'},
8  'trackSip2dVal': {'default': -1, 'max_idx': 3, 'type': 'f'},
9  'vertexBoostOverSqrtJetPt': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
10  'trackEtaRel': {'default': -1, 'max_idx': 3, 'type': 'f'},
11  'vertexMass': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
12  'trackDecayLenVal': {'default': -0.1, 'max_idx': 3, 'type': 'f'},
13  'trackJetPt': {'default': -1, 'type': 'f'},
14  'neutralHadronMultiplicity': {'default': -1, 'type': 'i'},
15  'flightDistance3dVal': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
16  'trackJetDist': {'default': -0.1, 'max_idx': 3, 'type': 'f'},
17  'leptonSip3d': {'default': -10000, 'max_idx': 3, 'type': 'f'},
18  'neutralHadronEnergyFraction': {'default': -0.1, 'type': 'f'},
19  'trackPtRatio': {'default': -0.1, 'max_idx': 3, 'type': 'f'},
20  'hadronMultiplicity': {'default': -1, 'type': 'i'},
21  'trackSumJetEtRatio': {'default': -0.1, 'type': 'f'},
22  'vertexJetDeltaR': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
23  'leptonRatioRel': {'default': -1, 'max_idx': 3, 'type': 'f'},
24  'chargedHadronMultiplicity': {'default': -1, 'type': 'i'},
25  'jetNTracks': {'default': -0.1, 'type': 'i'},
26  'trackDeltaR': {'default': -0.1, 'max_idx': 3, 'type': 'f'},
27  'vertexFitProb': {'default': -1, 'max_idx': 1, 'type': 'f'},
28  'trackSip3dValAboveCharm': {'default': -1, 'max_idx': 1, 'type': 'f'},
29  'jetEta': {'default': -3, 'type': 'f'},
30  'leptonDeltaR': {'default': -1, 'max_idx': 3, 'type': 'f'},
31  'hadronPhotonMultiplicity': {'default': -1, 'type': 'i'},
32  'leptonPtRel': {'default': -1, 'max_idx': 3, 'type': 'f'},
33  'flightDistance2dVal': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
34  'trackSumJetDeltaR': {'default': -0.1, 'type': 'f'},
35  'photonMultiplicity': {'default': -1, 'type': 'i'},
36  'chargedHadronEnergyFraction': {'default': -0.1, 'type': 'f'},
37  'trackSip3dSigAboveQuarterCharm': {'default': -999, 'max_idx': 1, 'type': 'f'},
38  'vertexLeptonCategory': {'default': -1, 'type': 'i'},
39  'massVertexEnergyFraction': {'default': -0.1, 'max_idx': 1, 'type': 'f'},
40  'trackSip2dSig': {'default': -100, 'max_idx': 3, 'type': 'f'},
41  'flightDistance2dSig': {'default': -1, 'max_idx': 1, 'type': 'f'},
42  'jetPt': {'default': -1, 'type': 'f'},
43  'totalMultiplicity': {'default': -1, 'type': 'i'},
44  'trackSip2dValAboveCharm': {'default': -1, 'max_idx': 1, 'type': 'f'},
45  'electronEnergyFraction': {'default': -0.1, 'type': 'f'},
46  'jetNSecondaryVertices': {'default': 0, 'type': 'i'},
47  'trackSip2dSigAboveCharm': {'default': -999, 'max_idx': 1, 'type': 'f'},
48  'vertexCategory': {'default': -1, 'type': 'i'},
49  'vertexEnergyRatio': {'default': -10, 'max_idx': 1, 'type': 'f'},
50  'photonEnergyFraction': {'default': -0.1, 'type': 'f'},
51  'flavour': {'default': -1, 'type': 'i'},
52  'muonEnergyFraction': {'default': -0.1, 'type': 'f'},
53  'vertexNTracks': {'default': 0, 'max_idx': 1, 'type': 'i'},
54  'trackSip2dSigAboveQuarterCharm': {'default': -999, 'max_idx': 1, 'type': 'f'},
55  'trackSip3dVal': {'default': -1, 'max_idx': 3, 'type': 'f'},
56  'leptonRatio': {'default': -1, 'max_idx': 3, 'type': 'f'},
57  'trackPtRel': {'default': -1, 'max_idx': 3, 'type': 'f'},
58  'leptonEtaRel': {'default': -1, 'max_idx': 3, 'type': 'f'},
59  'trackPParRatio': {'default': 1.1, 'max_idx': 3, 'type': 'f'},
60  'trackSip3dSig': {'default': -100, 'max_idx': 3, 'type': 'f'},
61  'trackSip3dSigAboveCharm': {'default': -999, 'max_idx': 1, 'type': 'f'},
62  'electronMultiplicity': {'default': -1, 'type': 'i'}
63 }
64 
65 #
66 # This could be a python class, but given it only used to convert the previous dict
67 # to CMSSW format I think is overkill
68 #
69 varname_regex_=re.compile(r'(?P<name>[a-zA-Z0-9]+)(:?_(?P<idx>\d+))?$')
70 def var_match(varname):
71  '''matches the name used in the MVA training to
72  get the TaggingVariableName and index'''
73  match = varname_regex_.match(varname)
74  if not match:
75  raise ValueError(
76  'Variable name {0} does not match '
77  'the default regular expression'.format(varname)
78  )
79  return match
80 
81 def get_var_name(varname):
82  'returns the TaggingVariableName of a MVA Name'
83  match = var_match(varname)
84  name = match.group('name')
85  if name not in training_vars:
86  raise ValueError(
87  'Variable name {0}, matched to name {1}, '
88  'is not among the known trainig variables.'.format(
89  varname, name)
90  )
91  return name
92 
93 def get_var_default(varname):
94  'returns the default value used in the traing'
95  name = get_var_name(varname)
96  return training_vars[name]['default']
97 
98 def get_var_idx(varname):
99  'returns the index in case of vectorial TaggingVariableName'
100  match = var_match(varname)
101  idx = match.group('idx')
102  return int(idx) if idx else None
103 
104 def get_var_pset(mvaname):
105  'returns the cms.PSet to be used by CharmTaggerESProducer'
106  pset = cms.PSet(
107  name = cms.string(mvaname),
108  taggingVarName = cms.string(get_var_name(mvaname)),
109  default = cms.double(get_var_default(mvaname))
110  )
111  idx = get_var_idx(mvaname)
112  if idx is not None:
113  pset.idx = cms.int32(idx)
114  return pset
115 
116 if __name__ == '__main__':
117  assert(varname_regex_.match('leptonEtaRel_10').groupdict() == {'name': 'leptonEtaRel', 'idx': '10'})
118  assert(varname_regex_.match('leptonEtaRel_1').groupdict() == {'name': 'leptonEtaRel', 'idx': '1'})
119  assert(varname_regex_.match('leptonEtaRel').groupdict() == {'name': 'leptonEtaRel', 'idx': None})
120  assert(varname_regex_.match('lepton_EtaRel') == None)
121 
122  assert(get_var_default('leptonEtaRel_10') == training_vars['leptonEtaRel']['default'])
123  assert(get_var_default('electronMultiplicity') == training_vars['electronMultiplicity']['default'])
124  assert(get_var_idx('leptonEtaRel_10') == 10)
125  assert(get_var_idx('leptonEtaRel_3') == 3)
126  assert(get_var_idx('FOOBAR') == None)
127 
assert(m_qm.get())
def get_var_default
Definition: trainingvars.py:93