Loading...
Searching...
No Matches
Par04InferenceSetup.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26#ifdef USE_INFERENCE
27#ifndef PAR04INFEERENCESETUP_HH
28#define PAR04INFEERENCESETUP_HH
29
30#include "CLHEP/Units/SystemOfUnits.h" // for mm
31#include "G4ThreeVector.hh" // for G4ThreeVector
32#include <G4String.hh> // for G4String
33#include <G4SystemOfUnits.hh> // for mm
34#include <G4Types.hh> // for G4int, G4double, G4bool, G4f...
35#include <memory> // for unique_ptr
36#include <vector> // for vector
40
41/**
42 * @brief Inference setup.
43 *
44 * Constructs the input vector of size b+c to run the inference, b represents
45 * the size of the latent space (or the encoded space in a Variational
46 * Autoencoder based model), c represents the size of the conditional vector.
47 *The b values of the input vector are randomly sampled from b-dimensional
48 *Gaussian distribution. The c values represent respectively the condition
49 *values of the particle energy, angle and detector geometry. These condition
50 *values are user-specific application. The energy rescaling is used to retrieve
51 *the original energy scale in MeV. Computes the cell position in the detector
52 *of each inferred energy value.
53 *
54 **/
55
57public:
60
61 /// Geometry setup
62 /// Check if inference should be performed for the particle
63 /// @param[in] aEnergy Particle's energy
64 G4bool IfTrigger(G4double aEnergy);
65 /// Set mesh size.
66 /// @param aSize (x,y,x) size for Carthesian coordinates, or (R, phi, z) for
67 /// cylindrical coordinates.
68 inline void SetMeshSize(const G4ThreeVector &aSize) { fMeshSize = aSize; };
69 /// Get mesh size.
70 /// @return G4ThreeVector (x,y,x) size for Carthesian coordinates, or (R, phi,
71 /// z) for cylindrical coordinates.
72 inline G4ThreeVector GetMeshSize() const { return fMeshSize; };
73 /// Set number of mesh cells.
74 /// @param aSize (x,y,x) size for Carthesian coordinates, or (R, phi, z) for
75 /// cylindrical coordinates.
76 inline void SetMeshNumber(const G4ThreeVector &aSize) {
77 fMeshNumber = aSize;
78 };
79 /// Get number of mesh cells.
80 /// @return G4ThreeVector (x,y,x) size for Carthesian coordinates, or (R, phi,
81 /// z) for cylindrical coordinates.
82 inline G4ThreeVector GetMeshNumber() const { return fMeshNumber; };
83 /// Set size of the condition vector
84 inline void SetSizeConditionVector(G4int aNumber) {
85 fSizeConditionVector = aNumber;
86 };
87 /// Get size of the condition vector
88 inline G4int GetSizeConditionVector() const { return fSizeConditionVector; };
89 /// Set size of the latent space vector
90 inline void SetSizeLatentVector(G4int aNumber) {
91 fSizeLatentVector = aNumber;
92 };
93 /// Get size of the latent space vector
94 inline G4int GetSizeLatentVector() const { return fSizeLatentVector; };
95 /// Set path and name of the model
96 inline void SetModelPathName(G4String aName) { fModelPathName = aName; };
97 /// Get path and name of the model
98 inline G4String GetModelPathName() const { return fModelPathName; };
99 /// Set profiling flag
100 inline void SetProfileFlag(G4int aNumber) { fProfileFlag = aNumber; };
101 /// Get profiling flag
102 inline G4int GetProfileFlag() const { return fProfileFlag; };
103 /// Set optimization flag
104 inline void SetOptimizationFlag(G4int aNumber) {
105 fOptimizationFlag = aNumber;
106 };
107 /// Get optimization flag
108 inline G4int GetOptimizationFlag() const { return fOptimizationFlag; };
109 /// Get name of the inference library
110 inline G4String GetInferenceLibrary() const { return fInferenceLibrary; };
111 /// Set name of the inference library and create a pointer to chosen inference
112 /// interface
113 void SetInferenceLibrary(G4String aName);
114 /// Check settings of the inference library
115 void CheckInferenceLibrary();
116 /// Set number of Mesh cells in cylindrical coordinates (r, phi, z)
117 inline void SetMeshNbOfCells(G4ThreeVector aNb) { fMeshNumber = aNb; };
118 /// Set number of Mesh cells in cylindrical coordinates
119 /// @param[in] aIndex index of cylindrical axis (0,1,2) = (r, phi, z)
120 inline void SetMeshNbOfCells(G4int aIndex, G4double aNb) {
121 fMeshNumber[aIndex] = aNb;
122 };
123 /// Get number of Mesh cells in cylindrical coordinates (r, phi, z)
124 inline G4ThreeVector GetMeshNbOfCells() const { return fMeshNumber; };
125 /// Set size of Mesh cells in cylindrical coordinates (r, phi, z)
126 inline void SetMeshSizeOfCells(G4ThreeVector aNb) { fMeshSize = aNb; };
127 /// Set size of Mesh cells in cylindrical coordinates
128 /// @param[in] aIndex index of cylindrical axis (0,1,2) = (r, phi, z)
129 inline void SetMeshSizeOfCells(G4int aIndex, G4double aNb) {
130 fMeshSize[aIndex] = aNb;
131 };
132 /// Get size of Mesh cells in cylindrical coordinates (r, phi, z)
133 inline G4ThreeVector GetMeshSizeOfCells() const { return fMeshSize; };
134 /// Setting execution providers flags
135 /// GPU
136 inline void SetCudaFlag(G4int aNumber) { fCudaFlag = aNumber; };
137 inline G4int GetCudaFlag() const { return fCudaFlag; };
138 /// Setting execution providers Options
139 /// Cuda
140 inline void SetCudaDeviceId(G4String aNumber) { fCudaDeviceId = aNumber; };
141 inline G4String GetCudaDeviceId() const { return fCudaDeviceId; };
142 inline void SetCudaGpuMemLimit(G4String aNumber) {
143 fCudaGpuMemLimit = aNumber;
144 };
145 inline G4String GetCudaGpuMemLimit() const { return fCudaGpuMemLimit; };
146 inline void SetCudaArenaExtendedStrategy(G4String aNumber) {
147 fCudaArenaExtendedStrategy = aNumber;
148 };
149 inline G4String GetCudaArenaExtendedStrategy() const {
150 return fCudaArenaExtendedStrategy;
151 };
152 inline void SetCudaCudnnConvAlgoSearch(G4String aNumber) {
153 fCudaCudnnConvAlgoSearch = aNumber;
154 };
155 inline G4String GetCudaCudnnConvAlgoSearch() const {
156 return fCudaCudnnConvAlgoSearch;
157 };
158 inline void SetCudaDoCopyInDefaultStream(G4String aNumber) {
159 fCudaDoCopyInDefaultStream = aNumber;
160 };
161 inline G4String GetCudaDoCopyInDefaultStream() const {
162 return fCudaDoCopyInDefaultStream;
163 };
164 inline void SetCudaCudnnConvUseMaxWorkspace(G4String aNumber) {
165 fCudaCudnnConvUseMaxWorkspace = aNumber;
166 };
167 inline G4String GetCudaCudnnConvUseMaxWorkspace() const {
168 return fCudaCudnnConvUseMaxWorkspace;
169 };
170
171 /// Execute inference
172 /// @param[out] aDepositsEnergies of inferred energies deposited in the
173 /// detector
174 /// @param[in] aParticleEnergy Energy of initial particle
175 void GetEnergies(std::vector<G4double> &aEnergies, G4double aParticleEnergy,
176 G4float aInitialAngle);
177
178 /// Calculate positions
179 /// @param[out] aDepositsPositions Vector of positions corresponding to
180 /// energies deposited in the detector
181 /// @param[in] aParticlePosition Initial particle position which is centre of
182 /// transverse plane of the mesh
183 /// and beginning of the mesh in the longitudinal direction
184 /// @param[in] aParticleDirection Initial particle direction for the mesh
185 /// rotation
186 void GetPositions(std::vector<G4ThreeVector> &aDepositsPositions,
187 G4ThreeVector aParticlePosition,
188 G4ThreeVector aParticleDirection);
189
190private:
191 /// Cell's size: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical
192 /// coordinates Can be changed with UI command `/example/mesh/size <x y z>/<r
193 /// phi z> <unit>`. For cylindrical coordinates phi is ignored and calculated
194 /// from fMeshNumber.
195 G4ThreeVector fMeshSize =
196 G4ThreeVector(2.325 * CLHEP::mm, 1, 3.4 * CLHEP::mm);
197 /// Number of cells: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical
198 /// coordinates. Can be changed with UI command `/example/mesh/number <Nx Ny
199 /// Nz>/<Nr Nphi Nz>`
200 G4ThreeVector fMeshNumber = G4ThreeVector(18, 50, 45);
201 /// Inference interface
202 std::unique_ptr<Par04InferenceInterface> fInferenceInterface;
203 /// Inference messenger
204 Par04InferenceMessenger *fInferenceMessenger;
205 /// Maximum particle energy value (in MeV) in the training range
206 float fMaxEnergy = 1024000.0;
207 /// Maximum particle angle (in degrees) in the training range
208 float fMaxAngle = 90.0;
209 /// Name of the inference library
210 G4String fInferenceLibrary = "ONNX";
211 /// Size of the latent space vector
212 G4int fSizeLatentVector = 10;
213 /// Size of the condition vector
214 G4int fSizeConditionVector = 4;
215 /// Name of the inference library
216 G4String fModelPathName = "MLModels/Generator.onnx";
217 /// ONNX specific
218 /// Profiling flag
219 G4bool fProfileFlag = false;
220 /// Optimization flag
221 G4bool fOptimizationFlag = false;
222 /// Optimization file
223 G4String fModelSavePath = "MLModels/Optimized-Generator.onnx";
224 /// Profiling file
225 G4String fProfilingOutputSavePath = "opt.json";
226 /// Intra-operation number of threads
227 G4int fIntraOpNumThreads = 1;
228 /// Flags for execution providers
229 /// GPU
230 G4bool fCudaFlag = false;
231 /// Execution Provider Options
232 /// Cuda options
233 G4String fCudaDeviceId = "0";
234 G4String fCudaGpuMemLimit = "2147483648";
235 G4String fCudaArenaExtendedStrategy = "kSameAsRequested";
236 G4String fCudaCudnnConvAlgoSearch = "DEFAULT";
237 G4String fCudaDoCopyInDefaultStream = "1";
238 G4String fCudaCudnnConvUseMaxWorkspace = "1";
239 std::vector<const char *> cuda_keys{
240 "device_id",
241 "gpu_mem_limit",
242 "arena_extend_strategy",
243 "cudnn_conv_algo_search",
244 "do_copy_in_default_stream",
245 "cudnn_conv_use_max_workspace",
246 };
247 std::vector<const char *> cuda_values{
248 fCudaDeviceId.c_str(),
249 fCudaGpuMemLimit.c_str(),
250 fCudaArenaExtendedStrategy.c_str(),
251 fCudaCudnnConvAlgoSearch.c_str(),
252 fCudaDoCopyInDefaultStream.c_str(),
253 fCudaCudnnConvUseMaxWorkspace.c_str(),
254 };
255};
256
257#endif /* PAR04INFEERENCESETUP_HH */
258#endif

Applications | User Support | Publications | Collaboration