You are here: Foswiki>AGLT2 Web>UsingGeant4 (15 Mar 2012, BenMeekhof)EditAttach

Setting up Geant4

A 64-bit gcc 4.1.2 Geant4 installation for SL5 is located at /afs/ All available data packages are included. For a list see

You have the option of using either the GNU Make toolchain or the Cmake toolchain. The documentation says that the GNU Make toolchain is provided only for backwards compatibility. The Geant4 manual encourages the usage of cmake instead. Cmake is a tool for generating configuration scripts for a variety of build tools. To see the possibilities type "cmake -h". You will probably just want to generate GNU Makefiles and this is the default on Linux if nothing is specified. Cmake can generate Visual Studio project files for building Geant4 binaries on windows as well as other formats for other tools which may not be as well supported by the Geant4 developers.

Example using Cmake

Cmake will generate a build script in your current working directory. When you build, the executable will be in your current working directory. Look at the file CMakeLists.txt in any of the example directories to see how this is configured. Below is an example:
source /afs/
mkdir testing
cd testing
cmake /afs/

(this generates some files in your current directory)



(X11 required, a windows should come up with a simple example)

Below is the CMakeLists.txt for example B1 used to generate the example above:
# Setup the project
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

# Find Geant4 package, activating all available UI and Vis drivers by default
# You can set WITH_GEANT4_UIVIS to OFF via the command line or ccmake/cmake-gui
# to build a batch mode only executable
option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON)
  find_package(Geant4 REQUIRED ui_all vis_all)
  find_package(Geant4 REQUIRED)

# Setup Geant4 include directories and compile definitions
# Setup include directory for this project

# Locate sources and headers for this project
# NB: headers are included so they will show up in IDEs
file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)                 

# Add the executable, and link it to the Geant4 libraries
add_executable(exampleB1 ${sources} ${headers})
target_link_libraries(exampleB1 ${Geant4_LIBRARIES})

# Copy all scripts to the build directory, i.e. the directory in which we
# build B1. This is so that we can run the executable directly because it
# relies on these scripts being in the current working directory.

foreach(_script ${EXAMPLEB1_SCRIPTS})

# For internal Geant4 use - but has no effect if you build this
# example standalone
add_custom_target(B1 DEPENDS exampleB1)                

# Install the executable to 'bin' directory under CMAKE_INSTALL_PREFIX
install(TARGETS exampleB1 DESTINATION bin)

Example using GNU Make

Programs using Geant4 libraries can also be built by writing makefiles without cmake. See GNUMakefile in any example directory for how to do this.

This script sets a default working directory of ~/geant4_workdir. Your compiled programs will be in geant4_workdir/bin. This can be modified by setting the G4WORKDIR environment variable to some other value (export G4WORKDIR="/some/other/path").
source /afs/

cd /afs/



Below is the GNUMakefile used to generate example B1:
# $Id: GNUmakefile,v 1.2 2000-10-19 12:22:10 stanaka Exp $
# --------------------------------------------------------------
# GNUmakefile for examples module.  Gabriele Cosmo, 06/04/98.
# --------------------------------------------------------------

name := exampleB1
G4TARGET := $(name)
G4EXLIB := true

ifndef G4INSTALL
  G4INSTALL = ../../..

.PHONY: all
all: lib bin

include $(G4INSTALL)/config/binmake.gmk

        rm -f g4*.prim g4*.eps g4*.wrl
        rm -f .DAWN_*

For user documentation see

For more information about usage for ATLAS simulation see

-- BenMeekhof - 15 Mar 2012
Topic revision: r2 - 15 Mar 2012 - 23:12:28 - BenMeekhof

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback