Commit e8e92e75 authored by Frank Gaede's avatar Frank Gaede
Browse files

allow to optionally use G4, GFlash or ONNX

 - update the README.md accordingly
 - some minor code cleanup
parent 203319b1
......@@ -46,6 +46,19 @@ where the macro `vis.mac` is executed - or in batch mode running a macro given o
In `test.mac` you can select the particle type, energy and whether GFlash (`/GFlash/flag 1`) or Geant4 (`/GFlash/flag 0`) are used.
### Running ML inference w/ ONNX
If built w/ the ONNX Runtime in the path you can also run the inference w/ a GAN.
For this edit `SimpleCaloConfig.hh` and set ` bool runMLInference = true ;` and recompile (w/ `make install`).
Then you can run the example w/ a ONNX inference using a pre-trained GAN:
```sh
../bin/SimpleCalo test_onnx.mac
```
## Output
The example creates an [EDM4hep](https://github.com/key4hep/EDM4hep) ROOT file `SimpleCaloEDM4hep.root` with two collections:
......
......@@ -35,7 +35,7 @@
#include "G4Timer.hh"
#include "G4UImanager.hh"
#ifdef G4MULTITHREADED_DISABLE
#ifdef G4MULTITHREADED
#include "G4MTRunManager.hh"
#else
#include "G4RunManager.hh"
......@@ -74,7 +74,7 @@ int main(int argc,char** argv)
#ifdef G4MULTITHREADED_DISABLE
#ifdef G4MULTITHREADED
G4MTRunManager * runManager = new G4MTRunManager;
runManager->SetNumberOfThreads(1);
G4cout<<"+-------------------------------------------------------+"<<G4endl;
......
......@@ -44,8 +44,10 @@ public:
}
bool useSamplingParameterisation = false ;//true ;
bool runMLInference = false ;// true
G4double samplingStepInX0 = 0.01 ; // default 0.1
G4double samplingStepInX0 = 0.001 ; // default 0.1
/// size of the experimental hall (cube)
G4double hallSide = 1000.*cm ;
......
......@@ -134,8 +134,10 @@ class SimpleCaloInferenceSetup
/// and beginning of the mesh in the longitudinal direction
/// @param[in] aParticleDirection Initial particle direction for the mesh
/// rotation
void GetPositions(std::vector<G4ThreeVector>& aDepositsPositions, G4ThreeVector aParticlePosition,
void GetPositions(const std::vector<G4double>& aEnergies, std::vector<G4ThreeVector>& aDepositsPositions, G4ThreeVector aParticlePosition,
G4ThreeVector aParticleDirection);
// void GetPositions(std::vector<G4ThreeVector>& aDepositsPositions, G4ThreeVector aParticlePosition,
// G4ThreeVector aParticleDirection);
private:
/// Cell's size: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical
......
......@@ -258,11 +258,11 @@ void SimpleCaloDetectorConstruction::ConstructSDandField()
fFastShowerModel = new GFlashShowerModel("fFastShowerModel", fRegion);
#ifdef USE_INFERENCE
new SimpleCaloMLFastSimModel("inferenceModel", fRegion);
if( SimpleCaloConfig::get().runMLInference ){
new SimpleCaloMLFastSimModel("inferenceModel", fRegion);
}
#endif
fFastShowerModel->SetStepInX0( SimpleCaloConfig::get().samplingStepInX0 ) ;
if( SimpleCaloConfig::get().useSamplingParameterisation ){
......
......@@ -145,54 +145,56 @@ void SimpleCaloInferenceSetup::GetEnergies(std::vector<G4double>& aEnergies, G4d
// Run the inference
fInferenceInterface->RunInference(genVector, aEnergies, size);
// // for(int i = 0; i < size; ++i)
// // {
// // std::cout << aEnergies[i] << ", " ;
// // if( !(i % 100) ) std::cout << std::endl ;
// // }
// // std::cout << std::endl ;
// for(int i = 0; i < size; ++i)
// {
// std::cout << aEnergies[i] << ", " ;
// if( !(i % 100) ) std::cout << std::endl ;
// }
// std::cout << std::endl ;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void SimpleCaloInferenceSetup::GetPositions(std::vector<G4ThreeVector>& aPositions, G4ThreeVector pos0,
void SimpleCaloInferenceSetup::GetPositions(const std::vector<G4double>& aEnergies, std::vector<G4ThreeVector>& aPositions, G4ThreeVector pos0,
G4ThreeVector direction)
{
aPositions.resize(fMeshNumber.x() * fMeshNumber.y() * fMeshNumber.z());
int nLayers = 30 ; // SimpleCaloConfig::get().nLayers ;
int nLayers = fMeshNumber.x() ;
int nCellsX = fMeshNumber.y() ;
int nCellsY = fMeshNumber.z() ;
double absTh = SimpleCaloConfig::get().absThickness ;
double senTh = SimpleCaloConfig::get().sensThickness ;
int nCellsX = 30 ; // round( SimpleCaloConfig::get().width / SimpleCaloConfig::get().cellSize );
int nCellsY = 30 ; // round( SimpleCaloConfig::get().width / SimpleCaloConfig::get().cellSize );
// // std::cout << "\n -------- position setup :"
// // << "\n nLayers " << nLayers
// // << "\n absTh " << absTh
// // << "\n senTh " << senTh
// // << "\n nCellsX " << nCellsX
// // << "\n nCellsY " << nCellsY
// // << "\n SimpleCaloConfig::get().cellSize " << SimpleCaloConfig::get().cellSize
// // << "\n SimpleCaloConfig::get().zStart " << SimpleCaloConfig::get().zStart
// // << std::endl ;
// std::cout << "\n -------- position setup :"
// << "\n nLayers " << nLayers
// << "\n absTh " << absTh
// << "\n senTh " << senTh
// << "\n nCellsX " << nCellsX
// << "\n nCellsY " << nCellsY
// << "\n SimpleCaloConfig::get().cellSize " << SimpleCaloConfig::get().cellSize
// << "\n SimpleCaloConfig::get().zStart " << SimpleCaloConfig::get().zStart
// << std::endl ;
int cpt = 0;
for(G4int iCellZ = 0; iCellZ < fMeshNumber.z(); iCellZ++){
// std::cout << " z = " << iCellZ * ( absTh + senTh ) + absTh + senTh/2. << ", " ;
for(G4int iCellX = 0; iCellX < fMeshNumber.x(); iCellX++){
for(G4int iCellY = 0; iCellY < fMeshNumber.y(); iCellY++){
aPositions[cpt] = {
float( ( iCellX - int( nCellsX / 2 ) + 0.5 ) * SimpleCaloConfig::get().cellSize ),
float( ( iCellY - int( nCellsY / 2 ) + 0.5 ) * SimpleCaloConfig::get().cellSize ),
//0. // this is the local position !?
// float( SimpleCaloConfig::get().zStart + iCellZ * ( absTh + senTh ) + absTh + senTh/2.)
float( iCellZ * ( absTh + senTh ) + absTh + senTh/2.) // start of the first absorber ?
} ;
// std::cout << " position " << aPositions[cpt] << std::endl ;
if( aEnergies[ cpt ] != 0. ) // do not compute the position foir empty cells
aPositions[cpt] = {
float( ( iCellX - int( nCellsX / 2 ) + 0.5 ) * SimpleCaloConfig::get().cellSize ),
float( ( iCellY - int( nCellsY / 2 ) + 0.5 ) * SimpleCaloConfig::get().cellSize ),
float( iCellZ * ( absTh + senTh ) + absTh + senTh/2.)
// here we choose the start of the first absorber, i.e. the entry point into the calorimeter
// this is needed to select the right sensitive volume for the sensitive detector
// as this is done relative to the first entry volume's local coordinate system ...
} ;
// std::cout << " position " << aPositions[cpt] << " energy: " << aEnergies[ cpt ] << std::endl ;
cpt++;
}
}
......
......@@ -92,7 +92,7 @@ void SimpleCaloMLFastSimModel::DoIt(const G4FastTrack& aFastTrack, G4FastStep& a
// calculate how to deposit energy within the detector
// get it from inference model
fInference->GetEnergies(fEnergies, energy, angle);
fInference->GetPositions(fPositions, position, direction);
fInference->GetPositions(fEnergies, fPositions, position, direction);
// deposit energy in the detector using calculated values of energy deposits
// and positions
......
......@@ -114,8 +114,6 @@ void SimpleCaloOnnxInference::RunInference(std::vector<float> aGenVector, std::v
}
}
// --- from python inference:
// --- batch_size = 1
// --- noise = torch.FloatTensor(batch_size, 100, 1, 1, 1).uniform_(-1, 1).detach()
// --- gen_labels = np.random.uniform(10, 100, batch_size)
......
......@@ -9,7 +9,7 @@
/gps/direction 0. 0. 1.0
# Paramatrisation on
/GFlash/flag 1
/GFlash/flag 0
/tracking/verbose 0
......
# exampleSimpleCalo_onnx.mac
#
# test_onnx.mac
# example script to run inference on a GAN w/ ONNX runtime
# --- the particle gun
/gps/pos/centre 0 0 0
/gps/particle e-
/gps/energy 50 GeV
......@@ -10,8 +11,6 @@
# Paramatrisation on
/GFlash/flag 0
# Inference Setup
## dimension of the latent vector (encoded vector in a Variational Autoencoder model)
/SimpleCalo/inference/setSizeLatentVector 100
......@@ -28,21 +27,13 @@
/SimpleCalo/inference/setInferenceLibrary ONNX
## set mesh size for inference == mesh size of a full sim that
## was used for training; it coincides with readout mesh size
/SimpleCalo/inference/setSizeOfRhoCells 2.325 mm
/SimpleCalo/inference/setSizeOfZCells 3.4 mm
#----- use these for x-y-z dimensions
#----- use these mesh numbers for for x-y-z dimensions of the GAN output
/SimpleCalo/inference/setNbOfRhoCells 30
/SimpleCalo/inference/setNbOfPhiCells 30
/SimpleCalo/inference/setNbOfZCells 30
# Fast Simulation
### /analysis/setFileName 10GeV_100events_fastsim_onnx.root
## dynamically set readout mesh from particle direction
## needs to be the first fast sim model!
/param/ActivateModel defineMesh
## ML fast sim, configured with the inference setup /SimpleCalo/inference
/param/ActivateModel inferenceModel
/run/beamOn 100
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment