5.4. Printing Values with Units
Table of Contents
Unit conversion
Geant4 stores all physical quantities internally in its own base units. For example, lengths are stored in millimeters, energies in MeV, and times in nanoseconds. When you print or store values, you must usually convert them to the units that are most convenient for you.
In C++ code, you convert by dividing by the unit constant. For a length stored in a variable G4double length, you can write length / mm to get the value expressed in millimeters, or length / cm to get centimeters. The same pattern works for all Geant4 units, such as m, keV, MeV, GeV, ns, and s.
Internally, a quantity $Q$ is stored as a pure number in base units. To obtain a numeric value in a specific unit $U$, you divide by $U$:
$$
Q_{\text{in unit }U} = \frac{Q_{\text{internal}}}{U}.
$$
For example, if you record an energy deposition edep in MeV units, and you want the value in keV for output, you should compute edep / keV. If edep is 0.511*MeV, then edep / MeV is 0.511, while edep / keV is 511.
Always divide by the unit constant to convert an internal Geant4 value into that unit. Never multiply by the unit constant when printing or storing values.
The same rule applies when you use units in arithmetic. If you write 10.*cm, the literal 10. is multiplied by the value of cm in base units, so the result is stored internally in millimeters. Later, to print this length in meters, you should divide by m and print length / m.
Using unit constants consistently helps you avoid mixing incompatible units. For example, if you accidentally compare a length divided by cm with a threshold expressed in mm, the numeric values will not match. Always pair each value with the same unit when you compare, store, or pass it to functions that expect a particular unit.
A common pattern is to use Geant4 units for all parameters and simply convert at the boundary between simulation and analysis. For example, you might store energy in MeV inside Geant4 and convert to keV or MeV only when filling histograms or writing text files.
Human-readable output
Geant4 provides helper functions in G4SystemOfUnits.hh and G4UnitsTable.hh to produce readable text output with units. This is useful when you want to print values to the screen or log files in a way that is easy to interpret.
The simplest approach is to use unit constants directly in stream output. For instance, if length is a G4double containing a length in internal units, you can write:
G4cout << "Length = " << length / cm << " cm" << G4endl;to obtain a clear message. The same idea applies to energy, time, angle, and other quantities.
To avoid hard coding unit names repeatedly, Geant4 also offers formatting utilities that choose appropriate scales and labels. The function G4BestUnit is often used in user code. It takes a value and a category, and returns a formatted string with a suitable unit. For example:
#include "G4UnitsTable.hh"
G4double edep = ...; // energy deposition
G4cout << "Edep = " << G4BestUnit(edep, "Energy") << G4endl;
prints the energy with an automatically selected unit and label, such as 1.23 MeV or 450 keV, depending on the size of the value.
Typical categories accepted by G4BestUnit include "Length", "Energy", "Time", "Angle", and others that match the quantity you are printing. This helps you keep output consistent and reduces the chance of mismatched units.
For tabular or file output, you may prefer to always use a fixed unit for each column. In that case, decide at the beginning which units you want, convert using division by the unit constants, and note the units in the file header. For example, for an output line containing position and energy, you might write:
// x, y, z in mm, energy in keV
G4cout << x / mm << " "
<< y / mm << " "
<< z / mm << " "
<< edep / keV << G4endl;
When printing time-related quantities, the same practices apply. If you have a global time t in internal units, you can print it in nanoseconds with t / ns << " ns" or let G4BestUnit(t, "Time") select ns, microseconds, or seconds automatically.
Using unit tags consistently in your messages is important for debugging and interpretation. Whenever you print a number, always include its unit explicitly, either as plain text such as " mm" or by using a helper that provides the unit name. This prevents confusion when you compare logs from different runs or share output with others.
Always include explicit units in your printed or stored values. Never print a bare number without its unit, since this makes later analysis and comparison error prone.
By combining Geant4 unit constants, division for conversion, and formatted helpers like G4BestUnit, you can keep both your internal calculations and your external output clear, consistent, and easy to read.
Views: 8
KAHIBARO