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, convs, beam_spot, 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  event.getByLabel(('reducedEgamma:reducedConversions'), conv_handle)
36  event.getByLabel(('offlineBeamSpot'), bs_handle)
37 
38  electrons = ele_handle.product()
39  convs = conv_handle.product()
40  beam_spot = bs_handle.product()
41  rho = rho_handle.product()
42 
43  mva, category = electron_mva_id(electron[0], convs, beam_spot, rho)
44  '''
45  if not self._init:
46  print('Initializing ' + self.name + self.tag)
47  ROOT.gSystem.Load("libRecoEgammaElectronIdentification")
48  categoryCutStrings = ROOT.vector(ROOT.string)()
49  for x in self.categoryCuts :
50  categoryCutStrings.push_back(x)
51  self.estimator = ROOT.ElectronMVAEstimatorRun2(
52  self.tag, self.name, len(self.xmls),
53  self.variablesFile, categoryCutStrings, self.xmls, self._debug)
54  self._init = True
55  extra_vars = self.estimator.getExtraVars(ele, convs, beam_spot, rho[0])
56  category = ctypes.c_int(0)
57  mva = self.estimator.mvaValue(ele, extra_vars, category)
58  return mva, category.value
59 
60 
62  '''Working Points. Keeps track of the cuts associated to a given flavour of the MVA ID
63  for each working point and allows to test the working points'''
64 
65  def __init__(self, name, tag, working_points, logistic_transform=False):
66  self.name = name
67  self.tag = tag
68  self.working_points = self._reformat_cut_definitions(working_points)
69  self.logistic_transform = logistic_transform
70 
71  def _reformat_cut_definitions(self, working_points):
72  new_definitions = dict()
73  for wpname, definitions in working_points.iteritems():
74  new_definitions[wpname] = dict()
75  for name, cut in definitions.cuts.iteritems():
76  categ_id = int(name.lstrip('cutCategory'))
77  cut = cut.replace('pt','x')
78  formula = ROOT.TFormula('_'.join([self.name, wpname, name]), cut)
79  new_definitions[wpname][categ_id] = formula
80  return new_definitions
81 
82  def passed(self, ele, mva, category, wp):
83  '''return true if ele passes wp'''
84  threshold = self.working_points[wp][category].Eval(ele.pt())
85  if self.logistic_transform:
86  mva = 2.0/(1.0+exp(-2.0*mva))-1
87  return mva > threshold
88 
89 
90 # Import information needed to construct the e/gamma MVAs
91 
93  import EleMVA_6CategoriesCuts, mvaVariablesFile, EleMVA_3CategoriesCuts
94 
95 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_iso_V2_cff \
96  import mvaWeightFiles as Fall17_iso_V2_weightFiles
97 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_noIso_V2_cff \
98  import mvaWeightFiles as Fall17_noIso_V2_weightFiles
99 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring16_GeneralPurpose_V1_cff \
100  import mvaSpring16WeightFiles_V1 as mvaSpring16GPWeightFiles_V1
101 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring16_HZZ_V1_cff \
102  import mvaSpring16WeightFiles_V1 as mvaSpring16HZZWeightFiles_V1
103 
104 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring16_GeneralPurpose_V1_cff \
105  import workingPoints as mvaSpring16GP_V1_workingPoints
106 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring16_HZZ_V1_cff \
107  import workingPoints as mvaSpring16HZZ_V1_workingPoints
108 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_iso_V2_cff \
109  import workingPoints as Fall17_iso_V2_workingPoints
110 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_noIso_V2_cff \
111  import workingPoints as Fall17_noIso_V2_workingPoints
112 
113 # Dictionary with the relecant e/gmma MVAs
114 
115 electron_mvas = {
116  "Fall17IsoV2" : ElectronMVAID("ElectronMVAEstimatorRun2","Fall17IsoV2",
117  EleMVA_6CategoriesCuts, Fall17_iso_V2_weightFiles, mvaVariablesFile),
118  "Fall17NoIsoV2" : ElectronMVAID("ElectronMVAEstimatorRun2","Fall17NoIsoV2",
119  EleMVA_6CategoriesCuts, Fall17_noIso_V2_weightFiles, mvaVariablesFile),
120  "Spring16HZZV1" : ElectronMVAID("ElectronMVAEstimatorRun2","Spring16HZZV1",
121  EleMVA_6CategoriesCuts, mvaSpring16HZZWeightFiles_V1, mvaVariablesFile),
122  "Spring16GPV1" : ElectronMVAID("ElectronMVAEstimatorRun2","Spring16GeneralPurposeV1",
123  EleMVA_3CategoriesCuts, mvaSpring16GPWeightFiles_V1, mvaVariablesFile),
124  }
125 
126 working_points = {
127  "Fall17IsoV2" : WorkingPoints("ElectronMVAEstimatorRun2","Fall17IsoV2",
128  Fall17_iso_V2_workingPoints),
129  "Fall17NoIsoV2" : WorkingPoints("ElectronMVAEstimatorRun2","Fall17NoIsoV2",
130  Fall17_noIso_V2_workingPoints),
131  "Spring16HZZV1" : WorkingPoints("ElectronMVAEstimatorRun2","Spring16HZZV1",
132  mvaSpring16HZZ_V1_workingPoints, logistic_transform=True),
133  "Spring16GPV1" : WorkingPoints("ElectronMVAEstimatorRun2","Spring16GeneralPurposeV1",
134  mvaSpring16GP_V1_workingPoints, logistic_transform=True),
135 
136  }
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def __call__(self, ele, convs, beam_spot, rho, debug=False)
Definition: FWLite.py:23
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def _reformat_cut_definitions(self, working_points)
Definition: FWLite.py:71
def __init__(self, name, tag, categoryCuts, xmls, variablesFile, debug=False)
Definition: FWLite.py:13
def __init__(self, name, tag, working_points, logistic_transform=False)
Definition: FWLite.py:65
def passed(self, ele, mva, category, wp)
Definition: FWLite.py:82