Geant4 offers the user the possibility to choose and use the
preferred units for any quantity. In fact, Geant4 takes
care of the units. Internally a consistent set on units
based on the
HepSystemOfUnits is used:
millimeter (mm) nanosecond (ns) Mega electron Volt (MeV) positron charge (eplus) degree Kelvin (kelvin) the amount of substance (mole) luminous intensity (candela) radian (radian) steradian (steradian)
All other units are defined from the basic ones.
millimeter = mm = 1; meter = m = 1000*mm; ... m3 = m*m*m; ...
In the file
from the CLHEP installation, one can find all untis definitions.
One can also change the system of units to be used by the kernel.
The user must give the units for the data to introduce:
G4double Size = 15*km, KineticEnergy = 90.3*GeV, density = 11*mg/cm3;
Geant4 assumes that these specifications for the units are respected, in order to assure independence from the units chosen in the client application.
If units are not specified in the client application, data are implicitly treated in internal Geant4 system units; this practice is however strongly discouraged.
If the data set comes from an array or from an external file, it is strongly recommended to set the units as soon as the data are read, before any treatment. For instance:
for (int j=0, j<jmax, j++) CrossSection[j] *= millibarn; ... my calculations ...
You can output your data with the wished units. To do so, it is sufficient to divide the data by the corresponding unit:
G4cout << KineticEnergy/keV << " keV"; G4cout << density/(g/cm3) << " g/cm3";
G4cout << KineticEnergy will print the
energy in the internal units system.
There is another way to output the data. Let Geant4 choose the most appropriate units for the actual numerical value of the data. It is sufficient to specify to which category the data belong to (Length, Time, Energy, etc.). For example
G4cout << G4BestUnit(StepSize, "Length");
StepSize will be printed in km, m, mm, fermi, etc.
depending of its actual value.
If wished to introduce new units, there are two methods:
You can extend the file
#include "SystemOfUnits.h" static const G4double inch = 2.54*cm;
Using this method, it is not easy to define composed units. It is better to do the following:
Instantiate an object of the class
G4UnitDefinition. These objects are owned by the global
G4UnitsTableat construction, and must not be deleted by the user.
new G4UnitDefinition ( name, symbol, category, value )
For example: define a few units for speed
new G4UnitDefinition ( "km/hour" , "km/h", "Speed", km/(3600*s) ); new G4UnitDefinition ( "meter/ns", "m/ns", "Speed", m/ns );
The category "Speed" does not exist by default in
G4UnitsTable, but it will be created automatically. The class
G4UnitDefinitionis defined in
You can print the list of units with the static function:
or with the interactive command: