Julia bindings for the Geant4 toolkit, which simulates the passage of particles through matter.
Geant4.jl gives you two ways to work with Geant4 from Julia:
- The Geant4 C++ classes. They are wrapped with CxxWrap.jl, and the wrapper code is generated automatically with WrapIt. You can call them in much the same way as you would in C++.
- A Julia interface, made of types prefixed with
G4JL. With it, you describe a whole application (detector, physics, primary generator, user actions, sensitive detectors, scoring) as aG4JLApplication, and you write the callbacks as plain Julia functions.
The Geant4 libraries and data files come from the binary package Geant4_jll, so you don't need a local Geant4 installation. The current release uses Geant4 11.4.
Geant4.jl is registered in the Julia General registry:
julia> using Pkg
julia> Pkg.add("Geant4")The first installation downloads the Geant4 libraries and data files, which can take a while.
This example shoots 1 GeV electrons into a box of water and reports how much energy is deposited:
using Geant4, Geant4.SystemOfUnits
# Detector: a box of water inside a world volume of air
struct WaterBox <: G4JLDetector
halfsize::Float64
end
function construct(det::WaterBox)::CxxPtr{G4VPhysicalVolume}
nist = G4NistManager!Instance()
air = FindOrBuildMaterial(nist, "G4_AIR")
water = FindOrBuildMaterial(nist, "G4_WATER")
s = det.halfsize
worldLV = G4LogicalVolume(move!(G4Box("World", 1.2s, 1.2s, 1.2s)), air, "World")
worldPV = G4PVPlacement(nothing, G4ThreeVector(), worldLV, "World", nothing, false, 0)
boxLV = G4LogicalVolume(move!(G4Box("Box", s, s, s)), water, "Box")
G4PVPlacement(nothing, G4ThreeVector(), boxLV, "Box", worldLV, false, 0)
return worldPV
end
Geant4.getConstructor(::WaterBox) = construct
# Simulation data: the total deposited energy
mutable struct EdepData <: G4JLSimulationData
edep::Float64
end
# Stepping action: accumulate the energy deposited in each step
function stepping(step::G4Step, app::G4JLApplication)::Nothing
getSIMdata(app).edep += GetTotalEnergyDeposit(step)
return
end
app = G4JLApplication(detector = WaterBox(20cm),
simdata = EdepData(0.),
generator = G4JLGunGenerator(particle = "e-", energy = 1GeV,
position = G4ThreeVector(0, 0, -23cm),
direction = G4ThreeVector(0, 0, 1)),
physics_type = FTFP_BERT,
stepaction_method = stepping)
configure(app)
initialize(app)
beamOn(app, 100)
println("Mean deposited energy per event: ", app.simdata[1].edep / 100 / MeV, " MeV")The documentation includes:
- a user guide to the
G4JLApplicationinterface: detectors, physics lists, fields, primary generators, user actions, sensitive detectors, scoring, multi-threading and visualization - the API reference
- a set of examples, rendered together with their output
For Geant4 itself, see the Application Developer Guide and the class reference.
The examples are in a separate repository, G4Examples.jl, because they need additional packages for plotting and visualization (Makie, Plots, FHist, ...). They are ports of the Geant4 basic, extended and advanced examples. To set them up:
git clone https://github.com/JuliaHEP/G4Examples.jl.git
cd G4Examples.jl
julia --project=. -e 'import Pkg; Pkg.instantiate()'
julia --project=. basic/B1/B1.jlEach example is also available as a Jupyter notebook.
Run the tests with:
julia --project=. -e 'import Pkg; Pkg.test()'The C++ wrapper library normally comes from the binary package Geant4_julia_jll. To wrap additional Geant4 classes, you can regenerate and build the wrappers locally. This needs WrapIt, which in turn needs libclang. When a local build exists in gen/build, Geant4.jl loads it in preference to the binary package. For details, see Building the wrapper code in the documentation.
