% $Id: tracking.tex,v 1.10 2005/12/11 02:16:01 dennis Exp $
\chapter{Tracking}

The tracking category manages the contribution of the processes to the 
evolution of a track's state and provides information in sensitive volumes 
for hits and digitization.

\section{Design Philosophy}

It is well known that the overall performance of a detector 
simulation depends critically on the CPU time spent
propagating the particle through one step.  The most 
important consideration in the object design of the tracking 
category is maintaining high execution speed in the {\sc Geant4}
simulation while utilizing the power of the object-oriented
approach. 

An extreme approach to the particle tracking design would be
to integrate all functionalities required for the propagation
of a particle into a single class.  This design approach looks 
object-oriented because a particle in the real world 
propagates by itself while interacting with the material 
surrounding it.  However, in terms of data hiding, which is 
one of the most important ingredients in the object-oriented 
approach, the design can be improved.

Combining all the necessary functionalities into a single 
class exposes all the data attributes to a large number of 
methods in the class.  This is basically equivalent to using 
a common block in Fortran.

Instead of the 'big-class' approach, a hierarchical design
was employed by {\sc Geant4}.  The hierarchical approach, which 
includes inheritance and aggregation, enables large, complex 
software systems to be designed in a structured way.  The 
simulation of a particle passing through matter is
a complex task involving particles, detector geometry, physics 
interactions and hits in the detector.  It is well-suited to
the hierarchical approach.  The hierarchical design manages the 
complexity of the tracking category by separating the system 
into layers.  Each layer may then be designed independently of 
the others. 

In order to maintain high-performance tracking, use of the 
inheritance ('is-a' relation) hierarchy in the tracking category
was avoided as much as possible.  For example, {\it track} and 
{\it particle} classes might have been designed so that a 
{\it track} 'is a' {\it particle}.  In this scheme, however, 
whenever a {\it track} object is used, time is spent copying 
the data from the {\it particle} object into the {\it track} 
object.  Adopting the aggregation ('has-a' relation) hierarchy 
requires only pointers to be copied, thus providing a 
performance advantage.

\section{Class Design}

Fig. (not yet available) 
% \ref{figure:tracking-1} 
shows a general overview of the tracking 
design in Unified Modelling Language Notation.

% \begin{figure}
% \begin{center}
% \includegraphics[angle=0,scale=0.4]{OOAnalysisDesign/Tracking/classDgmTracking.ps}
% \vspace{10pt}
% \caption{Tracking}
% \label{figure:tracking-1}
% \end{center}
% \end{figure}

\begin{itemize}
\item {\em G4TrackingManager} is an interface between the event 
and track categories and the tracking category.  It handles the
message passing between the upper hierarchical object, which is 
the event manager ({\tt G4EventManager}), and lower hierarchical 
objects in the tracking category.  {\tt G4TrackingManager} is 
responsible for processing one track which it receives from the 
event manager.

{\tt G4TrackingManager} aggregates the pointers to 
{\tt G4SteppingManager}, {\tt G4Trajectory} and 
{\tt G4UserTrackingAction}.  It also has a 'use' 
relation to {\tt G4Track}. 

\item {\em G4SteppingManager} plays an essential role in 
particle tracking.  It performs message passing to objects 
in all categories related to particle transport, such as 
geometry and physics processes.  Its public method {\tt Stepping()} 
steers the stepping of the particle.  The algorithm employed in 
this method is basically the same as that in Geant3.  The 
{\sc Geant4} implementation, however, relies on the inheritance 
hierarchy of the physics interactions.  The hierarchical 
design of the physics interactions enables the stepping 
manager to handle them as abstract objects.  Hence, the manager 
is not concerned with concrete interaction objects such as 
bremsstrahlung or pair creation.  The actual invocations of 
various interactions during the stepping are done through a
dynamic binding mechanism.  This mechanism shields the 
tracking category from any change in the design of the physics 
process classes, including the addition or subtraction of new
processes.

{\tt G4SteppingManager} also aggregates 
\begin{itemize}
\item{the pointers to {\tt G4Navigator} from the geometry category, 
      to the current {\tt G4Track}, and} 

\item{the list of secondaries from the current track 
(through a {\tt G4TrackVector}) to {\tt G4UserSteppingAction} 
and to {\tt G4VSteppingVerbose}.}
\end{itemize}  

It also has a 'use' relation to {\tt G4ProcessManager} and 
{\tt G4ParticleChange} in the physics processes class category. 

\item
{\em G4Track} - the class {\tt G4Track} represents a particle 
which is pushed by {\tt G4SteppingManager}.  It holds information 
required for stepping a particle, for example, the current position, 
the time since the start of stepping, the identification of the 
geometrical volume which contains the particle, etc.  Dynamic 
information, such as particle momentum and energy, is held in the 
class through a pointer to the {\tt G4DynamicParticle} class.  Static 
information, such as the particle mass and charge is stored in the 
{\tt G4DynamicParticle} class through the pointer to the 
{\tt G4ParticleDefinition} class.  Here the aggregation hierarchical 
design is extensively employed to maintain high tracking performance.

\item
{\em G4TrajectoryPoint} and {\em G4Trajectory} - the class  
{\tt G4TrajectoryPoint} holds the state of the particle after 
propagating one step.  Among other things, it includes information 
on space-time, energy-momentum and geometrical volumes.

{\tt G4Trajectory} aggregates all {\tt G4TrajectoryPoint} objects 
which belong to the particle being propagated. 
{\tt G4TrackingManager} takes care of adding the 
{\tt G4TrajectoryPoint} to a {\tt G4Trajectory} object if the user 
requested it (see \cite{applGuide}). 
The life of a {\tt G4Trajectory} object spans an event, contrary to 
{\tt G4Track} objects, which are deleted from memory after being 
processed.

\item
{\b G4UserTrackingAction and G4UserSteppingAction} - 
{\tt G4UserTrackingAction} is a base class from which user actions 
at the beginning or end of tracking may be derived.  Similarly, 
{\tt G4UserSteppingAction} is a base class from which user actions 
at the beginning or end of each step may be derived. 

\end{itemize}

\section{Tracking Algorithm}
The key classes for tracking in {\sc Geant4} are 
{\tt G4TrackingManager} and {\tt G4SteppingManager}.  The 
singleton object "TrackingManager" from {\tt G4TrackingManager} 
keeps all information related to a particular track, and it 
also manages all actions necessary to complete the tracking.  The 
tracking proceeds by pushing a particle by a step, the length 
of which is defined by one of the active processes.
The "TrackingManager" object delegates management of each of the 
steps to the "SteppingManager" object.  This object keeps all 
information related to a particular step. 

The public method {\tt ProcessOneTrack()} in {\tt G4TrackingManager} 
is the key to managing the tracking, while the public method 
{\tt Stepping()} is the key to managing one step.  The algorithms 
used in these methods are explained below.\newline

\mbox{}
{\bf ProcessOneTrack() in G4TrackingManager}
\begin{enumerate}
\item Actions before tracking the particle:\newline 
         Clear secondary particle vector
\item Pre tracking user intervention process. 
\item Construct a trajectory if it is requested
\item Give SteppingManager the pointer to the track which will 
      be tracked 
\item Inform beginning of tracking to physics processes
\item Track the particle Step-by-Step while it is alive
\begin{itemize}
  \item Call Stepping method of G4SteppingManager
  \item Append a trajectory point to the trajectory object if it 
        is requested    
\end{itemize}
\item Post tracking user intervention process. 
\item Destroy the trajectory if it was created
\end{enumerate}

\mbox{}
{\bf Stepping() in G4SteppingManager }
\begin{enumerate}
\item Initialize current step
\item If particle is stopped, get the minimum life time from all 
      the at rest processes and invoke InvokeAtRestDoItProcs for 
      the selected AtRest processes 
\item If particle is not stopped:
\begin{itemize}
  \item Invoke DefinePhysicalStepLength, that finds the minimum 
        step length demanded by the active processes 
  \item Invoke InvokeAlongStepDoItProcs
  \item Update current track properties by taking into account all 
        changes by AlongStepDoIt
  \item Update the {\em safety}
  \item Invoke PostStepDoIt of the active discrete process.
  \item Update the track length
  \item Send G4Step information to Hit/Dig if the volume is sensitive
  \item Invoke the user intervention process.
  \item Return the value of the StepStatus.
\end{itemize}
\end{enumerate}

\section{Interaction with Physics Processes}

The interaction of the tracking category with the physics processes 
is done in two ways.  First each process can limit the step length 
through one of its three {\tt GetPhysicalInteractionLength()} methods, 
AtRest, AlongStep, or PostStep.  Second, for the selected processes 
the DoIt (AtRest, AlongStep or PostStep) methods are invoked. 
All this interaction is managed by the Stepping method of 
{\tt G4SteppingManager}.  To calculate the step length, the 
{\tt DefinePhysicalStepLength()} method is called.  The flow of this 
method is the following:

\begin{itemize}
\item Obtain maximum allowed Step in the volume define by the user 
      through G4UserLimits.
\item The PostStepGetPhysicalInteractionLength of all active processes 
      is called.  Each process returns a step length and the minimum
      one is chosen.  This method also returns a G4ForceCondition flag, 
      to indicate if the process is forced or not:
        = Forced    : Corresponding PostStepDoIt is forced.
        = NotForced : Corresponding PostStepDoIt is not forced 
                      unless this process limits the step.
        = Conditionally : Only when AlongStepDoIt limits the 
                          step, corresponding PoststepDoIt is invoked.
        = ExclusivelyForced : Corresponding PostStepDoIt is exclusively 
                              forced.
        All other DoIt including AlongStepDoIts are ignored.
\item The AlongStepGetPhysicalInteractionLength method of all active 
      processes is called.  Each process returns a step length and the 
      minimum of these and the 
      This method also returns a fGPILSelection flag, to indicate if 
      the process is the selected one can be is forced or not:
        = CandidateForSelection: this process can be the winner.  If 
          its step length is the smallest, it will be the process 
          defining the step (the process 
        = NotCandidateForSelection: this process cannot be the winner. 
          Even if its step length is taken as the smallest, it will not 
          be the process defining the step 
\end{itemize}

The method {\tt G4SteppingManager::InvokeAlongStepDoIts()} is in charge 
of calling the AlongStepDoIt methods of the different processes:
\begin{itemize}
\item If the current step is defined by a 'ExclusivelyForced' 
PostStepGetPhysicalInteractionLength, no AlongStepDoIt method will be invoked
\item Else, all the active continuous processes will be invoked, and they return the ParticleChange. 
After it for each process the following is executed:
  \begin{itemize}
  \item Update the G4Step information by using final state information of the track given by a physics process. This is done through the UpdateStepForAlongStep method of the ParticleChange
  \item Then for each secondary:
    \begin{itemize}
    \item It is checked if its kinetic energy is smaller than the energy threshold for the material. In this case the particle is assigned a 0. kinetic energy and its energy is added as deposited energy of the parent track. 
This check is only done if the flag ApplyCutFlag is set for the particle (by default it is set to 'false' for all particles, user may change it in its G4VUserPhysicsList). 
If the track has the flag IsGoodForTracking 'true' this check will have no effect (used mainly to track particles below threshold)
    \item The parentID and the process pointer which created this track are set    \item The secondary track is added to the list of secondaries. If it has 0. kinetic energy, it is only added if it it invokes a rest process at the beginning of the tracking
    \end{itemize}
   \item The track status is set according to what the process defined
  \end{itemize}
\end{itemize}

The method G4SteppingManager::InvokePostStepDoIts is on charge of calling the PostStepDoIt methods of the different processes.
\begin{itemize}
\item Invoke the PostStepDoIt methods of the specified discrete process (the one selected by the PostStepGetPhysicalInteractionLength, and they return the ParticleChange. The order of invocation of processes is inverse to the order used for the GPIL methods.
After it for each process the following is executed:
  \begin{itemize}
  \item Update PostStepPoint of Step according to ParticleChange
  \item Update G4Track according to ParticleChange after each PostStepDoIt
  \item Update safety\footnote{} after each invocation of PostStepDoIts
  \item The secondaries from ParticleChange are stored to SecondaryList
  \item Then for each secondary:
    \begin{itemize}
    \item It is checked if its kinetic energy is smaller than the energy threshold for the material. In this case the particle is assigned a 0. kinetic energy and its energy is added as deposited energy of the parent track. 
This check is only done if the flag ApplyCutFlag is set for the particle (by default it is set to 'false' for all particles, user may change it in its G4VUserPhysicsList). 
If the track has the flag IsGoodForTracking 'true' this check will have no effect (used mainly to track particles below threshold)
    \item The parentID and the process pointer which created this track are set    \item The secondary track is added to the list of secondaries. If it has 0. kinetic energy, it is only added if it it invokes a rest process at the beginning of the tracking
    \end{itemize}
   \item The track status is set according to what the process defined
  \end{itemize}
\end{itemize}

The method G4SteppingManager::InvokeAtRestDoIts is called instead of the three above methods in case the track status is {\em fStopAndALive}. It is on charge of selecting the rest process which has the shortest time before and then invoke it:
\begin{itemize}
\item To select the process with shortest tiem, the AtRestGPIL method of all active processes is called. 
      Each process returns an lifetime and the minimum one is chosen.
      This method returm also a G4ForceCondition flag, to indicate if the process is forced or not:
        = Forced    : Corresponding AtRestDoIt is forced.
        = NotForced : Corresponding AtRestDoIt is not forced 
                      unless this process limits the step.
\item Set the step length of current track and step to 0.
\item Invoke the AtRestDoIt methods of the specified at rest process, and they return the ParticleChange. The order of invocation of processes is inverse to the order used for the GPIL methods.

After it for each process the following is executed:
  \begin{itemize}
  \item Set the current process as a process which defined this Step length.
   \item Update the G4Step information by using final state information of the track given by a physics process. This is done through the UpdateStepForAtRest method of the ParticleChange.
  \item The secondaries from ParticleChange are stored to SecondaryList
  \item Then for each secondary:
    \begin{itemize}
    \item It is checked if its kinetic energy is smaller than the energy threshold for the material. In this case the particle is assigned a 0. kinetic energy and its energy is added as deposited energy of the parent track. 
This check is only done if the flag ApplyCutFlag is set for the particle (by default it is set to 'false' for all particles, user may change it in its G4VUserPhysicsList). 
If the track has the flag IsGoodForTracking 'true' this check will have no effect (used mainly to track particles below threshold)
    \item The parentID and the process pointer which created this track are set    \item The secondary track is added to the list of secondaries. If it has 0. kinetic energy, it is only added if it it invokes a rest process at the beginning of the tracking
    \end{itemize}
   \item The track is updated and its status is set according to what the process defined
  \end{itemize}
\end{itemize}


\section{Ordering of Methods of Physics Processes}
The ProcessManager of a particle is responsible for providing the 
correct ordering of process invocations.  {\tt G4SteppingManager} 
invokes the processes at each phase just following the order given 
by the ProcessManager of the corresponding particle. 

For some processes the order is important.
{\sc Geant4} provides by default the right ordering. 
It is always possible for the user to choose the order of process 
invocations at the initial set up phase of {\sc Geant4}. 
This default ordering is the following:

\begin{enumerate}
\item Ordering of GetPhysicalInteractionLength 
   \begin{itemize}
   \item In the loop of GetPhysicalInteractionLength of 
         AlongStepDoIt, the Transportation process has to be invoked 
         at the end. 
   \item In the loop of GetPhysicalInteractionLength of 
         AlongStepDoIt, the Multiple Scattering process 
         has to be invoked just before the Transportation process. 
   \end{itemize}
\item Ordering of DoIts 
   \begin{itemize}
   \item There is only some special cases.
         For example, the Cherenkov process needs the energy 
         loss information of the current step for its DoIt
         invocation. Therefore, the EnergyLoss process has to 
         be invoked before the Cherenkov process. This 
         ordering is provided by the process manager. Energy 
         loss information necessary for the Cherenkov process 
         is passed using G4Step (or the static dE/dX table is 
         used together with the step length information in 
         G4Step to obtain the energy loss information). 
Any other?
   \end{itemize}
\end{enumerate}

\section{Status of this chapter}

       created by ? \\
10.06.02 partially re-written by D.H. Wright \\
14.11.02 updated and partially re-written by P. Arce \\ 

\begin{latexonly}

\begin{thebibliography}{999}
\bibitem{applGuide} Geant4 Users' Guide for Application Developers.
\end{thebibliography}

\end{latexonly}

\begin{htmlonly}

\section{Bibliography}

\begin{enumerate}
\item Geant4 Users' Guide for Application Developers.
\end{enumerate}

\end{htmlonly}