3.3.  System of units

3.3.1.  Basic units

Geant4 offers the user the possibility to choose and use the units he prefers for any quantity. In fact, the Geant4 kernel takes care of the units. Internally it uses a consistent set on units based on the HepSystemOfUnits:

                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 source/global/management/include/SystemOfUnits.h you will find all of these definitions. That file is part of CLHEP.

Moreover, the user is free to 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

You must give the units for the data you are going to introduce:

 G4double Size = 15*km, KineticEnergy = 90.3*GeV, density = 11*mg/cm3;

Indeed, the full Geant4 code is written respecting these specifications, and this makes it independent of the units chosen by the user.

If the units are not specified, it is understood that the data is implicitly in the internal G4 system, but this is 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 the units to be specified.

For instance:

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

If the 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 units you wish. 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 your data. Let Geant4 choose the most appropriate units for the actual numerical value of your data. It is sufficient to specify to which category your data belong (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 you wish 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:

  • You can 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