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.gInterpreter.Declare('#include "RecoEgamma/ElectronIdentification/interface/ElectronMVAEstimatorRun2.h"')
44  ROOT.gSystem.Load("libRecoEgammaElectronIdentification")
45  categoryCutStrings = ROOT.vector(ROOT.string)()
46  for x in self.categoryCuts :
47  categoryCutStrings.push_back(x)
48  self.estimator = ROOT.ElectronMVAEstimatorRun2(
49  self.tag, self.name, len(self.xmls),
50  self.variablesFile, categoryCutStrings, self.xmls, self._debug)
51  self._init = True
52  category = ctypes.c_int(0)
53  mva = self.estimator.mvaValue(ele, rho[0], category)
54  return mva, category.value
55 
56 
58  '''Working Points. Keeps track of the cuts associated to a given flavour of the MVA ID
59  for each working point and allows to test the working points'''
60 
61  def __init__(self, name, tag, working_points, logistic_transform=False):
62  self.name = name
63  self.tag = tag
64  self.working_points = self._reformat_cut_definitions(working_points)
65  self.logistic_transform = logistic_transform
66 
67  def _reformat_cut_definitions(self, working_points):
68  new_definitions = dict()
69  for wpname, definitions in working_points.items():
70  new_definitions[wpname] = dict()
71  for name, cut in definitions.cuts.items():
72  categ_id = int(name.lstrip('cutCategory'))
73  cut = cut.replace('pt','x')
74  formula = ROOT.TFormula('_'.join([self.name, wpname, name]), cut)
75  new_definitions[wpname][categ_id] = formula
76  return new_definitions
77 
78  def passed(self, ele, mva, category, wp):
79  '''return true if ele passes wp'''
80  threshold = self.working_points[wp][category].Eval(ele.pt())
81  if self.logistic_transform:
82  mva = 2.0/(1.0+exp(-2.0*mva))-1
83  return mva > threshold
84 
85 
86 # Import information needed to construct the e/gamma MVAs
87 
89  import EleMVA_6CategoriesCuts, mvaVariablesFile, mvaVariablesFileRun3, mvaVariablesFileRun3NonIso, EleMVA_3CategoriesCuts
90 
91 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_iso_V2_cff \
92  import mvaWeightFiles as Fall17_iso_V2_weightFiles
93 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_noIso_V2_cff \
94  import mvaWeightFiles as Fall17_noIso_V2_weightFiles
95 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_RunIIIWinter22_iso_V1_cff \
96  import mvaWeightFiles as RunIIIWinter22_iso_V1_weightFiles
97 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_RunIIIWinter22_noIso_V1_cff \
98  import mvaWeightFiles as RunIIIWinter22_noIso_V1_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 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Winter22_HZZ_V1_cff \
104  import mvaWeightFiles as mvaWinter22HZZWeightFiles_V1
105 
106 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring16_GeneralPurpose_V1_cff \
107  import workingPoints as mvaSpring16GP_V1_workingPoints
108 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring16_HZZ_V1_cff \
109  import workingPoints as mvaSpring16HZZ_V1_workingPoints
110 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_iso_V2_cff \
111  import workingPoints as Fall17_iso_V2_workingPoints
112 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Fall17_noIso_V2_cff \
113  import workingPoints as Fall17_noIso_V2_workingPoints
114 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_RunIIIWinter22_iso_V1_cff \
115  import workingPoints as RunIIIWinter22_iso_V1_workingPoints
116 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_RunIIIWinter22_noIso_V1_cff \
117  import workingPoints as RunIIIWinter22_noIso_V1_workingPoints
118 from RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Winter22_HZZ_V1_cff \
119  import workingPoints as Winter22_HZZ_V1_workingPoints
120 
121 # Dictionary with the relecant e/gmma MVAs
122 
123 electron_mvas = {
124  "RunIIIWinter22IsoV1" : ElectronMVAID("ElectronMVAEstimatorRun2","RunIIIWinter22IsoV1",
125  EleMVA_6CategoriesCuts, RunIIIWinter22_iso_V1_weightFiles, mvaVariablesFileRun3),
126  "RunIIIWinter22NoIsoV1" : ElectronMVAID("ElectronMVAEstimatorRun2","RunIIIWinter22NoIsoV1",
127  EleMVA_6CategoriesCuts, RunIIIWinter22_noIso_V1_weightFiles, mvaVariablesFileRun3NonIso),
128  "Fall17IsoV2" : ElectronMVAID("ElectronMVAEstimatorRun2","Fall17IsoV2",
129  EleMVA_6CategoriesCuts, Fall17_iso_V2_weightFiles, mvaVariablesFile),
130  "Fall17NoIsoV2" : ElectronMVAID("ElectronMVAEstimatorRun2","Fall17NoIsoV2",
131  EleMVA_6CategoriesCuts, Fall17_noIso_V2_weightFiles, mvaVariablesFile),
132  "Spring16HZZV1" : ElectronMVAID("ElectronMVAEstimatorRun2","Spring16HZZV1",
133  EleMVA_6CategoriesCuts, mvaSpring16HZZWeightFiles_V1, mvaVariablesFile),
134  "Spring16GPV1" : ElectronMVAID("ElectronMVAEstimatorRun2","Spring16GeneralPurposeV1",
135  EleMVA_3CategoriesCuts, mvaSpring16GPWeightFiles_V1, mvaVariablesFile),
136  "Winter22HZZV1" : ElectronMVAID("ElectronMVAEstimatorRun2","Winter22HZZV1",
137  EleMVA_6CategoriesCuts, mvaWinter22HZZWeightFiles_V1, mvaVariablesFileRun3),
138  }
139 
140 working_points = {
141  "RunIIIWinter22IsoV1" : WorkingPoints("ElectronMVAEstimatorRun2","RunIIIWinter22IsoV1",
142  RunIIIWinter22_iso_V1_workingPoints),
143  "RunIIIWinter22NoIsoV1" : WorkingPoints("ElectronMVAEstimatorRun2","RunIIIWinter22NoIsoV1",
144  RunIIIWinter22_noIso_V1_workingPoints),
145  "Fall17IsoV2" : WorkingPoints("ElectronMVAEstimatorRun2","Fall17IsoV2",
146  Fall17_iso_V2_workingPoints),
147  "Fall17NoIsoV2" : WorkingPoints("ElectronMVAEstimatorRun2","Fall17NoIsoV2",
148  Fall17_noIso_V2_workingPoints),
149  "Spring16HZZV1" : WorkingPoints("ElectronMVAEstimatorRun2","Spring16HZZV1",
150  mvaSpring16HZZ_V1_workingPoints, logistic_transform=True),
151  "Spring16GPV1" : WorkingPoints("ElectronMVAEstimatorRun2","Spring16GeneralPurposeV1",
152  mvaSpring16GP_V1_workingPoints, logistic_transform=True),
153  "Winter22HZZV1" : WorkingPoints("ElectronMVAEstimatorRun2","Winter22HZZV1",
154  Winter22_HZZ_V1_workingPoints),
155 
156  }
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def __call__(self, ele, rho, debug=False)
Definition: FWLite.py:23
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def _reformat_cut_definitions(self, working_points)
Definition: FWLite.py:67
def __init__(self, name, tag, categoryCuts, xmls, variablesFile, debug=False)
Definition: FWLite.py:13
deadvectors [0] push_back({0.0175431, 0.538005, 6.80997, 13.29})
def __init__(self, name, tag, working_points, logistic_transform=False)
Definition: FWLite.py:61
def passed(self, ele, mva, category, wp)
Definition: FWLite.py:78