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"
63 if (
m_nn !=
nullptr) {
72 <<
IsA()->GetName() <<
" != " <<
ref.IsA()->GetName());
91 const std::string &etaBin,
const std::string &FastCaloTXTInputFolderName) {
110 std::string inputFileName = FastCaloTXTInputFolderName +
111 "MeanStdDevEnergyFractions_eta_" + etaBin +
114 std::ifstream inputTXT(inputFileName);
115 if (inputTXT.is_open()) {
117 while (getline(inputTXT, line)) {
118 std::stringstream
ss(line);
119 unsigned int counter = 0;
122 getline(
ss, substr,
' ');
124 if (substr !=
"etrue") {
125 int index = std::stoi(substr.substr(substr.find(
'_') + 1));
130 }
else if (counter == 1) {
132 }
else if (counter == 2) {
149std::map<std::string, double>
151 const float truthE)
const {
152 std::map<std::string, double> inputVariables;
156 const std::string layer = std::to_string(ilayer);
162 inputVariables[
"ef_" + layer] =
166 ATH_MSG_ERROR(
"Normalization information not found for layer "
174 inputVariables[
"etrue"] = (truthE - std::as_const(
m_normMeans)->at(
index)) /
177 inputVariables[
"pdgId"] = 1;
179 inputVariables[
"pdgId"] = 0;
181 return inputVariables;
191 std::map<std::string, double> inputVariables =
195 auto outputs =
m_nn->compute(inputVariables);
200 << ilayer <<
" weight: "
201 << outputs[
"extrapWeight_" + std::to_string(ilayer)]);
202 float weight = outputs[
"extrapWeight_" + std::to_string(ilayer)];
206 }
else if (weight > 1) {
212 "Setting weight=0.5 for layer = " << std::to_string(ilayer));
213 simulstate.
setAuxInfo<
float>(ilayer, float(0.5));
229 extrapWeight = simulstate.
getAuxInfo<
float>(cs);
232 "Simulstate is not decorated with extrapolation weights for cs = "
233 << std::to_string(cs));
241 float extrapWeight_for_r_z = extrapWeight;
243 extrapWeight_for_r_z = 0.5;
245 "Will use extrapWeight=0.5 for r and z when constructing a hit");
247 ATH_MSG_DEBUG(
"Will use predicted extrapWeight also for r and z when "
248 "constructing a hit");
250 double r = (1. - extrapWeight_for_r_z) * extrapol->
r(cs,
SUBPOS_ENT) +
251 extrapWeight_for_r_z * extrapol->
r(cs,
SUBPOS_EXT);
252 double z = (1. - extrapWeight_for_r_z) * extrapol->
z(cs,
SUBPOS_ENT) +
253 extrapWeight_for_r_z * extrapol->
z(cs,
SUBPOS_EXT);
255 if (!std::isfinite(
r) || !std::isfinite(
z) || !std::isfinite(
eta) ||
256 !std::isfinite(
phi)) {
257 ATH_MSG_WARNING(
"Extrapolator contains NaN or infinite number.\nSetting "
258 "center position to calo boundary.");
260 <<
r <<
" center_z: " <<
z <<
" center_phi: " <<
phi
261 <<
" center_eta: " <<
eta <<
" weight: " << extrapWeight
269 <<
r <<
" center_z: " <<
z <<
" center_phi: " <<
phi
270 <<
" center_eta: " <<
eta <<
" weight: " << extrapWeight
283 <<
" weight: " << extrapWeight <<
" cs: " << cs);
291 int pid,
const std::string &etaBin,
292 const std::string &FastCaloNNInputFolderName) {
295 "Using FastCaloNNInputFolderName: " << FastCaloNNInputFolderName);
298 std::string inputFileName =
299 FastCaloNNInputFolderName +
"NN_" + etaBin +
".json";
301 if (inputFileName.empty()) {
305 ATH_MSG_INFO(
"For pid: " << pid <<
" and etaBin" << etaBin
306 <<
", loading json file " << inputFileName);
307 std::ifstream input(inputFileName);
308 std::stringstream sin;
309 sin << input.rdbuf();
311 auto config = lwt::parse_json(sin);
312 m_nn =
new lwt::LightweightNeuralNetwork(config.inputs, config.layers,
314 if (
m_nn ==
nullptr) {
315 ATH_MSG_ERROR(
"Could not create LightWeightNeuralNetwork from "
322 m_input =
new std::string(sin.str());
325 for (
auto name : config.outputs) {
326 int layer = std::stoi(
335void TFCSPredictExtrapWeights::Streamer(TBuffer &R__b) {
338 if (R__b.IsReading()) {
339 R__b.ReadClassBuffer(TFCSPredictExtrapWeights::Class(),
this);
340 if (
m_nn !=
nullptr) {
345 std::stringstream
sin;
347 auto config = lwt::parse_json(sin);
351#ifndef __FastCaloSimStandAlone__
360 R__b.WriteClassBuffer(TFCSPredictExtrapWeights::Class(),
this);
369 const std::string this_file = __FILE__;
370 const std::string parent_dir = this_file.substr(0, this_file.find(
"/src/"));
371 const std::string norm_path = parent_dir +
"/share/NormPredExtrapSample/";
372 std::string net_path =
"/cvmfs/atlas-nightlies.cern.ch/repo/data/data-art/"
373 "FastCaloSim/LWTNNPredExtrapSample/";
374 test_path(net_path, norm_path, simulstate, truth, extrapol);
383 std::string &net_path, std::string
const &norm_path,
388 << net_path.substr(net_path.length() - 20)
389 <<
" and norm path ..."
390 << norm_path.substr(norm_path.length() - 20));
393#if defined(__FastCaloSimStandAlone__)
401 t->SetPtEtaPhiM(524288000, 0, 0, 130);
407 e->set_IDCaloBoundary_eta(truth->Eta());
408 for (
int i = 0; i < 24; ++i) {
426 simulstate->
set_E(0, 1028.77124023);
427 simulstate->
set_E(1, 68199.0625);
428 simulstate->
set_E(2, 438270.78125);
429 simulstate->
set_E(3, 3024.02929688);
430 simulstate->
set_E(12, 1330.10131836);
431 simulstate->
set_E(1028.77124023 + 68199.0625 + 438270.78125 + 3024.02929688 +
433 simulstate->
set_Efrac(0, simulstate->
E(0) / simulstate->
E());
434 simulstate->
set_Efrac(1, simulstate->
E(1) / simulstate->
E());
435 simulstate->
set_Efrac(2, simulstate->
E(2) / simulstate->
E());
436 simulstate->
set_Efrac(3, simulstate->
E(3) / simulstate->
E());
437 simulstate->
set_Efrac(12, simulstate->
E(12) / simulstate->
E());
439 const int pdgId = truth->
pdgid();
440 const float Ekin = truth->
Ekin();
441 const float eta = truth->Eta();
447 const int Eta =
eta * 10;
448 std::string etaBin =
"";
449 for (
int i = 0; i <= 25; ++i) {
451 if (Eta >= etaTmp && Eta < (etaTmp + 5)) {
452 etaBin = std::to_string(i * 5) +
"_" + std::to_string((i + 1) * 5);
460 const int pid = truth->
pdgid();
467 std::map<std::string, double> inputVariables =
472 auto outputs = NN.
m_nn->compute(inputVariables);
473 const std::vector<int> layers = {0, 1, 2, 3, 12};
474 for (
int ilayer : layers) {
476 ilayer, outputs[
"extrapWeight_" + std::to_string(ilayer)]);
486 TFile *fNN =
new TFile(
"FCSNNtest.root",
"RECREATE");
493 fNN = TFile::Open(
"FCSNNtest.root");
505 bool shortprint = opt.Index(
"short") >= 0;
506 bool longprint =
msgLvl(MSG::DEBUG) || (
msgLvl(MSG::INFO) && !shortprint);
507 TString optprint = opt;
508 optprint.ReplaceAll(
"short",
"");
512 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.