### Install geant4-pybind Source: https://github.com/haarigerharald/geant4_pybind/blob/main/README.md Installs the geant4-pybind package from PyPI using pip. ```bash pip3 install geant4-pybind ``` -------------------------------- ### Build geant4-pybind from Source Source: https://github.com/haarigerharald/geant4_pybind/blob/main/README.md Clones the repository and installs the package locally. Requires specific build prerequisites. ```bash git clone --recursive https://github.com/HaarigerHarald/geant4_pybind pip3 install ./geant4_pybind ``` -------------------------------- ### Minimal Geant4 Python Shell Source: https://github.com/haarigerharald/geant4_pybind/blob/main/README.md A basic Python script to start a Geant4 interactive session using the pybind11 bindings. ```python from geant4_pybind import * import sys ui = G4UIExecutive(len(sys.argv), sys.argv) ui.SessionStart() ``` -------------------------------- ### Required Includes for Geant4 Pybind11 Source: https://github.com/haarigerharald/geant4_pybind/blob/main/DESIGN.md Lists the essential header files required for integrating Geant4 with Python using pybind11, including pybind11 core, STL support, and custom typecasters for Geant4 compatibility. ```cpp #include #include #include "typecast.hh" // disown_ptr and G4String typecasters #include "opaques.hh" // all opaque declarations ``` -------------------------------- ### CMake Project Setup and Compiler Flags Source: https://github.com/haarigerharald/geant4_pybind/blob/main/CMakeLists.txt Configures the minimum CMake version, project name, C++ standard, and compiler visibility. It also sets specific compiler flags for MSVC and GCC/Clang to enhance code safety and warning levels. ```cmake cmake_minimum_required(VERSION 3.16) project(geant4_pybind) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_VISIBILITY_PRESET hidden) if(MSVC) if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") string(REGEX REPLACE "/W[0-4]" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3") endif() elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long") endif() ``` -------------------------------- ### Disown Pointer Ownership Transfer Source: https://github.com/haarigerharald/geant4_pybind/blob/main/DESIGN.md Demonstrates how to wrap a C++ function that transfers ownership of a raw pointer using a lambda with pybind11's `disown_ptr`. This is crucial for managing Geant4 objects where ownership is implicitly transferred. ```cpp g4RunManager.def("SetUserInitialization", [](G4RunManager &self, py::disown_ptr detector) { self.SetUserInitialization(detector); }); ``` -------------------------------- ### Nodelete for Disowned Objects Source: https://github.com/haarigerharald/geant4_pybind/blob/main/DESIGN.md Illustrates how to use the `py::nodelete` template argument when defining a pybind11 class. This is necessary for Geant4 objects that are disowned upon construction and added to a global store, preventing pybind11 from attempting to delete them. ```cpp py::class_ g4box(m, "G4Box"); ``` -------------------------------- ### CMake Configuration for geant4_pybind Module Source: https://github.com/haarigerharald/geant4_pybind/blob/main/source/CMakeLists.txt This snippet outlines the CMake commands used to configure the geant4_pybind module. It includes defining source files, adding the Python module using pybind11, setting include directories, defining preprocessor macros based on found Geant4 features, and linking the necessary libraries. ```cmake file(GLOB_RECURSE SOURCE CONFIGURE_DEPENDS analysis/*.cc digits_hits/*.cc event/*.cc geometry/*.cc global/*.cc graphics_reps/*.cc intercoms/*.cc interface/*.cc materials/*.cc particles/*.cc persistency/*.cc physics_lists/*.cc processes/*.cc run/*.cc tasking/*.cc track/*.cc tracking/*.cc visualization/*.cc ) pybind11_add_module(geant4_pybind ${SOURCE} geant4_pybind.cc ) target_include_directories(geant4_pybind PRIVATE "${CMAKE_CURRENT_LIST_DIR}") target_compile_definitions(geant4_pybind PRIVATE PYBIND11_USE_SMART_HOLDER_AS_DEFAULT $<$:G4_HAS_GDML> $<$:G4_HAS_TCSH> $<$:G4_HAS_OPENGLX> $<$:G4_HAS_RAYTRACERX> $<$:G4_HAS_OPENGLXM> $<$:G4_HAS_OPENGLWIN> $<$:G4_HAS_QT> ) target_link_libraries(geant4_pybind PRIVATE ${Geant4_LIBRARIES}) ``` -------------------------------- ### Geant4 and pybind11 Integration Source: https://github.com/haarigerharald/geant4_pybind/blob/main/CMakeLists.txt Finds the required Geant4 version and optional components, sets the Python version for pybind11, and includes the pybind11 and project source subdirectories. It also conditionally includes tests if testing is enabled. ```cmake find_package(Geant4 11.0 REQUIRED) find_package(Geant4 OPTIONAL_COMPONENTS gdml ui_tcsh motif vis_raytracer_x11 vis_opengl_x11 vis_opengl_win32 qt) set(PYBIND11_PYTHON_VERSION 3.5) add_subdirectory(pybind11) add_subdirectory(source) include(CTest) if(BUILD_TESTING) add_subdirectory(tests) endif() ``` -------------------------------- ### Define and Configure Tests Source: https://github.com/haarigerharald/geant4_pybind/blob/main/tests/CMakeLists.txt Defines individual tests using CMake's `add_test` command, specifying the Python executable and the test script. It then sets the `PYTHONPATH` environment variable for these tests using `set_tests_properties` to ensure the pybind module is discoverable. ```cmake add_test(NAME test_B1 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_B1.py) add_test(NAME test_destruction COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_destruction.py) add_test(NAME test_examples COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_examples.py) set_tests_properties(test_B1 test_destruction test_examples PROPERTIES ENVIRONMENT PYTHONPATH=$) ``` -------------------------------- ### Update Git Submodules Source: https://github.com/haarigerharald/geant4_pybind/blob/main/README.md Updates the git submodules for the repository if it was cloned non-recursively. ```bash git -C ./geant4_pybind submodule update --init ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.