Geant4 Python Installation

While trying to install python modules, I come across this error while running cmake wondering if anyone has experienced the same issue

$ cmake /Users/Joe/Geant4/geant4.10.06.p02/environments/g4py

CMake Error at G4PythonHelpers.cmake:15 (file):
file RELATIVE_PATH must be passed a full path to the file:
Call Stack (most recent call first):
CMakeLists.txt:14 (include)

CMake Error at CMakeLists.txt:23 (geant4_add_feature):
Unknown CMake command “geant4_add_feature”.

As of 10.6.2, the Python binding can be done as part of the main Geant4 build. Apologies that this wasn’t updated in the docs. To enable it, build Geant4 as per the install guide, but add the option

-DGEANT4_USE_PYTHON=ON

This requires Python 3, plus Boost with Python3 support. If you want Geant4 built in Multithreaded mode with Python, this will also require the options:

-DGEANT4_BUILD_MULTITHREADED=ON
-DGEANT4_BUILD_TLS_MODEL=global-dynamic

Hi thank you very much! I’ve downloaded the python script in the lib directory but I can’t seem to execute it. Is there any way of running Geant4 in anaconda?

Hi,

I have a question on the same. I installed it using commands:

cmake -DCMAKE_INSTALL_PREFIX=/tmp/shilpi/geant4.10.06-install /tmp/shilpi/geant4.10.06.p02 -DGEANT4_INSTALL_DATA=ON -DGEANT4_USE_PYTHON=ON
make -j2
make install

It got installed all fine.
But when I try this:
python
from Geant4 import *

It gave me this error: undefined symbol: _ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE

Could it be that the boost version is not compatible with the python version? For reference, I am using python version 3.6 and boost version 1.53.0

What would be the recommended version of boost? Also, how to specify with cmake?

Thanks
Shilpi

Is that Boost version compiled for Python 3.6 (what OS/version is this on, and id the Boost install a system package or self installed)? That undefined symbol can be a symptom of using a Boost.Python that was compiled for Python 2.

Geant4’s Python interface has been built against Boost 1.70 successfully, and I’d guess that most recent versions should work fine. To point CMake to a specific Boost installed at, say /my/boost/install, add that path to CMAKE_PREFIX_PATH either in the environment, or as an argument to cmake (cmake -DCMAKE_PREFIX_PATH=/my/boost/install <otherargs>)

Hi Benjamin,

Thanks for your prompt reply. I am working on cern lxplus and so pointing libraries from there.
I tried this command:

cmake -DCMAKE_INSTALL_PREFIX=/tmp/shilpi/geant4.10.06-install /tmp/shilpi/geant4.10.06.p02 -DGEANT4_INSTALL_DATA=ON -DGEANT4_USE_PYTHON=ON -DCMAKE_PREFIX_PATH=/cvmfs/cms.cern.ch/slc7_amd64_gcc820/external/boost/1.72.0-gchjei/

I get this warning:

– Found PythonInterp: /cvmfs/cms.cern.ch/slc7_amd64_gcc820/cms/cmssw/CMSSW_10_6_1/external/slc7_amd64_gcc820/bin/python3 (found suitable version “3.6.4”, minimum required is “3.0”)
– Found PythonLibs: /cvmfs/cms.cern.ch/slc7_amd64_gcc820/cms/cmssw/CMSSW_10_6_1/external/slc7_amd64_gcc820/lib/libpython3.6m.so (found version “3.6.8”)
CMake Warning at /cvmfs/sft.cern.ch/lcg/contrib/CMake/3.14.2/Linux-x86_64/share/cmake-3.14/Modules/FindBoost.cmake:896 (message):
New Boost version may have incorrect or missing dependencies and imported
targets
Call Stack (most recent call first):
/cvmfs/sft.cern.ch/lcg/contrib/CMake/3.14.2/Linux-x86_64/share/cmake-3.14/Modules/FindBoost.cmake:1018 (_Boost_COMPONENT_DEPENDENCIES)
/cvmfs/sft.cern.ch/lcg/contrib/CMake/3.14.2/Linux-x86_64/share/cmake-3.14/Modules/FindBoost.cmake:1694 (_Boost_MISSING_DEPENDENCIES)
environments/g4py/CMakeLists.txt:11 (find_package)

– Boost version: 1.72.0
– Found the following Boost libraries:
– python

But everything compiled as usual. It still gave me the same error when I did:

python
from GEANT4 import *

If I look inside /cvmfs/cms.cern.ch/slc7_amd64_gcc820/external/boost/1.72.0-gchjei/lib/, I see this: libboost_python27.so

so probably its compiled with python2.7 - I am not sure…
So your suggestion would be install a boost version 1.7 using python3?

Also, may I ask which versions of GEANT4 can work with python2? I can try with that as well.

Thanks a lot,
Shilpi

Seems that GEANT4 version 10.5 (to be precise: geant4.10.05.p01) works. Just listing down the steps in case it is useful for someone:

setenv PATH /cvmfs/sft.cern.ch/lcg/contrib/CMake/3.14.2/Linux-x86_64/bin:$PATH
cmake -DCMAKE_INSTALL_PREFIX=/tmp/shilpi/geant4.10.05.p01-install /tmp/shilpi/geant4.10.05.p01 -DGEANT4_INSTALL_DATA=ON -DGEANT4_USE_OPENGL_X11=ON
make -j2
make install

cd /tmp/shilpi/geant4.10.05.p01/environments/g4py/
mkdir build
cd build
setenv GEANT4_INSTALL /tmp/shilpi/geant4.10.05.p01-install
cmake -DCMAKE_INSTALL_PREFIX=$GEANT4_INSTALL -DXERCESC_ROOT_DIR=/cvmfs/sft.cern.ch/lcg/external/XercesC/3.1.1p2/x86_64-slc6-gcc47-opt/ …

To run:
setenv PYTHONPATH /tmp/shilpi/geant4.10.05.p01-install/lib64/:$PYTHONPATH

setenv PYTHONPATH /tmp/shilpi/geant4.10.05.p01-install/lib64/g4py/:$PYTHONPATH
setenv PYTHONPATH /tmp/shilpi/geant4.10.05.p01-install/lib64/Geant4/:$PYTHONPATH
setenv PYTHONPATH /tmp/shilpi/geant4.10.05.p01-install/lib64/tests/:$PYTHONPATH
setenv PYTHONPATH /tmp/shilpi/geant4.10.05.p01-install/lib64/examples/:$PYTHONPATH

Now any code can be run

Hi,

I have another problem in running the test codes. I am using code inside: environments/g4py/tests/gtest01/
If I do:
python test.py, I get this error:

Traceback (most recent call last):
File “test.py”, line 103, in
pg.SetParticleByName(“e-”)
Boost.Python.ArgumentError: Python argument types in
G4ParticleGun.SetParticleByName(G4ParticleGun, str)
did not match C++ signature:
SetParticleByName(G4ParticleGun*, std::__cxx11::basic_string<char, std::char_traits, std::allocator >)

which to me seems can arise if the compiled boost version is not compatible with the python version I am using. For reference, I am using python version 2.7.15+ and boost version 1.72.0 which seems to have libboost_python27.so in its lib directory so my guess its compiled with python2.7.

Do you know what could be causing this error?

Thanks
Shilpi

I’d recommend sticking with Geant4 10.6.2 as this has significant improvements to the build of the Python bindings. As you are running on lxplus, it would be easiest to use the LCG packages either via a view, e.g.

$ . /cvmfs/sft.cern.ch/lcg/views/LCG_98python3/x86_64-centos7-gcc8-opt/setup.sh

or by setting them up individually, e.g.

$ . /cvmfs/sft.cern.ch/lcg/releases/LCG_98python3/CMake/3.17.3/x86_64-centos7-gcc8-opt/CMake-env.sh
$ . /cvmfs/sft.cern.ch/lcg/releases/LCG_98python3/Boost/1.73.0/x86_64-centos7-gcc8-opt/Boost-env.sh
... other as needed ...

The views come with Geant4, but shouldn’t clash as long as when running you prepend the directories holding your Geant4 libraries and python modules to LD_LIBRARY_PATH and PYTHONPATH respectively.

I am getting an error while

>> import Geant4
** from G4interface import ***
Import error: libG4intercom.so cannot open shared file object: No such file or directory

this libG4intercom.so is in lib directory geant4-install folder, I have updated the
export GEANT4_INSTALL=/path
export PYTHONPATH =/path/:$PYTHOPNPATH

kindly Help

Regards