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