Files for this page are in /psec/web/psec/library/tutorials/geant4.

Here we will compile the B1 example provided by Geant4.

Versions of software using
ROOT 6.16.00
Geant4 4.10.5
This software was compiled for RedHat7, so any computer running RedHat7 can be used for this tutorial. To see which operating system you're using, look at the file /etc/redhat-release. For this example, we're using psec1.

psec1:~ $ cat /etc/redhat-release 
Red Hat Enterprise Linux Workstation release 7.7 (Maipo)

  1. Make a directory to work in
    psec1:~ $ mkdir g4test
    psec1:~ $ cd g4test
    
  2. Copy the example files to your working directory
    psec1:g4test $ cp -R /code/geant4.10.05/share/Geant4-10.5.0/examples/basic/B1 .
    
  3. Make yourself a build directory to use to compile everything
    psec1:g4test $ mkdir B1build
    psec1:g4test $ ll
    total 8
    drwxr-xr-x 4 maryh support 4096 Feb 12 13:34 B1
    drwxr-xr-x 2 maryh support 4096 Feb 12 13:35 B1build
    
  4. Change to your build directory and set up your environment
    psec1:B1build $ export G4INSTALL=/code/geant4.10.05
    psec1:B1build $ export G4DIR=$G4INSTALL/lib64
    psec1:B1build $ printenv G4INSTALL
    /code/geant4.10.05
    psec1:B1build $ printenv G4DIR
    /code/geant4.10.05/lib64
    
    psec1:B1build $ which root
    /usr/bin/which: no root in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/support/maryh/bin)
    
    psec1:B1build $ . /code/geant4.10.05/bin/geant4.sh
    psec1:B1build $ . /code/root-6.16.00/bin/thisroot.sh 
    psec1:B1build $ which root
    /code/root-6.16.00/bin/root
    
  5. Use cmake to generate the make files that we're going to use to compile the program.
    psec1:B1build $ cmake -DGeant4_DIR=$G4DIR ../B1
    -- The C compiler identification is GNU 4.8.5
    -- The CXX compiler identification is GNU 4.8.5
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/support/maryh/g4test/B1build
    
    psec1:B1build $ ll
    total 92
    -rw-r--r-- 1 maryh support 12218 Feb 12 13:40 CMakeCache.txt
    drwxr-xr-x 6 maryh support  4096 Feb 12 13:40 CMakeFiles
    -rw-r--r-- 1 maryh support  2595 Feb 12 13:40 cmake_install.cmake
    -rw-r--r-- 1 maryh support   226 Feb 12 13:40 exampleB1.in
    -rw-r--r-- 1 maryh support 35617 Feb 12 13:40 exampleB1.out
    -rw-r--r-- 1 maryh support   338 Feb 12 13:40 init_vis.mac
    -rw-r--r-- 1 maryh support 12840 Feb 12 13:40 Makefile
    -rw-r--r-- 1 maryh support   553 Feb 12 13:40 run1.mac
    -rw-r--r-- 1 maryh support   448 Feb 12 13:40 run2.mac
    -rw-r--r-- 1 maryh support  3931 Feb 12 13:40 vis.mac
    
  6. Compile the program with make
    psec1:B1build $ make
    Scanning dependencies of target exampleB1
    [ 14%] Building CXX object CMakeFiles/exampleB1.dir/exampleB1.cc.o
    [ 28%] Building CXX object CMakeFiles/exampleB1.dir/src/B1RunAction.cc.o
    [ 42%] Building CXX object CMakeFiles/exampleB1.dir/src/B1DetectorConstruction.cc.o
    [ 57%] Building CXX object CMakeFiles/exampleB1.dir/src/B1ActionInitialization.cc.o
    [ 71%] Building CXX object CMakeFiles/exampleB1.dir/src/B1EventAction.cc.o
    [ 85%] Building CXX object CMakeFiles/exampleB1.dir/src/B1PrimaryGeneratorAction.cc.o
    [100%] Building CXX object CMakeFiles/exampleB1.dir/src/B1SteppingAction.cc.o
    Linking CXX executable exampleB1
    [100%] Built target exampleB1
    
    psec1:B1build $ ll
    total 2304
    -rw-r--r-- 1 maryh support   12218 Feb 12 13:40 CMakeCache.txt
    drwxr-xr-x 6 maryh support    4096 Feb 12 13:41 CMakeFiles
    -rw-r--r-- 1 maryh support    2595 Feb 12 13:40 cmake_install.cmake
    -rwxr-xr-x 1 maryh support 2265064 Feb 12 13:41 exampleB1  <-- This is your executable
    -rw-r--r-- 1 maryh support     226 Feb 12 13:40 exampleB1.in
    -rw-r--r-- 1 maryh support   35617 Feb 12 13:40 exampleB1.out
    -rw-r--r-- 1 maryh support     338 Feb 12 13:40 init_vis.mac
    -rw-r--r-- 1 maryh support   12840 Feb 12 13:40 Makefile
    -rw-r--r-- 1 maryh support     553 Feb 12 13:40 run1.mac
    -rw-r--r-- 1 maryh support     448 Feb 12 13:40 run2.mac
    -rw-r--r-- 1 maryh support    3931 Feb 12 13:40 vis.mac
    
  7. Run it
    psec1:B1build $ ./exampleB1 
    Available UI session types: [ GAG, tcsh, csh ]
    
    **************************************************************
     Geant4 version Name: geant4-10-05    (7-December-2018)
                           Copyright : Geant4 Collaboration
                          References : NIM A 506 (2003), 250-303
                                     : IEEE-TNS 53 (2006), 270-278
                                     : NIM A 835 (2016), 186-225
                                 WWW : http://geant4.org/
    **************************************************************
    
    <<< Reference Physics List QBBC 
    Visualization Manager instantiating with verbosity "warnings (3)"...
    Visualization Manager initialising...
    Registering graphics systems...
    
    You have successfully registered the following graphics systems.
    Current available graphics systems are:
    ASCIITree (ATree)
    DAWNFILE (DAWNFILE)
    G4HepRep (HepRepXML)
    G4HepRepFile (HepRepFile)
    RayTracer (RayTracer)
    VRML1FILE (VRML1FILE)
    VRML2FILE (VRML2FILE)
    gMocrenFile (gMocrenFile)
    OpenGLImmediateX (OGLIX, OGLI)
    OpenGLStoredX (OGLSX, OGL, OGLS)
    
    Registering model factories...
    
    You have successfully registered the following model factories.
    Registered model factories:
      generic
      drawByAttribute
      drawByCharge
      drawByOriginVolume
      drawByParticleID
      drawByEncounteredVolume
    
    Registered filter factories:
      attributeFilter
      chargeFilter
      originVolumeFilter
      particleFilter
      encounteredVolumeFilter
    
    You have successfully registered the following user vis actions.
    Run Duration User Vis Actions: none
    End of Event User Vis Actions: none
    End of Run User Vis Actions: none
    
    Some /vis commands (optionally) take a string to specify colour.
    "/vis/list" to see available colours.
    /control/saveHistory
    /run/verbose 2
    #
    # Change the default number of threads (in multi-threaded mode)
    #/run/numberOfThreads 4
    #
    # Initialize kernel
    /run/initialize
    userDetector->Construct() start.
    Checking overlaps for volume Envelope ... OK! 
    Checking overlaps for volume Shape1 ... OK! 
    Checking overlaps for volume Shape2 ... OK! 
    World is registered to the default region.
    physicsList->Construct() start.
    ### Adding tracking cuts for neutron  TimeCut(ns)= 10000  KinEnergyCut(MeV)= 0
    physicsList->CheckParticleList() start.
    physicsList->setCut() start.
    #
    # Visualization setting
    /control/execute vis.mac
    # Macro file for the visualization setting in the initialization phase
    # of the B1 example when running in interactive mode
    #
    # Use these open statements to open selected visualization
    #
    # Use this open statement to create an OpenGL view:
    /vis/open OGL 600x600-0+0
    /vis/sceneHandler/create OGL
    /vis/viewer/create ! ! 600x600-0+0
    

    !!!! The errors below are because the program is trying to open a window and we're running remotely. If we were physically sitting in front of psec1, this would work fine. But for what you're doing, you don't need any visualization. There's a way to turn this off, but I don't remember exactly how right now.

    libGL error: No matching fbConfigs or visuals found
    libGL error: failed to load driver: swrast
    X Error of failed request:  BadValue (integer parameter out of range for operation)
      Major opcode of failed request:  151 (GLX)
      Minor opcode of failed request:  3 (X_GLXCreateContext)
      Value in failed request:  0x0
      Serial number of failed request:  33
      Current serial number in output stream:  34