Import Geant4 11.4.0.beta source tree
This commit is contained in:
@@ -104,7 +104,7 @@ int Par04Hit::operator==(const Par04Hit& aRight) const
|
||||
void Par04Hit::Draw()
|
||||
{
|
||||
/// Arbitrary size corresponds to the example macros
|
||||
G4ThreeVector meshSize(2.325 * mm, 2 * CLHEP::pi / 50. * CLHEP::rad, 3.4 * mm);
|
||||
G4ThreeVector meshSize(4.65 * mm, 2 * CLHEP::pi / 18. * CLHEP::rad, 3.4 * mm);
|
||||
G4int numPhiCells = CLHEP::pi * 2. / meshSize.y();
|
||||
G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
|
||||
// Hits can be filtered out in visualisation
|
||||
@@ -166,7 +166,7 @@ std::vector<G4AttValue>* Par04Hit::CreateAttValues() const
|
||||
|
||||
void Par04Hit::Print()
|
||||
{
|
||||
std::cout << "\tHit " << fEdep / MeV << " MeV from " << fNdep << " deposits at " << fPos / cm
|
||||
<< " cm rotation " << fRot << " (R,phi,z)= (" << fRhoId << ", " << fPhiId << ", "
|
||||
<< fZId << "), " << fTime << " ns" << std::endl;
|
||||
G4cout << "\tHit " << fEdep / MeV << " MeV from " << fNdep << " deposits at " << fPos / cm
|
||||
<< " cm with rotation " << fRot << " (R,phi,z)= (" << fRhoId << ", " << fPhiId << ", "
|
||||
<< fZId << "), " << fTime << " ns" << G4endl;
|
||||
}
|
||||
|
||||
@@ -78,6 +78,12 @@ Par04InferenceMessenger::Par04InferenceMessenger(Par04InferenceSetup* aInference
|
||||
fModelPathNameCmd->AvailableForStates(G4State_Idle);
|
||||
fModelPathNameCmd->SetToBeBroadcasted(true);
|
||||
|
||||
fModelTypeCmd = new G4UIcmdWithAString("/Par04/inference/setModelType", this);
|
||||
fModelTypeCmd->SetGuidance("Model type");
|
||||
fModelTypeCmd->SetParameterName("Name", false);
|
||||
fModelTypeCmd->AvailableForStates(G4State_Idle);
|
||||
fModelTypeCmd->SetToBeBroadcasted(true);
|
||||
|
||||
fProfileFlagCmd = new G4UIcmdWithAnInteger("/Par04/inference/setProfileFlag", this);
|
||||
fProfileFlagCmd->SetGuidance("Flag to save a json file for model execution profiling.");
|
||||
fProfileFlagCmd->SetParameterName("ProfileFlag", false);
|
||||
@@ -193,6 +199,7 @@ Par04InferenceMessenger::~Par04InferenceMessenger()
|
||||
delete fSizeLatentVectorCmd;
|
||||
delete fSizeConditionVectorCmd;
|
||||
delete fModelPathNameCmd;
|
||||
delete fModelTypeCmd;
|
||||
delete fProfileFlagCmd;
|
||||
delete fOptimizationFlagCmd;
|
||||
delete fMeshNbRhoCellsCmd;
|
||||
@@ -218,6 +225,9 @@ void Par04InferenceMessenger::SetNewValue(G4UIcommand* aCommand, G4String aNewVa
|
||||
if (aCommand == fModelPathNameCmd) {
|
||||
fInference->SetModelPathName(aNewValue);
|
||||
}
|
||||
if (aCommand == fModelTypeCmd) {
|
||||
fInference->SetModelType(aNewValue);
|
||||
}
|
||||
if (aCommand == fProfileFlagCmd) {
|
||||
fInference->SetProfileFlag(std::stoi(aNewValue));
|
||||
}
|
||||
@@ -284,6 +294,9 @@ G4String Par04InferenceMessenger::GetCurrentValue(G4UIcommand* aCommand)
|
||||
if (aCommand == fModelPathNameCmd) {
|
||||
cv = fModelPathNameCmd->ConvertToString(fInference->GetModelPathName());
|
||||
}
|
||||
if (aCommand == fModelTypeCmd) {
|
||||
cv = fModelTypeCmd->ConvertToString(fInference->GetModelType());
|
||||
}
|
||||
if (aCommand == fProfileFlagCmd) {
|
||||
cv = fSizeLatentVectorCmd->ConvertToString(fInference->GetProfileFlag());
|
||||
}
|
||||
|
||||
@@ -119,15 +119,19 @@ void Par04InferenceSetup::CheckInferenceLibrary()
|
||||
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
|
||||
|
||||
void Par04InferenceSetup::GetEnergies(std::vector<G4double>& aEnergies, G4double aInitialEnergy,
|
||||
G4float aInitialAngle)
|
||||
G4float aTheta, G4float aPhi)
|
||||
{
|
||||
// First check if inference library was set correctly
|
||||
CheckInferenceLibrary();
|
||||
// size represents the size of the output vector
|
||||
int size = fMeshNumber.x() * fMeshNumber.y() * fMeshNumber.z();
|
||||
std::vector<G4float> genVector;
|
||||
|
||||
if (fModelType == "VAE")
|
||||
{
|
||||
genVector.assign(fSizeLatentVector + fSizeConditionVector, 0);
|
||||
|
||||
// randomly sample from a gaussian distribution in the latent space
|
||||
std::vector<G4float> genVector(fSizeLatentVector + fSizeConditionVector, 0);
|
||||
for (int i = 0; i < fSizeLatentVector; ++i) {
|
||||
genVector[i] = CLHEP::RandGauss::shoot(0., 1.);
|
||||
}
|
||||
@@ -144,18 +148,49 @@ void Par04InferenceSetup::GetEnergies(std::vector<G4double>& aEnergies, G4double
|
||||
// 1. energy
|
||||
genVector[fSizeLatentVector] = aInitialEnergy / fMaxEnergy;
|
||||
// 2. angle
|
||||
genVector[fSizeLatentVector + 1] = (aInitialAngle / (CLHEP::deg)) / fMaxAngle;
|
||||
genVector[fSizeLatentVector + 1] = (aTheta / (CLHEP::deg)) / fMaxAngle;
|
||||
// 3. geometry
|
||||
genVector[fSizeLatentVector + 2] = 0;
|
||||
genVector[fSizeLatentVector + 3] = 1;
|
||||
} else if (fModelType == "CaloDiT-2")
|
||||
{
|
||||
// fSizeLatentVector & fSizeConditionVector are ignored for CaloDiT-2
|
||||
// Conditions (dim) are energy (1), phi (1), theta (1) and geo (5)
|
||||
// The energy range here is 1 GeV - 1TeV, phi goes from 0 to 2pi,
|
||||
// and theta goes from 0.87 to 2.27.
|
||||
// And, geo is one-hot encoding describing the 4 geometries the model
|
||||
// is trained on.
|
||||
// Order of the geo condition is Par04SiW (this one), Par04SciPb, ODD, FCCeeCLD
|
||||
// As CaloDiT-2 is trained on these 4 detectors, it can be quickly adapted to
|
||||
// any new detector (see CaloDiT-2 readme for adaptation) of your choice. Thus
|
||||
// reusing the knowledge from these previous detectors.
|
||||
// To use the adapted model, make the following changes for inference:
|
||||
// genVector[3] = 0.0; (turning OFF Par04SiW)
|
||||
// genVector[7] = 1.0; (turning ON a new detector)
|
||||
genVector.assign(8, 0);
|
||||
|
||||
genVector[0] = aInitialEnergy / 1000; // convert to GeV
|
||||
genVector[1] = aPhi;
|
||||
genVector[2] = aTheta;
|
||||
genVector[3] = 1.0; //Par04SiW
|
||||
}
|
||||
// Run the inference
|
||||
fInferenceInterface->RunInference(genVector, aEnergies, size);
|
||||
|
||||
// After the inference rescale back to the initial energy (in this example the
|
||||
// energies of cells were normalized to the energy of the particle)
|
||||
// After the inference rescale back to the initial energy
|
||||
|
||||
if (fModelType == "VAE")
|
||||
// For VAE, energies of cells were normalized to the energy of the particle
|
||||
{
|
||||
for (int i = 0; i < size; ++i) {
|
||||
aEnergies[i] = aEnergies[i] * aInitialEnergy;
|
||||
}
|
||||
} else if (fModelType == "CaloDiT-2")
|
||||
// For CaloDiT-2, energies were scaled by a factor of 1000
|
||||
{
|
||||
for (int i = 0; i < size; ++i){
|
||||
aEnergies[i] = aEnergies[i] * 1000;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -90,18 +90,19 @@ void Par04MLFastSimModel::DoIt(const G4FastTrack& aFastTrack, G4FastStep& aFastS
|
||||
{
|
||||
// remove particle from further processing by G4
|
||||
aFastStep.KillPrimaryTrack();
|
||||
aFastStep.SetPrimaryTrackPathLength(0.0);
|
||||
aFastStep.ProposePrimaryTrackPathLength(0.);
|
||||
G4double energy = aFastTrack.GetPrimaryTrack()->GetKineticEnergy();
|
||||
aFastStep.SetTotalEnergyDeposited(energy);
|
||||
aFastStep.ProposeTotalEnergyDeposited(energy);
|
||||
G4ThreeVector position = aFastTrack.GetPrimaryTrack()->GetPosition();
|
||||
G4ThreeVector direction = aFastTrack.GetPrimaryTrack()->GetMomentumDirection();
|
||||
|
||||
// calculate the incident angle
|
||||
G4float angle = direction.theta();
|
||||
// calculate the incident angles
|
||||
G4float theta = direction.theta();
|
||||
G4float phi = direction.phi();
|
||||
|
||||
// calculate how to deposit energy within the detector
|
||||
// get it from inference model
|
||||
fInference->GetEnergies(fEnergies, energy, angle);
|
||||
fInference->GetEnergies(fEnergies, energy, theta, phi);
|
||||
fInference->GetPositions(fPositions, position, direction);
|
||||
|
||||
// deposit energy in the detector using calculated values of energy deposits
|
||||
|
||||
@@ -28,13 +28,13 @@
|
||||
|
||||
# include "Par04InferenceInterface.hh" // for Par04InferenceInterface
|
||||
|
||||
# include <onnxruntime_cxx_api.h> // for Value, Session, Env
|
||||
# include <algorithm> // for copy, max
|
||||
# include <cassert> // for assert
|
||||
# include <cstddef> // for size_t
|
||||
# include <cstdint> // for int64_t
|
||||
# include <utility> // for move
|
||||
|
||||
# include <core/session/onnxruntime_cxx_api.h> // for Value, Session, Env
|
||||
# ifdef USE_CUDA
|
||||
# include "cuda_runtime_api.h"
|
||||
# endif
|
||||
|
||||
@@ -48,35 +48,44 @@ Par04TorchInference::Par04TorchInference(G4String modelPath) : Par04InferenceInt
|
||||
void Par04TorchInference::RunInference(std::vector<float> aGenVector,
|
||||
std::vector<G4double>& aEnergies, int aSize)
|
||||
{
|
||||
// latentSize : size of the latent space
|
||||
// 4 is the size of the condition vector
|
||||
int latentSize = aGenVector.size() - 4;
|
||||
// split into latent and condition vectors
|
||||
std::vector<float> latent;
|
||||
for (int i = 0; i < latentSize; i++) {
|
||||
latent.push_back(aGenVector[i]);
|
||||
}
|
||||
std::vector<float> energy;
|
||||
energy.push_back(aGenVector[latentSize + 1]);
|
||||
std::vector<float> angle;
|
||||
energy.push_back(aGenVector[latentSize + 2]);
|
||||
std::vector<float> geo;
|
||||
for (int i = latentSize + 2; i < latentSize + 4; i++) {
|
||||
geo.push_back(aGenVector[i]);
|
||||
}
|
||||
|
||||
// convert vectors to tensors
|
||||
torch::Tensor latentVector = torch::tensor(latent);
|
||||
torch::Tensor eTensor = torch::tensor(energy);
|
||||
torch::Tensor angleTensor = torch::tensor(angle);
|
||||
torch::Tensor geoTensor = torch::tensor(geo);
|
||||
|
||||
std::vector<torch::jit::IValue> genInput;
|
||||
|
||||
genInput.push_back(latentVector);
|
||||
genInput.push_back(eTensor);
|
||||
genInput.push_back(angleTensor);
|
||||
genInput.push_back(geoTensor);
|
||||
if (aGenVector.size()!=8) {
|
||||
// VAE
|
||||
// latentSize : size of the latent space
|
||||
// 4 is the size of the condition vector
|
||||
int latentSize = aGenVector.size() - 4;
|
||||
// split into latent and condition vectors
|
||||
std::vector<float> latent;
|
||||
for (int i = 0; i < latentSize; i++) {
|
||||
latent.push_back(aGenVector[i]);
|
||||
}
|
||||
std::vector<float> energy;
|
||||
energy.push_back(aGenVector[latentSize + 1]);
|
||||
std::vector<float> angle;
|
||||
angle.push_back(aGenVector[latentSize + 2]);
|
||||
std::vector<float> geo;
|
||||
for (int i = latentSize + 2; i < latentSize + 4; i++) {
|
||||
geo.push_back(aGenVector[i]);
|
||||
}
|
||||
|
||||
// convert vectors to tensors
|
||||
torch::Tensor latentVector = torch::tensor(latent);
|
||||
torch::Tensor eTensor = torch::tensor(energy);
|
||||
torch::Tensor angleTensor = torch::tensor(angle);
|
||||
torch::Tensor geoTensor = torch::tensor(geo);
|
||||
|
||||
genInput.push_back(latentVector);
|
||||
genInput.push_back(eTensor);
|
||||
genInput.push_back(angleTensor);
|
||||
genInput.push_back(geoTensor);
|
||||
} else {
|
||||
// CaloDiT-2
|
||||
torch::Tensor conditions = torch::tensor(aGenVector);
|
||||
genInput.push_back(conditions);
|
||||
}
|
||||
// equivalent to torch.no_grad()
|
||||
torch::NoGradGuard no_grad;
|
||||
|
||||
at::Tensor outTensor = fModule.forward(genInput).toTensor().contiguous();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user