ATLAS Offline Software
Loading...
Searching...
No Matches
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
23class 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;
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
47
48 void set_default_hit_energy(float energy) { m_default_hit_energy = 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?
57 m_ai_simulator->loadSimulator(filename);
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>>>>&
85
86 const std::vector<float>& get_upscaling_energies() const {
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
TFCSBinnedShowerBase(const char *name=nullptr, const char *title=nullptr)
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)
virtual void compute_n_hits_and_elayer(TFCSSimulationState &simulstate) const
float get_default_hit_energy() const
long unsigned int get_energy_index(TFCSSimulationState &simulstate, long unsigned int layer_index, long unsigned int hit_index) const
void load_bin_boundaries(const std::string &filename, long unsigned int layer_index)
void load_simulator(std::string &filename)
std::vector< layer_bins_t > event_bins_t
std::vector< float > m_upscaling_energies
std::vector< std::vector< std::vector< std::vector< float > > > > m_sub_bin_distribution
virtual bool is_match_Ekin_bin(int) const override
void set_default_hit_energy(float energy)
void set_coordinates(const event_bins_t &coordinates)
const std::vector< float > & get_upscaling_energies() const
TFCSMLCalorimeterSimulator * m_ai_simulator
const long unsigned int m_n_layers
const event_bins_t & get_coordinates()
virtual float get_layer_energy(TFCSSimulationState &simulstate, long unsigned int layer_index) const override
void load_sub_bin_distribution(const std::string &filename)
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)
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
const std::vector< std::vector< std::vector< std::vector< float > > > > & get_sub_bin_distribution() const
TFCSBinnedShowerONNX(const char *name=nullptr, const char *title=nullptr)
void set_max_hits_per_voxel(int max_hits)
void load_meta_data(const std::string &filename, std::vector< long unsigned int > &layers)
std::tuple< std::vector< float >, std::vector< hsize_t >, bool > load_hdf5_dataset(const std::string &filename, const std::string &datasetname)
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
virtual long unsigned int get_n_hits(TFCSSimulationState &simulstate, long unsigned int layer_index) const override
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
void load_upscaling_distribution(const std::string &filename, long unsigned int layer_index)
virtual void delete_event(TFCSSimulationState &simulstate) const override