ATLAS Offline Software
Classes | Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes | List of all members
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. More...
 
MsgStream & msg () const
 Return a stream for sending messages directly (no decoration) More...
 
MsgStream & msg (const MSG::Level lvl) const
 Return a decorated starting stream for sending messages. More...
 
MSG::Level level () const
 Retrieve output level. More...
 
virtual void setLevel (MSG::Level lvl)
 Update outputlevel. More...
 

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. More...
 

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. More...
 

Static Private Attributes

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

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 
116  event_t event;
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 }

◆ level()

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

Retrieve output level.

Definition at line 201 of file MLogging.h.

201 { return msg().level(); }

◆ 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 }

◆ 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 }

◆ 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 }

◆ 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 }

◆ 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 }

◆ Print()

void TFCSMLCalorimeterSimulator::Print ( ) const

Definition at line 202 of file TFCSMLCalorimeterSimulator.cxx.

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

◆ 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 }

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.

◆ 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.


The documentation for this class was generated from the following files:
nEvents
const int nEvents
Definition: fbtTestBasics.cxx:78
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
VNetworkBase::NetworkOutputs
std::map< std::string, double > NetworkOutputs
Format for network outputs.
Definition: VNetworkBase.h:100
TFCSMLCalorimeterSimulator::m_onnx_model
std::unique_ptr< VNetworkBase > m_onnx_model
Definition: TFCSMLCalorimeterSimulator.h:54
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
VNetworkBase::representNetworkOutputs
static std::string representNetworkOutputs(NetworkOutputs const &outputs, int maxValues=3)
String representation of network outputs.
Definition: VNetworkBase.cxx:57
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
ISF_FCS::MLogging::level
MSG::Level level() const
Retrieve output level.
Definition: MLogging.h:201
TRT::Track::event
@ event
Definition: InnerDetector/InDetCalibEvent/TRT_CalibData/TRT_CalibData/TrackInfo.h:74
VNetworkBase::NetworkInputs
std::map< std::string, std::map< std::string, double > > NetworkInputs
Format for network inputs.
Definition: VNetworkBase.h:90
TFCSSimulationState::randomEngine
CLHEP::HepRandomEngine * randomEngine()
Definition: TFCSSimulationState.h:36
MuonR4::to_string
std::string to_string(const SectorProjector proj)
Definition: MsTrackSeeder.cxx:66
TFCSNetworkFactory::create
static std::unique_ptr< VNetworkBase > create(std::string input)
Given a string, make a network.
Definition: TFCSNetworkFactory.cxx:66
postInclude.inputs
inputs
Definition: postInclude.SortInput.py:15
dq_defect_bulk_create_defects.line
line
Definition: dq_defect_bulk_create_defects.py:27
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
ISF_FCS::MLogging::msg
MsgStream & msg() const
Return a stream for sending messages directly (no decoration)
Definition: MLogging.h:231
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TFCSMLCalorimeterSimulator::m_nVoxels
long unsigned int m_nVoxels
Definition: TFCSMLCalorimeterSimulator.h:62
ParticleGun_FastCalo_ChargeFlip_Config.energy
energy
Definition: ParticleGun_FastCalo_ChargeFlip_Config.py:78
TrigConf::MSGTC::ALWAYS
@ ALWAYS
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:29
lumiFormat.i
int i
Definition: lumiFormat.py:85
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
ISF_FCS::MLogging::m_nm
std::string m_nm
Message source name.
Definition: MLogging.h:211
calibdata.exception
exception
Definition: calibdata.py:495
file
TFile * file
Definition: tile_monitor.h:29
TFCSMLCalorimeterSimulator::m_nLayers
long unsigned int m_nLayers
Definition: TFCSMLCalorimeterSimulator.h:63
TFCSMLCalorimeterSimulator::m_used_layers
std::vector< long unsigned int > m_used_layers
Definition: TFCSMLCalorimeterSimulator.h:61
python.CreateTierZeroArgdict.outputs
outputs
Definition: CreateTierZeroArgdict.py:189
merge.output
output
Definition: merge.py:16
TFCSMLCalorimeterSimulator::m_nEvents
int m_nEvents
Definition: TFCSMLCalorimeterSimulator.h:56
TrigConf::MSGTC::NUM_LEVELS
@ NUM_LEVELS
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:30
VNetworkBase::representNetworkInputs
static std::string representNetworkInputs(NetworkInputs const &inputs, int maxValues=3)
String representation of network inputs.
Definition: VNetworkBase.cxx:37
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:23
TFCSMLCalorimeterSimulator::predictVoxels
VNetworkBase::NetworkOutputs predictVoxels() const
Definition: TFCSMLCalorimeterSimulator.cxx:147
TrigConf::MSGTC::NIL
@ NIL
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:22
TFCSMLCalorimeterSimulator::m_layer_boundaries
std::vector< long unsigned int > m_layer_boundaries
Definition: TFCSMLCalorimeterSimulator.h:60
python.SystemOfUnits.ms
float ms
Definition: SystemOfUnits.py:148