File indexing completed on 2026-09-09 08:29:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #ifdef USE_INFERENCE
0030 # include "Par04InferenceSetup.hh"
0031
0032 # include "Par04InferenceInterface.hh" // for Par04InferenceInterface
0033 # include "Par04InferenceMessenger.hh" // for Par04InferenceMessenger
0034 # ifdef USE_INFERENCE_ONNX
0035 # include "Par04OnnxInference.hh" // for Par04OnnxInference
0036 # endif
0037 # ifdef USE_INFERENCE_LWTNN
0038 # include "Par04LwtnnInference.hh" // for Par04LwtnnInference
0039 # endif
0040 # ifdef USE_INFERENCE_TORCH
0041 # include "Par04TorchInference.hh" // for Par04TorchInference
0042 # endif
0043 # include "CLHEP/Random/RandGauss.h" // for RandGauss
0044
0045 # include "G4RotationMatrix.hh" // for G4RotationMatrix
0046
0047 # include <CLHEP/Units/SystemOfUnits.h> // for pi, GeV, deg
0048 # include <CLHEP/Vector/Rotation.h> // for HepRotation
0049 # include <CLHEP/Vector/ThreeVector.h> // for Hep3Vector
0050 # include <G4Exception.hh> // for G4Exception
0051 # include <G4ExceptionSeverity.hh> // for FatalException
0052 # include <G4ThreeVector.hh> // for G4ThreeVector
0053 # include <algorithm> // for max, copy
0054 # include <cmath> // for cos, sin
0055 # include <string> // for char_traits, basic_string
0056
0057 # include <ext/alloc_traits.h> // for __alloc_traits<>::value_type
0058
0059
0060
0061 Par04InferenceSetup::Par04InferenceSetup() : fInferenceMessenger(new Par04InferenceMessenger(this))
0062 {}
0063
0064
0065
0066 Par04InferenceSetup::~Par04InferenceSetup() {}
0067
0068
0069
0070 G4bool Par04InferenceSetup::IfTrigger(G4double aEnergy)
0071 {
0072
0073 if (aEnergy > 1 * CLHEP::GeV || aEnergy < 1024 * CLHEP::GeV) return true;
0074 return false;
0075 }
0076
0077
0078
0079 void Par04InferenceSetup::SetInferenceLibrary(G4String aName)
0080 {
0081 fInferenceLibrary = aName;
0082
0083 # ifdef USE_INFERENCE_ONNX
0084 if (fInferenceLibrary == "ONNX")
0085 fInferenceInterface = std::unique_ptr<Par04InferenceInterface>(new Par04OnnxInference(
0086 fModelPathName, fProfileFlag, fOptimizationFlag, fIntraOpNumThreads, fCudaFlag, cuda_keys,
0087 cuda_values, fModelSavePath, fProfilingOutputSavePath));
0088 # endif
0089 # ifdef USE_INFERENCE_LWTNN
0090 if (fInferenceLibrary == "LWTNN")
0091 fInferenceInterface =
0092 std::unique_ptr<Par04InferenceInterface>(new Par04LwtnnInference(fModelPathName));
0093 # endif
0094 # ifdef USE_INFERENCE_TORCH
0095 if (fInferenceLibrary == "TORCH")
0096 fInferenceInterface =
0097 std::unique_ptr<Par04InferenceInterface>(new Par04TorchInference(fModelPathName));
0098 # endif
0099
0100 CheckInferenceLibrary();
0101 }
0102
0103
0104
0105 void Par04InferenceSetup::CheckInferenceLibrary()
0106 {
0107 G4String msg = "Please choose inference library from available libraries (";
0108 # ifdef USE_INFERENCE_ONNX
0109 msg += "ONNX,";
0110 # endif
0111 # ifdef USE_INFERENCE_LWTNN
0112 msg += "LWTNN,";
0113 # endif
0114 # ifdef USE_INFERENCE_TORCH
0115 msg += "TORCH";
0116 # endif
0117 if (fInferenceInterface == nullptr)
0118 G4Exception("Par04InferenceSetup::CheckInferenceLibrary()", "InvalidSetup", FatalException,
0119 (msg + "). Current name: " + fInferenceLibrary).c_str());
0120 }
0121
0122
0123
0124 void Par04InferenceSetup::GetEnergies(std::vector<G4double>& aEnergies, G4double aInitialEnergy,
0125 G4float aTheta, G4float aPhi)
0126 {
0127
0128 CheckInferenceLibrary();
0129
0130 int size = fMeshNumber.x() * fMeshNumber.y() * fMeshNumber.z();
0131 std::vector<G4float> genVector;
0132
0133 if (fModelType == "VAE")
0134 {
0135 genVector.assign(fSizeLatentVector + fSizeConditionVector, 0);
0136
0137
0138 for (int i = 0; i < fSizeLatentVector; ++i) {
0139 genVector[i] = CLHEP::RandGauss::shoot(0., 1.);
0140 }
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 genVector[fSizeLatentVector] = aInitialEnergy / fMaxEnergy;
0153
0154 genVector[fSizeLatentVector + 1] = (aTheta / (CLHEP::deg)) / fMaxAngle;
0155
0156 genVector[fSizeLatentVector + 2] = 0;
0157 genVector[fSizeLatentVector + 3] = 1;
0158 } else if (fModelType == "CaloDiT-2")
0159 {
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173 genVector.assign(8, 0);
0174
0175 genVector[0] = aInitialEnergy / 1000;
0176 genVector[1] = aPhi;
0177 genVector[2] = aTheta;
0178 genVector[3] = 1.0;
0179 }
0180
0181 fInferenceInterface->RunInference(genVector, aEnergies, size);
0182
0183
0184
0185 if (fModelType == "VAE")
0186
0187 {
0188 for (int i = 0; i < size; ++i) {
0189 aEnergies[i] = aEnergies[i] * aInitialEnergy;
0190 }
0191 } else if (fModelType == "CaloDiT-2")
0192
0193 {
0194 for (int i = 0; i < size; ++i){
0195 aEnergies[i] = aEnergies[i] * 1000;
0196 }
0197 }
0198 }
0199
0200
0201
0202 void Par04InferenceSetup::GetPositions(std::vector<G4ThreeVector>& aPositions, G4ThreeVector pos0,
0203 G4ThreeVector direction)
0204 {
0205 aPositions.resize(fMeshNumber.x() * fMeshNumber.y() * fMeshNumber.z());
0206
0207
0208
0209 G4RotationMatrix rotMatrix = G4RotationMatrix();
0210 double particleTheta = direction.theta();
0211 double particlePhi = direction.phi();
0212 rotMatrix.rotateZ(-particlePhi);
0213 rotMatrix.rotateY(-particleTheta);
0214 G4RotationMatrix rotMatrixInv = CLHEP::inverseOf(rotMatrix);
0215
0216 int cpt = 0;
0217 for (G4int iCellR = 0; iCellR < fMeshNumber.x(); iCellR++) {
0218 for (G4int iCellPhi = 0; iCellPhi < fMeshNumber.y(); iCellPhi++) {
0219 for (G4int iCellZ = 0; iCellZ < fMeshNumber.z(); iCellZ++) {
0220 aPositions[cpt] =
0221 pos0
0222 + rotMatrixInv
0223 * G4ThreeVector(
0224 (iCellR + 0.5) * fMeshSize.x()
0225 * std::cos((iCellPhi + 0.5) * 2 * CLHEP::pi / fMeshNumber.y() - CLHEP::pi),
0226 (iCellR + 0.5) * fMeshSize.x()
0227 * std::sin((iCellPhi + 0.5) * 2 * CLHEP::pi / fMeshNumber.y() - CLHEP::pi),
0228 (iCellZ + 0.5) * fMeshSize.z());
0229 cpt++;
0230 }
0231 }
0232 }
0233 }
0234
0235 #endif