Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-23 07:42:00

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /// \file Par04InferenceSetup.hh
0027 /// \brief Definition of the Par04InferenceSetup class
0028 
0029 #ifdef USE_INFERENCE
0030 #  ifndef PAR04INFEERENCESETUP_HH
0031 #    define PAR04INFEERENCESETUP_HH
0032 
0033 #    include "CLHEP/Units/SystemOfUnits.h"  // for mm
0034 
0035 #    include "G4ThreeVector.hh"  // for G4ThreeVector
0036 
0037 #    include <G4String.hh>  // for G4String
0038 #    include <G4SystemOfUnits.hh>  // for mm
0039 #    include <G4Types.hh>  // for G4int, G4double, G4bool, G4f...
0040 #    include <memory>  // for unique_ptr
0041 #    include <vector>  // for vector
0042 class Par04DetectorConstruction;
0043 class Par04InferenceInterface;
0044 class Par04InferenceMessenger;
0045 
0046 /**
0047  * @brief Inference setup.
0048  *
0049  * Constructs the input vector of size b+c to run the inference, b represents
0050  * the size of the latent space (or the encoded space in a Variational
0051  * Autoencoder based model), c represents the size of the conditional vector.
0052  *The b values of the input vector are randomly sampled from b-dimensional
0053  *Gaussian distribution. The c values represent respectively the condition
0054  *values of the particle energy, angle and detector geometry. These condition
0055  *values are user-specific application. The energy rescaling is used to retrieve
0056  *the original energy scale in MeV. Computes the cell position in the detector
0057  *of each inferred energy value.
0058  *
0059  **/
0060 
0061 class Par04InferenceSetup
0062 {
0063   public:
0064     Par04InferenceSetup();
0065     ~Par04InferenceSetup();
0066 
0067     /// Geometry setup
0068     /// Check if inference should be performed for the particle
0069     /// @param[in] aEnergy Particle's energy
0070     G4bool IfTrigger(G4double aEnergy);
0071     /// Set mesh size.
0072     /// @param aSize (x,y,x) size for Carthesian coordinates, or (R, phi, z) for
0073     /// cylindrical coordinates.
0074     inline void SetMeshSize(const G4ThreeVector& aSize) { fMeshSize = aSize; };
0075     /// Get mesh size.
0076     /// @return G4ThreeVector (x,y,x) size for Carthesian coordinates, or (R, phi,
0077     /// z) for cylindrical coordinates.
0078     inline G4ThreeVector GetMeshSize() const { return fMeshSize; };
0079     /// Set number of mesh cells.
0080     /// @param aSize (x,y,x) size for Carthesian coordinates, or (R, phi, z) for
0081     /// cylindrical coordinates.
0082     inline void SetMeshNumber(const G4ThreeVector& aSize) { fMeshNumber = aSize; };
0083     /// Get number of mesh cells.
0084     /// @return G4ThreeVector (x,y,x) size for Carthesian coordinates, or (R, phi,
0085     /// z) for cylindrical coordinates.
0086     inline G4ThreeVector GetMeshNumber() const { return fMeshNumber; };
0087     /// Set size of the condition vector
0088     inline void SetSizeConditionVector(G4int aNumber) { fSizeConditionVector = aNumber; };
0089     /// Get size of the condition vector
0090     inline G4int GetSizeConditionVector() const { return fSizeConditionVector; };
0091     /// Set size of the latent space vector
0092     inline void SetSizeLatentVector(G4int aNumber) { fSizeLatentVector = aNumber; };
0093     /// Get size of the latent space vector
0094     inline G4int GetSizeLatentVector() const { return fSizeLatentVector; };
0095     /// Set path and name of the model
0096     inline void SetModelPathName(G4String aName) { fModelPathName = aName; };
0097     /// Get path and name of the model
0098     inline G4String GetModelPathName() const { return fModelPathName; };
0099     /// Set model type
0100     inline void SetModelType(G4String aName) { fModelType = aName; };
0101     /// Get model type
0102     inline G4String GetModelType() const { return fModelType; };
0103     /// Set profiling flag
0104     inline void SetProfileFlag(G4int aNumber) { fProfileFlag = aNumber; };
0105     /// Get profiling flag
0106     inline G4int GetProfileFlag() const { return fProfileFlag; };
0107     /// Set optimization flag
0108     inline void SetOptimizationFlag(G4int aNumber) { fOptimizationFlag = aNumber; };
0109     /// Get optimization flag
0110     inline G4int GetOptimizationFlag() const { return fOptimizationFlag; };
0111     /// Get name of the inference library
0112     inline G4String GetInferenceLibrary() const { return fInferenceLibrary; };
0113     /// Set name of the inference library and create a pointer to chosen inference
0114     /// interface
0115     void SetInferenceLibrary(G4String aName);
0116     /// Check settings of the inference library
0117     void CheckInferenceLibrary();
0118     /// Set number of Mesh cells in cylindrical coordinates (r, phi, z)
0119     inline void SetMeshNbOfCells(G4ThreeVector aNb) { fMeshNumber = aNb; };
0120     /// Set number of Mesh cells in cylindrical coordinates
0121     /// @param[in] aIndex index of cylindrical axis (0,1,2) = (r, phi, z)
0122     inline void SetMeshNbOfCells(G4int aIndex, G4double aNb) { fMeshNumber[aIndex] = aNb; };
0123     /// Get number of Mesh cells in cylindrical coordinates (r, phi, z)
0124     inline G4ThreeVector GetMeshNbOfCells() const { return fMeshNumber; };
0125     /// Set size of Mesh cells in cylindrical coordinates (r, phi, z)
0126     inline void SetMeshSizeOfCells(G4ThreeVector aNb) { fMeshSize = aNb; };
0127     /// Set size of Mesh cells in cylindrical coordinates
0128     /// @param[in] aIndex index of cylindrical axis (0,1,2) = (r, phi, z)
0129     inline void SetMeshSizeOfCells(G4int aIndex, G4double aNb) { fMeshSize[aIndex] = aNb; };
0130     /// Get size of Mesh cells in cylindrical coordinates (r, phi, z)
0131     inline G4ThreeVector GetMeshSizeOfCells() const { return fMeshSize; };
0132     /// Setting execution providers flags
0133     /// GPU
0134     inline void SetCudaFlag(G4int aNumber) { fCudaFlag = aNumber; };
0135     inline G4int GetCudaFlag() const { return fCudaFlag; };
0136     /// Setting execution providers Options
0137     /// Cuda
0138     inline void SetCudaDeviceId(G4String aNumber) { fCudaDeviceId = aNumber; };
0139     inline G4String GetCudaDeviceId() const { return fCudaDeviceId; };
0140     inline void SetCudaGpuMemLimit(G4String aNumber) { fCudaGpuMemLimit = aNumber; };
0141     inline G4String GetCudaGpuMemLimit() const { return fCudaGpuMemLimit; };
0142     inline void SetCudaArenaExtendedStrategy(G4String aNumber)
0143     {
0144       fCudaArenaExtendedStrategy = aNumber;
0145     };
0146     inline G4String GetCudaArenaExtendedStrategy() const { return fCudaArenaExtendedStrategy; };
0147     inline void SetCudaCudnnConvAlgoSearch(G4String aNumber)
0148     {
0149       fCudaCudnnConvAlgoSearch = aNumber;
0150     };
0151     inline G4String GetCudaCudnnConvAlgoSearch() const { return fCudaCudnnConvAlgoSearch; };
0152     inline void SetCudaDoCopyInDefaultStream(G4String aNumber)
0153     {
0154       fCudaDoCopyInDefaultStream = aNumber;
0155     };
0156     inline G4String GetCudaDoCopyInDefaultStream() const { return fCudaDoCopyInDefaultStream; };
0157     inline void SetCudaCudnnConvUseMaxWorkspace(G4String aNumber)
0158     {
0159       fCudaCudnnConvUseMaxWorkspace = aNumber;
0160     };
0161     inline G4String GetCudaCudnnConvUseMaxWorkspace() const
0162     {
0163       return fCudaCudnnConvUseMaxWorkspace;
0164     };
0165 
0166     /// Execute inference
0167     /// @param[out] aDepositsEnergies of inferred energies deposited in the
0168     /// detector
0169     /// @param[in] aParticleEnergy Energy of initial particle
0170     void GetEnergies(std::vector<G4double>& aEnergies, G4double aParticleEnergy,
0171                    G4float aTheta, G4float aPhi);
0172 
0173     /// Calculate positions
0174     /// @param[out] aDepositsPositions Vector of positions corresponding to
0175     /// energies deposited in the detector
0176     /// @param[in] aParticlePosition Initial particle position which is centre of
0177     /// transverse plane of the mesh
0178     ///            and beginning of the mesh in the longitudinal direction
0179     /// @param[in] aParticleDirection Initial particle direction for the mesh
0180     /// rotation
0181     void GetPositions(std::vector<G4ThreeVector>& aDepositsPositions,
0182                       G4ThreeVector aParticlePosition, G4ThreeVector aParticleDirection);
0183 
0184   private:
0185     /// Cell's size: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical
0186     /// coordinates Can be changed with UI command `/example/mesh/size <x y z>/<r
0187     /// phi z> <unit>`. For cylindrical coordinates phi is ignored and calculated
0188     /// from fMeshNumber.
0189     G4ThreeVector fMeshSize = G4ThreeVector(2.325 * CLHEP::mm, 1, 3.4 * CLHEP::mm);
0190     /// Number of cells: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical
0191     /// coordinates. Can be changed with UI command `/example/mesh/number <Nx Ny
0192     /// Nz>/<Nr Nphi Nz>`
0193     G4ThreeVector fMeshNumber = G4ThreeVector(18, 50, 45);
0194     /// Inference interface
0195     std::unique_ptr<Par04InferenceInterface> fInferenceInterface;
0196     /// Inference messenger
0197     Par04InferenceMessenger* fInferenceMessenger;
0198     /// Maximum particle energy value (in MeV) in the training range
0199     float fMaxEnergy = 1024000.0;
0200     /// Maximum particle angle (in degrees) in the training range
0201     float fMaxAngle = 90.0;
0202     /// Name of the inference library
0203     G4String fInferenceLibrary = "ONNX";
0204     /// Size of the latent space vector
0205     G4int fSizeLatentVector = 10;
0206     /// Size of the condition vector
0207     G4int fSizeConditionVector = 4;
0208     /// Name of the inference library
0209     G4String fModelPathName = "MLModels/Generator.onnx";
0210     /// Model type
0211     G4String fModelType = "VAE";
0212     /// ONNX specific
0213     /// Profiling flag
0214     G4bool fProfileFlag = false;
0215     /// Optimization flag
0216     G4bool fOptimizationFlag = false;
0217     /// Optimization file
0218     G4String fModelSavePath = "MLModels/Optimized-Generator.onnx";
0219     /// Profiling file
0220     G4String fProfilingOutputSavePath = "opt.json";
0221     /// Intra-operation number of threads
0222     G4int fIntraOpNumThreads = 1;
0223     /// Flags for execution providers
0224     /// GPU
0225     G4bool fCudaFlag = false;
0226     /// Execution Provider Options
0227     /// Cuda options
0228     G4String fCudaDeviceId = "0";
0229     G4String fCudaGpuMemLimit = "2147483648";
0230     G4String fCudaArenaExtendedStrategy = "kSameAsRequested";
0231     G4String fCudaCudnnConvAlgoSearch = "DEFAULT";
0232     G4String fCudaDoCopyInDefaultStream = "1";
0233     G4String fCudaCudnnConvUseMaxWorkspace = "1";
0234     std::vector<const char*> cuda_keys{
0235       "device_id",
0236       "gpu_mem_limit",
0237       "arena_extend_strategy",
0238       "cudnn_conv_algo_search",
0239       "do_copy_in_default_stream",
0240       "cudnn_conv_use_max_workspace",
0241     };
0242     std::vector<const char*> cuda_values{
0243       fCudaDeviceId.c_str(),
0244       fCudaGpuMemLimit.c_str(),
0245       fCudaArenaExtendedStrategy.c_str(),
0246       fCudaCudnnConvAlgoSearch.c_str(),
0247       fCudaDoCopyInDefaultStream.c_str(),
0248       fCudaCudnnConvUseMaxWorkspace.c_str(),
0249     };
0250 };
0251 
0252 #  endif /* PAR04INFEERENCESETUP_HH */
0253 #endif