Apptainer/Singularity install for Geant4-11.3

This is a guide to building a Geant4 singularity image file, as I did not find something similar online.

I used a container, as our old HPC has glibc 2.17. I needed G4 with GDML and xerces-c needs glibc 3.4 (install ‘succeeds’, but errors when making a G4 example).

As the data files are upgraded over time, you may need to update the versions in the definition ‘def’ file (in %environment).

Once built, run

apptainer exec geant4-11.3.0-alma.sif ls /opt/geant4/install/share/Geant4/data

then update the definition ‘def’ file with the correct datafile versions as necessary, rebuild.

The definition file geant4-11.3.0-alma.def is as below:

Bootstrap: docker
From: almalinux:9

%post
# Install required packages
   # Install development tools and dependencies
    dnf group install -y "Development Tools"
    dnf install -y \
    epel-release
    dnf install -y \
        cmake \
        expat-devel \
        gcc-c++ \
        make \
        xerces-c-devel \
        git \
        wget

    # Create installation directories
    mkdir -p /opt/geant4/build
    mkdir -p /opt/geant4/install

    # Download and install Geant4
    cd /opt/geant4
    wget https://gitlab.cern.ch/geant4/geant4/-/archive/v11.3.0/geant4-v11.3.0.tar.gz
    tar xzf geant4-v11.3.0.tar.gz
    cd build

   # Configure and build Geant4
    cmake ../geant4-v11.3.0 \
        -DCMAKE_INSTALL_PREFIX=/opt/geant4/install \
        -DGEANT4_INSTALL_DATA=ON \
        -DGEANT4_USE_GDML=ON \
        -DGEANT4_USE_SYSTEM_EXPAT=ON \
        -DGEANT4_BUILD_MULTITHREADED=ON

    # Build and install
    make -j$(nproc)
    make install

    # Cleanup
    cd /opt/geant4
    rm -rf build geant4-v11.3.0.tar.gz
    rm -f /root/*.pkg.tar*
    rm -f /root/*.tar.gz 

%files

%environment
export PATH="/opt/geant4/install/bin:${PATH}"
export G4INSTALL="/opt/geant4/install"
export G4EXAMPLES="/opt/geant4/install/share/Geant4/examples"
export G4NEUTRONHPDATA="/opt/geant4/install/share/Geant4/data/G4NDL4.7.1"
export G4LEDATA="/opt/geant4/install/share/Geant4/data/G4EMLOW8.6.1"
export G4LEVELGAMMADATA="/opt/geant4/install/share/Geant4/data/PhotonEvaporation6.1"
export G4RADIOACTIVEDATA="/opt/geant4/install/share/Geant4/data/RadioactiveDecay6.1.2"
export G4PARTICLEXSDATA="/opt/geant4/install/share/Geant4/data/G4PARTICLEXS4.1"
export G4PIIDATA="/opt/geant4/install/share/Geant4/data/G4PII1.3"
export G4REALSURFACEDATA="/opt/geant4/install/share/Geant4/data/RealSurface2.2"
export G4SAIDXSDATA="/opt/geant4/install/share/Geant4/data/G4SAIDDATA2.0"
export G4ABLADATA="/opt/geant4/install/share/Geant4/data/G4ABLA3.3"
export G4INCLDATA="/opt/geant4/install/share/Geant4/data/G4INCL1.2"
export G4ENSDFSTATEDATA="/opt/geant4/install/share/Geant4/data/G4ENSDFSTATE3.0"

%runscript
    exec "$@"

%help
Geant4-11.3.0-1
Built with system xerces-c for GDML support

To build:

sudo apptainer build geant4-11.3.0-alma.sif geant4-11.3.0-alma.def

Once built, you can test using an example:

apptainer exec geant4-11.3.0-alma.sif cp -r /opt/geant4/install/share/Geant4/examples .
cd examples/extended/electromagnetic/
mkdir TestEm1_build
cd TestEm1_build
apptainer exec geant4-11.3.0-alma.sif cmake ../TestEm1
apptainer exec geant4-11.3.0-alma.sif make TestEm1
apptainer exec geant4-11.3.0-alma.sif ./TestEm1 annihil.mac

Thanks to Simon Biggs.