Introduction to Geant4.jl#
Geant4.jl provides the Julia bindings for the Geant4 particle transportation toolkit. It is using CxxWrap.jl package to wrap C++ types and functions to Julia. Since the Geant4 toolkit is rather large and complex, writing the wrapper code by hand is not really an option. For this we use the package WrapIt that automates the generation of the wrapper code making use of the clang library.
Documentation of the concepts and how to write applications with the Geant4 toolkit can be found with the Application Developer Guide or the Classes and Members reference guide for a detailed description of each C++ class. In this tutorial we will only highlight the differences between the Julia and the C++ API. Therefore it is assumed some prior knowledge of the Geant4 C++ interfaces.
Why Julia?#
The Julia programming language and ecosystem has a nice set of properties that are particularly effective for scientific workflows, in for particular simulation.
Here are some of Julia’s properties
High-performance (usually within a factor of 0.5 of C; often at parity)
Fully Unicode supporting - including LaTeX markup support in REPL, and natural mathematical symbols for common operations (\(\sin^2\theta + cos^2\theta = 1\))
First-class support in Jupyter notebooks (it’s the Ju bit of the name)
Supports generic programming (with multiple dispatch), functional-style method chaining, and other modern paradigms
Dynamic typing, with efficient function specialisation (via JIT) and optional typing supported deeply in language (unlike Python)
Has an excellent, robust package manager making installation and reproducible environments a breeze
Julia is also just a really fun language to program in!
New to Julia?#
There are many good tutorials and books for the Julia programming language. A short introduction targeting people from high energy and nuclear physics with some experience with C++ has been put together by the JuliaHEP team. Other tutorials can be found also here.
Installation#
The Geant4.jl package does no require any special installation. Stable releases are registered to the Julia general registry, and therefore, it can be deployed with the standard Pkg
Julia package manager.
Please note that the first time the Geant4
package is added it may take some time since it downloads all the binary libraries and data files of Geant4
using Pkg
Pkg.add("Geant4")
Wrapped classes#
The notebook 02-wrapped-classes.ipynb introduces some basics for the interaction with the Geant4 wrapped classes. It is important to follow it in order to get familiar with some basic concepts and the mapping between the Julia and C++ constructs.
Geant4 Julia interface#
The main goal for defining a Geant4 application in the Julia interface is to create an instance of the G4JLApplication
type. The instance is constructed specifying all the needed elements for running a Geant4 application as shown in the figure. In particular we need to define the detector geometry, the physics list, the primary particle generator, the type of run manager, the user actions, the sensitive detectors, etc.
The type G4JLApplication
is not a wrapped type from the underlying Geant4 toolkit, it has been added to facilitate development of applications. To distinguish these added types, we have prefixed them by G4JL
.
In the following notebooks will describe and exercise each of these elements needed to build a complete application:
03-geometry.ipynb - to learn how to define detector geometries and materials
04-physics-list.ipynb - to learn how to use predefined physics lists or compose new ones
05-primary-particles.ipynb - to learn how to define primary particles
06-field.ipynb - to learn how to define magnetic field
07-applications.ipynb - building compete applications and using the UI interface to control them
08-sensitive-detectors.ipynb - building applications with sensitive detectors
09-scoring-meshes.ipynb - building applications with scoring meshes
10-histograms.ipynb - to learn how to use the histogram extension to provide analysis functionality
11-event-display.ipynb - use the event display capability and learn how to customize it
Complete examples#
We have also a set of complete examples that combines all the elements described previously.
TestEm3.ipynb - TestEm3 example. HEP calorimeter made of layers, implemented with user actions.
HBC30.ipynb - CERN Liquid Hydrogen Bubble Chamber. It uses user actions and is able to display events that fullfil certain conditions (veto trigger)
WaterPhantom.ipynb - Water Phantom Simulation with scoring meshes to obtain dose distributions.
B3a.ipynb - The example simulates schematically a Positron Emitted Tomography system. It uses a custom primary particle generator, a custom physics list and makes use of two sensitive detectors.
Scintillation.ipynb - Example from the original at settwi/g4-basic-scintillation and adapted to the G4JL interface. Introduces optical photons and a custom physics list. It produces histograms as a result.