I’m just back from ANIMMA 2017, which was held in Liège, Belgium at the Palais des Congrès, from where I took the photo. This is the first time I’ve presented work at a conference since I moved towards radiation detector development; think it went quite well. The weather was so warm as to be uncomfortable (humid and rarely below 30C/86F), but I enjoyed the conference, met a lot of new people and discussed some really interesting science. Looking forward to getting back to work and implementing some new ideas.

Using GEANT4 (Still for Dummies) Part 2: Detector Construction (2)

The geometry of each object in Geant4 consists of three components:

  • The solid,
  • The logical volume,
  • The physical volume.

The solid defines the shape of the object, box or sphere, for instance. A comprehensive list of these and their usage can be found on the Geant4 website. For example B1, the shape of the simulation space is defined as:

G4Box* solidWorld = 
  new G4Box("World",                                       //its name
  0.5*world_sizeXY, 0.5*world_sizeXY, 0.5*world_sizeZ);    //its size

G4Box defines the shape, the argument “World” defines its name, and the size is defined on the final line, where the variables world_sizeXY and world_sizeZ  were defined earlier.

The logical volume is defined next:

G4LogicalVolume* logicWorld = 
  new G4LogicalVolume(solidWorld,                       //its solid
  world_mat,                                           //its material
  "World");                                            //its name

Here, the first argument defines which solid the logical volume is related to, the second defines the material (as described in the last post), and the final line names the volume.

Finally, the physical volume is defined:

G4VPhysicalVolume* physWorld =new G4PVPlacement(
              0,                     //no rotation
              G4ThreeVector(),      //at (0,0,0)
              logicWorld,           //its logical volume
              "World",              //its name
              0,                   //its mother volume
              false,               //no boolean operation
              0,                   //copy number
              checkOverlaps);     //overlaps checking

The first argument defines the rotation of the physical volume; in this case, none. Next, the position is defined, and for this volume it is centred at the origin. Then the name of the logical volume to which the physical volume is connected is given, followed by the name of the physical volume. Since this volume is the extent of the model, it has no mother volume (which is the volume in which it is contained). The boolean operation is a method by which solids can be combined within Geant4, and is not used here. Copy numbers are used if the volume is repeated, and the final argument is used to ensure that no volumes overlap.

Using GEANT4 (Still for Dummies) Part 2: Detector Construction (1)

As any GEANT4 introduction will earnestly tell you, GEANT4 is a toolkit. In practice, this means there are a great many methods of design and implementation such that when you start learning about all the options, you feel as though you’re using a wrecking ball where a screwdriver might do (to belabour the metaphor).

A GEANT4 installation comes with a number of example simulations, which are very useful, cover a wide range of situations, and may be modified to fit your purpose. The README.txt file in the basic/, advanced/, and extended/ directories give a summary of the purposes of the simulations in each directory. Going further into the directories B1/, B2/, B3/, and so forth, a further README.txt file gives more in-depth information about each simulation (for example, the source, geometries, and physics used).

Different things are defined in different sections of the code. Using the basic B1 simulation as an example, I will describe what these different sections are doing.

Geometries, materials, fields, sensitive detectors – DetectorConstruction()

In the DetectorConstruction() class, geometries, materials, fields and sensitive detectors are defined.

Since materials are the simplest to explain, I shall start with them.

Some materials are predefined in GEANT4, a list of these can be found here. If you wish to make use of these, the relevant header file must be included with the other headers:

#include "G4NistManager.hh"

Within the G4VPhysicalVolume constructor, to get the NIST materials manager and choose one of the predefined materials in this manner :

G4NistManager* nist = G4NistManager::Instance();
G4Material* env_mat = nist->FindOrBuildMaterial("G4_WATER");

The NIST manager can also be used to define materials made of a single element, in a similar manner.

You can also define your own materials. This is not shown in example B1, but can be done like this:

 G4Element* Cd = new G4Element("Cadmium","Cd",48., 112.41*g/mole);
 G4Element* Zn = new G4Element("Zinc","Zn", 30., 65.38*g/mole);
 G4Element* Te = new G4Element("Tellurium","Te", 52., 127.60*g/mole);
 G4Material* CZT = new G4Material("CZT", 5.8*g/cm3, ncomponents=3);
 CZT->AddElement(Cd, 48*perCent);
 CZT->AddElement(Zn, 02*perCent);
 CZT->AddElement(Te, 50*perCent);

More to follow.

Using GEANT4 (Still for Dummies) Part 1: Some notes on C++

While in the process of diving straight into a GEANT4 example (the subject of part 2), it occurred to me that a note on programming in C++ would be of use.

Making a model in GEANT4 requires the use of C++ to build the model. A model therefore requires a main(), source files, and header files, as a minimum. It is useful to at least be aware of some general programming concepts, and their application to C++ in particular.

The C++ features that GEANT4 uses are:

  • Inheritance.
  • Singletons.
  • Templates.
  • Classes.
    • Constructors/destructors, methods and arguments, constant initialisation and operator overloading.

I am not going to go into detail on these, because more comprehensive explanations are available in textbooks and online, and I consider the purpose of this series of posts to be a collection of information that is not necessarily as readily available. Here is a useful link, which gives an overview of C++ programming.

Geant4 for Dummies (Part 3)

So, that job I was talking about in part 1: I got it. Now I’m two months in, and need to use Geant4 for models.

So, one of the useful things about the Geant4 installation is that it comes with several examples which can be changed to be more relevant to your model. Unfortunately, this is not the most simple thing in the world.

So, to begin, with any single example, create a build directory in your main Geant4 folder.

cd Geant4
mkdir B1-build
cd B1-build

After this, you may need to specify the location of the geant4.(c)sh file, as well as use cmake, and then make, in a similar manner to the installation. For cmake, you specify this Geant4-10.2.1 folder (or whatever version you’re using; the latest is currently 10.2.2), and the original directory of the example of interest.

source /home/[user]/GEANT4/install/bin/
cmake -DGEANT4_DIR=/home/[user]/GEANT4/install/lib/Geant4-10.2.1 
make -j2

This should take a few minutes. There are then a number of ways to run the executable. One is:


This opens a visualisation window, into which commands can be written (I’ll expand on this in a later edit).

Another option is:

./exampleB1 example.mac

Here, a macro file exists which will run a model. This should produce an *.out file, and possibly a *.root file, and will take a few minutes, depending on the complexity of the model and the number of runs specified.

In a later edit, I’ll go into the details of the necessary files to create a viable Geant4 simulation, as well as a discussion of the outputs (both *.out and *.root).

Geant4 for Dummies (Part 2)

At this point, this is just for my reference. I’ll expand on this a bit when I’ve got more to grips with the software.

To make and run an executable:

cd GEANT4/geant4.10.02.p01/examples/basic/B1
mkdir test
cd test
source /home/[user]/GEANT4/install/bin/
cmake -DGEANT4_DIR=/home/[user]/GEANT4/install/lib/Geant4-10.2.1 
make -j2

(Edit: see part 3 for a more in-depth explanation of how to make and run an executable.)

Geant4 for Dummies (Part 1)

So, I’ve got an interview later in the week, and the job spec asks for experience in MCNP or Geant4. Since I have used MCNP in the past, I thought I’d install and get to grips with Geant4 before the interview. Well, that’s easier said than done. There isn’t really a single webpage which gives you all the info, if you’re installing Geant4 from scratch with no particular experience in using it before.

I’m installing Geant4 on an Ubuntu 16.04 virtual machine, running through VMWare. I’ve also set this up using CentOS 7, and the only main difference is that the version of cmake which you can install on CentOS via the repository is not sufficiently recent enough, so you have to go and hunt that out. There is a cmake website, which is useful in this regard.

Also of use are the following packages, which can be installed using

sudo apt-get install [..]

The answer to this forum post (which I will reproduce here) is what I shall refer to as a ‘dependencies goldmine’, although there was one slight change required (libXmu-dev from the original post only works if you change it to libxmu-dev, as I have done below. I’ve also included xauth and freeglut3.)

sudo apt-get install cmake build-essential libgl1-mesa-dev libqt4-dev 
    libxmu-dev zlibc libxerces-c-dev root-system xauth freeglut3 qt-sdk

Anyway, Geant4 downloads are found here. I’m using the GNU/Linux tar source file, which I’ve downloaded to a new folder in my home directory, which I’ve imaginatively named GEANT4. As well as the tarball, I’ve also made two new directories within this folder, namely build and install. Unzip the tarball via the terminal:

tar xvzf geant4.10.02.p1.tar.gz

This will create a new folder (geant4.10.02.p1). After this, the real fun begins.

Now, move into the build directory:

cd build
cmake -DCMAKE_INSTALL_PREFIX=/home/[user]/GEANT4/install 
    /home/[user]/GEANT4/geant4.10.02.p1 -DGEANT4_INSTALL_DATA=ON 

The INSTALL_DATA bit will download and install the Geant4 datasets, which I believe are useful if you want to actually do anything.

[NB: the dependencies goldmine used:

    -DGEANT4_USE_SYSTEM_EXPAT=OFF /yourpath/geant4.10.01


Now for the bit that will take a while (I’m not kidding, this took around 8-9 hours, so make sure you have the time [although on my quicker work machine this only took ~2.5 hours]). Type in the terminal:

make -jN

where N is the number of cores on a multi-core system. I personally set N to be 2, although if you can commit more, this should speed up this bit. Once this is (finally) completed:

make install

So that’s the basic installation.

The thought has occurred that a bash script would be a simple way to download and install Geant4. Once I’ve made an installation that I’m happy with, I’ll see about making a simple bash script for this purpose, such that the end user would merely having to make some simple changes to the script and run a single terminal command.

[Edit 25/04/17: I’ve just installed Geant4-10.3.1 on a new Ubuntu virtual machine, and the method detailed here still works.]

In part two, I’ll explain how to run the example codes given in the install, once I’ve figured out how to do it myself.

Oh, and here’s some webpages I’ve found useful in all this:

CERN: building and installing Geant4

CERN: Geant4 build options

Stanford/SLAC setting up Geant4

Youtube video of how to run an example in Geant4

(Edited 07/06/16: info on visualisation.)

(Edited 09/06/16: dependencies goldmine added. Should make it possible to use visualisation.)

(Edited 10/06/16: Bit of tidying up. Thoughts on bash scripts added.)

(Edited 26/08/16: Repetition of ‘cmake’ removed from dependencies install.)

(Edited 05/09/16: included qt-sdk in dependencies.)