5.2. Using Units in C++
Table of Contents
`mm`
Geant4 uses a coherent internal unit system. Instead of writing raw numbers that you must mentally interpret, you multiply by unit constants such as mm to state the physical unit explicitly.
In C++ code, these unit constants are simple constexpr numbers defined in Geant4 headers. For length, the base unit is millimeter. The symbol mm corresponds to 1 internal length unit, and other length units are defined relative to it.
You normally get the unit definitions by including:
#include "G4SystemOfUnits.hh"After this, you can write:
G4double thickness = 5.0 * mm;
G4double gap = 0.1 * mm;Geant4 then stores these values in its internal unit system. You can still do arithmetic as usual:
G4double total = thickness + 2.0 * gap; // result in internal units (millimeters)
Always multiply numeric values by a Geant4 unit symbol (like mm, cm, MeV) when you assign physical quantities. Never assume that a plain number such as 5.0 is interpreted as a particular unit.
When you print or log values, you can divide by mm to convert back:
G4cout << "Thickness = " << thickness / mm << " mm" << G4endl;`cm`
Centimeters are also available as a unit constant, defined in terms of millimeters:
// Conceptually (inside Geant4):
const G4double cm = 10.0 * mm;In your code, you can write:
G4double length = 10.0 * cm;
G4double radius = 2.5 * cm;
Both cm and mm describe the same physical dimension of length, only with different scales. Geant4 converts everything to its internal base automatically.
You can freely mix units in expressions:
G4double size = 2.0 * cm + 5.0 * mm; // OK, result in internal unitsFor readable output, keep consistent units when you print:
G4cout << "Length = " << length / cm << " cm" << G4endl;
Use the same unit consistently when printing or interpreting values. If you store a length using cm, divide by cm when printing, not by mm, to avoid confusion.
`m`
Meters are defined from centimeters and millimeters:
// Conceptually:
const G4double m = 100.0 * cm; // or 1000.0 * mm
You can use m for large dimensions, such as room sizes or shielding thicknesses:
G4double roomSize = 3.0 * m;
G4double distance = 1.2 * m;
Geant4 treats these like any other lengths, internally expressed in millimeters. Use m for clarity when your geometry or source positions are naturally on a meter scale.
For converting back to meters in output:
G4cout << "Distance = " << distance / m << " m" << G4endl;
Do not hard-code meter-to-millimeter conversions such as distance = 1200.0;. Always write the physical intent explicitly, for example distance = 1.2 * m;.
`keV`
For energy, Geant4 defines a base unit and commonly used multiples. The symbol keV represents kilo electron volt. It is defined conceptually as:
// Conceptually:
const G4double keV = 1.e-3 * MeV;
where MeV is the commonly used base for nuclear and particle physics in Geant4.
You can use keV when defining low energies, such as X rays or some electron beams:
G4double xrayEnergy = 50.0 * keV;
G4double threshold = 10.0 * keV;The unit behaves like an ordinary number in arithmetic:
G4double sum = xrayEnergy + threshold; // result in internal energy unitsFor human-readable output:
G4cout << "X-ray energy = " << xrayEnergy / keV << " keV" << G4endl;
Never mix numeric values that you think are in keV with numeric values in MeV without using the correct unit constants. Always attach keV or MeV to make the unit explicit.
`MeV`
Most Geant4 examples and documentation use MeV as the reference energy unit. The symbol MeV stands for mega electron volt and is used extremely often:
G4double beamEnergy = 1.0 * MeV;
G4double cutEnergy = 0.5 * MeV;In many physics settings, you will see expressions like:
particleGun->SetParticleEnergy(511.0 * keV);
particleGun->SetParticleEnergy(10.0 * MeV);Here are typical conversions using the Geant4 units:
| Physical value | In Geant4 code |
|---|---|
| 511 keV | 511.0 * keV |
| 1 MeV | 1.0 * MeV |
| 10 MeV | 10.0 * MeV |
| 0.25 MeV | 0.25 MeV or 250.0 keV |
When printing energies in MeV:
G4cout << "Beam energy = " << beamEnergy / MeV << " MeV" << G4endl;
Treat MeV as a unit symbol, not as a type. You must always multiply a G4double by MeV when assigning an energy in mega electron volts, and divide by MeV when converting for output.
`GeV`
For higher energies, such as accelerator beams or high energy cosmic rays, you can use GeV, giga electron volt. Conceptually:
// Conceptually:
const G4double GeV = 1000.0 * MeV;Example uses:
G4double colliderEnergy = 7.0 * GeV;
G4double cutoff = 0.1 * GeV; // 100 MeV
GeV integrates seamlessly with other units:
G4double totalEnergy = 500.0 * MeV + 0.5 * GeV; // OKWhen you output in GeV:
G4cout << "Collider energy = " << colliderEnergy / GeV << " GeV" << G4endl;You can also convert explicitly between units by dividing by one and multiplying by another:
G4double energyInMeV = colliderEnergy / MeV; // numeric value in MeV
G4double energyInGeV = colliderEnergy / GeV; // numeric value in GeV
Choose one main energy unit (often MeV) for your analysis and be consistent. Use keV, MeV, and GeV properly in code and when interpreting printed values, to avoid unit mismatches and mistakes.
Views: 8
KAHIBARO