ATLAS Offline Software
Loading...
Searching...
No Matches
TFCSMLCalorimeterSimulator Class Reference

#include <TFCSMLCalorimeterSimulator.h>

Inheritance diagram for TFCSMLCalorimeterSimulator:
Collaboration diagram for TFCSMLCalorimeterSimulator:

Classes

struct  event_t
struct  layer_t

Public Member Functions

 TFCSMLCalorimeterSimulator ()
virtual ~TFCSMLCalorimeterSimulator ()
bool loadSimulator (std::string &filename)
void Print () const
VNetworkBase::NetworkOutputs predictVoxels (TFCSSimulationState &simulstate, float eta, float energy) const
event_t getEvent (TFCSSimulationState &simulstate, float eta, float energy) const
VNetworkBase::NetworkOutputs predictVoxels () const
void setInputShapes (std::vector< long unsigned int > &layer_boundaries, std::vector< long unsigned int > &used_layers)
bool msgLvl (const MSG::Level lvl) const
 Check whether the logging system is active at the provided verbosity level.
MsgStream & msg () const
 Return a stream for sending messages directly (no decoration)
MsgStream & msg (const MSG::Level lvl) const
 Return a decorated starting stream for sending messages.
MSG::Level level () const
 Retrieve output level.
virtual void setLevel (MSG::Level lvl)
 Update outputlevel.

Static Public Member Functions

static std::string startMsg (MSG::Level lvl, const std::string &file, int line)
 Make a message to decorate the start of logging.

Private Attributes

std::unique_ptr< VNetworkBasem_onnx_model = nullptr
int m_nEvents = 1
std::vector< long unsigned int > m_layer_boundaries = {0, 36, 200, 310, 346, 382}
std::vector< long unsigned int > m_used_layers = {0, 1, 2, 3, 12}
long unsigned int m_nVoxels = 382
long unsigned int m_nLayers = 5
std::string m_nm
 Message source name.

Static Private Attributes

static boost::thread_specific_ptr< MsgStream > m_msg_tls ATLAS_THREAD_SAFE
 Do not persistify!

Detailed Description

Definition at line 23 of file TFCSMLCalorimeterSimulator.h.

Constructor & Destructor Documentation

◆ TFCSMLCalorimeterSimulator()

TFCSMLCalorimeterSimulator::TFCSMLCalorimeterSimulator ( )

Definition at line 12 of file TFCSMLCalorimeterSimulator.cxx.

12{}

◆ ~TFCSMLCalorimeterSimulator()

TFCSMLCalorimeterSimulator::~TFCSMLCalorimeterSimulator ( )
virtual

Definition at line 14 of file TFCSMLCalorimeterSimulator.cxx.

14{}

Member Function Documentation

◆ getEvent()

TFCSMLCalorimeterSimulator::event_t TFCSMLCalorimeterSimulator::getEvent ( TFCSSimulationState & simulstate,
float eta,
float energy ) const

Definition at line 85 of file TFCSMLCalorimeterSimulator.cxx.

85 {
86
87 // Get the voxel energies
89
90 // check if the output contains a nan
91 // If yes: retry up to 5 times
92 float first_output = outputs.begin()->second;
93 bool contains_nan = std::isnan(first_output);
94 if (contains_nan) {
95 int retry = 0;
96 while (contains_nan) {
97
98 if (retry > 5) {
99 ATH_MSG_WARNING("Network output contains NaN. Giving up.");
100 break;
101 }
102
103 ATH_MSG_WARNING("Network output contains NaN. Retrying.");
104 outputs = predictVoxels(simulstate, eta, energy);
105 first_output = outputs.begin()->second;
106 contains_nan = std::isnan(first_output);
107
108 retry++;
109 }
110 }
111
112 // Fill the event structure with the voxel energies
113 std::vector<unsigned int> bin_index_vector;
114 std::vector<float> E_vector;
115
117
118 long unsigned int layer_index = 0;
119 long unsigned int layer = m_used_layers.at(layer_index);
120
121 for (long unsigned int voxel_index = 0; voxel_index < m_nVoxels; ++voxel_index) {
122
123
124 if (voxel_index == m_layer_boundaries[layer_index+1]) {
125 layer_index = layer_index + 1;
126 layer = m_used_layers.at(layer_index);
127 }
128
129 float voxel_energy = outputs[std::to_string(voxel_index)];
130
131 if (voxel_energy > 0) {
132 if (event.event_data.size() <= layer) {
133 event.event_data.resize(layer+1);
134 }
135 event.event_data.at(layer).bin_index_vector.push_back(voxel_index - m_layer_boundaries[layer_index]);
136
137 // We need energy fractions, not MeV values
138 event.event_data.at(layer).E_vector.push_back(voxel_energy/energy);
139 }
140
141 }
142
143 return event;
144
145}
Scalar eta() const
pseudorapidity method
#define ATH_MSG_WARNING(x)
VNetworkBase::NetworkOutputs predictVoxels() const
std::vector< long unsigned int > m_used_layers
std::vector< long unsigned int > m_layer_boundaries
std::map< std::string, double > NetworkOutputs
Format for network outputs.
@ layer
Definition HitInfo.h:79

◆ level()

MSG::Level ISF_FCS::MLogging::level ( ) const
inlineinherited

Retrieve output level.

Definition at line 201 of file MLogging.h.

201{ return msg().level(); }
MsgStream & msg() const
Return a stream for sending messages directly (no decoration)
Definition MLogging.h:231

◆ loadSimulator()

bool TFCSMLCalorimeterSimulator::loadSimulator ( std::string & filename)

Definition at line 16 of file TFCSMLCalorimeterSimulator.cxx.

16 {
17 // Load the simulator
18 try {
20 } catch (std::exception &e) {
21 ATH_MSG_ERROR("Failed to load simulator from file " << filename << " with error " << e.what());
22 return false;
23 }
24
25 if (m_onnx_model == nullptr) {
26 ATH_MSG_ERROR("Failed to load simulator from file " << filename);
27 return false;
28 }
29
30 return true;
31}
#define ATH_MSG_ERROR(x)
std::unique_ptr< VNetworkBase > m_onnx_model
static std::unique_ptr< VNetworkBase > create(std::string input)
Given a string, make a network.

◆ msg() [1/2]

MsgStream & ISF_FCS::MLogging::msg ( ) const
inlineinherited

Return a stream for sending messages directly (no decoration)

Definition at line 231 of file MLogging.h.

231 {
232 MsgStream *ms = m_msg_tls.get();
233 if (!ms) {
234 ms = new MsgStream(Athena::getMessageSvc(), m_nm);
235 m_msg_tls.reset(ms);
236 }
237 return *ms;
238}
std::string m_nm
Message source name.
Definition MLogging.h:211
IMessageSvc * getMessageSvc(bool quiet=false)

◆ msg() [2/2]

MsgStream & ISF_FCS::MLogging::msg ( const MSG::Level lvl) const
inlineinherited

Return a decorated starting stream for sending messages.

Definition at line 240 of file MLogging.h.

240 {
241 return msg() << lvl;
242}

◆ msgLvl()

bool ISF_FCS::MLogging::msgLvl ( const MSG::Level lvl) const
inlineinherited

Check whether the logging system is active at the provided verbosity level.

Definition at line 222 of file MLogging.h.

222 {
223 if (msg().level() <= lvl) {
224 msg() << lvl;
225 return true;
226 } else {
227 return false;
228 }
229}
MSG::Level level() const
Retrieve output level.
Definition MLogging.h:201

◆ predictVoxels() [1/2]

VNetworkBase::NetworkOutputs TFCSMLCalorimeterSimulator::predictVoxels ( ) const

Definition at line 147 of file TFCSMLCalorimeterSimulator.cxx.

147 {
148
149 // For testing...
150 // This function sets the input dimensionality and the number of predicted layers
151 // to work with the currently best photon ML simulation model. 382 voxels spanned over the
152 // presampler, the three EMB layers and the first HCAL layer.
153 // This allows for easier testing calls.
154
155 int nEvents = 1;
156 int nVoxels = 382;
157 int nLayers= 5;
158
159 std::vector<float> eta_vector(nEvents, 2.0);
160 std::vector<float> energy_vector(nEvents, 65536.0);
161 std::vector<float> z_shape_vector(nEvents*nVoxels, 0.5);
162 std::vector<float> z_energy_vector(nEvents*nLayers, 0.5);
163
165
166 int i = 0;
167 for (float eta : eta_vector) {
168 inputs["inn_eta_in"].insert(std::pair<std::string, double>("variable_" + std::to_string(i), eta));
169 i++;
170 }
171
172 i = 0;
173 for (float energy : energy_vector) {
174 inputs["inn_einc_in"].insert(std::pair<std::string, double>("variable_" + std::to_string(i), energy));
175 i++;
176 }
177
178 i = 0;
179 for (float z_shape : z_shape_vector) {
180 inputs["cfm_z_shape"].insert(std::pair<std::string, double>("variable_" + std::to_string(i), z_shape));
181 i++;
182 }
183
184 i = 0;
185 for (float z_energy : z_energy_vector) {
186 inputs["inn_z_energy"].insert(std::pair<std::string, double>("variable_" + std::to_string(i), z_energy));
187 i++;
188 }
189
191
193
195
196 return outputs;
197
198
199}
#define ATH_MSG_DEBUG(x)
std::map< std::string, std::map< std::string, double > > NetworkInputs
Format for network inputs.
static std::string representNetworkOutputs(NetworkOutputs const &outputs, int maxValues=3)
String representation of network outputs.
static std::string representNetworkInputs(NetworkInputs const &inputs, int maxValues=3)
String representation of network inputs.
const int nEvents

◆ predictVoxels() [2/2]

VNetworkBase::NetworkOutputs TFCSMLCalorimeterSimulator::predictVoxels ( TFCSSimulationState & simulstate,
float eta,
float energy ) const

Definition at line 33 of file TFCSMLCalorimeterSimulator.cxx.

33 {
34
35 // Bring eta into the needed range
36 eta = std::abs(eta) * 10;
37
38 // Initialize the energy and eta input vectors
39 std::vector<float> eta_vector(m_nEvents, eta);
40 std::vector<float> energy_vector(m_nEvents, energy);
41
42 // sample the z vectors according to a standard normal distribution
43 std::vector<float> z_shape_vector(m_nEvents * m_nVoxels, 0.0);
44 std::vector<float> z_energy_vector(m_nEvents * m_nLayers, 0.0);
45 for (auto& z_shape : z_shape_vector) {
46 z_shape = CLHEP::RandGauss::shoot(simulstate.randomEngine(), 0.0, 1.0);
47 }
48 for (auto& z_energy : z_energy_vector) {
49 z_energy = CLHEP::RandGauss::shoot(simulstate.randomEngine(), 0.0, 1.0);
50 }
51
52 // Prepare the inputs for the network
54
55 int i = 0;
56 for (float thisEta : eta_vector) {
57 inputs["inn_eta_in"].insert(std::pair<std::string, double>("variable_" + std::to_string(i), thisEta));
58 i++;
59 }
60
61 i = 0;
62 for (float thisEnergy : energy_vector) {
63 inputs["inn_einc_in"].insert(std::pair<std::string, double>("variable_" + std::to_string(i), thisEnergy));
64 i++;
65 }
66
67 i = 0;
68 for (float z_shape : z_shape_vector) {
69 inputs["cfm_z_shape"].insert(std::pair<std::string, double>("variable_" + std::to_string(i), z_shape));
70 i++;
71 }
72
73 i = 0;
74 for (float z_energy : z_energy_vector) {
75 inputs["inn_z_energy"].insert(std::pair<std::string, double>("variable_" + std::to_string(i), z_energy));
76 i++;
77 }
78
79 // Compute the network outputs
81
82 return outputs;
83}
CLHEP::HepRandomEngine * randomEngine()

◆ Print()

void TFCSMLCalorimeterSimulator::Print ( ) const

Definition at line 202 of file TFCSMLCalorimeterSimulator.cxx.

202 {
203 ATH_MSG_INFO("ONNX AICalorimeterSimulator");
204}
#define ATH_MSG_INFO(x)

◆ setInputShapes()

void TFCSMLCalorimeterSimulator::setInputShapes ( std::vector< long unsigned int > & layer_boundaries,
std::vector< long unsigned int > & used_layers )
inline

Definition at line 45 of file TFCSMLCalorimeterSimulator.h.

45 {
46 m_layer_boundaries = layer_boundaries;
47 m_used_layers = used_layers;
48 m_nVoxels = layer_boundaries.back();
49 m_nLayers = used_layers.size();
50 };

◆ setLevel()

void ISF_FCS::MLogging::setLevel ( MSG::Level lvl)
virtualinherited

Update outputlevel.

Definition at line 105 of file MLogging.cxx.

105 {
106 lvl = (lvl >= MSG::NUM_LEVELS) ? MSG::ALWAYS
107 : (lvl < MSG::NIL) ? MSG::NIL
108 : lvl;
109 msg().setLevel(lvl);
110}

◆ startMsg()

std::string ISF_FCS::MLogging::startMsg ( MSG::Level lvl,
const std::string & file,
int line )
staticinherited

Make a message to decorate the start of logging.

Print a message for the start of logging.

Definition at line 116 of file MLogging.cxx.

116 {
117 int col1_len = 20;
118 int col2_len = 5;
119 int col3_len = 10;
120 auto last_slash = file.find_last_of('/');
121 int path_len = last_slash == std::string::npos ? 0 : last_slash;
122 int trim_point = path_len;
123 int total_len = file.length();
124 if (total_len - path_len > col1_len)
125 trim_point = total_len - col1_len;
126 std::string trimmed_name = file.substr(trim_point);
127 const char *LevelNames[MSG::NUM_LEVELS] = {
128 "NIL", "VERBOSE", "DEBUG", "INFO", "WARNING", "ERROR", "FATAL", "ALWAYS"};
129 std::string level = LevelNames[lvl];
130 std::string level_string = std::string("(") + level + ") ";
131 std::stringstream output;
132 output << std::setw(col1_len) << std::right << trimmed_name << ":"
133 << std::setw(col2_len) << std::left << line << std::setw(col3_len)
134 << std::right << level_string;
135 return output.str();
136}
output
Definition merge.py:16
TFile * file

Member Data Documentation

◆ ATLAS_THREAD_SAFE

boost::thread_specific_ptr<MsgStream> m_msg_tls ISF_FCS::MLogging::ATLAS_THREAD_SAFE
inlinestaticprivateinherited

Do not persistify!

MsgStream instance (a std::cout like with print-out levels)

Definition at line 215 of file MLogging.h.

◆ m_layer_boundaries

std::vector<long unsigned int> TFCSMLCalorimeterSimulator::m_layer_boundaries = {0, 36, 200, 310, 346, 382}
private

Definition at line 60 of file TFCSMLCalorimeterSimulator.h.

60{0, 36, 200, 310, 346, 382};

◆ m_nEvents

int TFCSMLCalorimeterSimulator::m_nEvents = 1
private

Definition at line 56 of file TFCSMLCalorimeterSimulator.h.

◆ m_nLayers

long unsigned int TFCSMLCalorimeterSimulator::m_nLayers = 5
private

Definition at line 63 of file TFCSMLCalorimeterSimulator.h.

◆ m_nm

std::string ISF_FCS::MLogging::m_nm
privateinherited

Message source name.

Definition at line 211 of file MLogging.h.

◆ m_nVoxels

long unsigned int TFCSMLCalorimeterSimulator::m_nVoxels = 382
private

Definition at line 62 of file TFCSMLCalorimeterSimulator.h.

◆ m_onnx_model

std::unique_ptr<VNetworkBase> TFCSMLCalorimeterSimulator::m_onnx_model = nullptr
private

Definition at line 54 of file TFCSMLCalorimeterSimulator.h.

◆ m_used_layers

std::vector<long unsigned int> TFCSMLCalorimeterSimulator::m_used_layers = {0, 1, 2, 3, 12}
private

Definition at line 61 of file TFCSMLCalorimeterSimulator.h.

61{0, 1, 2, 3, 12};

The documentation for this class was generated from the following files: