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:
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
.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/orRunAction
and eventually messaging their pointer to theSteppingAction
.A similar approach is illustrated in
examples/basic/B2
,B4
,extended/electromagnetic
,optical
, and many others…In
DetectorConstruction
, by declaring volume A as aSensitiveDetector
. At stepping time, the Geant4 kernel will automatically check that a particle is inside volume A and will handle the control to a specific functionG4VSensitiveDetector::ProcessHits()
. It is just necessary to instanciate a class inherited fromG4VSensitiveDetector
, sayVolumeA_SD
, and do whatever needed by implementing the functionVolumeA_SD::ProcessHits()
, as described in Option 2 above.In addition to Option 3 above, should create a
HitsCollection
to store the information. AHitsCollection
can be created inVolumeA_SD::Initialize()
. AHit
can be created or filled inVolumeA_SD::ProcessHits()
. Additional operations onHitsCollection
can be performed inVolumeA_SD::EndOfEvent()
.This approach is illustrated in
examples/basic/B2
,B4
andextended/analysis
,extended/runAndEvent/RE01
, etc…In
DetectorConstruction
, volume A can be declared asSensitiveDetector
, and one or several pre-defined scorers can be attached to volume A. In this case, neither aSteppingAction
nor a spcificVolumeA_SD
sensitive detector is needed any longer. It is just necessary to create a dedicated scorer, e.g.MyRunScorer
, inherited fromG4Run
, and handle theHitsCollections
withinMyRunScorer::RecordEvent()
.MyRunScorer
itself can be instanciated fromRunAction::GenerateRun()
.This approach is illustrated in
examples/novice/N07
,extended/runAndEvent/RE02
.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
.