CMS 3D CMS Logo

FWLite.py
Go to the documentation of this file.
1 import ROOT
2 import ctypes
3 import pprint
4 from numpy import exp
5 
6 # Python wrappers around the Electron MVAs.
7 # Usage example in RecoEgamma/ElectronIdentification/test
8 
10  """ Electron MVA wrapper class.
11  """
12 
13  def __init__(self, name, tag, categoryCuts, xmls, variablesFile, debug=False):
14  self.name = name
15  self.tag = tag
16  self.categoryCuts = categoryCuts
17  self.variablesFile = variablesFile
18  self.xmls = ROOT.vector(ROOT.string)()
19  for x in xmls: self.xmls.push_back(x)
20  self._init = False
21  self._debug = debug
22 
23  def __call__(self, ele, rho, debug=False):
24  '''returns a tuple mva_value, category
25  ele: a reco::GsfElectron
26  convs: conversions
27  beam_spot: beam spot
28  rho: energy density in the event
29  debug: enable debugging mode.
30 
31  example:
32 
33  event.getByLabel(('slimmedElectrons'), ele_handle)
34  event.getByLabel(('fixedGridRhoFastjetAll'), rho_handle)
35 
36  electrons = ele_handle.product()
37  rho = rho_handle.product()
38 
39  mva, category = electron_mva_id(electron[0], rho)
40  '''
41  if not self._init:
42  print('Initializing ' + self.name + self.tag)
43  ROOT.gSystem.Load("libRecoEgammaElectronIdentification")
44  categoryCutStrings = ROOT.vector(ROOT.string)()
45  for x in self.categoryCuts :
46  categoryCutStrings.push_back(x)
47  self.estimator = ROOT.ElectronMVAEstimatorRun2(
48  self.tag, self.name, len(self.xmls),
49  self.variablesFile, categoryCutStrings, self.xmls, self._debug)
50  self._init = True
51  category = ctypes.c_int(0)
52  mva = self.estimator.mvaValue(ele, rho[0], category)
53  return mva, category.value
54 
55 
57  '''Working Points. Keeps track of the cuts associated to a given flavour of the MVA ID
58  for each working point and allows to test the working points'''
59 
60  def __init__(self, name, tag, working_points, logistic_transform=False):
61  self.name = name
62  self.tag = tag
63  self.working_points = self._reformat_cut_definitions(working_points)
64  self.logistic_transform = logistic_transform
65 
66  def _reformat_cut_definitions(self, working_points):
67  new_definitions = dict()
68  for wpname, definitions in working_points.iteritems():
69  new_definitions[wpname] = dict()
70  for name, cut in definitions.cuts.iteritems():
71  categ_id = int(name.lstrip('cutCategory'))
72  cut = cut.replace('pt','x')
73  formula = ROOT.TFormula('_'.join([self.name, wpname, name]), cut)
74  new_definitions[wpname][categ_id] = formula
75  return new_definitions
76 
77  def passed(self, ele, mva, category, wp):
78  '''return true if ele passes wp'''
79  threshold = self.working_points[wp][category].Eval(ele.pt())
80  if self.logistic_transform:
81  mva = 2.0/(1.0+exp(-2.0*mva))-1
82  return mva > threshold
83 
84 
85 # Import information needed to construct the e/gamma MVAs
86 
88  import EleMVA_6CategoriesCuts, mvaVariablesFile, EleMVA_3CategoriesCuts
89 
90 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_iso_V2_cff \
91  import mvaWeightFiles as Fall17_iso_V2_weightFiles
92 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_noIso_V2_cff \
93  import mvaWeightFiles as Fall17_noIso_V2_weightFiles
94 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring16_GeneralPurpose_V1_cff \
95  import mvaSpring16WeightFiles_V1 as mvaSpring16GPWeightFiles_V1
96 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring16_HZZ_V1_cff \
97  import mvaSpring16WeightFiles_V1 as mvaSpring16HZZWeightFiles_V1
98 
99 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring16_GeneralPurpose_V1_cff \
100  import workingPoints as mvaSpring16GP_V1_workingPoints
101 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring16_HZZ_V1_cff \
102  import workingPoints as mvaSpring16HZZ_V1_workingPoints
103 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_iso_V2_cff \
104  import workingPoints as Fall17_iso_V2_workingPoints
105 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_noIso_V2_cff \
106  import workingPoints as Fall17_noIso_V2_workingPoints
107 
108 # Dictionary with the relecant e/gmma MVAs
109 
110 electron_mvas = {
111  "Fall17IsoV2" : ElectronMVAID("ElectronMVAEstimatorRun2","Fall17IsoV2",
112  EleMVA_6CategoriesCuts, Fall17_iso_V2_weightFiles, mvaVariablesFile),
113  "Fall17NoIsoV2" : ElectronMVAID("ElectronMVAEstimatorRun2","Fall17NoIsoV2",
114  EleMVA_6CategoriesCuts, Fall17_noIso_V2_weightFiles, mvaVariablesFile),
115  "Spring16HZZV1" : ElectronMVAID("ElectronMVAEstimatorRun2","Spring16HZZV1",
116  EleMVA_6CategoriesCuts, mvaSpring16HZZWeightFiles_V1, mvaVariablesFile),
117  "Spring16GPV1" : ElectronMVAID("ElectronMVAEstimatorRun2","Spring16GeneralPurposeV1",
118  EleMVA_3CategoriesCuts, mvaSpring16GPWeightFiles_V1, mvaVariablesFile),
119  }
120 
121 working_points = {
122  "Fall17IsoV2" : WorkingPoints("ElectronMVAEstimatorRun2","Fall17IsoV2",
123  Fall17_iso_V2_workingPoints),
124  "Fall17NoIsoV2" : WorkingPoints("ElectronMVAEstimatorRun2","Fall17NoIsoV2",
125  Fall17_noIso_V2_workingPoints),
126  "Spring16HZZV1" : WorkingPoints("ElectronMVAEstimatorRun2","Spring16HZZV1",
127  mvaSpring16HZZ_V1_workingPoints, logistic_transform=True),
128  "Spring16GPV1" : WorkingPoints("ElectronMVAEstimatorRun2","Spring16GeneralPurposeV1",
129  mvaSpring16GP_V1_workingPoints, logistic_transform=True),
130 
131  }
FWLite.WorkingPoints.passed
def passed(self, ele, mva, category, wp)
Definition: FWLite.py:77
mvaElectronID_tools
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
FWLite.ElectronMVAID.tag
tag
Definition: FWLite.py:15
FWLite.WorkingPoints
Definition: FWLite.py:56
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
FWLite.ElectronMVAID.estimator
estimator
Definition: FWLite.py:47
FWLite.WorkingPoints.name
name
Definition: FWLite.py:61
FWLite.ElectronMVAID
Definition: FWLite.py:9
FWLite.WorkingPoints.working_points
working_points
Definition: FWLite.py:63
FWLite.WorkingPoints.logistic_transform
logistic_transform
Definition: FWLite.py:64
FWLite.ElectronMVAID.categoryCuts
categoryCuts
Definition: FWLite.py:16
FWLite.ElectronMVAID.__call__
def __call__(self, ele, rho, debug=False)
Definition: FWLite.py:23
FWLite.ElectronMVAID.name
name
Definition: FWLite.py:14
FWLite.ElectronMVAID._init
_init
Definition: FWLite.py:20
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
createfilelist.int
int
Definition: createfilelist.py:10
FWLite.WorkingPoints._reformat_cut_definitions
def _reformat_cut_definitions(self, working_points)
Definition: FWLite.py:66
FWLite.ElectronMVAID._debug
_debug
Definition: FWLite.py:21
FWLite.WorkingPoints.tag
tag
Definition: FWLite.py:62
FWLite.ElectronMVAID.__init__
def __init__(self, name, tag, categoryCuts, xmls, variablesFile, debug=False)
Definition: FWLite.py:13
FWLite.ElectronMVAID.xmls
xmls
Definition: FWLite.py:18
FWLite.WorkingPoints.__init__
def __init__(self, name, tag, working_points, logistic_transform=False)
Definition: FWLite.py:60
FWLite.ElectronMVAID.variablesFile
variablesFile
Definition: FWLite.py:17
JetChargeProducer_cfi.exp
exp
Definition: JetChargeProducer_cfi.py:6