Skip to main content

Geant4 Python Application

Binder Binder

Wheels Build and Test

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

Prebuilt wheels for macOS (Apple Silicon and Intel) and Linux x86_64, Python 3.8 – 3.13, bundle Geant4 — no separate Geant4 (C++) installation is needed:

pip install "geant4-python-application[gui]"

The gui extra adds the interactive Qt viewer; leave it out for a physics-only install.

Geant4's physics data files download automatically on first use, so you can run an example straight away:

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.

Building from source (other platforms, development), the Qt viewer, and data file management are covered in INSTALLATION.md.

Usage

import geant4_python_application as g4

# Use a temporary directory for the Geant4 data files (remove this line to use the default location)
g4.application_directory(temp=True)

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()

Release files for geant4-python-application 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for geant4-python-application 0.1.0
File Size Uploaded
geant4_python_application-0.1.0.tar.gz 152.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for geant4-python-application 0.1.0
File
geant4_python_application-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ x86-64 Details
geant4_python_application-0.1.0-cp313-cp313-macosx_11_0_x86_64.whl CPython 3.13 CPython 3.13 macOS 11.0+ x86-64 Details
geant4_python_application-0.1.0-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
geant4_python_application-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64 Details
geant4_python_application-0.1.0-cp312-cp312-macosx_11_0_x86_64.whl CPython 3.12 CPython 3.12 macOS 11.0+ x86-64 Details
geant4_python_application-0.1.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
geant4_python_application-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ x86-64 Details
geant4_python_application-0.1.0-cp311-cp311-macosx_11_0_x86_64.whl CPython 3.11 CPython 3.11 macOS 11.0+ x86-64 Details
geant4_python_application-0.1.0-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
geant4_python_application-0.1.0-cp310-cp310-manylinux_2_28_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.28+ x86-64 Details
geant4_python_application-0.1.0-cp310-cp310-macosx_11_0_x86_64.whl CPython 3.10 CPython 3.10 macOS 11.0+ x86-64 Details
geant4_python_application-0.1.0-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
geant4_python_application-0.1.0-cp39-cp39-manylinux_2_28_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.28+ x86-64 Details
geant4_python_application-0.1.0-cp39-cp39-macosx_11_0_x86_64.whl CPython 3.9 CPython 3.9 macOS 11.0+ x86-64 Details
geant4_python_application-0.1.0-cp39-cp39-macosx_11_0_arm64.whl CPython 3.9 CPython 3.9 macOS 11.0+ ARM64 Details
geant4_python_application-0.1.0-cp38-cp38-manylinux_2_28_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.28+ x86-64 Details
geant4_python_application-0.1.0-cp38-cp38-macosx_11_0_x86_64.whl CPython 3.8 CPython 3.8 macOS 11.0+ x86-64 Details
geant4_python_application-0.1.0-cp38-cp38-macosx_11_0_arm64.whl CPython 3.8 CPython 3.8 macOS 11.0+ ARM64 Details

Total release size: 362.2 MB

Release files / geant4_python_application-0.1.0.tar.gz

Download URL geant4_python_application-0.1.0.tar.gz
Size 152.4 kB
Tags Source
SHA-256 checksum
How to use checksums
afb74a645cfff36c3410c977cc5e6e7973271ee8c06d3c76e0ad75334315d95b
BLAKE2b-256 checksum
How to use checksums
e5af634514e5cce2459a8d9d1891d909a5f9bd3baba0c1e77c1d593c66f70b26
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.0-cp313-cp313-manylinux_2_28_x86_64.whl

Download URL geant4_python_application-0.1.0-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
d1f714718db576f0ee1d92064efefab9d439357e60a1610e5a5b59b837d65dff
BLAKE2b-256 checksum
How to use checksums
98a5a7fee155372539b7757b7906f330f7b4748d19617b68a63990fc83021588
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.0-cp313-cp313-macosx_11_0_x86_64.whl

Download URL geant4_python_application-0.1.0-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
2ce8ac76f2a76c4f49e9b5784fd9a69a3c226068063780aecab9da50e7b5dd3e
BLAKE2b-256 checksum
How to use checksums
c70c319011c3e91122bb077affe6aeb78b86dea873e2a8059e039d513e9bdf73
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.0-cp313-cp313-macosx_11_0_arm64.whl

Download URL geant4_python_application-0.1.0-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
5969c0c38c62c37970175d766782dc9575afdfbfa313058526c4b72a2990f10a
BLAKE2b-256 checksum
How to use checksums
f9685b9cbf6dd43ce30ce7c809d478e4d0e36c1bce1930a8facff96b74fe0fb4
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.0-cp312-cp312-manylinux_2_28_x86_64.whl

Download URL geant4_python_application-0.1.0-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
87012481cb68a6b327261af91881df29d2fec62e2bebf36d35679a5bb5544ee6
BLAKE2b-256 checksum
How to use checksums
a760ebea92234d15894cecae2b9c8381706b0c4a2d537410179fe8ff9dbd9ff8
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.0-cp312-cp312-macosx_11_0_x86_64.whl

Download URL geant4_python_application-0.1.0-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
0887f727857fa3449331b7c22c2c5034dcd70c88f6b916ed4f7744ca5454c23f
BLAKE2b-256 checksum
How to use checksums
d9a1190a8097f18c7a63da2710fc18e1fe0ccb3b6e94d1ee835312e64444e808
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.0-cp312-cp312-macosx_11_0_arm64.whl

Download URL geant4_python_application-0.1.0-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
6b65964383ac325a534d3b86605dccca8a79e0ea24685b0bef54fa43d77be5eb
BLAKE2b-256 checksum
How to use checksums
b38d37cb6836c1d94345838dd567b98551a6f5b6e48456d61bef8fd6870d8bcd
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.0-cp311-cp311-manylinux_2_28_x86_64.whl

Download URL geant4_python_application-0.1.0-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
8aef87cc837a76d893297c0f147ca73919d57f2d7991eb0e3bf65d25f0d82498
BLAKE2b-256 checksum
How to use checksums
88d2ba96a4e405448a0166b75cafb39a7087721a7d3fdf62d71394276f982d4b
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.0-cp311-cp311-macosx_11_0_x86_64.whl

Download URL geant4_python_application-0.1.0-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
f015ceec56a817c532cef020f2f16aea94098ffb6082ffdd5d8ebe902a216cb2
BLAKE2b-256 checksum
How to use checksums
fe80183f6b365cba1a94b2c8e2fa9b876650e1664997452b8e59e1ecfdd28561
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.0-cp311-cp311-macosx_11_0_arm64.whl

Download URL geant4_python_application-0.1.0-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
393850b1af049cadbbc0c2751f4a63ca9b5e47d9bb55100d70defb7e279c3337
BLAKE2b-256 checksum
How to use checksums
bc27feec8e214e5c788e3a1574ca2d83e3d6ee52bc4d1d97f09cbe6fc96ac46b
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.0-cp310-cp310-manylinux_2_28_x86_64.whl

Download URL geant4_python_application-0.1.0-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
6c52aea688b44ab4dbbbc0b3310a105ff1e082ad6ef2ddfa60a7d6e8ee65d599
BLAKE2b-256 checksum
How to use checksums
6a164efa9ff88613646b57e324a353db6aea5355dc1f7fb664efeeab46d4ab9a
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.0-cp310-cp310-macosx_11_0_x86_64.whl

Download URL geant4_python_application-0.1.0-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
1c7bd4a135ef14bcd55a1845765d43debd4dc6d2cc0c57a33e490b957985ae52
BLAKE2b-256 checksum
How to use checksums
4272557aeb806f8a952375230be901f96e86292deae0864f4ee601a145e37884
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.0-cp310-cp310-macosx_11_0_arm64.whl

Download URL geant4_python_application-0.1.0-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
16ad28193d8f59f8bea87855e85b4efae0501a86254e5f2bc7791a1c5a87683e
BLAKE2b-256 checksum
How to use checksums
23df5274f0b94a25f62af0a1398270f0712331567aafb946b432830f9f2bdb07
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.0-cp39-cp39-manylinux_2_28_x86_64.whl

Download URL geant4_python_application-0.1.0-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
d0b5ce36de0aae33e8097b6e4bcdb3a36ddd4ad9a97a38d546d774d6acf4072f
BLAKE2b-256 checksum
How to use checksums
08c202a1cbdb9060c41f2ac0999c5801e1c60a24b69bfb8ec26971156f352913
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.0-cp39-cp39-macosx_11_0_x86_64.whl

Download URL geant4_python_application-0.1.0-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
2372d7efbf647444e57c68fe6e89e1e6e050829ff8f5d86af89ab81a8c3f43bb
BLAKE2b-256 checksum
How to use checksums
6df7a8bbd8f1a8c9b972b941452f8ea5073fd4b06fdde81fa465242646963688
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.0-cp39-cp39-macosx_11_0_arm64.whl

Download URL geant4_python_application-0.1.0-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
89cbc14ad92c89723b9c01e06c01f19cc748b1209668772eede625a0835e06f9
BLAKE2b-256 checksum
How to use checksums
493591d43fecaf12f6e229fe155e68dfd6cc84477d3d2e9ee9f42f8c8a0a6506
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.0-cp38-cp38-manylinux_2_28_x86_64.whl

Download URL geant4_python_application-0.1.0-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
789763d4781c3ffd7bef927d4d0776505da2ff7ecd9a7f23bb7a2727d7c3564e
BLAKE2b-256 checksum
How to use checksums
edb7d42aac978fa76ebb75c45f60458b1a00b259e054dd16d9eb0e7151610bd6
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.0-cp38-cp38-macosx_11_0_x86_64.whl

Download URL geant4_python_application-0.1.0-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
1ffc29d188265c9d9c2eb5e2dfd2e7ff1f1315e7f38f20aee139f50db8643f41
BLAKE2b-256 checksum
How to use checksums
93acb4faef164063f4e6e5642cecd74c1232439ac79c049cdc8ca3b1ec3d311e
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.0-cp38-cp38-macosx_11_0_arm64.whl

Download URL geant4_python_application-0.1.0-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
a1b6ce0c9a49dd99d2a9a079d8b880bc9eecb4106a601be59c8ae0d516a28bd6
BLAKE2b-256 checksum
How to use checksums
e4dc5c202c6c3e70c3f5ed13352b13b209436fa1befc0b3b8994163abcbc5061
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release history Release notifications | RSS feed

0.1.1

19 release files

This release

0.1.0 This release

19 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page