Alternative Python bindings for Geant4 via pybind11. It is loosely based on g4py, but retains an API closer to the standard C++ API and does not depend on Boost.Python.
It currently includes all g4py bindings plus a large portion of very commonly used classes and functions that aren't currently present in g4py. However, it is still off from replicating the full Geant4 API and is thus (currently) only applicable for simple simulations.
It's available on PyPI for all major platforms as a fully self-contained (i.e. statically linked) wheel.
pip3 install geant4-pybind
Unix (Linux, OS X) prerequisites
- A Geant4 installation version 11.0+, compiled with CMake option
GEANT4_BUILD_TLS_MODEL=global-dynamic
(make sure it is also added to the path) - A C++17 compiler (preferably the one Geant4 was compiled with)
- Python3
- pip
- git
Windows prerequisites
- A Geant4 installation version 11.0+, compiled with CMake option
GEANT4_BUILD_TLS_MODEL=global-dynamic
(make sure it is also added to the PATH) - Visual Studio 2019 for C++ development
- Python3
- pip
- git
Note: You can let geant4_pybind manage and download the datasets, if the necessary environment variables haven't been set yet.
Installation
git clone --recursive https://github.com/HaarigerHarald/geant4_pybind
pip3 install ./geant4_pybind
Windows notes
The above commands have to be executed in a "Developer Command Prompt for VS 2019" and require that Python3, pip and git are added to the PATH environment variable.
General notes
If the repository was cloned non-recursively you can check out the submodule with:
git -C ./geant4_pybind submodule update --init
Minimal
A minimal example that just starts a Geant4 shell:
from geant4_pybind import *
import sys
ui = G4UIExecutive(len(sys.argv), sys.argv)
ui.SessionStart()
Simulation examples
Check out the examples directory, which contains the Geant4 basic examples ported to Python. However, writing simulations in Python should be pretty straightforward as the API is almost identical to the C++ one. One notable exception is template classes, which have been renamed such that their type replaces the T
(i.e. G4TScoreNtupleWriter<G4GenericAnalysisManager>
becomes G4GenericScoreNtupleWriter
).
There is a small overview, outlining the design choices made. It should be helpful to anyone seeking to extend the bindings.
Yes! Please create an issue if you encounter one.
This builds upon Geant4 and g4py and thus the Geant4 license applies. The binding code uses pybind11. All my contributions are public domain.