How To Install G4 Python Environment for Mac (g4py)

Hi there,

I am trying to install the g4py environment on macOS and whilst trying to run ‘make’ I encounter the error:

**/usr/local/include/boost/python/suite/indexing/vector_indexing_suite.hpp:85:27:** error: no matching constructor for initialization of 'G4UnitsTable'
            return object(Container(container.begin()+from, container.begin()+to));

Has anyone encountered this error, or is familiar with this type of error and how to fix it?

Update

After commenting out the affected line it runs through the build with lots of warnings and then subsequently crashes at a different build point with error:

/environments/g4py/source/global/pyglobals.cc:43:23: error: functions that differ only in
      their return type cannot be overloaded
extern G4strstreambuf G4coutbuf;

Any help with the general installing of g4py on Mac would be very appreciated!

Thanks,

Michael

Which Geant4 version, macOS version is this (including Xcode), and Python and Boost.Python are being used?

Geant4 version 10.06-p01, macOS Catalina 10.15.6, Xcode version 12.0, python3.8.5 and boost 1.73.0.

Thanks,

Michael

Thanks! I’d recommend updating using Geant4 10.6.2 as that integrates the build of Geant4Py with the main build. As long as Python and Boost are visible to CMake (via PATH and/or CMAKE_PREFIX_PATH), then just add the -DGEANT4_USE_PYTHON=ON option to CMake when configuring Geant4. This will compile both the Geant4 libraries and the Python bindings. After installing, the modules will be in <CMAKE_INSTALL_PREFIX>/lib/python3.8/site-packages/Geant4 so site-packages will need to be added to PYTHONPATH.

Great, I will give this a go! Thanks a lot!

Michael

Apologies, I forgot one thing: if you want to build Geant4 multithreading and Python bindings, then both -DGEANT4_BUILD_MULTITHREADED=ON and -DGEANT4_USE_TLS_MODEL=global-dynamic -DGEANT4_BUILD_TLS_MODEL=global-dynamic must be passed to cmake.

This is because we default to initial-exec thread local storage as it provides a small performance boost, but dynamic loading (as Python does) requires TLS to be global-dynamic. If you only require serial builds (i.e. -DGEANT4_BUILD_MULTITHREADED=OFF) then you can ignore this! CMake will error out on inconsistent MT/Python build settings, but wanted to avoid any confusion!

I am literally staring at that error as we speak. Thanks again, will add this.

Cheers,

Michael

Hi again,

Sadly I have hit a wall again, where I get the error that these two files below cannot be found, despite both boost and boost-python being installed in my /usr/local/ folder.

boost_pythonConfig.cmake
boost_python-config.cmake

Any ideas ?

Thanks,

Michael

Hi Again,

I fixed the above problem but now have:

 CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
   Target "pyG4global" links to target "Boost::python" but the target was not
   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
   an ALIAS target is missing?
 Call Stack (most recent call first):
   environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
   environments/g4py/source/global/CMakeLists.txt:2 (geant4_add_pymodule)

When generating the files with cmake. This is the same for every module e.g. pyg4emcalculator etc. They cannot link to “Boost::python”.

I am not sure how to proceed from here?

Michael

How did you install Boost and Boost.Python (thought it might be Homebrew given /usr/local?), and whereabouts under /usr/local are the boost .cmake files?
Was there any additional output from CMake about finding Boost and Python, and what, if any entries are there in CMakeCache.txt (in the directory you’re building in) relating to Boost?

Both CMake’s builtin FindBoost and Boost’s .cmake files should work as-is, but it’s possible something odd is happening here.

Yeah I installed using

brew install boost
brew install boost-python

Boost is installed under

/usr/local/Cellar/boost/
/usr/local/Cellar/boost-python/

it is also linked under

/usr/local/include/boost/

The .cmake boost files are under:

/usr/local/lib/cmake/Boost-1.73.0
/usr/local/lib/cmake/

I seem to get a build with zero errors and then when I finally generate, I get the error that the target “Boost::python” cannot be found.
The CmakeCache.txt is attached. You can see here the Boost:python target.
CMakeCache.txt (52.4 KB)

Any insights would be great!

Cheers,

Michael

Thanks for the info! I think you’re running into a annoyance of Boost.python (and why we want to move to pybind11 next year), which is that it needs to be compiled for the right Python. Homebrew does this, but the formula to use is boost-python3. I think if you do

brew rm boost-python
brew install boost-python3

then rerun cmake it should now be o.k.

I’ll double check the Geant4Py CMake scripts as it should be requiring Boost.Python to be found when it clearly hasn’t been, but hopefully the above will get you a working install.

NB: One more thing I remembered: you’ll still need to source the installed geant4.sh script before starting python3 and importing Geant4Py so that the data library environment variables are available. At the moment importing Geant4Py modules without these set will cause a failure with an unhelpful error message or even segfault is these aren’t set.

Unfortunately I still get “Target “pyG4physicslists” links to target “Boost::python” but the target
was not found.” etc. when generating the ccmake. This leads to something like

ld: library not found for -lBoost::python

When just continuing ahead and running make.

Any ideas?

Thanks,

Michael

This is my exact output if it helps:

-- The C compiler identification is AppleClang 11.0.3.11030032
-- The CXX compiler identification is AppleClang 11.0.3.11030032
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test HAVE_TLS
-- Performing Test HAVE_TLS - Success
-- Found EXPAT: /usr/lib/libexpat.dylib (found version "2.2.8") 
-- Found OpenGL: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/OpenGL.framework   
-- Found X11: /usr/X11R6/include   
-- Looking for XOpenDisplay in /usr/X11R6/lib/libX11.dylib;/usr/X11R6/lib/libXext.dylib
-- Looking for XOpenDisplay in /usr/X11R6/lib/libX11.dylib;/usr/X11R6/lib/libXext.dylib - found
-- Looking for gethostbyname
-- Looking for gethostbyname - found
-- Looking for connect
-- Looking for connect - found
-- Looking for remove
-- Looking for remove - found
-- Looking for shmat
-- Looking for shmat - found
-- Looking for IceConnectionNumber in ICE
-- Looking for IceConnectionNumber in ICE - found
-- Found XQuartzGL: /usr/X11R6/include  
-- Configuring download of missing dataset G4NDL (4.6)
-- Configuring download of missing dataset G4EMLOW (7.9.1)
-- Configuring download of missing dataset PhotonEvaporation (5.5)
-- Configuring download of missing dataset RadioactiveDecay (5.4)
-- Configuring download of missing dataset G4PARTICLEXS (2.1)
-- Configuring download of missing dataset G4PII (1.3)
-- Configuring download of missing dataset RealSurface (2.1.1)
-- Configuring download of missing dataset G4SAIDDATA (2.0)
-- Configuring download of missing dataset G4ABLA (3.1)
-- Configuring download of missing dataset G4INCL (1.0)
-- Configuring download of missing dataset G4ENSDFSTATE (2.2)
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of off64_t
-- Check size of off64_t - failed
-- Looking for fseeko
-- Looking for fseeko - found
-- Looking for unistd.h
-- Looking for unistd.h - found
-- Found PythonInterp: /usr/local/bin/python3 (found suitable version "3.8.5", minimum required is "3.0") 
-- Found PythonLibs: /usr/local/Frameworks/Python.framework/Versions/3.8/lib/libpython3.8.dylib (found version "3.8.5") 
-- Found Boost: /usr/local/lib/cmake/Boost-1.73.0/BoostConfig.cmake (found version "1.73.0")  
-- The following Geant4 features are enabled:
GEANT4_BUILD_CXXSTD: Compiling against C++ Standard '11'
GEANT4_BUILD_MULTITHREADED: Build multithread enabled libraries
GEANT4_BUILD_TLS_MODEL: Building with TLS model 'global-dynamic'
GEANT4_USE_SYSTEM_EXPAT: Using system EXPAT library
GEANT4_USE_QT: Build Geant4 with Qt support
GEANT4_USE_OPENGL_X11: Build Geant4 OpenGL driver with X11 support
GEANT4_USE_PYTHON: Building bindings for Python 3.8

-- Configuring done
CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4global" links to target "Boost::python" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/global/CMakeLists.txt:2 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4interfaces" links to target "Boost::python" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/interface/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4intercoms" links to target "Boost::python" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/intercoms/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4run" links to target "Boost::python" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/run/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4event" links to target "Boost::python" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/event/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4tracking" links to target "Boost::python" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/tracking/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4track" links to target "Boost::python" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/track/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4geometry" links to target "Boost::python" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/geometry/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4materials" links to target "Boost::python" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/materials/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4particles" links to target "Boost::python" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/particles/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4processes" links to target "Boost::python" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/processes/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4digits_hits" links to target "Boost::python" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/digits_hits/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4visualization" links to target "Boost::python" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/visualization/CMakeLists.txt:16 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4graphics_reps" links to target "Boost::python" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/graphics_reps/CMakeLists.txt:4 (geant4_add_pymodule)


CMake Error at environments/g4py/G4PythonHelpers.cmake:22 (add_library):
  Target "pyG4physicslists" links to target "Boost::python" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  environments/g4py/G4PythonHelpers.cmake:58 (g4py_add_module)
  environments/g4py/source/physics_lists/CMakeLists.txt:4 (geant4_add_pymodule)


-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.

The compiler is finding Boost just not this “target” “Boost:python”

Hold On! I think it is working now. In my hurry to fix the Boost:python problem I had forgotten I edited the g4py/CmakeLists.txt file to try and force it and it clearly wasn’t liking it. Redownloaded the source and everything seems to be working so far! Thanks a lot for your help, really appreciate it!

Michael

I can now use

from Geant4 import *

But I cannot run the TesEm0 example as it cannot find any module named tesmem0. Is there another folder which must be added to site-packages?

These are the contents of site-packages/Geant4/
‘G4digits_hits.so G4global.so G4interfaces.so G4physicslists.so G4track.so init.py g4thread.py
G4event.so G4graphics_reps.so G4materials.so G4processes.so G4tracking.so pycache g4viscp.py
G4geometry.so G4intercoms.so G4particles.so G4run.so G4visualization.so colortable.py hepunit.py’

I can also not import g4py for some reason or run any of the other examples!

Has something gone wrong with the installation?

Thanks,

Michael

Glad you’ve got it working, and there’s no issue with the installation. The Geant4Py examples are no longer part of the installation since they are intended to be read, modified etc. However, their build has not been updated yet to make them fully usable (apologies for that).

If you can give me a couple of days, I’ll get this done and post them here.

Ah, I see, thank you! No need to rush I am figuring it out as I go and am starting to understand how to implement it! A working example would be great though to see the full functionality!

Thanks again,

Michael

Would it be possible for you to do a tutorial using gEmCalculator? I am currently trying to use it and it is just prints zeros when I have:

from Geant4 import *
    water = gNistManager.FindOrBuildMaterial("G4_WATER")
    electron = gParticleTable.FindParticle("e-")
    dedx_list = []
    for counter, energy in enumerate(binValues):
        dedx= {}
        dedx["total"] = gEmCalculator.ComputeElectronicDEDX(energy, electron, water, 10 * keV)
        dedx_list.append((dedx, energy))
    print(dedx_list)

Is there something I am missing from this script?

Thanks,

Michael

Hi @bmorgan, do you have any update on the above discussion?

Thanks

Michael