3.3.  System of units

3.3.1.  Basic units

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.

For instance:

                 millimeter = mm = 1;
                 meter = m = 1000*mm;
                 ...
                 m3 = m*m*m;
                 ...

In the file $CLHEP_BASE_DIR/include/CLHEP/Units/SystemOfUnits.h from the CLHEP installation, one can find all untis definitions.

One can also change the system of units to be used by the kernel.

3.3.2.  Input your data

3.3.2.1.  Avoid 'hard coded' data

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 indeipendence 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
  ...

3.3.2.2.  Interactive commands

Some built-in commands from the User Interface (UI) also require units to be specified.

For instance:

    /gun/energy 15.2 keV
    /gun/position 3 2 -7 meter

If units are not specified, or are not valid, the command is refused.

3.3.3.  Output your data

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";

Of course, 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.

3.3.4.  Introduce new units

If wished to introduce new units, there are two methods:

  • You can complete the file SystemOfUnits.h

           
          #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

           
          G4UnitDefinition ( name, symbol, category, value )
        

    For example: define a few units for speed

           
          G4UnitDefinition ( "km/hour" , "km/h", "Speed", km/(3600*s) );
          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 G4UnitDefinition is located in source/global/management.

3.3.5.  Print the list of units

You can print the list of units with the static function: G4UnitDefinition::PrintUnitsTable();

or with the interactive command: /units/list