Geant4 Interface To Work With IAEA Phase-Space Files
Geant4 Interface To Work With IAEA Phase-Space Files
Geant4 Interface To Work With IAEA Phase-Space Files
1
Dep. Fı́sica Atómica, Molecular y Nuclear, Universidad de Sevilla, Ap. 1065, E-41080 Sevilla, Spain
2
NAPC–Nuclear Data Section, International Atomic Energy Agency, Vienna A-1400, Austria
Contents
1 Introduction to this guide 2
4 Examples 13
1
1 Introduction to this guide
The aim of this document is to be the reference manual for a user that needs to read or
write IAEA formatted phase-space files (PSF) in his/her Geant4 application. All the classes
included in this interface are carefully explained in the following sections, therefore the user
is encouraged to read through this manual before using them.
In section 2 a detailed description of G4IAEAphspReader and G4IAEAphspWriter classes
is presented, including for each one a reference list with the available public methods. A list
showing the IAEA public functions used in these classes is provided as well.
Section 3 explains how these files must be included in a Geant4 application. Next, the
instructions of use, including some pieces of code as a brief examples, are given in order to
provide the user a guide clarifying how to use these classes properly.
Finally, in section 4 some typical examples are shown. These are real examples that should
work in a Geant4 application. Their goal is to show how the most important methods of these
classes can be used.
• The files defining the Geant4 reader class: G4IAEAphspReader (.hh and .cc)
• The files defining the Geant4 writer class: G4IAEAphspWriter (.hh and .cc)
The description of these files and classes is presented in the following sections.
• iaea_config.h
2
IAEA function Used in G4 classes?
iaea_check_file_size_byte_order() Yes (R)
iaea_copy_header() No
iaea_destroy_source() Yes (R/W)
iaea_get_constant_variable() Yes (R)
iaea_get_extra_numbers() Yes (R)
iaea_get_max_particles() Yes (R)
iaea_get_maximum_energy() No
iaea_get_particle() Yes (R)
iaea_get_total_original_particles() Yes (R)
iaea_get_type_extra_variables() Yes (R)
iaea_get_used_original_particles() Yes (R)
iaea_new_source() Yes (R/W)
iaea_print_header() Yes (W)
iaea_set_constant_variable() Yes (W)
iaea_set_extra_numbers() Yes (W)
iaea_set_paralell() No
iaea_set_record() No
iaea_set_total_original_particles() Yes (W)
iaea_set_type_extrafloat_variable() No
iaea_set_type_extralong_variable() Yes (W)
iaea_update_header() Yes (W)
iaea_write_particle() Yes (W)
Table 1: List of the public functions declared in the file iaea phsp.h. More information about
these functions can be found in the code itself or in the documentation provided in [2]. The
right column indicates whether the function is invoked in the Geant4 interface or not. “R”
means that it is used in G4IAEAphspReader class, whereas “W” means that it is called in
G4IAEAphspWriter class.
Whenever the corresponding data of a certain phase space are recorded, these routines
create two files with extensions .IAEAphsp and .IAEAheader, respectively. The .IAEAphsp
file is a binary in which the previously asked data of all the particles passing through the
defined plane is stored, including extra-variables when requested. Currently, the IAEA phsp
format supports the storage of the following particles: photons, electrons, positrons, neutrons
and protons. In addition, in the .IAEAheader, which is an ASCII file, the user can find
information related to the phase-space data stored in the file, such as a statistical summary,
number of original histories, number of particles of each kind that crossed the phase-space
plane. . . and more.
3
function is used in the reader class (G4IAEAphspReader ), whereas a “W” means that the
IAEA function is invoked in the writer class (G4IAEAphspWriter ). Obviously 1, the functions
that create and destroy an IAEA phsp object are used in both Geant4 classes. Not all functions
of the IAEA interface have been used so far. There are utilities not needed for the current
version, but they might be included in future versions of the release. If the reader needs to
use any of them, he/she should contact authors, and an updated version would be prepared
and released.
• In order to do a proper statistical analysis, it is very important to give the user the
possibility of taking into account existing correlations. The class has been designed
so that all generated particles that may have correlations between them are generated
in the same Geant4 event. Correlations mostly come from two different sources: (a)
particles sharing the same original history and (b) particles recycled several times. The
user can set the number of times that each particle is recycled by means of the method
SetTimesRecycled(int). The criterium followed for the argument is the same as the one
used in the code EGSnrc [3, 4], therefore ‘0’ means that each particle is used only once,
and n means that each particle is used n + 1 times.
In Geant4, the most direct way to keep all the possible correlations between particles is
by generating all these correlated particles in the same Geant4 event. This part is very
important for the user because it means that one Geant4 event represents one (and only
one) independent event and it doesn’t corresponds to one unique particle. Therefore, all
the particles stored in the PSF that share the same original history are thrown in the
same event. In addition, in case of recycling, each particle would be repeated the desired
amount of times, always in the same Geant4 event. The most important fact that comes
out of this paragraph is that the user must bear in mind that a certain number of events
in a Geant4 run corresponds to that number of statistically independent events, and not
to that number of single particles. In other words, in one Geant4 event all the particles
coming from the same original history are thrown, and they are repeated as many times
as the user has set previously.
• With the methods SetTotalParallelRuns(int) the PSF can be divided into fragments.
Therefore, the user can prepare parallel runs in different machines (CPU’s) by using
only a fixed fragment in each machine. The method SetParallelRun(int) is coded to
choose one of these fragments.
4
• This interface allows the user to make any translation or rotation of the phase-space
plane. Rotations can be performed around any axis of the global reference frame, in
arbitrary order. Moreover, the user can define the direction of the rotation axis for both
the gantry and the treatment head of the medical linac, and rotate the phase-space plane
around each of these axes.
• Methods needed to make spatial transformation to the position and momenta of the
particles. The transformation that is applied first is the translation of the phase-space
plane. This is followed by the rotations around the axis of the global coordinate system.
At the end, the rotations around the isocenter (if defined) are considered. Two different
rotations can be applied around the isocenter. If defined, the first one that it is applied
is the rotation around the treatment head axis. Later, the rotation around the gantry
rotation axis is considered.
5
– void SetRotationZ(G4double ): it sets the rotation angle around the Z axis of
the global frame.
– void SetIsocenterPosition(const G4ThreeVector& ): it sets the position of
the isocenter related to the global coordinate system. This method is mandatory
when the user applies isocentric rotations to the phase-space file by calling the
methods defined next.
– void SetCollimatorRotationAxis(const G4ThreeVector& ): in a medical linac,
this sets the direction of the rotation axis of the treatment head.
– void SetGantryRotationAxis(const G4ThreeVector& ): in a medical linac, this
sets the direction of the rotation axis of the gantry.
– void SetCollimatorAngle(G4double ): it sets the rotation angle for the treat-
ment head.
– void SetGantryAngle(G4double ): it sets the rotation angle for the gantry.
• Methods that return the particle-related variables stored in a event. All of them return
a vector that stores the variables asked. One given particle is referred by the same
component in all the vectors, provided they’re called during the same event.
6
– std::vector<G4int>* GetParticleTypeVector(): it returns a vector containing
the integers that identify the particles passing through the phase-space plane in the
given event.
– std::vector<G4double>* GetEnergyVector(): it returns a vector containing the
kinetic energy of each particle passing through the phase-space plane in the given
event.
– std::vector<G4ThreeVector>* GetPositionVector(): it returns a vector which
contains the position of the particle when crossing the phase-space plane.
– std::vector<G4ThreeVector>* GetMomentumVector(): it returns a vector con-
taining the direction cosines of the linear momentum for each particle when it’s
crossing the phase-space plane.
– std::vector<G4double>* GetWeightVector(): it returns a vector which stores
the statistical weight for each particle passing through the phase-space plane.
– std::vector< std::vector<G4double> >* GetExtraFloatsVector(): it is used
to obtain the vector which stores the float-type extra variables for each particle.
– std::vector< std::vector<G4long> >* GetExtraIntsVector(): it is used to
obtain the vector which stores the integer-type extra variables for each particle.
• Methods which return information about number of fragments which the PSF is divided
into and about recycling:
• Methods coded to return all the information on the spatial transformations that will
be made to the position and momentum of each particle stored in the phase-space file
before generating them in the Geant4 simulation.
7
– G4ThreeVector GetGantryRotationAxis(): it returns the direction of the rota-
tion axis of the gantry for the machine used to generate this PSF.
– G4double GetCollimatorAngle(): it returns the rotation angle around the rota-
tion axis of the collimator for the machine used to generate this PSF.
– G4double GetGantryAngle(): it returns the rotation angle around the rotation
axis of the gantry for the machine used to generate this PSF.
• Methods that must be called in the User Actions (UAs) of the Geant4 application. These
methods take over the process of writing a IAEA phsp file. In section 3 can be found
how to use them properly:
8
– void UserSteppingAction(const G4Run* ): this method checks whether the par-
ticle has crossed any of the scoring planes.
– void UpdateHeaders(): it updates all the IAEAheader files created during the
run. The user is suggested to invoked it in the “Event Action” class of the Geant4
application.
2. The .cpp files must be renamed as .cc files. Otherwise they would not be properly
compiled by GNUmakefile.
3. After placing and renaming the files as indicated, the Geant4 application must be com-
piled. No errors should appear.
4. The next step is to prepare the application to read and/or write IAEA phsp files. This
includes changes in the Geant4 application source code, which are explained in the
following paragraphs. These changes may require the user to create new User Actions
in the Geant4 application.
5. Once the suitable changes has been made, a compilation is mandatory. If these changes
are done properly, there should not be compilation errors.
1. Add a pointer to G4IAEAphspReader object in the header file as a data member of the
Primary Generator class.
9
2. In the Primary Generator class constructor: the pointer to G4IAEAphspReader object
must be created, passing the name of the phase-space file as argument (without including
the IAEA extension). It is very important to remark that, up to date, the reader class
can deal with only one IAEA phsp file. In next versions, it is possible that the reader
class may be able to manage more than one IAEA phsp file as a source.
These minimum requirements are shown in the following example, where the Primary
Generator class is called PrimaryGeneratorAction.
Example 1
Header file:
#ifndef PrimaryGeneratorAction_h
#define PrimaryGeneratorAction_h 1
#include "G4VUserPrimaryGeneratorAction.hh"
class G4Event;
class G4IAEAphspReader;
void GeneratePrimaries(G4Event*);
private:
// Phase space reader
G4IAEAphspReader* theIAEAReader;
};
#endif
Source file:
#include "PrimaryGeneratorAction.hh"
#include "G4Event.hh"
#include "G4IAEAphspReader.hh"
PrimaryGeneratorAction::PrimaryGeneratorAction()
{
G4String fileName = "PSF";
theIAEAReader = new G4IAEAphspReader(fileName);
}
PrimaryGeneratorAction::~PrimaryGeneratorAction()
10
{
if (theIAEAReader) delete theIAEAReader;
}
In case that some of the utility methods (for partitioning or spatial transformation) are
needed by the user, they can be invoked in the same way as the modifier methods of the
Primary Generator classes commonly used (G4ParticleGun or G4GeneralParticleSource). In
section 4 some examples on these methods are provided.
1. This interface needs the Geant4 application to have these optional User Actions defined:
Let RunAction be the name of the “Run Action” class. The following lines should appear
in the source file:
#include "RunAction.hh"
#include "G4Run.hh"
#include "globals.hh"
#include "G4IAEAphspWriter.hh"
RunAction::RunAction()
{}
RunAction::~RunAction()
11
{}
//-----
void RunAction::BeginOfRunAction(const G4Run* aRun)
{
G4IAEAphspWriter* IAEAWriter = G4IAEAphspWriter::GetInstance();
IAEAWriter->SetZStop(10.*cm);
// Add more ZStops if needed:
// IAEAWriter->SetZStop(50.*cm);
// IAEAWriter->SetZStop(90.*cm);
IAEAWriter->BeginOfRunAction(aRun);
}
//-----
void RunAction::EndOfRunAction(const G4Run* aRun)
{
G4IAEAphspWriter::GetInstance()->EndOfRunAction(aRun);
}
3. In the “Event Action” class: the user only needs to include one line in the BeginOfEven-
tAction() method. In this line a pointer to the singleton must be obtained, and the
BeginOfEventAction() method of this singleton must be invoked.
The source file of a “Event Action” with this minimum requirement should be like the
following example:
#include "EventAction.hh"
#include "G4Event.hh"
#include "G4IAEAphspWriter.hh"
EventAction::EventAction()
{}
EventAction::~EventAction()
{}
//-----
void EventAction::BeginOfEventAction(const G4Event* aEvent)
{
G4IAEAphspWriter::GetInstance()->BeginOfEventAction(aEvent);
}
4. In the “Stepping Action” class: only one line is needed here. The user must use the
singleton method called UserSteppingAction(), as indicated in the following example:
#include "SteppingAction.hh"
#include "G4Step.hh"
#include "G4IAEAphspWriter.hh"
12
SteppingAction::SteppingAction()
{}
SteppingAction::~SteppingAction()
{}
//-----
void SteppingAction::UserSteppingAction(const G4Step* aStep)
{
G4IAEAphspWriter::GetInstance()->UserSteppingAction(aStep);
}
4 Examples
Example 2: source file of a Primary Generator class which generates the particles from a
IAEA phsp file called PSF_afterJaws.IAEAphsp. Its corresponding gantry has been rotated
45 degrees, and the treatment head has been rotated 90 degrees. The isocenter is placed at
risoc = (0, 0, 1) m. In addition, this PSF will be divided into 8 fragments in order to create 8
parallel jobs, and the particles are taken from the 3rd fragment in this particular job. Each
particle will be recycled 24 times, so they will be used 25 times.
Source file:
#include "PrimaryGeneratorAction.hh"
#include "G4Event.hh"
#include "G4IAEAphspReader.hh"
PrimaryGeneratorAction::PrimaryGeneratorAction()
{
G4String fileName = "PSF_afterJaws";
theIAEAReader = new G4IAEAphspReader(fileName);
13
}
PrimaryGeneratorAction::~PrimaryGeneratorAction()
{
if (theIAEAReader) delete theIAEAReader;
}
Example 3: source file of a Primary Generator class which generates the particles from
a IAEA phsp file called PSF_beforeJaws.IAEAphsp. They were generated considering the
isocenter at (0, 0, 1) m, but the simulation considers the isocenter at the origin of the coordinate
reference system. Two rotations must be done: first around the Z axis, 90 degrees, and later
around Y axis, 45 degrees. In other words, the same rotations as in the previous example.
Source file:
#include "PrimaryGeneratorAction.hh"
#include "G4Event.hh"
#include "G4IAEAphspReader.hh"
PrimaryGeneratorAction::PrimaryGeneratorAction()
{
G4String fileName = "PSF_beforeJaws";
theIAEAReader = new G4IAEAphspReader(fileName);
// rotations
theIAEAReader->SetRotationOrder(321);
theIAEAReader->SetRotationZ(90.*deg);
theIAEAReader->SetRotationY(45.*deg);
}
PrimaryGeneratorAction::~PrimaryGeneratorAction()
{
if (theIAEAReader) delete theIAEAReader;
}
14
References
[1] S. Agostinelli, J. Allison, K. Amako, J. Apostolakis, et al, “Geant4 – a simulation toolkit
”, Nucl. Instr.& Meth. Phys. Res. A506 (2003) 250-303.
[3] I. Kawrakow, “Accurate condensed history Monte Carlo simulation of electron transport.
I. EGSnrc, the new EGS4 version” , Med. Phys. 27 (2000) 485–498.
[4] I. Kawrakow and D. W. O. Rogers, “The EGSnrc code system: Monte Carlo simulation
of electron and photon transport” , Tech. Rep. PIRS-701, National Research Council of
Canada, Ottawa, Canada, 2000.
15