Tracks and steps

Access Track Info

Q: How can I access the track information through the step object and what information am I allowed to access ?

A: A G4Step object consists of two points:

const G4StepPoint* preStepPoint = step->GetPreStepPoint();
const G4StepPoint* postStepPoint = step->GetPostStepPoint();

Note that we are encouraging the use of const. This is good practice. It introduces safety - you cannot accidentally change a const.

To get their positions in the global coordinate system:

const G4ThreeVector& preStepPosition = preStepPoint->GetPosition();
const G4ThreeVector& postStepPosition = postStepPoint->GetPosition();

Also here, note, we encourage const <type>&, i.e., a const reference, which avoids copying the quantity if possible, which saves CPU time. (We simply do const for built-in types such as G4int, which is really an int, because copying is no slower than creating a reference, but for composite types, such as G4ThreeVector, there is an advantage.)

Hereafter we call current volume the volume where the step has just gone through. Geometrical informations are available from preStepPoint. G4VTouchable and its derivates keep these geometrical informations. We retrieve a touchable by creating a handle for it:

const G4TouchableHandle& preStepTouch = preStepPoint->GetTouchableHandle();

To get the current volume:

const G4VPhysicalVolume* volume = preStepTouch->GetVolume();

To get its name:

const G4String& name = volume->GetName();

To get the physical volume copy number:

const G4int copyNumber = preStepTouch->GetCopyNumber();

To get logical volume:

const G4LogicalVolume* lVolume = volume->GetLogicalVolume();

To get the associated material: the following statements are equivalent:

const G4Material* material = preStepPoint ->GetMaterial();
const G4Material* material = lVolume ->GetMaterial();

To get the geometrical region:

const G4Region* region = lVolume->GetRegion();

To get its mother volume:

const G4VPhysicalVolume* mother = preStepTouch->GetVolume(depth=1);

grandMother: depth=2 …etc…

To get the copy number of the mother volume:

G4int depth;
const G4int copyNumber = preStepTouch->GetCopyNumber(depth=1);

grandMother: depth=2 …etc…

To get the process which has limited the current step:

const G4VProcess* aProcess = postStepPoint->GetProcessDefinedStep();

To check that the particle has just entered in the current volume (i.e. it is at the first step in the volume; the preStepPoint is at the boundary):

if (preStepPoint->GetStepStatus() == fGeomBoundary)

To check that the particle is leaving the current volume (i.e. it is at the last step in the volume; the postStepPoint is at the boundary):

if (postStepPoint->GetStepStatus() == fGeomBoundary)

In the above situation, to get touchable of the next volume: G4TouchableHandle touch2 = postStepPoint->GetTouchableHandle(); From touch2, all informations on the next volume can be retrieved as above.

Physics quantities are available from the step (G4Step) or from the track (G4Track).

To get the energy deposition, step length, displacement and time of flight spent by the current step:

const G4double eDeposit = step->GetTotalEnergyDeposit();
const G4double sLength = step->GetStepLength();
const G4ThreeVector& displace = step->GetDeltaPosition();
const G4double tof = step->GetDeltaTime();

To get momentum, kinetic energy and global time (time since the beginning of the event) of the track after the completion of the current step:

const G4Track* track = step->GetTrack();
const G4ThreeVector& momentum = track->GetMomentum();
const G4double kinEnergy = track->GetKineticEnergy();
const G4double globalTime = track->GetGlobalTime();
...etc...

Note: To transform a position from the global coordinate system to the local system of the current volume, use the preStepPoint transformation, as described in the Conversion Global to Local Co-ordinates above.

Q: How can I get and store (or plot) informations at tracking time from a given volume ?

A: To get the information at tracking time in a given volume A, one can adopt either one or a combination of the following strategies:

  1. If the geometry is simple enough, and wish to score some commonly used physics quantities (e.g. energy deposition, dose, flux, etc.), just activate G4ScoringManager in your main program, and use the scorer-based UI commands to transform volume A into a scorer.

    See Option 6 below, and the example examples/extended/runAndEvent/RE03.

  2. Through the SteppingAction, check that the particle is inside volume A and do whatever needed. Hints can be found in Access Track Info of this FAQ document.

    Usually, the hits containers and histograms are attributes of a Track, Event or Run and can be managed through either a TrackingAction, EventAction and/or RunAction and eventually messaging their pointer to the SteppingAction.

    A similar approach is illustrated in examples/basic/B2, B4, extended/electromagnetic, optical, and many others…

  3. In DetectorConstruction, by declaring volume A as a SensitiveDetector. At stepping time, the Geant4 kernel will automatically check that a particle is inside volume A and will handle the control to a specific function G4VSensitiveDetector::ProcessHits(). It is just necessary to instanciate a class inherited from G4VSensitiveDetector, say VolumeA_SD, and do whatever needed by implementing the function VolumeA_SD::ProcessHits(), as described in Option 2 above.

  4. In addition to Option 3 above, should create a HitsCollection to store the information. A HitsCollection can be created in VolumeA_SD::Initialize(). A Hit can be created or filled in VolumeA_SD::ProcessHits(). Additional operations on HitsCollection can be performed in VolumeA_SD::EndOfEvent().

    This approach is illustrated in examples/basic/B2, B4 and extended/analysis, extended/runAndEvent/RE01, etc…

  5. In DetectorConstruction, volume A can be declared as SensitiveDetector, and one or several pre-defined scorers can be attached to volume A. In this case, neither a SteppingAction nor a spcific VolumeA_SD sensitive detector is needed any longer. It is just necessary to create a dedicated scorer, e.g. MyRunScorer, inherited from G4Run, and handle the HitsCollections within MyRunScorer::RecordEvent(). MyRunScorer itself can be instanciated from RunAction::GenerateRun().

    This approach is illustrated in examples/novice/N07, extended/runAndEvent/RE02.

  6. A set of build-in scorer-based UI commands allows to perform most possible operations described through the previous Option 5 directly from run-time macros.

    See example extended/runAndEvent/RE03.