Python Interface
Python is a popular scripting language with an interactive interpreter.
Geant4Py, a Geant4-Python bridge, provides a bridge for Geant4 classes.
This enables to directly access Geant4 classes from Python scripting.
User applications can be easily configured with many Python
third-party modules, such as PyROOT, on the Python software bus.
Geant4Py is located in the directory environments/g4py/.
Installation
Software Requirements
Geant4Py requires
Boost-C++ external library,
which helps Python binding of C++ codes.
A Precompiled package is available for many Linux distributions
(SL, SuSE, Ubuntu, etc) and Mac OSX.
Geant4 libraries can be built as "static" and "granular",
where library components (variables, functions, ...)
used in the application are linked statically and shipped
with the application.
On the other hands, in dynamic binding, the library components
are not included in the application, and their binding is
carried out at run time.
The modular approach of Python is based on dynamic binding mechanism,
so you have Geant4 shared libraries are required instead.
For generic information about building Geant4 libraries,
please refer to the Installation Guide.
Here are some tips for manually building "global" and "shared" libraries
from an already existing "static + granular" installation.
After setting additional environment variables like:
Additional environment variables for building global and shared library
Environment Variable
Description
Value (example)
G4LIB
Path where the Geant4 libraries are installed
$G4INSTALL/slib
G4TMP
Path where temporary files are placed
$G4INSTALL/tmp-slib
G4LIB_BUILD_SHARED
Flag for building shared libraries
1
execute the following:
# cd $G4INSTALL/source
# make
# make global
In addition, it is required that all header files are installed
in a single directory.
# cd $G4INSTALL/source
# make includes
This will collect all relevant header files
in $G4INSTALL/include (or $G4INCLUDE).
There are also additional tools for helping building a Geant4 library in
the directory g4py/tools/.
Building Geant4Py module
Geant4Py provides a configure scrtipt for building modules.
# ./configure --help
`configure' configures Geant4Py to adapt to many kinds of systems.
Usage: ./configure SYSTEM [OPTION]... [VAR=VALUE]...
SYSTEM: System type (see Supported Arhitectures)
Options:
-h, --help Display this help and exit
Installation directories:
--prefix=PREFIX Installation prefix [./]
--libdir=DIR Python modules dir [PREFIX/lib]
Fine tuning of the library path:
--with-g4-incdir=DIR Geant4 header dir [$G4INCLUDE]
--with-g4-libdir=DIR Geant4 library dir [$G4LIB/$G4SYSTEM]
--with-clhep-incdir=DIR CLHEP header dir [$CLHEP_INCLUDE_DIR]
--with-clhep-libdir=DIR CLHEP library dir [$CLHEP_LIB_DIR]
--with-clhep-lib=LIB library name of libCLHEP.so [CLHEP|$CLHEP_LIB]
--with-python-incdir=DIR Python header dir [/usr/include/python(2.#)],
(location of pyconfig.h)
--with-python-libdir=DIR Python library dir [/usr/lib(64)]
--with-boost-incdir=DIR BOOST-C++ header dir [/usr/include],
(location of boost/)
--with-boost-libdir=DIR BOOST-C++ library dir [/usr/lib]
--with-boost-python-lib=LIB library name of libboost_python.so [boost_python]
--with-package-dir=DIR Geant4 Package dir
--with-extra-dir=DIR Install path for extra packages [/usr/local]
--with-xercesc-incdir=DIR Xerces-C header dir [/usr/include]
--with-xercesc-libdir=DIR Xerces-C library dir [/usr/lib(64)]
Enable/disable options: prefix with either --enable- or --disable-
openglx OpenGLX support [auto]
openglxm OpenGLXm support [disable, $G4VIS_USE_OPENGLXM]
raytracerx RayTracerX support [disable, $G4VIS_USE_RAYTRACERX]
Supported Architectures:
linux for Linux gcc 3.x and 4.x (32bit)
linux64 for Linux gcc 3.x and 4.x (64bit, alias to linuxx8664gcc)
linuxx8664gcc for AMD Opteron and Intel EM64T Linux gcc 3.x and 4.x
macosx for MacOSX with gcc (Tiger/Leopard and Xcode)
For example, you run it like
# ./configure linux64
--with-g4-incdir=/opt/heplib/Geant4/geant4.9.3/include
--with-g4-libdir=/opt/heplib/Geant4/geant4.9.3/slib/Linux-g++
--with-clhep-incdir=/opt/heplib/CLHEP/2.0.4.4/include
--with-clhep-libdir=/opt/heplib/CLHEP/2.0.4.4/lib
--with-clhep-lib=CLHEP-2.0.4.4
The configure script automatically check your environment,
and create config/config.gmk,
which describes your envrionment.
After executing the configure script successfully, then
# make
# make install
Using Geant4Py
PYTHONPATH environment variable
should be set at tun time.
PYTHONPATH environment variable indicates
Python module search directories, given by a colon-separated list
of directories. Practically, the variable is
(your g4py directory)/lib.
Import Geant4
To use Geant4Py, you start with importing the module called "Geant4".
# python
Python 2.5.2 (r252:60911, Aug 1 2008, 00:37:21)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Geant4 import *
*************************************************************
Geant4 version Name: geant4-09-03 (18-December-2009)
Copyright : Geant4 Collaboration
Reference : NIM A 506 (2003), 250-303
WWW : http://cern.ch/geant4
*************************************************************
Visualization Manager instantiating...
>>>
Access to Geant4 Globals
When importing the Geant4 module, the G4RunManager
object will be automatically instantiated. Geant4 singleton objects are also
automatically instantiated. These singleton objects can be accessed
by "gXXXX" variables, like "gRunManager".
gLossTableManager gTerminate
gApplyUICommand gMaterialTable gTrackingManager
gControlExecute gNistManager gTransportationManager
gElementTable gParticleIterator gUImanager
gEmCalculator gParticleTable gVisManager
gEventManager gProcessTable
gExceptionHandler gProductionCutsTable
gG4Date gRunManager
gG4VERSION_NUMBER gRunManagerKernel
gG4Version gStackManager
gGeometryManager gStartUISession
gGetCurrentValues gStateManager
Call Geant4 Methods
Once a Python object of a Geant4 class instantiated,
Geant4 methods can be directly called the same way as in C++.
>>> from Geant4 import *
*************************************************************
Geant4 version Name: geant4-09-03 (18-December-2009)
Copyright : Geant4 Collaboration
Reference : NIM A 506 (2003), 250-303
WWW : http://cern.ch/geant4
*************************************************************
Visualization Manager instantiating...
>>> print gRunManager.GetVersionString()
Geant4 version Name: geant4-09-03 (18-December-2009)
Site-modules
Geant4Py provides additional utility modules called "g4py" in the directory
site-modules. It consists of predifined geometries,
materials, physics lists, primary generator actions, and so on.
ezgeom module
The ezgeom module provides an alternative way
of defining simple geometry.
An example code for defining a simple geometry is shown here:
import g4py.ezgeom
from g4py.ezgeom import G4EzVolume
def ConstructGeom():
print "* Constructing geometry..."
# reset world material
air= G4Material.GetMaterial("G4_AIR")
g4py.ezgeom.SetWorldMaterial(air)
# a target box is placed
global target
target= G4EzVolume("Target")
au= G4Material.GetMaterial("G4_Au")
target.CreateTubeVolume(au, 0., 1.*cm, 1.*mm)
target.PlaceIt(G4ThreeVector(0.,0.,-10.*cm))
NISTmaterials module
The NISTmaterials module provides an instant
use of Geant4 NIST materials.
An example code for creating NIST materials:
from Geant4 import *
import g4py.NISTmaterials
g4py.NISTmaterials.Construct()
print Geant4.gMaterialTable
ParticleGun module
The ParticleGun module provides
a primary generator action with G4ParticleGun.
An example code is shown here:
import g4py.ParticleGun
# normal way for constructing user primary generator action
#pgPGA= g4py.ParticleGun.ParticleGunAction()
#gRunManager.SetUserAction(pgPGA)
#pg= pgPGA.GetParticleGun()
# 2nd way, short-cut way
pg= g4py.ParticleGun.Construct()
# set parameters of particle gun
pg.SetParticleByName("e-")
pg.SetParticleEnergy(300.*MeV)
primary_position= G4ThreeVector(0.,0., -14.9*cm)
primary_direction= G4ThreeVector(0.2, 0., 1.)
pg.SetParticlePosition(primary_position)
pg.SetParticleMomentumDirection(primary_direction)
Examples
There are some examples of Geant4Py in the directories
"tests/" and "examples/".
In the "tests/" directory,
gtest01 : exposes a user application
gtest02 : test for using site-module packages
gtest03 : test for ezgeom package
gtest04 : test for getting command tree and command information
gtest05 : test for constructing CSG geometries in Python
gtest06 : test for constructing/visualizing boolean geoemtries
gtest07 : test for checking overlapped geometries
The "examples/" directory contains
a set of examples of Geant4Py.
demos/water_phantom
An example of "water phantom dosimetry".
This demo program shows that a Geant4 application well coworks with Root
on Python front end. VisManager, PrimaryGeneratorAction, UserAction-s,
histogramming with Root are implemented in Python;
dose calculation in a water phantom
Python overloading of user actions
on-line histogramming with Root
visualization
education
Educational examples with Graphical User Interface using
TKinter
* lesson1
The first version of the courseware of the mass attenuation coefficient.
* lesson2
GUI interface of ExN03, which can control geometry configuration,
intial particle condition, physics processes, cut value,
magnetic field and visualization outputs.
emplot
Examples of plotting photon cross sections and stopping powers with Root.
gdml
Examples of writing/reading user's geometry to/from a GDML file