This example demonstrates how to use the Machine Learning (ML) inference to create energy deposits as a fast simulation model using ONNX runtime, LWTNN, and LibTorch libraries.
The model used in this example was trained externally (in Python) on data from this examples' full simulation and can be applied to perform fast simulation. The python scripts are available in the training folder.
The geometry used in the example is a cylindrical setup of layers: tungsten absorber and silicon as the active material. 3D readout geometry (cylindrical) is defined dynamically, based on the particle direction at the entrance to the calorimeter. This is set using a fast simulation model that is triggered at detector entrance. Analysis of energy deposits is done in the event action, ntuple with hits is stored.
The detector consists of cylindrical layers of passive and active material, tungsten and silicon, respectively.
Fast simulation is attached to the region of the detector.
Input macro can specify which layer is considered an active layer (sensitive detector is attached to it). For fast simulation both layers should be marked as sensitive. It is connected to the way the deposits are created: position is centre of the layer, which may often fall within the absorber (which is thicker than the active material). In a realistic detector setup, the positions used in fast simulation would be calculated properly, to deposit energy within the active material.
This SD scores energy originating from showers, in a cylinder around the particle direction and position in the calorimeter. Sensitive detector inherits from both base classes:
This SD represents a physical readout structure to the detector (a regular grid). UI settings are available to set number of slices (azimuthal segmentation) and number of rows (segmentation along beam axis). Number of layers cannot be changed as it corresponds to the number of layers placed at the detector construction time. Only deposits in the active (sensitive) layers are scored in this SD.
This SD represents a physical readout that takes into account deposits originating from fast simulation, so cells span over active and passive layers. This allows to account all energy from the parameterisation.
Particle gun is used as a primary generator. 10 GeV electron is used by default. By default particles are generated along y axis. Those values can be changed using /gun/ UI commands.
FTFP_BERT modular physics list is used. On top of it, fast simulation physics is registered for selected particles (electrons, positrons).
The execution of the program (examplePar04) produces an output with histograms. Ntuples are also stored. They are not merged if the application is run on multiple threads.
The macro file examplePar04.mac is used to run full simulation. It will simulate 100 events, for single 10 GeV electron beams. If CMake is able to find inference libraries (LWTNN and/or ONNX Runtime and/or LibTorch), a configuration macro will be available for that library (examplePar04_lwtnn.mac and/or examplePar04_onnx.mac and/or examplePar04_torch.mac). It will use a trained model to run inference and create showers in the detector by directly depositing energy.
CMAKE_PREFIX_PATH
: % source /cvmfs/sft.cern.ch/lcg/contrib/gcc/11.3.0/x86_64-centos7/setup.sh % cmake -DCMAKE_PREFIX_PATH="/cvmfs/sft.cern.ch/lcg/releases/LCG_102b/lwtnn/2.11.1/x86_64-centos7-gcc11-opt/;/cvmfs/sft.cern.ch/lcg/releases/LCG_102b/onnxruntime/1.11.1/x86_64-centos7-gcc11-opt/;/cvmfs/sft.cern.ch/lcg/releases/LCG_102b/torch/1.11.0/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/torch/" <Par04_SOURCE>
% cmake <Par04_SOURCE> % make
% ./examplePar04 -m examplePar04.macwhich produces two root file for full simulation.
% ./examplePar04 -i -m vis.macwhich allows to visualize hits (from full simulation).
% ./examplePar04 -m examplePar04_onnx.macFor interactive mode with visualization:
% ./examplePar04 -i -m vis_onnx.mac
% ./examplePar04 -m examplePar04_lwtnn.macFor interactive mode with visualization:
% ./examplePar04 -i -m vis_lwtnn.mac
% ./examplePar04 -m examplePar04_torch.macFor interactive mode with visualization:
% ./examplePar04 -i -m vis_torch.mac
Additional options available:
% ./examplePar04 -m examplePar04.mac -r 0
For serial run manager mode
% ./examplePar04 -m examplePar04.mac -r 1 -t 8
For multi-threaded run manager mode with 8 threads
% ./examplePar04 -m examplePar04.mac -r 2
For tasking run manager mode with number of tasks that can be change via env variable G4FORCE_EVENTS_PER_TASK
By default, CMake will attempt to build fast simulation with ONNX Runtime and LWTNN. However, if none of those libraries is found, it will proceed with full simulation only. The search can be switched off manually switching CMake flag INFERENCE_LIB
to OFF
(-DINFERENCE_LIB=OFF
)
common_settings.mac - A macro with common settings, executed by all other macros (e.g. detector settings).
vis.mac - Allows to run visualization. Pass it to the example in interactive mode ("-i" passed to the executable). It can be used to visualize full simulation.
vis_onnx.mac - Allows to run visualization with ONNX Runtime inference. Pass it to the example in interactive mode ("-i" passed to the executable). It contains necessary settings of the inference.
vis_lwtnn.mac - Allows to run visualization with LWTNN inference. Pass it to the example in interactive mode ("-i" passed to the executable). It contains necessary settings of the inference.
vis_torch.mac - Allows to run visualization with LibTorch inference. Pass it to the example in interactive mode ("-i" passed to the executable). It contains necessary settings of the inference.
examplePar04.mac - Runs full simulation. It will run 100 events with single electrons, 10 GeV and along y axis.
examplePar04_onnx.mac - Available only if ONNX Runtime is found by CMake. Runs fast simulation with a NN stored in onnx file.
examplePar04_lwtnn.mac - Available only if LWTNN is found by CMake. Runs fast simulation with a NN stored in json file.
examplePar04_torch.mac - Available only if LibTorch is found by CMake. Runs fast simulation with a NN stored in pt file.
UI commands useful in this example:
/param/ActivateModel inferenceModel /param/InActivateModel inferenceModel
particle gun commands
/gun/particle e- /gun/energy 10 GeV /gun/direction 0 1 0 /gun/position 0 0 0
UI commands defined in this example:
/Par04/detector/setDetectorInnerRadius 80 cm /Par04/detector/setDetectorLength 2 m /Par04/detector/setNbOfLayers 90 /Par04/detector/setAbsorber 0 G4_W 1.4 mm false /Par04/detector/setAbsorber 1 G4_Si 0.3 mm true
/Par04/mesh/setSizeOfRhoCells 2.325 mm /Par04/mesh/setSizeOfZCells 3.4 mm /Par04/mesh/setNbOfRhoCells 18 /Par04/mesh/setNbOfPhiCells 50 /Par04/mesh/setNbOfZCells 45
/Par04/inference/setSizeLatentVector 10 /Par04/inference/setSizeConditionVector 4 /Par04/inference/setModelPathName MLModels/Generator.onnx /Par04/inference/setProfileFlag 0 /Par04/inference/setOptimizationFlag 0 /Par04/inference/setInferenceLibrary ONNX /Par04/inference/setSizeOfRhoCells 2.325 mm /Par04/inference/setSizeOfZCells 3.4 mm /Par04/inference/setNbOfRhoCells 18 /Par04/inference/setNbOfPhiCells 50 /Par04/inference/setNbOfZCells 45
The scripts available in the training folder were used to firstly convert the ROOT files to the h5 files, preprocess the data and then train the VAE model of this example. More details can be found in training/README.
Data generated with full simulation with this example has been published on zenodo.