16#include "G4FastTrack.hh"
17#include "G4FastStep.hh"
20#include "lwtnn/parse_json.hh"
23#include <libxml/xmlmemory.h>
24#include <libxml/parser.h>
25#include <libxml/tree.h>
26#include <libxml/xmlreader.h>
27#include <libxml/xpath.h>
28#include <libxml/xpathInternals.h>
31 : base_class(
type, name, parent) {
36 ATH_MSG_DEBUG(
"[ punchthroughclassifier ] Initializing PunchThroughG4Classifier" );
47 return StatusCode::SUCCESS;
52 ATH_MSG_DEBUG(
"[punchthroughclassifier] finalize() successful" );
54 return StatusCode::SUCCESS;
63 doc = xmlParseFile( scalerConfigFile.c_str() );
65 ATH_MSG_DEBUG(
"[ punchthroughclassifier ] Loading scaler: " << scalerConfigFile);
67 for( xmlNodePtr nodeRoot = doc->children; nodeRoot !=
nullptr; nodeRoot = nodeRoot->next) {
69 if (xmlStrEqual( nodeRoot->name, BAD_CAST
"Transformations" )) {
70 for( xmlNodePtr nodeTransform = nodeRoot->children; nodeTransform !=
nullptr; nodeTransform = nodeTransform->next ) {
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));
77 if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST
"max")) !=
nullptr) {
78 m_scalerMax = atof(
reinterpret_cast<const char*
>(xmlBuff));
83 if (xmlStrEqual( nodeTransform->name, BAD_CAST
"VarScales" )) {
84 std::string name =
"";
87 if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST
"name")) !=
nullptr) {
88 name =
reinterpret_cast<const char*
>(xmlBuff);
90 if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST
"min")) !=
nullptr) {
91 min = atof(
reinterpret_cast<const char*
>(xmlBuff));
93 if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST
"max")) !=
nullptr) {
94 max = atof(
reinterpret_cast<const char*
>(xmlBuff));
108 return StatusCode::SUCCESS;
113 ATH_MSG_DEBUG(
"[ punchthroughclassifier ] Loading classifier: " << networkConfigFile);
115 std::ifstream input(networkConfigFile);
117 ATH_MSG_ERROR(
"Could not find json file " << networkConfigFile );
118 return StatusCode::FAILURE;
121 m_graph = std::make_unique<lwt::LightweightGraph>(lwt::parse_json_graph(input));
123 ATH_MSG_ERROR(
"Could not parse graph json file " << networkConfigFile );
124 return StatusCode::FAILURE;
128 return StatusCode::SUCCESS;
137 ATH_MSG_DEBUG(
"[ punchthroughclassifier ] Loading calibrator: " << calibratorConfigFile);
139 doc = xmlParseFile( calibratorConfigFile.c_str() );
141 for( xmlNodePtr nodeRoot = doc->children; nodeRoot !=
nullptr; nodeRoot = nodeRoot->next) {
143 if (xmlStrEqual( nodeRoot->name, BAD_CAST
"Transformations" )) {
144 for( xmlNodePtr nodeTransform = nodeRoot->children; nodeTransform !=
nullptr; nodeTransform = nodeTransform->next ) {
147 if (xmlStrEqual( nodeTransform->name, BAD_CAST
"LimitValues" )) {
148 if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST
"min")) !=
nullptr) {
151 if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST
"max")) !=
nullptr) {
157 if (xmlStrEqual( nodeTransform->name, BAD_CAST
"LinearNorm" )) {
160 if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST
"orig")) !=
nullptr) {
161 orig = atof(
reinterpret_cast<const char*
>(xmlBuff));
163 if ((xmlBuff = xmlGetProp(nodeTransform, BAD_CAST
"norm")) !=
nullptr) {
164 norm = atof(
reinterpret_cast<const char*
>(xmlBuff));
177 return StatusCode::SUCCESS;
182 std::map<std::string, std::map<std::string, double> > networkInputs =
computeInputs(fastTrack, simE, simEfrac);
186 std::map<std::string, double> networkOutputs =
m_graph->compute(networkInputs);
190 return calibratedOutput;
197 std::map<std::string, std::map<std::string, double> > networkInputs;
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},
208 for (
unsigned int i = 0; i < simEfrac.size(); i++) {
209 networkInputs[
"node_0"].insert({
"variable_" + std::to_string(i + 4), simEfrac[i]});
212 return networkInputs;
219 for (
auto& var : inputs[
"node_0"]) {
248 auto lower =
upper--;
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;
#define ATH_CHECK
Evaluate an expression and check for errors.
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
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
StringProperty m_scalerConfigFileName
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
StringProperty m_networkConfigFileName
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
StringProperty m_calibratorConfigFileName
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