15#include "HepPDT/ParticleData.hh"
16#include "HepPDT/ParticleDataTable.hh"
18#include "CLHEP/Random/RandGauss.h"
19#include "CLHEP/Random/RandFlat.h"
21#if defined(__FastCaloSimStandAlone__)
22#include "CLHEP/Random/TRandomEngine.h"
24#include <CLHEP/Random/RanluxEngine.h>
31#include "lwtnn/LightweightGraph.hh"
32#include "lwtnn/LightweightNeuralNetwork.hh"
33#include "lwtnn/parse_json.hh"
36#include <libxml/xmlmemory.h>
37#include <libxml/parser.h>
38#include <libxml/tree.h>
39#include <libxml/xmlreader.h>
40#include <libxml/xpath.h>
41#include <libxml/xpathInternals.h>
71 if (
m_nn !=
nullptr) {
80 <<
IsA()->GetName() <<
" != " <<
ref.IsA()->GetName());
99 const std::string &etaBin,
const std::string &FastCaloTXTInputFolderName) {
118 std::string inputFileName = FastCaloTXTInputFolderName +
119 "MeanStdDevEnergyFractions_eta_" + etaBin +
122 std::ifstream inputTXT(inputFileName);
123 if (inputTXT.is_open()) {
125 while (getline(inputTXT, line)) {
126 std::stringstream
ss(line);
127 unsigned int counter = 0;
130 getline(
ss, substr,
' ');
132 if (substr !=
"etrue") {
133 int index = std::stoi(substr.substr(substr.find(
'_') + 1));
138 }
else if (counter == 1) {
140 }
else if (counter == 2) {
157std::map<std::string, double>
159 const float truthE)
const {
160 std::map<std::string, double> inputVariables;
164 const std::string layer = std::to_string(ilayer);
170 inputVariables[
"ef_" + layer] =
174 ATH_MSG_ERROR(
"Normalization information not found for layer "
182 inputVariables[
"etrue"] = (truthE - std::as_const(
m_normMeans)->at(
index)) /
185 inputVariables[
"pdgId"] = 1;
187 inputVariables[
"pdgId"] = 0;
189 return inputVariables;
199 std::map<std::string, double> inputVariables =
203 auto outputs =
m_nn->compute(inputVariables);
208 << ilayer <<
" weight: "
209 << outputs[
"extrapWeight_" + std::to_string(ilayer)]);
210 float weight = outputs[
"extrapWeight_" + std::to_string(ilayer)];
214 }
else if (weight > 1) {
220 "Setting weight=0.5 for layer = " << std::to_string(ilayer));
221 simulstate.
setAuxInfo<
float>(ilayer, float(0.5));
237 extrapWeight = simulstate.
getAuxInfo<
float>(cs);
240 "Simulstate is not decorated with extrapolation weights for cs = "
241 << std::to_string(cs));
245 double eta = (1. - extrapWeight) * extrapol->eta(cs,
SUBPOS_ENT) +
247 double phi = (1. - extrapWeight) * extrapol->phi(cs,
SUBPOS_ENT) +
249 float extrapWeight_for_r_z = extrapWeight;
251 extrapWeight_for_r_z = 0.5;
253 "Will use extrapWeight=0.5 for r and z when constructing a hit");
255 ATH_MSG_DEBUG(
"Will use predicted extrapWeight also for r and z when "
256 "constructing a hit");
258 double r = (1. - extrapWeight_for_r_z) * extrapol->r(cs,
SUBPOS_ENT) +
259 extrapWeight_for_r_z * extrapol->r(cs,
SUBPOS_EXT);
260 double z = (1. - extrapWeight_for_r_z) * extrapol->z(cs,
SUBPOS_ENT) +
261 extrapWeight_for_r_z * extrapol->z(cs,
SUBPOS_EXT);
263 if (!std::isfinite(
r) || !std::isfinite(
z) || !std::isfinite(
eta) ||
264 !std::isfinite(
phi)) {
265 ATH_MSG_WARNING(
"Extrapolator contains NaN or infinite number.\nSetting "
266 "center position to calo boundary.");
268 <<
r <<
" center_z: " <<
z <<
" center_phi: " <<
phi
269 <<
" center_eta: " <<
eta <<
" weight: " << extrapWeight
272 r = extrapol->IDCaloBoundary_r();
273 z = extrapol->IDCaloBoundary_z();
274 eta = extrapol->IDCaloBoundary_eta();
275 phi = extrapol->IDCaloBoundary_phi();
277 <<
r <<
" center_z: " <<
z <<
" center_phi: " <<
phi
278 <<
" center_eta: " <<
eta <<
" weight: " << extrapWeight
291 <<
" weight: " << extrapWeight <<
" cs: " << cs);
299 int pid,
const std::string &etaBin,
300 const std::string &FastCaloNNInputFolderName) {
303 "Using FastCaloNNInputFolderName: " << FastCaloNNInputFolderName);
306 std::string inputFileName =
307 FastCaloNNInputFolderName +
"NN_" + etaBin +
".json";
309 if (inputFileName.empty()) {
313 ATH_MSG_INFO(
"For pid: " << pid <<
" and etaBin" << etaBin
314 <<
", loading json file " << inputFileName);
315 std::ifstream input(inputFileName);
316 std::stringstream sin;
317 sin << input.rdbuf();
319 auto config = lwt::parse_json(sin);
322 if (
m_nn ==
nullptr) {
323 ATH_MSG_ERROR(
"Could not create LightWeightNeuralNetwork from "
330 m_input =
new std::string(sin.str());
333 for (
auto name :
config.outputs) {
334 int layer = std::stoi(
343void TFCSPredictExtrapWeights::Streamer(TBuffer &R__b) {
346 if (R__b.IsReading()) {
347 R__b.ReadClassBuffer(TFCSPredictExtrapWeights::Class(),
this);
348 if (
m_nn !=
nullptr) {
353 std::stringstream
sin;
355 auto config = lwt::parse_json(sin);
359#ifndef __FastCaloSimStandAlone__
368 R__b.WriteClassBuffer(TFCSPredictExtrapWeights::Class(),
this);
377 const std::string this_file = __FILE__;
378 const std::string parent_dir = this_file.substr(0, this_file.find(
"/src/"));
379 const std::string norm_path = parent_dir +
"/share/NormPredExtrapSample/";
380 std::string net_path =
"/cvmfs/atlas-nightlies.cern.ch/repo/data/data-art/"
381 "FastCaloSim/LWTNNPredExtrapSample/";
382 test_path(net_path, norm_path, simulstate, truth, extrapol);
391 std::string &net_path, std::string
const &norm_path,
396 << net_path.substr(net_path.length() - 20)
397 <<
" and norm path ..."
398 << norm_path.substr(norm_path.length() - 20));
401#if defined(__FastCaloSimStandAlone__)
409 t->SetPtEtaPhiM(524288000, 0, 0, 130);
415 e->set_IDCaloBoundary_eta(truth->Eta());
416 for (
int i = 0; i < 24; ++i) {
434 simulstate->
set_E(0, 1028.77124023);
435 simulstate->
set_E(1, 68199.0625);
436 simulstate->
set_E(2, 438270.78125);
437 simulstate->
set_E(3, 3024.02929688);
438 simulstate->
set_E(12, 1330.10131836);
439 simulstate->
set_E(1028.77124023 + 68199.0625 + 438270.78125 + 3024.02929688 +
441 simulstate->
set_Efrac(0, simulstate->
E(0) / simulstate->
E());
442 simulstate->
set_Efrac(1, simulstate->
E(1) / simulstate->
E());
443 simulstate->
set_Efrac(2, simulstate->
E(2) / simulstate->
E());
444 simulstate->
set_Efrac(3, simulstate->
E(3) / simulstate->
E());
445 simulstate->
set_Efrac(12, simulstate->
E(12) / simulstate->
E());
447 const int pdgId = truth->
pdgid();
448 const float Ekin = truth->
Ekin();
449 const float eta = truth->Eta();
455 const int Eta =
eta * 10;
456 std::string etaBin =
"";
457 for (
int i = 0; i <= 25; ++i) {
459 if (Eta >= etaTmp && Eta < (etaTmp + 5)) {
460 etaBin = std::to_string(i * 5) +
"_" + std::to_string((i + 1) * 5);
468 const int pid = truth->
pdgid();
475 std::map<std::string, double> inputVariables =
480 auto outputs = NN.
m_nn->compute(inputVariables);
481 const std::vector<int> layers = {0, 1, 2, 3, 12};
482 for (
int ilayer : layers) {
484 ilayer, outputs[
"extrapWeight_" + std::to_string(ilayer)]);
494 TFile *fNN =
new TFile(
"FCSNNtest.root",
"RECREATE");
501 fNN = TFile::Open(
"FCSNNtest.root");
513 bool shortprint = opt.Index(
"short") >= 0;
514 bool longprint =
msgLvl(MSG::DEBUG) || (
msgLvl(MSG::INFO) && !shortprint);
515 TString optprint = opt;
516 optprint.ReplaceAll(
"short",
"");
520 ATH_MSG_INFO(optprint <<
" m_input (TFCSPredictExtrapWeights): "
const boost::regex ref(r_ef)
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define ATH_MSG_WARNING(x)
#define ATH_MSG_NOCLASS(logger_name, x)
FCSReturnCode
Base class for all FastCaloSim parametrizations Functionality in derivde classes is provided through ...
Helper for getting a const version of a pointer.
bool msgLvl(const MSG::Level lvl) const
Check whether the logging system is active at the provided verbosity level.
virtual void setLevel(MSG::Level lvl)
Update outputlevel.
void setCenter_phi(float phi)
void setCenter_r(float r)
void setCenter_z(float z)
void setCenter_eta(float eta)
TFCSLateralShapeParametrizationHitBase(const char *name=nullptr, const char *title=nullptr)
void Print(Option_t *option="") const override
bool compare(const TFCSParametrizationBase &ref) const
void set_calosample(int cs)
bool compare(const TFCSParametrizationBase &ref) const
Do not persistify!
virtual bool is_match_pdgid(int id) const override
virtual void set_pdgid(int id)
bool compare(const TFCSParametrizationBase &ref) const
double Efrac(int sample) const
void set_E(int sample, double Esample)
bool hasAuxInfo(std::uint32_t index) const
const T getAuxInfo(std::uint32_t index) const
void set_Efrac(int sample, double Efracsample)
void setAuxInfo(std::uint32_t index, const T &val)
void setRandomEngine(CLHEP::HepRandomEngine *engine)
static Root::TMsgLogger logger("iLumiCalc")
const T * as_const_ptr(const T *p)
Helper for getting a const version of a pointer.
#define IsA
Declare the TObject style functions.