ATLAS Offline Software
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
PunchThroughG4Classifier Class Reference

#include <PunchThroughG4Classifier.h>

Inheritance diagram for PunchThroughG4Classifier:
Collaboration diagram for PunchThroughG4Classifier:

Public Member Functions

 PunchThroughG4Classifier (const std::string &, const std::string &, const IInterface *)
 Constructor. More...
 
virtual ~PunchThroughG4Classifier ()=default
 Destructor. More...
 
virtual StatusCode initialize () override
 AlgTool initialize method. More...
 
virtual StatusCode finalize () override
 AlgTool finalize method. More...
 
StatusCode initializeScaler (const std::string &scalerConfigFile)
 input variable MinMaxScaler initialize method More...
 
StatusCode initializeNetwork (const std::string &networkConfigFile)
 neural network initialize method More...
 
StatusCode initializeCalibrator (const std::string &calibratorConfigFile)
 isotonic regressor calibrator initialize method More...
 
virtual double computePunchThroughProbability (const G4FastTrack &fastTrack, const double simE, const std::vector< double > &simEfrac) const override
 interface method to return probability prediction of punch through More...
 
std::map< std::string, std::map< std::string, double > > scaleInputs (std::map< std::string, std::map< std::string, double > > &inputs) const
 scale NN inputs using MinMaxScaler More...
 
double calibrateOutput (double &networkOutput) const
 calibrate NN output using isotonic regressor More...
 

Static Public Member Functions

static std::map< std::string, std::map< std::string, double > > computeInputs (const G4FastTrack &fastTrack, const double simE, const std::vector< double > &simEfrac)
 calcalate NN inputs based on G4FastTrack and simulstate More...
 

Private Attributes

std::unique_ptr< lwt::LightweightGraph > m_graph {}
 NN graph. More...
 
double m_scalerMin {}
 input variable MinMaxScaler members More...
 
double m_scalerMax {}
 
std::map< std::string, double > m_scalerMinMap
 
std::map< std::string, double > m_scalerMaxMap
 
std::string m_calibratorConfigFile
 isotonic regressor calibrator members More...
 
double m_calibrationMin {}
 
double m_calibrationMax {}
 
std::map< double, double > m_calibrationMap
 
StringProperty m_scalerConfigFileName {this, "ScalerConfigFileName", "", ""}
 
StringProperty m_networkConfigFileName {this, "NetworkConfigFileName", "", ""}
 
StringProperty m_calibratorConfigFileName {this, "CalibratorConfigFileName", "", ""}
 

Detailed Description

Definition at line 22 of file PunchThroughG4Classifier.h.

Constructor & Destructor Documentation

◆ PunchThroughG4Classifier()

PunchThroughG4Classifier::PunchThroughG4Classifier ( const std::string &  type,
const std::string &  name,
const IInterface *  parent 
)

Constructor.

Definition at line 30 of file PunchThroughG4Classifier.cxx.

31  : base_class(type, name, parent) {
32 }

◆ ~PunchThroughG4Classifier()

virtual PunchThroughG4Classifier::~PunchThroughG4Classifier ( )
virtualdefault

Destructor.

Member Function Documentation

◆ calibrateOutput()

double PunchThroughG4Classifier::calibrateOutput ( double &  networkOutput) const

calibrate NN output using isotonic regressor

Definition at line 234 of file PunchThroughG4Classifier.cxx.

234  {
235 
236  //calibrate output of network using isotonic regressor model
237 
238  //if network output is outside of the range of isotonic regressor then return min and max values
239  if (networkOutput < m_calibrationMin){
240  return m_calibrationMin;
241  }
242  else if (networkOutput > m_calibrationMax){
243  return m_calibrationMax;
244  }
245 
246  //otherwise find neighbouring points in isotonic regressor
247  auto upper = m_calibrationMap.upper_bound(networkOutput);
248  auto lower = upper--;
249 
250  //Perform linear interpolation between points
251  double m = (upper->second - lower->second)/(upper->first - lower->first);
252  double c = lower->second - m * lower->first;
253  double calibrated = m * networkOutput + c;
254 
255  return calibrated;
256 }

◆ computeInputs()

std::map< std::string, std::map< std::string, double > > PunchThroughG4Classifier::computeInputs ( const G4FastTrack &  fastTrack,
const double  simE,
const std::vector< double > &  simEfrac 
)
static

calcalate NN inputs based on G4FastTrack and simulstate

Definition at line 193 of file PunchThroughG4Classifier.cxx.

193  {
194 
195  //calculate inputs for NN
196 
197  std::map<std::string, std::map<std::string, double> > networkInputs;
198 
199  //add initial particle and total energy variables
200  networkInputs["node_0"] = {
201  {"variable_0", fastTrack.GetPrimaryTrack()->GetMomentum().mag() },
202  {"variable_1", std::abs(fastTrack.GetPrimaryTrack()->GetPosition().eta()) },
203  {"variable_2", fastTrack.GetPrimaryTrack()->GetPosition().phi() },
204  {"variable_3", simE},
205  };
206 
207  //add energy fraction variables
208  for (unsigned int i = 0; i < simEfrac.size(); i++) { //from 0 to 23, 24 layers
209  networkInputs["node_0"].insert({"variable_" + std::to_string(i + 4), simEfrac[i]});
210  }
211 
212  return networkInputs;
213 }

◆ computePunchThroughProbability()

double PunchThroughG4Classifier::computePunchThroughProbability ( const G4FastTrack &  fastTrack,
const double  simE,
const std::vector< double > &  simEfrac 
) const
overridevirtual

interface method to return probability prediction of punch through

Definition at line 180 of file PunchThroughG4Classifier.cxx.

180  {
181 
182  std::map<std::string, std::map<std::string, double> > networkInputs = computeInputs(fastTrack, simE, simEfrac); //compute inputs
183 
184  networkInputs = scaleInputs(networkInputs); //scale inputs
185 
186  std::map<std::string, double> networkOutputs = m_graph->compute(networkInputs); //call neural network on inputs
187 
188  double calibratedOutput = calibrateOutput(networkOutputs["out_0"]); //calibrate neural network output
189 
190  return calibratedOutput;
191 }

◆ finalize()

StatusCode PunchThroughG4Classifier::finalize ( )
overridevirtual

AlgTool finalize method.

Definition at line 50 of file PunchThroughG4Classifier.cxx.

50  {
51 
52  ATH_MSG_DEBUG( "[punchthroughclassifier] finalize() successful" );
53 
54  return StatusCode::SUCCESS;
55 }

◆ initialize()

StatusCode PunchThroughG4Classifier::initialize ( )
overridevirtual

AlgTool initialize method.

Definition at line 34 of file PunchThroughG4Classifier.cxx.

34  {
35 
36  ATH_MSG_DEBUG( "[ punchthroughclassifier ] Initializing PunchThroughG4Classifier" );
37 
38  std::string resolvedScalerFileName = PathResolverFindCalibFile (m_scalerConfigFileName);
39  ATH_CHECK ( initializeScaler(resolvedScalerFileName) );
40 
41  std::string resolvedNetworkFileName = PathResolverFindCalibFile (m_networkConfigFileName);
42  ATH_CHECK ( initializeNetwork(resolvedNetworkFileName) );
43 
44  std::string resolvedCalibratorFileName = PathResolverFindCalibFile (m_calibratorConfigFileName);
45  ATH_CHECK ( initializeCalibrator(resolvedCalibratorFileName) );
46 
47  return StatusCode::SUCCESS;
48 }

◆ initializeCalibrator()

StatusCode PunchThroughG4Classifier::initializeCalibrator ( const std::string &  calibratorConfigFile)

isotonic regressor calibrator initialize method

Definition at line 131 of file PunchThroughG4Classifier.cxx.

131  {
132  // Initialize pointers
133  xmlDocPtr doc;
134  xmlChar* xmlBuff = nullptr;
135 
136  //parse xml that contains config for isotonic regressor used to calibrate the network output
137  ATH_MSG_DEBUG( "[ punchthroughclassifier ] Loading calibrator: " << calibratorConfigFile);
138 
139  doc = xmlParseFile( calibratorConfigFile.c_str() );
140 
141  for( xmlNodePtr nodeRoot = doc->children; nodeRoot != nullptr; nodeRoot = nodeRoot->next) {
142 
143  if (xmlStrEqual( nodeRoot->name, BAD_CAST "Transformations" )) {
144  for( xmlNodePtr nodeTransform = nodeRoot->children; nodeTransform != nullptr; nodeTransform = nodeTransform->next ) {
145 
146  //get lower and upper bounds of isotonic regressor
147  if (xmlStrEqual( nodeTransform->name, BAD_CAST "LimitValues" )) {
148  if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST "min")) != nullptr) {
149  m_calibrationMin = atof(reinterpret_cast<const char*>(xmlBuff));
150  }
151  if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST "max")) != nullptr) {
152  m_calibrationMax = atof(reinterpret_cast<const char*>(xmlBuff));
153  }
154  }
155 
156  //get defined points where isotonic regressor knows transform
157  if (xmlStrEqual( nodeTransform->name, BAD_CAST "LinearNorm" )) {
158  double orig = -1;
159  double norm = -1;
160  if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST "orig")) != nullptr) {
161  orig = atof(reinterpret_cast<const char*>(xmlBuff));
162  }
163  if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST "norm")) != nullptr) {
164  norm = atof(reinterpret_cast<const char*>(xmlBuff));
165  }
166 
167  // Insert into maps
168  m_calibrationMap.insert ( std::pair<double,double>(orig, norm) );
169  }
170  }
171  }
172  }
173 
174  // free memory when done
175  xmlFreeDoc(doc);
176 
177  return StatusCode::SUCCESS;
178 }

◆ initializeNetwork()

StatusCode PunchThroughG4Classifier::initializeNetwork ( const std::string &  networkConfigFile)

neural network initialize method

Definition at line 111 of file PunchThroughG4Classifier.cxx.

111  {
112 
113  ATH_MSG_DEBUG( "[ punchthroughclassifier ] Loading classifier: " << networkConfigFile);
114 
115  std::ifstream input(networkConfigFile);
116  if(!input){
117  ATH_MSG_ERROR("Could not find json file " << networkConfigFile );
118  return StatusCode::FAILURE;
119  }
120 
121  m_graph = std::make_unique<lwt::LightweightGraph>(lwt::parse_json_graph(input));
122  if(!m_graph){
123  ATH_MSG_ERROR("Could not parse graph json file " << networkConfigFile );
124  return StatusCode::FAILURE;
125  }
126 
127 
128  return StatusCode::SUCCESS;
129 }

◆ initializeScaler()

StatusCode PunchThroughG4Classifier::initializeScaler ( const std::string &  scalerConfigFile)

input variable MinMaxScaler initialize method

Definition at line 57 of file PunchThroughG4Classifier.cxx.

57  {
58  // Initialize pointers
59  xmlDocPtr doc;
60  xmlChar* xmlBuff = nullptr;
61 
62  // Parse xml that contains config for MinMaxScaler for each of the network inputs
63  doc = xmlParseFile( scalerConfigFile.c_str() );
64 
65  ATH_MSG_DEBUG( "[ punchthroughclassifier ] Loading scaler: " << scalerConfigFile);
66 
67  for( xmlNodePtr nodeRoot = doc->children; nodeRoot != nullptr; nodeRoot = nodeRoot->next) {
68 
69  if (xmlStrEqual( nodeRoot->name, BAD_CAST "Transformations" )) {
70  for( xmlNodePtr nodeTransform = nodeRoot->children; nodeTransform != nullptr; nodeTransform = nodeTransform->next ) {
71 
72  //Get min and max values that we normalise values to
73  if (xmlStrEqual( nodeTransform->name, BAD_CAST "ScalerValues" )) {
74  if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST "min")) != nullptr) {
75  m_scalerMin = atof(reinterpret_cast<const char*>(xmlBuff));
76  }
77  if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST "max")) != nullptr) {
78  m_scalerMax = atof(reinterpret_cast<const char*>(xmlBuff));
79  }
80  }
81 
82  //Get values necessary to normalise each input variable
83  if (xmlStrEqual( nodeTransform->name, BAD_CAST "VarScales" )) {
84  std::string name = "";
85  double min=-1, max=-1;
86 
87  if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST "name")) != nullptr) {
88  name = reinterpret_cast<const char*>(xmlBuff);
89  }
90  if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST "min")) != nullptr) {
91  min = atof(reinterpret_cast<const char*>(xmlBuff));
92  }
93  if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST "max")) != nullptr) {
94  max = atof(reinterpret_cast<const char*>(xmlBuff));
95  }
96 
97  // Insert into maps
98  m_scalerMinMap.insert ( std::pair<std::string, double>(name, min) );
99  m_scalerMaxMap.insert ( std::pair<std::string, double>(name, max) );
100  }
101  }
102  }
103  }
104 
105  // free memory when done
106  xmlFreeDoc(doc);
107 
108  return StatusCode::SUCCESS;
109 }

◆ scaleInputs()

std::map< std::string, std::map< std::string, double > > PunchThroughG4Classifier::scaleInputs ( std::map< std::string, std::map< std::string, double > > &  inputs) const

scale NN inputs using MinMaxScaler

Definition at line 215 of file PunchThroughG4Classifier.cxx.

215  {
216 
217  //apply MinMaxScaler to network inputs
218 
219  for (auto& var : inputs["node_0"]) {
220 
221  double x_std;
222  if(m_scalerMaxMap.at(var.first) != m_scalerMinMap.at(var.first)){
223  x_std = (var.second - m_scalerMinMap.at(var.first)) / (m_scalerMaxMap.at(var.first) - m_scalerMinMap.at(var.first));
224  }
225  else{
226  x_std = (var.second - m_scalerMinMap.at(var.first));
227  }
228  var.second = x_std * (m_scalerMax - m_scalerMin) + m_scalerMin;
229  }
230 
231  return inputs;
232 }

Member Data Documentation

◆ m_calibrationMap

std::map<double, double> PunchThroughG4Classifier::m_calibrationMap
private

Definition at line 71 of file PunchThroughG4Classifier.h.

◆ m_calibrationMax

double PunchThroughG4Classifier::m_calibrationMax {}
private

Definition at line 70 of file PunchThroughG4Classifier.h.

◆ m_calibrationMin

double PunchThroughG4Classifier::m_calibrationMin {}
private

Definition at line 69 of file PunchThroughG4Classifier.h.

◆ m_calibratorConfigFile

std::string PunchThroughG4Classifier::m_calibratorConfigFile
private

isotonic regressor calibrator members

Definition at line 68 of file PunchThroughG4Classifier.h.

◆ m_calibratorConfigFileName

StringProperty PunchThroughG4Classifier::m_calibratorConfigFileName {this, "CalibratorConfigFileName", "", ""}
private

Definition at line 78 of file PunchThroughG4Classifier.h.

◆ m_graph

std::unique_ptr<lwt::LightweightGraph> PunchThroughG4Classifier::m_graph {}
private

NN graph.

Definition at line 59 of file PunchThroughG4Classifier.h.

◆ m_networkConfigFileName

StringProperty PunchThroughG4Classifier::m_networkConfigFileName {this, "NetworkConfigFileName", "", ""}
private

Definition at line 77 of file PunchThroughG4Classifier.h.

◆ m_scalerConfigFileName

StringProperty PunchThroughG4Classifier::m_scalerConfigFileName {this, "ScalerConfigFileName", "", ""}
private

Definition at line 76 of file PunchThroughG4Classifier.h.

◆ m_scalerMax

double PunchThroughG4Classifier::m_scalerMax {}
private

Definition at line 63 of file PunchThroughG4Classifier.h.

◆ m_scalerMaxMap

std::map<std::string, double> PunchThroughG4Classifier::m_scalerMaxMap
private

Definition at line 65 of file PunchThroughG4Classifier.h.

◆ m_scalerMin

double PunchThroughG4Classifier::m_scalerMin {}
private

input variable MinMaxScaler members

Definition at line 62 of file PunchThroughG4Classifier.h.

◆ m_scalerMinMap

std::map<std::string, double> PunchThroughG4Classifier::m_scalerMinMap
private

Definition at line 64 of file PunchThroughG4Classifier.h.


The documentation for this class was generated from the following files:
xmlChar
unsigned char xmlChar
Definition: TGoodRunsListWriter.h:28
PunchThroughG4Classifier::m_calibrationMin
double m_calibrationMin
Definition: PunchThroughG4Classifier.h:69
beamspotnt.var
var
Definition: bin/beamspotnt.py:1393
PlotCalibFromCool.norm
norm
Definition: PlotCalibFromCool.py:100
FlavorTagInference::SaltModelGraphConfig::parse_json_graph
GraphConfig parse_json_graph(const nlohmann::json &metadata)
Definition: SaltModelGraphConfig.cxx:40
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
upper
int upper(int c)
Definition: LArBadChannelParser.cxx:49
PunchThroughG4Classifier::m_graph
std::unique_ptr< lwt::LightweightGraph > m_graph
NN graph.
Definition: PunchThroughG4Classifier.h:59
PunchThroughG4Classifier::calibrateOutput
double calibrateOutput(double &networkOutput) const
calibrate NN output using isotonic regressor
Definition: PunchThroughG4Classifier.cxx:234
postInclude.inputs
inputs
Definition: postInclude.SortInput.py:15
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
PunchThroughG4Classifier::m_scalerConfigFileName
StringProperty m_scalerConfigFileName
Definition: PunchThroughG4Classifier.h:76
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
PunchThroughG4Classifier::computeInputs
static std::map< std::string, std::map< std::string, double > > computeInputs(const G4FastTrack &fastTrack, const double simE, const std::vector< double > &simEfrac)
calcalate NN inputs based on G4FastTrack and simulstate
Definition: PunchThroughG4Classifier.cxx:193
PunchThroughG4Classifier::m_scalerMax
double m_scalerMax
Definition: PunchThroughG4Classifier.h:63
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
test_pyathena.parent
parent
Definition: test_pyathena.py:15
PunchThroughG4Classifier::initializeCalibrator
StatusCode initializeCalibrator(const std::string &calibratorConfigFile)
isotonic regressor calibrator initialize method
Definition: PunchThroughG4Classifier.cxx:131
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
PunchThroughG4Classifier::m_calibrationMap
std::map< double, double > m_calibrationMap
Definition: PunchThroughG4Classifier.h:71
CxxUtils::atof
double atof(std::string_view str)
Converts a string into a double / float.
Definition: Control/CxxUtils/Root/StringUtils.cxx:91
PunchThroughG4Classifier::m_networkConfigFileName
StringProperty m_networkConfigFileName
Definition: PunchThroughG4Classifier.h:77
PunchThroughG4Classifier::initializeScaler
StatusCode initializeScaler(const std::string &scalerConfigFile)
input variable MinMaxScaler initialize method
Definition: PunchThroughG4Classifier.cxx:57
merge_scale_histograms.doc
string doc
Definition: merge_scale_histograms.py:9
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
PunchThroughG4Classifier::m_calibratorConfigFileName
StringProperty m_calibratorConfigFileName
Definition: PunchThroughG4Classifier.h:78
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:283
PunchThroughG4Classifier::m_scalerMin
double m_scalerMin
input variable MinMaxScaler members
Definition: PunchThroughG4Classifier.h:62
PunchThroughG4Classifier::m_scalerMinMap
std::map< std::string, double > m_scalerMinMap
Definition: PunchThroughG4Classifier.h:64
PunchThroughG4Classifier::m_calibrationMax
double m_calibrationMax
Definition: PunchThroughG4Classifier.h:70
PunchThroughG4Classifier::m_scalerMaxMap
std::map< std::string, double > m_scalerMaxMap
Definition: PunchThroughG4Classifier.h:65
PunchThroughG4Classifier::initializeNetwork
StatusCode initializeNetwork(const std::string &networkConfigFile)
neural network initialize method
Definition: PunchThroughG4Classifier.cxx:111
python.compressB64.c
def c
Definition: compressB64.py:93
python.SystemOfUnits.m
float m
Definition: SystemOfUnits.py:106
PunchThroughG4Classifier::scaleInputs
std::map< std::string, std::map< std::string, double > > scaleInputs(std::map< std::string, std::map< std::string, double > > &inputs) const
scale NN inputs using MinMaxScaler
Definition: PunchThroughG4Classifier.cxx:215