Recent Changes - Search:

Public

Physics

Engineering

Literature
Journal Club
People

MineralPET
DAQ

Computing

Africa


Geant4RadioactiveDecay

Radioactive decay with Geant 4.9.5 (4.10.0)

Note: The full files used in this example are available for download at the end of this page.

This is a step-by-step guide to compiling the radioactive decay example from geant4.9.5.p02/examples/extended/radioactivedecay/rdecay02. Also included:

  • Getting the example to work with Root histograms
  • Adding visualisation code

Getting the basic example to work

Look here for notes on whether to use cmake or make. The rest of this section will use the cmake method.

Two steps: .... make a "build" directly, and from within it ... type
cmake -DGeant4_DIR=<your geant4 install folder> ../rdecay02/
make rdecay02 

The second argument of cake is the relative path to the source directory. The example should compile and run, so if it does not work, fix any issues first.

Adding visualisation

First, we need to adjust the CMakeLists.txt to link to the visualisation libraries. Replace the following line:
find_package(Geant4 REQUIRED)

with
option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON)
if(WITH_GEANT4_UIVIS)
  find_package(Geant4 REQUIRED ui_all vis_all)
else()
  find_package(Geant4 REQUIRED)
endif() 

(This is already done like this in geant4.10.0)'
Because we have changed the cmake settings, we will need to clear the build folder, and run cmake again in the empty folder. Than make rdecay02 again.

Before Geant4 will draw anything, it needs to be sent the necessary commands. The easiest way to do this is via a .mac file. Create a file called vis.mac with the following contents:
/run/initialize
/vis/open OGL
/vis/drawVolume
/vis/scene/add/hits
/vis/scene/add/trajectories

Save this file to your build directory, where the executable is (if you are using Xcode, this might be in the Debug folder)

Next, we need to tell the main program to run vis.mac. Look for the line where the G4UIExecutive is created:
G4UIExecutive* ui = new G4UIExecutive(argc, argv);

Add the following straight after this:
#ifdef G4VIS_USE
      UImanager->ApplyCommand("/control/execute vis.mac");
#endif 

make again, then run. It should draw the geometry, and show the tracks when you use /run/beamOn.

Adding root histograms

Here we need to edit the CMakeLists.txt file again to link to the Root library. This can be done using a FindROOT.cmake file. This is provided in the Geant4 source folder. Copy the contents of geant4.9.5.p02/cmake/Modules/ into a directory called cmake in your source folder. Then, go to your CmakeLists.txt file, after this line:
include(${Geant4_USE_FILE})

Add the following:
#----------------------------------------------------------------------------
# Find ROOT (optional package)
#
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
find_package(ROOT QUIET)
if(ROOT_FOUND)
  message(STATUS "ROOT found. --> compiling with ROOT enabled.") 
  # Uncomment this line if suitable (some customization may be needed)
  add_definitions(-DG4ANALYSIS_USE_ROOT)   
else()
  message(STATUS "G4 ROOT NOT found. --> ROOT disabled.") 
endif() 
Next, you need to edit the include_directories command to include ${ROOT_INCLUDE_DIR}, and the target_link_libraries command to include ${ROOT_LIBRARIES}. The whole file is listed here:
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

set(name rdecay02)
project(${name})

option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON)
if(WITH_GEANT4_UIVIS)
  find_package(Geant4 REQUIRED ui_all vis_all)
else()
  find_package(Geant4 REQUIRED)
endif()

include(${Geant4_USE_FILE})

#----------------------------------------------------------------------------
# Find ROOT (optional package)
#
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
find_package(ROOT QUIET)
if(ROOT_FOUND)
  message(STATUS "ROOT found. --> compiling with ROOT enabled.") 
  # Uncomment this line if suitable (some customization may be needed)
  add_definitions(-DG4ANALYSIS_USE_ROOT)   
else()
  message(STATUS "G4 ROOT NOT found. --> ROOT disabled.") 
endif()

#----------------------------------------------------------------------------
# Locate sources and headers for this project
#
include_directories(${PROJECT_SOURCE_DIR}/include 
                    ${Geant4_INCLUDE_DIR}
                    ${ROOT_INCLUDE_DIR})
file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
# the following line makes the headers visible in IDEs, for example Xcode
file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)


add_executable(${name} EXCLUDE_FROM_ALL ${name}.cc ${sources} ${headers})
target_link_libraries(${name} ${Geant4_LIBRARIES} ${ROOT_LIBRARIES}) 

Clear the build directory, cmake again, then make rdecay02 again. At the end of each run now, it should create a root histogram in the same folder as the executable called exrdm.root.

Adding variable seeds (needed for cluster deployment)

Firstly, we will collect useful commands into a macro file. Save the following in messenger.mac:
# These values are set with the messenger technology by the /control/alias statement
/random/setSeeds {seed1} {seed2}
Next, add a section to rdecay02.cc after the G4UIExecutive is created to execute /control/alias with any parameters on the command line:
else if  (argc>=3) {          // Cluster mode
	for (int ia=2;ia<argc;ia++){
    		// split an alias=value string and call /control/alias
   		char* alias=argv[ia];
    	char* value=strchr(alias,'=');
    	if (value!=NULL) {
	    	value[0]='\0';value++;
	        char cmd[1024];
	        snprintf(cmd,1024,"/control/alias %s %s",alias,value);
	        printf("%s\n",cmd);
	        UImanager->ApplyCommand(cmd);
	    }
	}
	//UImanager->ApplyCommand("/control/listAlias");   // nowadays out
	UImanager->ApplyCommand("/control/execute messenger.mac");
}

Now, make again. You should now be able to run the example by specifying the random seeds on the command line, for example:
./rdecay02 as74.mac seed1=123 seed2=321

You can change the name of the root file using a similar technique. For example, if martin.mac has the following:
/run/initialize
/histo/fileName {fileName}
/run/beamOn 200

you can execute:
./rdecay02 martin.mac fileName=hello seed1=123 seed2=321

and it will create a root file called hello.root.

Files

Here are the full files used in the above example:

Edit - History - Print - Recent Changes - Search
Page last modified on June 01, 2014, at 01:03 PM