Geant4 Python Application
This is an experiment at providing a pythonic interface to Geant4.
The goal is not to provide a full python interface to Geant4, but rather to provide a high-level interface to a generic Geant4 application which is highly configurable.
If you are looking for a full set of python bindings for Geant4, I recommend looking at geant4_pybind.
Overview
- Configurable Geant4 application written in C++
- Python interface to the application via pybind11
- The complete event data is available as an awkward array
New to Geant4 or this package? See the Python developer guide.
Platform support
Tested on macOS and Ubuntu; still undergoing broader testing. If you hit a bug, please report it via GitHub Issues or reach out to kavyavadhwa@gmail.com.
Installation
The package is published on PyPI. Install it into a fresh virtual environment: mixing it with packages already in your system or base conda Python (other Qt/PyQt, numpy or Geant4 builds) is the most common cause of library mismatch errors.
python3 -m venv g4-env
source g4-env/bin/activate
python -m pip install --upgrade pip
pip install "geant4-python-application[gui]"
With conda, create a new environment instead of using base:
conda create -n g4 python=3.12 -y
conda activate g4
pip install "geant4-python-application[gui]"
Activate the environment again (source g4-env/bin/activate or
conda activate g4) in every new terminal before using the package.
-
Prebuilt wheels for macOS (Apple Silicon and Intel) and Linux x86_64, Python 3.8–3.13. Geant4 is bundled, so no separate Geant4 (C++) installation is needed.
-
[gui]adds the interactive Qt viewer (app.visualize()); leave it out for a physics-only install. -
Check the installation:
python -c "import geant4_python_application as g4; print(g4.__version__)"
-
The first simulation downloads Geant4's data files (~2 GB) once per user, to
~/Library/Application Support/geant4_python_application(macOS) or~/.local/share/geant4_python_application(Linux). SetGEANT4_PYTHON_APPLICATION_DIRto use another location. -
Windows, Linux aarch64, building from source and data file management are covered in INSTALLATION.md. On Windows, WSL works with the Linux wheels.
The examples are not part of the package; run them from a clone of the repository:
git clone https://github.com/lobis/geant4-python-application
cd geant4-python-application
python examples/geant4-examples/Geant4-11.4.2-examples-python/basic/B1/b1.py --batch -n 10
--batch runs headless; drop it to open the Qt viewer.
Usage
import geant4_python_application as g4
with g4.Application(gdml=g4.basic_gdml, seed=137) as app:
events = app.run(100)
print(events)
A Geant4 reference physics list can be selected by name:
with g4.Application(gdml=g4.basic_gdml, physics="FTFP_BERT") as app:
events = app.run(100)
print(g4.Application.available_physics_lists())
Particle guns and Geant4's General Particle Source have a Python interface:
with g4.Application(gdml=g4.basic_gdml) as app:
app.generator.use_gps().particle("gamma").energy(2, "MeV")
app.generator.position(0, 0, -10, "cm").direction(0, 0, 1)
events = app.run(100)
Advanced GPS distributions remain accessible through
app.generator.commands(["/gps/pos/type Plane", ...]).
Optical physics and uniform global magnetic/electric fields can be enabled directly:
with g4.Application(gdml=my_optical_gdml, optical=True) as app:
app.detector.magnetic_field = (0.0, 0.0, 1.5) # tesla
app.detector.electric_field = (0.0, 0.0, 0.5) # kV/cm
events = app.run(100)
Optical scintillator GDML + tuning helpers:
from geant4_python_application import optical as gopt
with g4.Application(gdml=gopt.optical_water_gdml, optical=True) as app:
app.commands(gopt.scintillation_commands(yield_factor=1.0))
events = app.run(100) # track.particle contains 'opticalphoton'
Recorded steps can be scored per event or into a Cartesian energy mesh:
energy_per_event = g4.Scoring.energy_deposit(events, volume="detector")
mesh, edges = g4.Scoring.energy_mesh(events, bins=(20, 20, 40))
Python run/event/track/step callbacks (offline over awkward arrays):
with g4.Application(gdml=g4.basic_gdml) as app:
events = app.run_with_callbacks(
100,
on_run=lambda evs: print(len(evs)),
on_event=lambda ev: None,
on_track=lambda tr, ev: None,
on_step=lambda st, tr, ev: None,
)
Extra physics constructors incl. DNA, CAD/VTK/ROOT/HepMC, and MPI splitting:
print(g4.Application.available_extra_physics())
with g4.Application(gdml=g4.basic_gdml, physics="FTFP_BERT") as app:
app.add_physics(
"G4EmDNAPhysics_option2"
) # or G4EmExtraPhysics, G4OpticalPhysics...
events = app.run(100)
from geant4_python_application import cad, io as gio, distributed as dist
gdml = cad.mesh_to_gdml(*cad.cube_mesh(100.0)) # or cad.stl_ascii_to_gdml(stl_text)
gio.mesh_to_vtk(mesh, edges, "mesh.vtk")
gio.events_to_parquet(events, "events.parquet")
gio.events_to_root(events, "events.root") # needs uproot
primaries = gio.hepmc_to_primaries(open("events.hepmc").read())
rank, size = dist.get_rank_size()
start, count = dist.split_count(1000, rank, size) # mpiexec -n 2 python run.py
Interactive Qt visualization
Install with the gui extra (pip install "geant4-python-application[gui]"),
or for source builds see
INSTALLATION.md.
Then open Geant4's interactive Qt viewer. The call returns when the window is
closed, and commands can be entered in the viewer's command panel:
import geant4_python_application as g4
with g4.Application(gdml=g4.basic_gdml, seed=137) as app:
app.command("/gun/particle e-")
app.command("/gun/energy 100 MeV")
app.visualize()
Metadata
Release files for geant4-python-application 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| geant4_python_application-0.2.0.tar.gz | 158.5 kB | Details |
Built distributions (wheels)
Total release size: 384.8 MB
Release files / geant4_python_application-0.2.0.tar.gz
| Download URL | geant4_python_application-0.2.0.tar.gz |
|---|---|
| Size | 158.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a4d3170f4557eb02b2fc036b23dcd5185be61e97fac7976c3e66637a5db0f5fd
|
|
BLAKE2b-256 checksum How to use checksums |
72936669f913d6674a654129ad1390077c22f01b078597d3043a5de39c24548c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp313-cp313-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp313-cp313-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 25.4 MB |
| Tags | CPython 3.13 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
6681d4ac2004963abf2abe57bada1aaac4f848e257369ddb9d5a5467ac5353e0
|
|
BLAKE2b-256 checksum How to use checksums |
e85027780b99589d1672c966a264e8b1aeed967488d196ede16164d0a25d6434
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp313-cp313-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp313-cp313-macosx_11_0_x86_64.whl |
|---|---|
| Size | 20.3 MB |
| Tags | CPython 3.13 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
992c8423d152e024ee14432c84f11167be3f6afe66a354900ee8b15a48c441c1
|
|
BLAKE2b-256 checksum How to use checksums |
fc1075529d5c443b5f96a3b5ce33c561386258bf170d73a657cf5e2ab6f36714
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.2.0-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 18.4 MB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
b6201055e5e52f927ab7a32230e0d3d0b32f3d45fe5f656700c0320c5468726a
|
|
BLAKE2b-256 checksum How to use checksums |
07348a089e44aad86efd923e878b8f44acf6c1d64b05cc3329fba7da8e9d7264
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp312-cp312-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp312-cp312-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 25.4 MB |
| Tags | CPython 3.12 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
74b178f1b88f92455454ca7e153f942378db23629261e2177b1cfea307924bfb
|
|
BLAKE2b-256 checksum How to use checksums |
c478dca4d3ae1ef2682ac40e96e254367dba5ebecb7fc6bdddef704c850fe0bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp312-cp312-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp312-cp312-macosx_11_0_x86_64.whl |
|---|---|
| Size | 20.3 MB |
| Tags | CPython 3.12 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
ae066c29a9a0405eee072c4255f75b1c22a4d290d3652abe8c76db95efa8d857
|
|
BLAKE2b-256 checksum How to use checksums |
2fa30cda02c4d58288edd79ab49f5a6252fe67af83dc0fd98592ecc941912bd2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.2.0-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 18.4 MB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
5aaded56000e4d105554f145586322c9dbe21503b317aae19073b99d326af271
|
|
BLAKE2b-256 checksum How to use checksums |
d1981d505eba1ae66730f416ea5a979522f6ed3925c0ce6ca8f6413a972337fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp311-cp311-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp311-cp311-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 25.4 MB |
| Tags | CPython 3.11 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
bfe04bcec079acf847e7b863728ebe4aa94f097754ebcd742854e48bae786015
|
|
BLAKE2b-256 checksum How to use checksums |
d1f5211415c18c473fa0c6131325bbe39075dfbed3cfabcc3146a6a5e675aa25
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp311-cp311-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp311-cp311-macosx_11_0_x86_64.whl |
|---|---|
| Size | 20.3 MB |
| Tags | CPython 3.11 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
a999d63cbc635a6f4f4d549bf0d3601a692a345c7530a4dd05888f0ee426c2cc
|
|
BLAKE2b-256 checksum How to use checksums |
4b0218d843a1491701629ad9378964f8f75ae4ad5bd0e3ab448e235a94a395d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.2.0-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 18.4 MB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
708fc9e93d7b400373219b6bfdf7ca95646e9749d77d2ff4322c4bc7139b4606
|
|
BLAKE2b-256 checksum How to use checksums |
433098aaae34b39e0b6fb6d16ad49f81bfee3612d9cf921d51f915c0cf9c61b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp310-cp310-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp310-cp310-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 25.4 MB |
| Tags | CPython 3.10 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
68b5d033515dec3ba9a882d4d5219e9acbabdce4be1c5e4162094618bace15f3
|
|
BLAKE2b-256 checksum How to use checksums |
79998f7d8e5b7c3e65c3e9a33f406df6a897478e9e123ba7ae412f12841316a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp310-cp310-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp310-cp310-macosx_11_0_x86_64.whl |
|---|---|
| Size | 20.3 MB |
| Tags | CPython 3.10 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
0b408d1ec3a65f3ffd1bf40f7f5ea6e4aabe84e2a96fa47066842280cca74385
|
|
BLAKE2b-256 checksum How to use checksums |
1eccd4ef67a9db5c4a43965e7af20b47836c839324912ba7b50a3a8288b574dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.2.0-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 18.4 MB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
8067e06cbb6833055027c1f20e15b69889747c9e73d90d52465884159b57e01e
|
|
BLAKE2b-256 checksum How to use checksums |
a41357495f7254669dec1f448df1f07c18a5582811722cd1d2a46b8e648f69c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp39-cp39-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp39-cp39-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 25.4 MB |
| Tags | CPython 3.9 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
5e73c0559c27621a55d29e556f98aa84c5d8415e6a5d24a3000833e2afe1cbd2
|
|
BLAKE2b-256 checksum How to use checksums |
55ed71f0a23442b09974d11b1960db8e7196be3a3369301b6ff5dd94a89a2bd9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp39-cp39-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp39-cp39-macosx_11_0_x86_64.whl |
|---|---|
| Size | 20.3 MB |
| Tags | CPython 3.9 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
fef3f702d258596b9f8cf3a6cfc542aad1919c71dd51d902742c60a8b1540e63
|
|
BLAKE2b-256 checksum How to use checksums |
e7f219f01acf4e232b51cf8c6a4239f15cd5bcd785785eebae1aaa0c856ddea1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp39-cp39-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.2.0-cp39-cp39-macosx_11_0_arm64.whl |
|---|---|
| Size | 18.4 MB |
| Tags | CPython 3.9 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
01bda479c75a6e7f99f17604f7409bd5efb519fac8bd82c4b4380f12cb94442c
|
|
BLAKE2b-256 checksum How to use checksums |
9e15958cd356ada15a12ce603f81dc96debb8205c34dc4d98656a7a1e2293e05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp38-cp38-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp38-cp38-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 25.4 MB |
| Tags | CPython 3.8 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
e17cd6bc37ff3501dfb24e378f442acd4e66d47108c8c8cfe0c74c3a0538e710
|
|
BLAKE2b-256 checksum How to use checksums |
9c0b2542fb75dd6ac02d7d3f45d9f5f2650b8d9724c7171e7c560366f0a9bb60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp38-cp38-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.2.0-cp38-cp38-macosx_11_0_x86_64.whl |
|---|---|
| Size | 20.3 MB |
| Tags | CPython 3.8 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
11b78bc1c1bf5843b9b572782ba6f51fef46dcd2f0a71aba34aec3b8e94e14e6
|
|
BLAKE2b-256 checksum How to use checksums |
6f55adb8f58eb0c875ccc40f518273241605009cb77033726782c7b647aed6f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / geant4_python_application-0.2.0-cp38-cp38-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.2.0-cp38-cp38-macosx_11_0_arm64.whl |
|---|---|
| Size | 18.4 MB |
| Tags | CPython 3.8 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
7b98d6315a9711562275a25f00da5157c3471bd67dc8e29be1596dce20e8a19a
|
|
BLAKE2b-256 checksum How to use checksums |
40c7ca6de7230c27412f35d87f0b87ed61799353d8b9cb4bb6e47c8287b64bbb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|