20.3. Particle Source Commands
Table of Contents
Changing particles
Geant4 macro commands let you control the primary particle type without recompiling your application. The exact commands depend on whether you use a G4ParticleGun or a G4GeneralParticleSource (GPS). In both cases you usually issue these commands before /run/beamOn.
For a particle gun, the key command is /gun/particle. For GPS, the equivalent is /gps/particle. You can change the particle type by specifying its Geant4 name, for example e-, e+, gamma, proton, neutron, alpha, or ions like ion with additional parameters.
A typical sequence with a particle gun looks like:
- Set the particle type, for example electrons:
/gun/particle e- - Optionally check available particles:
/particle/list
Similarly, with GPS you use:
/gps/particle gamma
Geant4 uses internal particle definitions, so the name must match one of the known particle names. If you try an unknown name, Geant4 prints an error and keeps the previous particle setting.
For ions, GPS and the ion gun use a special syntax. With /gun/particle ion or /gps/particle ion, you then define the ion species with:
/gun/ion Z A Q E or /gps/ion Z A Q E
where $Z$ is atomic number, $A$ is mass number, $Q$ is charge state, and $E$ is excitation energy. For many beginner applications you will only set $Z$ and $A$ and leave $Q$ and $E$ at default values provided by Geant4.
You can switch particle types between runs in the same session by issuing new particle commands before each /run/beamOn. This makes it easy to study different projectiles using the same geometry and physics list.
Important rule: Always set the particle type with /gun/particle or /gps/particle before starting a run with /run/beamOn. If you forget, Geant4 will keep the previous particle type and your results may not correspond to the particle you intended to simulate.
Changing energy
You can also control the primary particle energy entirely from macros. This allows quick parameter scans without changing C++ code. Again, the main commands differ between a particle gun and GPS.
For a particle gun, use:
/gun/energy E
where E is a value with a valid Geant4 unit, for example:
/gun/energy 1 MeV
/gun/energy 500 keV
/gun/energy 10 GeV
For GPS, use:
/gps/energy E
for monoenergetic beams:
/gps/energy 511 keV
/gps/energy 2 MeV
In both cases, you must always provide the unit. Geant4 does not assume a default unit in macro commands. The energy you set applies to all primary particles in that run, unless you define a more complex distribution with other GPS commands.
You can update the energy between runs by issuing a new command before each /run/beamOn. For example, to scan energies from 100 keV to 1 MeV, you could write a macro with repeated blocks:
- Set energy, for example:
/gun/energy 100 keV
/run/beamOn 10000 - Change energy:
/gun/energy 500 keV
/run/beamOn 10000 - Change again:
/gun/energy 1 MeV
/run/beamOn 10000
With GPS, you can also define energy distributions like Gaussian or user spectra, but for a beginner the monoenergetic command /gps/energy is usually sufficient.
Important rule: Always include an explicit unit when setting energy in macros, for example 1 MeV or 500 keV. If you omit the unit the command will fail or be interpreted incorrectly, and your simulated energy may not match what you expect.
Views: 8
KAHIBARO