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.1.2
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.1.2.tar.gz | 153.8 kB | Details |
Built distributions (wheels)
Total release size: 362.2 MB
Release files / geant4_python_application-0.1.2.tar.gz
| Download URL | geant4_python_application-0.1.2.tar.gz |
|---|---|
| Size | 153.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
016996aae94de0656c0b654b11ef433eaf08a4ebf16f994f4522f137a4a8adc8
|
|
BLAKE2b-256 checksum How to use checksums |
a4441d54ae48278549a1a9dd71fc1e6e16da491f1b949c9dbe2f1e4846c37d37
|
| 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.1.2-cp313-cp313-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp313-cp313-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 23.9 MB |
| Tags | CPython 3.13 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
631b9cce99e1e0853d361b3cb5d5e7ab3ed0f9a082cb8da6d569411d1e3429e0
|
|
BLAKE2b-256 checksum How to use checksums |
f46f42c3d89d4e6bc3e07a7eef4db84986fbbf30135f31b013ff91effcdfaebc
|
| 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.1.2-cp313-cp313-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp313-cp313-macosx_11_0_x86_64.whl |
|---|---|
| Size | 19.1 MB |
| Tags | CPython 3.13 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
91f7fb78e1e90fb93c188498dd5c137fc6cf0e7d922fc551aa1f2d2318d9cdeb
|
|
BLAKE2b-256 checksum How to use checksums |
e6d8315322847ca55235930d2d91a0117f9b6ad6aed8053dd4cae0a0ad3c1cd4
|
| 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.1.2-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.2-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 17.3 MB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
9b498cf380f18a9fc1c86cf24441e910cbdf193e668b8ac4aebc38b66e2c0bac
|
|
BLAKE2b-256 checksum How to use checksums |
4cb7121c1bedba129ee50bb7c39d727a75fcc86dfc1bafd99e093b230fc2d688
|
| 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.1.2-cp312-cp312-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp312-cp312-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 23.9 MB |
| Tags | CPython 3.12 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
e003a969ebf69ba8b9fd84e117c4b7f179577246eccc6f9bced005727e2f6eae
|
|
BLAKE2b-256 checksum How to use checksums |
ffdfcd80c0dadd140ebbc986965b4976255a72a4f440268514603d8581b19b61
|
| 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.1.2-cp312-cp312-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp312-cp312-macosx_11_0_x86_64.whl |
|---|---|
| Size | 19.0 MB |
| Tags | CPython 3.12 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
00e721072465a082a90ced8080b7aa83b672cb999bc828a22bf292d9a2663b4b
|
|
BLAKE2b-256 checksum How to use checksums |
49c8ed3aca456799bf6b09ed6941b18892acde28413b2bfb5708d00884be7fb4
|
| 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.1.2-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.2-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 17.3 MB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
0000b962accc6437c59e11e3b7d2fe1eee2a91c9758ca38e30090c193ab9cd8d
|
|
BLAKE2b-256 checksum How to use checksums |
a10793067a21d254fa1cc2a055413063e7b7a27a7c77e8acd68defac6a5dedfb
|
| 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.1.2-cp311-cp311-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp311-cp311-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 23.9 MB |
| Tags | CPython 3.11 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
64dd287c79744496f0a507f37461fc6652d16fdd5ad791ab9af903613f936bc3
|
|
BLAKE2b-256 checksum How to use checksums |
fc0d316d0b7d7877504975ea5f436ef8e751f4da97e437b0336351fa50f54291
|
| 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.1.2-cp311-cp311-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp311-cp311-macosx_11_0_x86_64.whl |
|---|---|
| Size | 19.1 MB |
| Tags | CPython 3.11 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
fd8c4cfde8929d6602dc062ddefe1ff27118802e1ca3eca5c6a82af6dac73964
|
|
BLAKE2b-256 checksum How to use checksums |
a78fb4ac98afb145f4e24dfae2e070ec4e5ce91c56d74d61f8f57073aaabce07
|
| 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.1.2-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.2-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 17.3 MB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
350561ef166ed21a04269fd6bf4e29ffcb5efd6dfd07c5d859141dff9b959684
|
|
BLAKE2b-256 checksum How to use checksums |
5137c4b0061daba0d7edaa04a84247cfe8abb256bfc751dab1953fe2d0c7dcb3
|
| 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.1.2-cp310-cp310-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp310-cp310-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 23.9 MB |
| Tags | CPython 3.10 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
a67d3a024158210d77c7d29aafb1bbdea6ae9967b39bf5cb2f8b075ba67fa834
|
|
BLAKE2b-256 checksum How to use checksums |
9a937bece1c44ed647f7aa79e420d5a7b3b06aa548723f76f71d23c50435522c
|
| 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.1.2-cp310-cp310-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp310-cp310-macosx_11_0_x86_64.whl |
|---|---|
| Size | 19.0 MB |
| Tags | CPython 3.10 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
34949644402d7d06821a3c95b03ed5affa8998efab010d872b625e1829e636f7
|
|
BLAKE2b-256 checksum How to use checksums |
271ec0308cab28528972794114f9ee6d90d3fe798905c15fe19b879c66e1f719
|
| 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.1.2-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.2-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 17.3 MB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
714a2c9a104c109f5dffe8c0d3b2ef831add6a4ee102db6456aa0a4cb00013aa
|
|
BLAKE2b-256 checksum How to use checksums |
219c6605cb3a59a0e7e163c345c3fbf98c36127d944f28586a365a6c264ee58a
|
| 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.1.2-cp39-cp39-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp39-cp39-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 23.9 MB |
| Tags | CPython 3.9 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
d16c9e57dd468a342019bb41d7836ed0341a39ab144438927d403ec22d504b73
|
|
BLAKE2b-256 checksum How to use checksums |
bd72d15ad1f74ccf5ff104fa568f9ff7c265c1211fad025d192d98818254ce8f
|
| 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.1.2-cp39-cp39-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp39-cp39-macosx_11_0_x86_64.whl |
|---|---|
| Size | 19.0 MB |
| Tags | CPython 3.9 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
1ab5fbbacda1c51a3da4fac68f2deb67f779f5f0b91dc189644489975dafdfb9
|
|
BLAKE2b-256 checksum How to use checksums |
ffd7f4fc61c2894a09e4087b797dd30528f198955f3b43bac88d567f958122fd
|
| 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.1.2-cp39-cp39-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.2-cp39-cp39-macosx_11_0_arm64.whl |
|---|---|
| Size | 17.3 MB |
| Tags | CPython 3.9 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
02ff7b362c151b2abeec154fb45cbc6897aca1a87e8a78b0e2c58ebc26023758
|
|
BLAKE2b-256 checksum How to use checksums |
ccf8448ec3274acb267ff287ebf431df5ba55b68018d8b61c6ceddb7dca7371a
|
| 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.1.2-cp38-cp38-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp38-cp38-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 23.9 MB |
| Tags | CPython 3.8 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
699fca10d56a1193fa454e729983e689c78412736b6821b99f49594b658bd9b3
|
|
BLAKE2b-256 checksum How to use checksums |
ab8fcae0f63dd5428eb84722d59ecd439abc4a5071172d93071f0eb8fd0bf1b9
|
| 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.1.2-cp38-cp38-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.2-cp38-cp38-macosx_11_0_x86_64.whl |
|---|---|
| Size | 19.0 MB |
| Tags | CPython 3.8 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
3e4c64c99bbf7a8edf31560015946f6a0c4a25a2e4ebb2f91a8632d655a262ff
|
|
BLAKE2b-256 checksum How to use checksums |
66faaaa286576681a9afbbd1459471697527d82de0ad67fdf3a22ce39026fc30
|
| 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.1.2-cp38-cp38-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.2-cp38-cp38-macosx_11_0_arm64.whl |
|---|---|
| Size | 17.3 MB |
| Tags | CPython 3.8 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
60b087d3f279adf2340b1e3f8ae18deaf4d92e66adacbf798b0e343903233b64
|
|
BLAKE2b-256 checksum How to use checksums |
7b25c5b1732c52a138e4122f039878f5fb1a222e34e47a1df69de2f472a8f33d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|