ATLAS Offline Software
TFCSBinnedShowerONNX.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ISF_FASTCALOSIMEVENT_TFCSBinnedShowerONNX_h
6 #define ISF_FASTCALOSIMEVENT_TFCSBinnedShowerONNX_h
7 
8 #include <RtypesCore.h>
9 #include <TMath.h>
10 #include <hdf5.h>
11 
12 #include <fstream>
13 #include <tuple>
14 #include <vector>
15 
21 
22 
23 class ICaloGeometry;
24 
26  public:
27 
28  typedef struct {
29  std::vector<float> R_lower;
30  std::vector<float> R_size;
31  std::vector<float> alpha_lower;
32  std::vector<float> alpha_size;
33  } layer_bins_t;
34 
35  typedef std::vector<layer_bins_t> event_bins_t;
36 
37  TFCSBinnedShowerONNX(const char *name = nullptr, const char *title = nullptr);
38 
39  virtual ~TFCSBinnedShowerONNX();
40 
41  virtual bool is_match_Ekin_bin(int /*Ekin_bin*/) const override {
42  return true;
43  };
44 
45  void enable_upscaling() { m_use_upscaling = true; };
46  void disable_upscaling() { m_use_upscaling = false; };
47 
49  float get_default_hit_energy() const { return m_default_hit_energy; }
50  void set_max_hits_per_voxel(int max_hits) { m_max_hits_per_voxel = max_hits; }
52 
53  // Loads the ONNX model for the calorimeter simulation.
54  void load_simulator(std::string &filename) {
55  // TODO: Do I have to delete it again?
58  }
59 
60  // Loads the voxel boundaries and (potentially) the average showers for the
61  // upscaling
62  // TODO: Define HDF5 file format somewhere
63  void load_meta_data(const std::string &filename,
64  std::vector<long unsigned int> &layers);
65 
67  const std::string &filename);
68 
69  // Allows to set the voxel boundaries for the given layer manually.
70  void set_bin_boundaries(long unsigned int layer_index,
71  const std::vector<float>& R_lower, const std::vector<float>& R_size,
72  const std::vector<float>& alpha_lower,
73  const std::vector<float>& alpha_size);
74 
76 
77  void set_coordinates(const event_bins_t & coordinates) {
78  m_coordinates = coordinates;
79  }
80 
81  const std::vector<std::vector<std::vector<std::vector<float>>>>&
84  }
85 
86  const std::vector<float>& get_upscaling_energies() const {
87  return m_upscaling_energies;
88  }
89 
91  const std::vector<std::vector<std::vector<std::vector<float>>>> &
92  sub_bin_distribution,
93  const std::vector<float> & upscaling_energies) {
94  m_sub_bin_distribution = sub_bin_distribution;
95  m_upscaling_energies = upscaling_energies;
96  m_use_upscaling = true;
97  }
98 
99  protected:
100  // Returns the event to be used
101  virtual void get_event(
102  TFCSSimulationState &simulstate, float eta_center, float phi_center,
103  float e_init, long unsigned int reference_layer_index) const override;
104 
105  // Returns the number of hits that are going to be simulated in the given
106  // layer.
107  virtual long unsigned int get_n_hits(
108  TFCSSimulationState &simulstate,
109  long unsigned int layer_index) const override;
110 
111  // Used to precompute the number of hits for all layers in the event.
112  virtual void compute_n_hits_and_elayer(TFCSSimulationState &simulstate) const;
113 
114  // Returns the position and energy of the corresponding hit in the given
115  // event, layer and bin
116  virtual float get_layer_energy(TFCSSimulationState &simulstate,
117  long unsigned int layer_index) const override;
118 
119  // Returns the position and energy of the corresponding hit in the given
120  // event, layer and bin
121  virtual std::tuple<float, float, float> get_hit_position_and_energy(
122  TFCSSimulationState &simulstate, long unsigned int layer_index,
123  long unsigned int hit_index) const override;
124 
125  // Delete all pointers that were created in get_event()
126  virtual void delete_event(TFCSSimulationState &simulstate) const override;
127 
128  private:
129  // Number of calorimeter layers
130  const long unsigned int m_n_layers =
132 
133  // What should be the average energy per hit in the library
135  // What is the mamimum number of hits per voxel (for runtime reasons)
137 
138  // Store the used event library
140 
141  std::tuple<float, float> get_coordinates(TFCSSimulationState &simulstate,
142  long unsigned int layer_index,
143  int bin_index) const;
144 
145  // If true, the sub-cell distribution is used to upscale the energy deposition
146  bool m_use_upscaling = false;
147 
148  // Can be used to interpolate the individual voxels according to a higher
149  // resolution average shower
150  std::vector<std::vector<std::vector<std::vector<float>>>>
152  std::vector<float> m_upscaling_energies; // energies of the avg showers
153 
154  void upscale(TFCSSimulationState &simulstate, float &R_min, float &R_max,
155  float &alpha_min, float &alpha_max,
156  long unsigned int layer_index, int bin_index) const;
157 
158  long unsigned int get_energy_index(TFCSSimulationState &simulstate,
159  long unsigned int layer_index,
160  long unsigned int hit_index) const;
161  // Helper functions to load the HDF5 dataset
162  std::tuple<std::vector<float>, std::vector<hsize_t>, bool> load_hdf5_dataset(
163  const std::string &filename, const std::string &datasetname);
164 
165 
166  void load_bin_boundaries(const std::string &filename,
167  long unsigned int layer_index);
168 
169  void load_upscaling_distribution(const std::string &filename,
170  long unsigned int layer_index);
171 
172  // Store a reference to the class that handles the actual ONNX call.
174 
175  ClassDefOverride(TFCSBinnedShowerONNX, 1) // TFCSBinnedShowerONNX
176 };
177 
178 #endif
TFCSBinnedShowerONNX
Definition: TFCSBinnedShowerONNX.h:25
TFCSBinnedShowerONNX::is_match_Ekin_bin
virtual bool is_match_Ekin_bin(int) const override
Definition: TFCSBinnedShowerONNX.h:41
TFCSBinnedShowerONNX::set_max_hits_per_voxel
void set_max_hits_per_voxel(int max_hits)
Definition: TFCSBinnedShowerONNX.h:50
TFCSBinnedShowerONNX::get_event
virtual void get_event(TFCSSimulationState &simulstate, float eta_center, float phi_center, float e_init, long unsigned int reference_layer_index) const override
do not persistify
Definition: TFCSBinnedShowerONNX.cxx:61
TFCSBinnedShowerONNX::m_max_hits_per_voxel
int m_max_hits_per_voxel
Definition: TFCSBinnedShowerONNX.h:136
TFCSBinnedShowerONNX::m_ai_simulator
TFCSMLCalorimeterSimulator * m_ai_simulator
Definition: TFCSBinnedShowerONNX.h:173
TFCSBinnedShowerONNX::m_upscaling_energies
std::vector< float > m_upscaling_energies
Definition: TFCSBinnedShowerONNX.h:152
TFCSBinnedShowerONNX::set_coordinates
void set_coordinates(const event_bins_t &coordinates)
Definition: TFCSBinnedShowerONNX.h:77
TFCSBinnedShowerONNX::get_max_hits_per_voxel
int get_max_hits_per_voxel() const
Definition: TFCSBinnedShowerONNX.h:51
TFCSBinnedShowerONNX::load_bin_boundaries
void load_bin_boundaries(const std::string &filename, long unsigned int layer_index)
Definition: TFCSBinnedShowerONNX.cxx:136
TFCSBinnedShowerONNX::set_bin_boundaries
void set_bin_boundaries(long unsigned int layer_index, const std::vector< float > &R_lower, const std::vector< float > &R_size, const std::vector< float > &alpha_lower, const std::vector< float > &alpha_size)
Definition: TFCSBinnedShowerONNX.cxx:194
module_driven_slicing.layers
layers
Definition: module_driven_slicing.py:113
TFCSBinnedShowerONNX::get_hit_position_and_energy
virtual std::tuple< float, float, float > get_hit_position_and_energy(TFCSSimulationState &simulstate, long unsigned int layer_index, long unsigned int hit_index) const override
Definition: TFCSBinnedShowerONNX.cxx:455
TFCSBinnedShowerONNX::load_simulator
void load_simulator(std::string &filename)
Definition: TFCSBinnedShowerONNX.h:54
TFCSBinnedShowerONNX::layer_bins_t::R_size
std::vector< float > R_size
Definition: TFCSBinnedShowerONNX.h:30
TFCSMLCalorimeterSimulator
Definition: TFCSMLCalorimeterSimulator.h:23
TFCSBinnedShowerONNX::get_coordinates
const event_bins_t & get_coordinates()
Definition: TFCSBinnedShowerONNX.h:75
TFCSMLCalorimeterSimulator::loadSimulator
bool loadSimulator(std::string &filename)
Definition: TFCSMLCalorimeterSimulator.cxx:16
TFCSBinnedShowerONNX::m_default_hit_energy
float m_default_hit_energy
Definition: TFCSBinnedShowerONNX.h:134
TFCSBinnedShowerONNX::layer_bins_t::alpha_size
std::vector< float > alpha_size
Definition: TFCSBinnedShowerONNX.h:32
TFCSMLCalorimeterSimulator.h
TFCSBinnedShowerONNX::get_n_hits
virtual long unsigned int get_n_hits(TFCSSimulationState &simulstate, long unsigned int layer_index) const override
Definition: TFCSBinnedShowerONNX.cxx:255
TFCSBinnedShowerBase.h
TFCSLateralShapeParametrizationHitBase.h
LArNewCalib_PedestalAutoCorr.MaxSample
int MaxSample
Definition: LArNewCalib_PedestalAutoCorr.py:15
TFCSBinnedShowerONNX::load_hdf5_dataset
std::tuple< std::vector< float >, std::vector< hsize_t >, bool > load_hdf5_dataset(const std::string &filename, const std::string &datasetname)
Definition: TFCSBinnedShowerONNX.cxx:102
TFCSBinnedShowerONNX::m_sub_bin_distribution
std::vector< std::vector< std::vector< std::vector< float > > > > m_sub_bin_distribution
Definition: TFCSBinnedShowerONNX.h:151
ParticleGun_FastCalo_ChargeFlip_Config.energy
energy
Definition: ParticleGun_FastCalo_ChargeFlip_Config.py:78
TFCSParametrizationBinnedChain.h
ICaloGeometry
Definition: ICaloGeometry.h:14
covarianceTool.title
title
Definition: covarianceTool.py:542
TFCSBinnedShowerONNX::get_default_hit_energy
float get_default_hit_energy() const
Definition: TFCSBinnedShowerONNX.h:49
TFCSBinnedShowerONNX::TFCSBinnedShowerONNX
TFCSBinnedShowerONNX(const char *name=nullptr, const char *title=nullptr)
Definition: TFCSBinnedShowerONNX.cxx:50
TFCSBinnedShowerONNX::load_upscaling_distribution
void load_upscaling_distribution(const std::string &filename, long unsigned int layer_index)
TFCSBinnedShowerBase
Definition: TFCSBinnedShowerBase.h:17
TFCSBinnedShowerONNX::compute_n_hits_and_elayer
virtual void compute_n_hits_and_elayer(TFCSSimulationState &simulstate) const
Definition: TFCSBinnedShowerONNX.cxx:208
TFCSBinnedShowerONNX::set_default_hit_energy
void set_default_hit_energy(float energy)
Definition: TFCSBinnedShowerONNX.h:48
TFCSBinnedShowerONNX::load_meta_data
void load_meta_data(const std::string &filename, std::vector< long unsigned int > &layers)
Definition: TFCSBinnedShowerONNX.cxx:80
TFCSBinnedShowerONNX::layer_bins_t
Definition: TFCSBinnedShowerONNX.h:28
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
TFCSBinnedShowerONNX::m_n_layers
const long unsigned int m_n_layers
Definition: TFCSBinnedShowerONNX.h:130
TFCSBinnedShowerONNX::load_sub_bin_distribution
void load_sub_bin_distribution(const std::string &filename)
Definition: TFCSBinnedShowerONNX.cxx:528
TFCSBinnedShowerONNX::layer_bins_t::alpha_lower
std::vector< float > alpha_lower
Definition: TFCSBinnedShowerONNX.h:31
TFCSBinnedShowerONNX::~TFCSBinnedShowerONNX
virtual ~TFCSBinnedShowerONNX()
Definition: TFCSBinnedShowerONNX.cxx:53
TFCSBinnedShowerONNX::get_energy_index
long unsigned int get_energy_index(TFCSSimulationState &simulstate, long unsigned int layer_index, long unsigned int hit_index) const
Definition: TFCSBinnedShowerONNX.cxx:288
TFCSBinnedShowerONNX::m_use_upscaling
bool m_use_upscaling
Definition: TFCSBinnedShowerONNX.h:146
TFCSBinnedShowerONNX::delete_event
virtual void delete_event(TFCSSimulationState &simulstate) const override
Definition: TFCSBinnedShowerONNX.cxx:498
TFCSBinnedShowerONNX::set_sub_bin_distribution_and_energies
void set_sub_bin_distribution_and_energies(const std::vector< std::vector< std::vector< std::vector< float >>>> &sub_bin_distribution, const std::vector< float > &upscaling_energies)
Definition: TFCSBinnedShowerONNX.h:90
TFCSBinnedShowerONNX::get_sub_bin_distribution
const std::vector< std::vector< std::vector< std::vector< float > > > > & get_sub_bin_distribution() const
Definition: TFCSBinnedShowerONNX.h:82
TFCSBinnedShowerONNX::event_bins_t
std::vector< layer_bins_t > event_bins_t
Definition: TFCSBinnedShowerONNX.h:35
TFCSBinnedShowerONNX::upscale
void upscale(TFCSSimulationState &simulstate, float &R_min, float &R_max, float &alpha_min, float &alpha_max, long unsigned int layer_index, int bin_index) const
Definition: TFCSBinnedShowerONNX.cxx:348
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:23
TFCSBinnedShowerONNX::get_upscaling_energies
const std::vector< float > & get_upscaling_energies() const
Definition: TFCSBinnedShowerONNX.h:86
TFCSSimulationState.h
TFCSBinnedShowerONNX::layer_bins_t::R_lower
std::vector< float > R_lower
Definition: TFCSBinnedShowerONNX.h:29
TFCSBinnedShowerONNX::enable_upscaling
void enable_upscaling()
Definition: TFCSBinnedShowerONNX.h:45
TFCSBinnedShowerONNX::m_coordinates
event_bins_t m_coordinates
Definition: TFCSBinnedShowerONNX.h:139
TFCSBinnedShowerONNX::get_layer_energy
virtual float get_layer_energy(TFCSSimulationState &simulstate, long unsigned int layer_index) const override
Definition: TFCSBinnedShowerONNX.cxx:270
TFCSSimulationState
Definition: TFCSSimulationState.h:32
TFCSBinnedShowerONNX::disable_upscaling
void disable_upscaling()
Definition: TFCSBinnedShowerONNX.h:46