ATLAS Offline Software
Loading...
Searching...
No Matches
PunchThroughG4Classifier.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6// PunchThroughG4Classifier.cxx, (c) ATLAS Detector software
9
10#include <fstream>
11
12// PathResolver
14
15// Geant4
16#include "G4FastTrack.hh"
17#include "G4FastStep.hh"
18
19//LWTNN
20#include "lwtnn/parse_json.hh"
21
24
25
26PunchThroughG4Classifier::PunchThroughG4Classifier(const std::string& type, const std::string& name, const IInterface* parent)
27 : base_class(type, name, parent) {
28}
29
31
32 ATH_MSG_DEBUG( "[ punchthroughclassifier ] Initializing PunchThroughG4Classifier" );
33
34 std::string resolvedScalerFileName = PathResolverFindCalibFile (m_scalerConfigFileName);
35 ATH_CHECK ( initializeScaler(resolvedScalerFileName) );
36
37 std::string resolvedNetworkFileName = PathResolverFindCalibFile (m_networkConfigFileName);
38 ATH_CHECK ( initializeNetwork(resolvedNetworkFileName) );
39
40 std::string resolvedCalibratorFileName = PathResolverFindCalibFile (m_calibratorConfigFileName);
41 ATH_CHECK ( initializeCalibrator(resolvedCalibratorFileName) );
42
43 return StatusCode::SUCCESS;
44}
45
47
48 ATH_MSG_DEBUG( "[punchthroughclassifier] finalize() successful" );
49
50 return StatusCode::SUCCESS;
51}
52
53StatusCode PunchThroughG4Classifier::initializeScaler(const std::string & scalerConfigFile){
55 std::unique_ptr<XMLCoreNode> doc = p.parse (scalerConfigFile);
56
57 ATH_MSG_DEBUG( "[ punchthroughclassifier ] Loading scaler: " << scalerConfigFile);
58
59 for (const XMLCoreNode* node : doc->get_children ("Transformations/*"))
60 {
61 if (node->get_name() == "ScalerValues") {
62 m_scalerMin = node->get_double_attrib ("min");
63 m_scalerMax = node->get_double_attrib ("max");
64 }
65 else if (node->get_name() == "VarScales") {
66 std::string name = node->get_attrib ("name");
67 m_scalerMinMap[name] = node->get_double_attrib ("min");
68 m_scalerMaxMap[name] = node->get_double_attrib ("max");
69 }
70 }
71
72 return StatusCode::SUCCESS;
73}
74
75StatusCode PunchThroughG4Classifier::initializeNetwork(const std::string & networkConfigFile){
76
77 ATH_MSG_DEBUG( "[ punchthroughclassifier ] Loading classifier: " << networkConfigFile);
78
79 std::ifstream input(networkConfigFile);
80 if(!input){
81 ATH_MSG_ERROR("Could not find json file " << networkConfigFile );
82 return StatusCode::FAILURE;
83 }
84
85 m_graph = std::make_unique<lwt::LightweightGraph>(lwt::parse_json_graph(input));
86 if(!m_graph){
87 ATH_MSG_ERROR("Could not parse graph json file " << networkConfigFile );
88 return StatusCode::FAILURE;
89 }
90
91
92 return StatusCode::SUCCESS;
93}
94
95StatusCode PunchThroughG4Classifier::initializeCalibrator(const std::string & calibratorConfigFile){
97 std::unique_ptr<XMLCoreNode> doc = p.parse (calibratorConfigFile);
98
99 //parse xml that contains config for isotonic regressor used to calibrate the network output
100 ATH_MSG_DEBUG( "[ punchthroughclassifier ] Loading calibrator: " << calibratorConfigFile);
101
102 for (const XMLCoreNode* node : doc->get_children ("Transformations/*"))
103 {
104 if (node->get_name() == "LimitValues") {
105 m_calibrationMin = node->get_double_attrib ("min");
106 m_calibrationMax = node->get_double_attrib ("max");
107 }
108 else if (node->get_name() == "LinearNorm") {
109 double orig = node->get_double_attrib ("orig");
110 double norm = node->get_double_attrib ("norm");
111 m_calibrationMap[orig] = norm;
112 }
113 }
114
115 return StatusCode::SUCCESS;
116}
117
118double PunchThroughG4Classifier::computePunchThroughProbability(const G4FastTrack& fastTrack, const double simE, const std::vector<double> & simEfrac) const {
119
120 std::map<std::string, std::map<std::string, double> > networkInputs = computeInputs(fastTrack, simE, simEfrac); //compute inputs
121
122 networkInputs = scaleInputs(networkInputs); //scale inputs
123
124 std::map<std::string, double> networkOutputs = m_graph->compute(networkInputs); //call neural network on inputs
125
126 double calibratedOutput = calibrateOutput(networkOutputs["out_0"]); //calibrate neural network output
127
128 return calibratedOutput;
129}
130
131std::map<std::string, std::map<std::string, double> > PunchThroughG4Classifier::computeInputs(const G4FastTrack& fastTrack, const double simE, const std::vector<double> & simEfrac) {
132
133 //calculate inputs for NN
134
135 std::map<std::string, std::map<std::string, double> > networkInputs;
136
137 //add initial particle and total energy variables
138 networkInputs["node_0"] = {
139 {"variable_0", fastTrack.GetPrimaryTrack()->GetMomentum().mag() },
140 {"variable_1", std::abs(fastTrack.GetPrimaryTrack()->GetPosition().eta()) },
141 {"variable_2", fastTrack.GetPrimaryTrack()->GetPosition().phi() },
142 {"variable_3", simE},
143 };
144
145 //add energy fraction variables
146 for (unsigned int i = 0; i < simEfrac.size(); i++) { //from 0 to 23, 24 layers
147 networkInputs["node_0"].insert({"variable_" + std::to_string(i + 4), simEfrac[i]});
148 }
149
150 return networkInputs;
151}
152
153std::map<std::string, std::map<std::string, double> > PunchThroughG4Classifier::scaleInputs(std::map<std::string, std::map<std::string, double> >& inputs) const{
154
155 //apply MinMaxScaler to network inputs
156
157 for (auto& var : inputs["node_0"]) {
158
159 double x_std;
160 if(m_scalerMaxMap.at(var.first) != m_scalerMinMap.at(var.first)){
161 x_std = (var.second - m_scalerMinMap.at(var.first)) / (m_scalerMaxMap.at(var.first) - m_scalerMinMap.at(var.first));
162 }
163 else{
164 x_std = (var.second - m_scalerMinMap.at(var.first));
165 }
166 var.second = x_std * (m_scalerMax - m_scalerMin) + m_scalerMin;
167 }
168
169 return inputs;
170}
171
172double PunchThroughG4Classifier::calibrateOutput(double& networkOutput) const {
173
174 //calibrate output of network using isotonic regressor model
175
176 //if network output is outside of the range of isotonic regressor then return min and max values
177 if (networkOutput < m_calibrationMin){
178 return m_calibrationMin;
179 }
180 else if (networkOutput > m_calibrationMax){
181 return m_calibrationMax;
182 }
183
184 //otherwise find neighbouring points in isotonic regressor
185 auto upper = m_calibrationMap.upper_bound(networkOutput);
186 auto lower = upper--;
187
188 //Perform linear interpolation between points
189 double m = (upper->second - lower->second)/(upper->first - lower->first);
190 double c = lower->second - m * lower->first;
191 double calibrated = m * networkOutput + c;
192
193 return calibrated;
194}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_DEBUG(x)
int upper(int c)
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Simple DOM-like node structure to hold the result of XML parsing.
std::unique_ptr< lwt::LightweightGraph > m_graph
NN graph.
std::map< std::string, double > m_scalerMaxMap
virtual StatusCode initialize() override
AlgTool initialize method.
double m_scalerMin
input variable MinMaxScaler members
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
std::map< double, double > m_calibrationMap
PunchThroughG4Classifier(const std::string &, const std::string &, const IInterface *)
Constructor.
std::map< std::string, double > m_scalerMinMap
StatusCode initializeCalibrator(const std::string &calibratorConfigFile)
isotonic regressor calibrator initialize method
virtual StatusCode finalize() override
AlgTool finalize method.
StatusCode initializeScaler(const std::string &scalerConfigFile)
input variable MinMaxScaler initialize method
StatusCode initializeNetwork(const std::string &networkConfigFile)
neural network initialize method
double calibrateOutput(double &networkOutput) const
calibrate NN output using isotonic regressor
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
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
Simple DOM-like node structure to hold the result of XML parsing.
Definition XMLCoreNode.h:46
Definition node.h:24