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:
pip install "geant4-python-application[gui]"
-
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.1
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.1.tar.gz | 152.9 kB | Details |
Built distributions (wheels)
Total release size: 362.2 MB
Release files / geant4_python_application-0.1.1.tar.gz
| Download URL | geant4_python_application-0.1.1.tar.gz |
|---|---|
| Size | 152.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
92b7f043751f3782aff8c24f2fe4d3524f4ce4a26be5937a182bfe260f611520
|
|
BLAKE2b-256 checksum How to use checksums |
2d4508e404b8ae086133276fe3ca78e0642de2b31ea792ba27e504108de6d7bf
|
| 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.1-cp313-cp313-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.1-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 |
24fe2c6fbe9e0a3c59c9d2d64b410620ec7c45492270725dc94e4ed020b4558a
|
|
BLAKE2b-256 checksum How to use checksums |
2275c32f67711aa0f9b2db3e4776425f0bf94ff948d566650f836a4281660019
|
| 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.1-cp313-cp313-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.1-cp313-cp313-macosx_11_0_x86_64.whl |
|---|---|
| Size | 19.0 MB |
| Tags | CPython 3.13 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
9f3b16a9ef0101fbe742aa0c4feaa71c656cb39f9ea53f12c3149382f668818c
|
|
BLAKE2b-256 checksum How to use checksums |
f2dcf9c0c2d554bf94a11a34b018faefee719d5c82339fa31cbec72e94fccbd6
|
| 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.1-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.1-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 |
074dfb6313690796120a20308b575985c58855ac24e2d40d2110d240a1e8a600
|
|
BLAKE2b-256 checksum How to use checksums |
27792dfad34a623ddef39d054881d7f99643f2975d9adaf6a6b0e0dc03499682
|
| 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.1-cp312-cp312-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.1-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 |
4b37eeb32c479bab404f6b30a9e74a0716b38f6737422e0f70471ea8b2b69790
|
|
BLAKE2b-256 checksum How to use checksums |
19822f4a8cc1cdf915d4627cafb969da8943d7fa13e5d542d6ae71c64d3f4155
|
| 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.1-cp312-cp312-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.1-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 |
4897ab10172fc2404429eb28e7cb2535167e69e541238dcaecf3f88bce99173f
|
|
BLAKE2b-256 checksum How to use checksums |
b8e464304981991297e81afe8ad1f3145e005cf0d5c1b7e0184756423de27204
|
| 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.1-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.1-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 |
7ca1da0aaf7f86b7237adf73be3bd068ad7ac04c520be58773e8c23b237fb399
|
|
BLAKE2b-256 checksum How to use checksums |
588e23e347e6883c3f1c9c6089ba510c7ab6c7d201fb9cfcc1ed63a81844c710
|
| 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.1-cp311-cp311-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.1-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 |
8d89466695ec305b2a5f83158322cd0916972640af6578695d97f296d570cb61
|
|
BLAKE2b-256 checksum How to use checksums |
648c1b89ccc233975a2b780c78fd11d9404a21e819ec4b9c4a55bbc4707fae53
|
| 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.1-cp311-cp311-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.1-cp311-cp311-macosx_11_0_x86_64.whl |
|---|---|
| Size | 19.0 MB |
| Tags | CPython 3.11 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
d68aaddbb7405e8dcccfbd3fcf24a6554cbbeb77cbc6b542f957f44b8b583b5e
|
|
BLAKE2b-256 checksum How to use checksums |
ace2b4a2609a20a59c1219681fe7ad66186396ccf45d10dcbd7f2e9e30df1b83
|
| 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.1-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.1-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 |
b47f64898f02b4294210f7f5efd2e60d1724effe58125af497f6b9d870e90c6a
|
|
BLAKE2b-256 checksum How to use checksums |
7e2794738afee786361c3fc254b5610309b47f68971a508cdb778d86dfb96d6c
|
| 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.1-cp310-cp310-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.1-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 |
edbb80765aa379e39a37eec9653a69f80143cc15e80e93a6ab3296ef6da3d0ed
|
|
BLAKE2b-256 checksum How to use checksums |
18b0f179b554fc0cbee70a823c1b08776ea5187294364adc4d6a97e61a2b2662
|
| 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.1-cp310-cp310-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.1-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 |
8f8a1d43ff384bc8ff61cc596e5740986479f10c0a4e558d69e7d51a69d84c4f
|
|
BLAKE2b-256 checksum How to use checksums |
3fc597a88076ef7f619a9ab776113455d8dc5a2c886e1b399fac76f68920dea6
|
| 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.1-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.1-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 |
55175322e3b2c4728711c2033a8662a93f73fdae77e175d8da10de689dcc43ca
|
|
BLAKE2b-256 checksum How to use checksums |
5dbc36425241943d7b292d97f14f3928e601aa40d77aa06ac62ae0886c253279
|
| 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.1-cp39-cp39-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.1-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 |
96f587623863f4cba9395f4e9f5947790e55ad29ad8f24fbaa92011ec38d0778
|
|
BLAKE2b-256 checksum How to use checksums |
f516a964924246b7cdbd63b0538bdf08cf182a5294d5a6aa09a3f04adb0aae18
|
| 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.1-cp39-cp39-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.1-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 |
71381562364cd8cac43f68cfbc2ac5b54f677614bc1578bf76f13bee2d0cf81e
|
|
BLAKE2b-256 checksum How to use checksums |
0c02d103ba37edc23130972ba78b07b2b45a60390fb1cdb071b925de0a31deb1
|
| 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.1-cp39-cp39-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.1-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 |
3b33a9716cb452e6c521a43b08f9b8939f04af8fe7c6dc46f52146d4114b305c
|
|
BLAKE2b-256 checksum How to use checksums |
a7d341d0b4b61970618471e6fb824ba07323b58a8cf2cfe0233dfc73b873c7d5
|
| 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.1-cp38-cp38-manylinux_2_28_x86_64.whl
| Download URL | geant4_python_application-0.1.1-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 |
e9a1167b1b3537eaabf1cb6236452c5a59c9118b808465885085f8ed1a151996
|
|
BLAKE2b-256 checksum How to use checksums |
b4a51cc1a4e7693506111ab4cb32a138f5bc712d2f5e6f79e3a2a25a1147b77d
|
| 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.1-cp38-cp38-macosx_11_0_x86_64.whl
| Download URL | geant4_python_application-0.1.1-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 |
0d9aef7b735440775c7e1910eee4078191e2c4097a8b1c70e7f785ae28e48f90
|
|
BLAKE2b-256 checksum How to use checksums |
f725a0234e8c484d8e8ef9061e7dd8407d658542a347e59f62721f967c606542
|
| 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.1-cp38-cp38-macosx_11_0_arm64.whl
| Download URL | geant4_python_application-0.1.1-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 |
7dd81ff5932dfbb81c696f36026e819487dce6547026b104f1d36a0905311c40
|
|
BLAKE2b-256 checksum How to use checksums |
7525a28e91ba2e07f52b96e2935a197145679e93ee5d3978497379986c5ecf5e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|