Warning, /geant4/examples/advanced/IAEAphsp/README.md is written in an unsupported language. File is not indexed.
0001 \page ExampleIAEAphsp Example IAEAphsp
0002
0003 # IAEAphsp — Geant4 Advanced Example
0004
0005 Author: M.A. Cortes-Giraldo et al.
0006 Date: 14 Oct. 2025
0007 Email: miancortes@us.es
0008
0009 IAEAphsp is an advanced Geant4 example that demonstrates **reading** and
0010 **writing** IAEA phase-space (IAEAphsp) files (a binary `*.IAEAphsp` with
0011 human-readable `*.IAEAheader`) within a minimal, configurable application.
0012 The IAEAphsp format is defined by the IAEA Nuclear Data Section; this example
0013 shows how to use IAEAphsp files as input source (i.e. as a primary generator)
0014 and how to produce IAEAphsp outputs at given scoring planes.
0015
0016 **REFERENCE PAPER:** If you use this code, please cite:
0017
0018 M.A. Cortes-Giraldo et al., Int J Radiat Biol 88(1-2): 200-208 (2012)
0019 [(doi: 10.3109/09553002.2011.627977)](https://doi.org/10.3109/09553002.2011.627977)
0020
0021 More information on the IAEAphsp format can be found at
0022 [https://www-nds.iaea.org/phsp/phsp.htmlx](https://www-nds.iaea.org/phsp/phsp.htmlx)
0023
0024 ---
0025
0026 ## Contents
0027
0028 The most specific files of this example are:
0029
0030 - `phsp/` directory: Contains two IAEAphsp examples,
0031 1. The "test" IAEAphsp file available from the IAEAphsp project website,
0032 2. "PSF_example", an illustrative phsp file storing 200 particles (40 of
0033 each kind) recorded during 1000 original histories.
0034 - `iaea_phsp/` directory: Contains the files defining the IAEAphsp format
0035 and routines.
0036 - Three testing macro files:
0037 - `test-reader.mac`: Case in which we only read an IAEAphsp file, no
0038 IAEAphsp outputs.
0039 - `test-writer.mac`: Regular particle gun is used, particles at specific
0040 phsp planes (z const) are recorded in IAEAphsp output files.
0041 - `test-rw.mac`: Performs both reading and writing operations with
0042 IAEAphsp files.
0043 - In addition, `vis.mac` macro is used for an interactive session.
0044
0045 ---
0046
0047 ## Geometry
0048
0049 Just a **box world volume** is created. You can configure world half-sizes at
0050 runtime via the UI commands in `/my_geom/` directory:
0051 ```
0052 /my_geom/worldXY <L> <unit> # world XY half-size
0053 /my_geom/worldZ <L> <unit> # world Z half-size
0054 ```
0055 The world material is `G4_Galactic`. No detector objects are required for the
0056 IAEAphsp writer; the scoring planes are
0057 **mathematical planes at constant Z positions** managed by the writer stack.
0058
0059 ---
0060
0061 ## Physics
0062
0063 This example **requires** the definition of a **reference physics list**
0064 chosen at runtime via UI command **before initialization** (i.e., before
0065 issuing the command `/run/initialize`). The required command is:
0066 ```
0067 /my_phys/setList <name> # e.g. QGSP_BIC_HP_EMZ, QGSP_BERT_HP
0068 ```
0069 Further, related verbosity can be controlled with:
0070 ```
0071 /my_phys/verbose <0|1|2>
0072 ```
0073 If no list is set before `/run/initialize`, the application will issue a
0074 **fatal error**.
0075
0076 You can also configure production cuts in the macro, as usual, e.g.:
0077 ```
0078 /run/setCut 0.1 mm
0079 ```
0080
0081 ---
0082
0083 ## Particle beam (used if no phsp reader is selected)
0084
0085 By default, 50 MeV are shot from the center, with momentum direction parallel
0086 to the z-axis, pointing randomly towards positive or negative direction.
0087 This is to illustrate the recording of the incremental history number variable
0088 (also known as `n_stat`) within the output IAEAphsp files.
0089
0090 Besides regular commands at `/gun/` directory, the beam can be controlled with
0091 the following UI commands:
0092 ```
0093 /my_beam/kinE <E> <unit> # mean kinetic energy
0094 /my_beam/DE <DE> <unit> # energy distribution half-width, flat distribution
0095 /my_beam/X0 <X0> <unit> # mean x-position of the beam
0096 /my_beam/Y0 <Y0> <unit> # mean y-position of the beam
0097 /my_beam/Z0 <Z0> <unit> # mean z-position of the beam
0098 /my_beam/DX <DX> <unit> # x-pos distribution half width, flat distribution
0099 /my_beam/DY <DY> <unit> # y-pos distribution half width, flat distribution
0100 /my_beam/DZ <DZ> <unit> # z-pos distribution half width, flat distribution
0101 ```
0102
0103 Its verbosity can be controlled with:
0104 ```
0105 /my_beam/verbose <0|1|2>
0106 ```
0107
0108 ---
0109
0110 ## Specific commands for the IAEAphsp classes
0111
0112 ### Activation of G4IAEAphspReader/Writer objects
0113
0114 The activation of IAEAphsp classes, either for reading or writing purpose,
0115 is done via UI commands defined at a messenger class of the
0116 ActionInitialization class, under the directory `/action/`.
0117 This design comes from the need of passing the information safely to worker
0118 threads and ensure MT-safe operations with the IAEAphsp routines.
0119 ```
0120 /action/IAEAphspReader/fileName <name> # reads from <name>.IAEA* files
0121
0122 /action/IAEAphspWriter/namePrefix <name> # writes <name>[_runID].IAEA* files
0123 /action/IAEAphspWriter/zphsp <z_phsp> <unit> # defines phsp plane at z-pos
0124 ```
0125
0126 The **G4IAEAphspReader** class only reads particle **from ONE file**.
0127 In contrast, **more than one** zphsp values can be set to **G4IAEAphspWriter**.
0128
0129 ### IAEAphsp Reader — controls & transforms
0130
0131 The G4IAEAphspReader object can be controlled with the following UI commands.
0132 These commands are only available at `Idle` state.
0133 Please see documentation within the class for further information.
0134
0135 Commands relevant for particle recycling:
0136 ```
0137 /IAEAphspReader/recycling <n_rec> # Each particle is created n_rec+1 times
0138 /IAEAphspReader/axialSymmetryX <true|false>
0139 /IAEAphspReader/axialSymmetryY <true|false>
0140 /IAEAphspReader/axialSymmetryZ <true|false>
0141 ```
0142
0143 Commands relevant for simulations run in parallel reading the same IAEAphsp:
0144 ```
0145 /IAEAphspReader/numberOfParallelRuns <n_chunk> # No. chunks defined in file
0146 /IAEAphspReader/parallelRun <chunk> # Defines the piece of phsp file to read
0147 ```
0148
0149 Commands to mimic rotations of a linac treatment head:
0150 ```
0151 /IAEAphspReader/collimatorAngle <angle> <unit>
0152 /IAEAphspReader/collimatorRotationAxis <u_coll> <v_coll> <w_coll>
0153 /IAEAphspReader/gantryAngle <angle> <unit>
0154 /IAEAphspReader/gantryRotationAxis <u_gantry> <v_gantry> <w_gantry>
0155 /IAEAphspReader/isocenterPosition <x_ic> <y_ic> <z_ic> <unit>
0156 ```
0157
0158 Commands for custom spatial transformations of the phsp file:
0159 ```
0160 /IAEAphspReader/rotateX <angle> <unit>
0161 /IAEAphspReader/rotateY <angle> <unit>
0162 /IAEAphspReader/rotateZ <angle> <unit>
0163 /IAEAphspReader/rotationOrder <123|231|312|132|321|213>
0164 /IAEAphspReader/translate <x> <y> <z> <unit>
0165 ```
0166
0167 Verbose command:
0168 ```
0169 /IAEAphspReader/verbose <0|1|2>
0170 ```
0171
0172 ---
0173
0174 ## Build
0175
0176 ```bash
0177 # Configure your Geant4 path (or source your env script)
0178 export Geant4_DIR=/path/to/geant4/lib/cmake/Geant4
0179
0180 # Configure & build
0181 mkdir build && cd build
0182 cmake -DGeant4_DIR="$Geant4_DIR" ..
0183 cmake --build . --parallel
0184 ```
0185 This produces the executable **`IAEAphsp`** and copies the example macros and
0186 phsp files into the `build/` directory.
0187
0188 ---
0189
0190 ## Running
0191
0192 ### Interactive UI
0193 ```bash
0194 ./IAEAphsp
0195 ```
0196 Launches `an interactive session (Qt/terminal depending on your Geant4 build)
0197
0198 ### Batch (macro)
0199 Use the provided macros from the build directory:
0200 ```bash
0201 ./IAEAphsp test-reader.mac
0202 ./IAEAphsp test-writer.mac
0203 ./IAEAphsp test-rw.mac
0204 ./IAEAphsp vis.mac
0205 ```
0206 This example accepts an **optional** thread-count as a second argument (MT
0207 builds):
0208 ```bash
0209 ./IAEAphsp test-reader.mac 4
0210 ```
0211 IAEAphsp outputs are written in the working directory.
0212
0213 ---
0214
0215 ## How it works (diagram)
0216
0217 ```
0218 +-------------------+
0219 | Run Manager |
0220 | (MT if enabled) |
0221 +-------------------+
0222 |
0223 v
0224 +----------------------------------------+
0225 | User Initializations |
0226 | - DetectorConstruction (via /my_geom/) |
0227 | - PhysicsList (via /my_phys) |
0228 | - ActionInitialization |
0229 +----------------------------------------+
0230 |
0231 v
0232 +--------------------------------------------------------------------+
0233 | PrimaryGeneratorAction |
0234 | - uses G4IAEAphspReader if /action/IAEAphspReader/fileName is set |
0235 | - else particle gun (via /my_beam/*) |
0236 +--------------------------------------------------------------------+
0237 |
0238 v
0239 +-----------------------------------------------------------------------+
0240 | SteppingAction |
0241 | - sends eligible tracks to |
0242 | G4IAEAphspWriterStack if /action/IAEAphspWriter/ commands are set |
0243 +-----------------------------------------------------------------------+
0244 |
0245 v
0246 +--------------------------+
0247 | IAEAphspRun (per thread) |
0248 | - merges at EndOfRun |
0249 | - opens/writes via |
0250 | G4IAEAphspWriter |
0251 +--------------------------+
0252 ```
0253
0254 At end-of-run, the master consolidates thread-local stacks and the writer
0255 produces `*.IAEAphsp`/`*.IAEAheader` files (name prefix set by
0256 `/action/IAEAphspWriter/namePrefix`) - one pair per defined **Z plane**
0257 (each set by `/action/IAEAphspWriter/zphsp`).
0258
0259 ---
0260
0261 ### Note on IAEASourceIdRegistry class
0262
0263 This example uses a thread-safe registry (`include/IAEASourceIdRegistry.hh`)
0264 to coordinate the `source_ID` values passed to the IAEA routines. The goal is
0265 to keep IDs **unique and stable** across threads and runs, avoiding surprises
0266 from the C layer’s internal allocator.
0267
0268 - Readers reserve one ID per worker and reuse it on new runs; the ID is
0269 released when the reader is destroyed (i.e. at the end of the entire job).
0270 - Writers reserve one ID per output plane during `OpenIAEAphspOutFiles()` and
0271 release them in `CloseIAEAphspOutFiles()`.
0272
0273 This mechanism is entirely internal; **no user commands are required**.