Back to home page

EIC code displayed by LXR

 
 

    


Warning, /geant4/examples/extended/parameterisations/Par04/README.md is written in an unsupported language. File is not indexed.

0001 \page ExamplePar04 Example Par04
0002 
0003 This example demonstrates how to use the Machine Learning (ML) inference
0004 to create energy deposits as a fast simulation model using
0005 <a href="https://github.com/microsoft/onnxruntime">ONNX runtime</a>,
0006  <a href="https://github.com/lwtnn/lwtnn">LWTNN</a>, and
0007  <a href="https://pytorch.org/cppdocs/frontend.html">LibTorch</a> libraries.
0008 
0009  The model used in this example was trained externally (in Python) on data
0010  from this examples' full simulation and can be applied to perform fast simulation.
0011  The python scripts are available in the training folder.
0012 
0013  The geometry used in the example is a cylindrical setup of layers: tungsten
0014  absorber and silicon as the active material. 3D readout geometry (cylindrical)
0015  is defined dynamically,  based on the particle direction at the entrance to the
0016  calorimeter. This is set using a fast simulation model that is triggered at
0017  detector entrance. Analysis of energy deposits is done in the event action,
0018  ntuple with hits is stored.
0019 
0020  ## 1. Detector description
0021 
0022  The detector consists of cylindrical layers of passive and active material,
0023  tungsten and silicon, respectively.
0024 
0025  Fast simulation is attached to the region of the detector.
0026 
0027  Input macro can specify which layer is considered an active layer (sensitive
0028  detector is attached to it). For fast simulation both layers should be marked
0029  as sensitive. It is connected to the way the deposits are created: position is
0030  centre of the layer, which may often fall within the absorber (which is thicker
0031  than the active material). In a realistic detector setup, the positions used in
0032  fast simulation would be calculated properly, to deposit energy within the active
0033  material.
0034 
0035  ## 2. Sensitive detector
0036 
0037  - 2.1. Par04SensitiveDetector
0038  
0039    This SD scores energy originating from showers, in a cylinder around the particle 
0040    direction and position in the calorimeter.
0041     Sensitive detector inherits from both base classes:
0042     - G4VSensitiveDetector: for processing of detailed/non-fast simulation hits
0043     - G4VFastSimSensitiveDetector: for processing of fast sim (G4FastSim) hits.
0044     Hits are placed in the same hit collection, with a different flag to distinguish
0045     between those originated in the full simulation, and those from the fast
0046     simulation.
0047     During visualisation, hits are represented as volumes of different colour:
0048     green for full simulation and red for fast simulation.
0049 
0050  - 2.2. Par04ParallelFullSensitiveDetector
0051 
0052    This SD represents a physical readout structure to the detector (a regular grid).
0053    UI settings are available to set number of slices (azimuthal segmentation) and number
0054    of rows (segmentation along beam axis). Number of layers cannot be changed as it
0055    corresponds to the number of layers placed at the detector construction time. Only
0056    deposits in the active (sensitive) layers are scored in this SD.
0057 
0058  - 2.2. Par04ParallelFastSensitiveDetector
0059 
0060    This SD represents a physical readout that takes into account deposits originating
0061    from fast simulation, so cells span over active and passive layers. This allows to
0062    account all energy from the parameterisation.
0063 
0064  ## 3. Primary generation
0065 
0066  Particle gun is used as a primary generator. 10 GeV electron is used by default.
0067  By default particles are generated along y axis. Those values
0068  can be changed using /gun/ UI commands.
0069 
0070  ## 4. Physics List
0071 
0072  FTFP_BERT modular physics list is used. On top of it, fast simulation physics
0073  is registered for selected particles (electrons, positrons).
0074 
0075 
0076  ## 5. User actions
0077 
0078  - Par04RunAction : run action used for initialization and termination
0079                     of the run. Histograms for analysis of shower development
0080                     in the detector are created.
0081 
0082  - Par04EventAction : event action used for initialization and termination
0083                       of the event. Analysis of shower development is performed
0084                       on event-by-event basis.
0085 
0086  ## 6. ML Inference
0087 
0088  - Par04MLFastSimModel : model used for parametrisation of electrons, positrons,
0089       and gammas. Energy is deposited and
0090       distributed according to inferred values from the ML model.
0091       This class triggers the inference setup, asks for values,
0092       and deposits energies at given positions.
0093 
0094  - Par04InferenceSetup : this class is used to initialize the inference parameters
0095        (user application specific) such as the inference library,
0096       the path and name of the inference model and the size of
0097       the input inference vector(latent dimension and and condition size).
0098       This class constructs this vector and triggers the interface
0099       corresponding to the specified input inference library.
0100       After the inference, the post processing step consists of
0101       scaling back inferred values to the original range.
0102 
0103  - Par04InferenceInterface : is a base class that allows to read in the ML model, configure
0104        and execute inference.
0105 
0106  - Par04OnnxInference and Par04LWTNNInference and Par04TorchInference : inference library specific
0107       classes that inherit from the base class Par04InferenceInterface.
0108 
0109 
0110  ## 7. Output
0111 
0112  The execution of the program (examplePar04) produces an output with histograms.
0113  Ntuples are also stored. They are not merged if the application is run on multiple threads.
0114 
0115  The macro file examplePar04.mac is used to run full simulation. It will simulate 100
0116  events, for single 10 GeV electron beams.
0117  If CMake is able to find inference libraries (LWTNN and/or ONNX Runtime and/or LibTorch), a configuration
0118  macro will be available for that library (examplePar04_lwtnn_vae.mac and/or examplePar04_onnx_vae.mac
0119  and/or examplePar04_torch_vae.mac and/or examplePar04_onnx_calodit.mac and/or
0120  examplePar04_torch_calodit.mac). It will use a trained model to run inference and create showers
0121  in the detector by directly depositing energy.
0122 
0123  There are two models available VAE and CaloDiT-2. CaloDiT-2 is a more sophisticated transformer-based
0124  diffusion model which gives much better accuracy, especially on the cell energy distribution, and
0125  also can be easily adapted to new detectors.
0126  Notes for CaloDiT-2; first, it operates on a lower granular cylindrical virtual mesh than VAE (which became
0127  from this release also the default for full simulation).
0128  Second, we do not support LWTNN inference, as PyTorch to LWTNN conversion is not straightforward.
0129 
0130  ## 8. How to build and run the example
0131 
0132 - LWTNN, ONNX Runtime, and LibTorch are available on LCG. In order to use them, you can set a `CMAKE_PREFIX_PATH`:
0133 ```
0134   % source /cvmfs/sft.cern.ch/lcg/contrib/gcc/11.3.0/x86_64-centos7/setup.sh
0135   % 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>
0136 ```
0137 
0138 - Compile and link to generate the executable (in your CMake build directory):
0139 ```
0140 % cmake <Par04_SOURCE>
0141 % make
0142 ```
0143 
0144 - Execute the application (in batch mode):
0145 ```
0146 % ./examplePar04 -m examplePar04.mac
0147 ```
0148   which produces two root file for full simulation.
0149 
0150 - Execute the application (in interactive mode):
0151 ```
0152 % ./examplePar04 -i -m vis.mac
0153 ```
0154   which allows to visualize hits (from full simulation).
0155 
0156 - If ONNX Runtime is available:
0157 ```
0158 % ./examplePar04 -m examplePar04_onnx_vae.mac
0159 % ./examplePar04 -m examplePar04_onnx_calodit.mac
0160 ```
0161   For interactive mode with visualization:
0162 ```
0163 % ./examplePar04 -i -m vis_onnx_vae.mac
0164 % ./examplePar04 -i -m vis_onnx_calodit.mac
0165 ```
0166 
0167 - If LWTNN is available:
0168 ```
0169 % ./examplePar04 -m examplePar04_lwtnn_vae.mac
0170 ```
0171   For interactive mode with visualization:
0172 ```
0173 % ./examplePar04 -i -m vis_lwtnn_vae.mac
0174 ```
0175 
0176 - If LibTorch is available:
0177 ```
0178 % ./examplePar04 -m examplePar04_torch_vae.mac
0179 % ./examplePar04 -m examplePar04_torch_calodit.mac
0180 ```
0181   For interactive mode with visualization:
0182 ```
0183 % ./examplePar04 -i -m vis_torch_vae.mac
0184 % ./examplePar04 -i -m vis_torch_calodit.mac
0185 ```
0186 
0187 - Additional options available:
0188 ```
0189 % ./examplePar04 -m examplePar04.mac -r 0
0190 ```
0191 For serial run manager mode
0192 ```
0193 % ./examplePar04 -m examplePar04.mac -r 1 -t 8
0194 ```
0195 For multi-threaded run manager mode with 8 threads
0196 ```
0197 % ./examplePar04 -m examplePar04.mac -r 2
0198 ```
0199 For tasking run manager mode with number of tasks that can be change via env variable G4FORCE_EVENTS_PER_TASK
0200 
0201  By default, CMake will attempt to build fast simulation with ONNX Runtime and LWTNN. However, if none
0202  of those libraries is found, it will proceed with full simulation only. The search can be switched
0203  off manually switching CMake flag `INFERENCE_LIB` to `OFF` (`-DINFERENCE_LIB=OFF`)
0204 
0205  ## 9. Macros
0206 
0207  common_settings_lowgran.mac - A macro with common settings, executed by all other macros that use low granularity
0208                               (e.g. detector settings). This can be used directly by fast simulation, and for full sim
0209                               the sensitivity of absorber must be set to false (it's done in examplePar04.mac or vis.mac).
0210 
0211  common_settings_highgran.mac - A macro with common settings, executed by all other macros that use high granularity
0212                               (e.g. detector settings). This can be used directly by fast simulation, and for full sim
0213                               the sensitivity of absorber must be set to false.
0214 
0215  common_settings_vis.mac - A macro with common settings, executed by all visualisation macros.
0216 
0217  common_settings_postInit.mac - A macro with common settings, executed after initialization, e.g. for particle gun settings.
0218 
0219  vis.mac - Allows to run visualization. Pass it to the example in interactive mode ("-i" passed to the executable).
0220            It can be used to visualize full simulation. Lower granularity is used for visualisation. To be compared to CaloDiT-2.
0221 
0222  vis_onnx_vae.mac - Allows to run visualization with ONNX Runtime inference using VAE. Pass it to the example in interactive mode
0223               ("-i" passed to the executable). It contains necessary settings of the inference.
0224 
0225  vis_lwtnn_vae.mac - Allows to run visualization with LWTNN inference using VAE. Pass it to the example in interactive mode
0226               ("-i" passed to the executable). It contains necessary settings of the inference.
0227 
0228  vis_torch_vae.mac - Allows to run visualization with LibTorch inference using VAE. Pass it to the example in interactive mode
0229               ("-i" passed to the executable). It contains necessary settings of the inference.
0230 
0231  examplePar04.mac - Runs full simulation. It will run 100 events with single electrons, 10 GeV and
0232                    along y axis. Lower granularity is used, to be compared with CaloDiT-2.
0233 
0234  examplePar04_onnx_vae.mac - Available only if ONNX Runtime is found by CMake. Runs fast simulation with
0235                         a NN stored in onnx file for VAE.
0236 
0237  examplePar04_lwtnn_vae.mac - Available only if LWTNN is found by CMake. Runs fast simulation with
0238                         a NN stored in json file for VAE.
0239 
0240  examplePar04_torch_vae.mac - Available only if LibTorch is found by CMake. Runs fast simulation with
0241                         a NN stored in pt file for VAE.
0242 
0243  vis_onnx_calodit.mac - Allows to run visualization with ONNX Runtime inference using CaloDiT-2.
0244 
0245  vis_torch_calodit.mac - Allows to run visualization with LibTorch inference using CaloDiT-2.
0246 
0247  examplePar04_onnx_calodit.mac - Available only if ONNX Runtime is found by CMake. Runs fast simulation with
0248                         a NN stored in onnx file for CaloDiT-2.
0249 
0250  examplePar04_torch_calodit.mac - Available only if LibTorch is found by CMake. Runs fast simulation with
0251                         a NN stored in pt file for CaloDiT-2.
0252 
0253  ## 10. UI commands
0254 
0255  UI commands useful in this example:
0256 
0257 - activation/disactivation of the fast simulation model:
0258 ```
0259 /param/ActivateModel inferenceModel
0260 /param/InActivateModel inferenceModel
0261 ```
0262 
0263 - particle gun commands
0264 ```
0265 /gun/particle e-
0266 /gun/energy 10 GeV
0267 /gun/direction 0 1 0
0268 /gun/position 0 0 0
0269 ```
0270 
0271 UI commands defined in this example:
0272 - detector settings
0273 ```
0274 /Par04/detector/setDetectorInnerRadius 80 cm
0275 /Par04/detector/setDetectorLength 2 m
0276 /Par04/detector/setNbOfLayers 90
0277 /Par04/detector/setAbsorber 0 G4_W 1.4 mm false
0278 /Par04/detector/setAbsorber 1 G4_Si 0.3 mm true
0279 ```
0280 
0281 - readout mesh
0282 ```
0283 /Par04/mesh/setSizeOfRhoCells 2.325 mm # (4.65 for CaloDiT-2)
0284 /Par04/mesh/setSizeOfZCells 3.4 mm
0285 /Par04/mesh/setNbOfRhoCells 18  # (9 for CaloDiT-2)
0286 /Par04/mesh/setNbOfPhiCells 50  # (16 for CaloDiT-2)
0287 /Par04/mesh/setNbOfZCells 45
0288 ```
0289 
0290 - inference setup
0291 ```
0292 /Par04/inference/setSizeLatentVector 10
0293 /Par04/inference/setSizeConditionVector 4
0294 /Par04/inference/setModelPathName MLModels/Generator.onnx  # (or cd.onnx for CaloDiT-2)
0295 /Par04/inference/setProfileFlag 0
0296 /Par04/inference/setOptimizationFlag 0
0297 /Par04/inference/setInferenceLibrary ONNX
0298 /Par04/inference/setSizeOfRhoCells 2.325 mm  # (4.65 for CaloDiT-2)
0299 /Par04/inference/setSizeOfZCells 3.4 mm
0300 /Par04/inference/setNbOfRhoCells 18  # (9 for CaloDiT-2)
0301 /Par04/inference/setNbOfPhiCells 50  # (16 for CaloDiT-2)
0302 /Par04/inference/setNbOfZCells 45
0303 ```
0304 
0305  ## 11. Python scripts for training
0306 
0307  The scripts available in the training folder were used to firstly convert
0308  the ROOT files to the h5 files, preprocess the data and then train
0309  the VAE model of this example. More details can be found in README in
0310  training_vae (\ref refPar04training_vae).
0311 
0312  For CaloDiT-2 training and adaptation to new detectors, refer README.md in
0313  training_calodit (\ref refPar04training_calodit).
0314 
0315  ## 12. Public data
0316 
0317  Data generated with full simulation with this example has been published on 
0318  <a href="https://doi.org/10.5281/zenodo.6082201">zenodo</a>.
0319  It was used (as well as VAE) for this publication:
0320   <a href="https://doi.org/10.1016/j.physletb.2023.138079">doi.org/10.1016/j.physletb.2023.138079</a>.
0321 
0322  Data generated with low granularity (so-called dataset2) and high granularity (so-called dataset3) are
0323  released for the CaloChallenge:
0324  dataset2 (lowgran):  <a href="https://doi.org/10.5281/zenodo.6366271">doi.org/10.5281/zenodo.6366271</a>.
0325  dataset3 (highgran):  <a href="https://doi.org/10.5281/zenodo.6366324">doi.org/10.5281/zenodo.6366324</a>.
0326 
0327 ## See more
0328 
0329 - \subpage refPar04training_calodit
0330 - \subpage refPar04training_vae