\section{Tracking}
Particles are propagated through the guide fields defined in the 'lattice' file, by integration of the equations of motion.
The electro-magnetic fields are represented by field maps or multipole expansions. The magnetic fields in the injection channel (backleg fringe field and inflector)
are described by field maps generated with finite element codes. There are a number of possible representations of the electric fields in the ring quadrupoles, including
a field map (generated with OPERA 3D), a multipole expansion in cartesian coordinates (as in the original quad paper) or in cylindrical coordinates based on McMillan functions.
The representation of your choice is selected in the 'lattice' file. The multipole representation of the quads assumes that the four plates are at the same voltage. 
Some analyses rely on independently powering the quad plates, 
in order, for example, to 
incorporate quad scraping, and/or voltage errors that are not common to the four plates simultaneously.  In such cases it is convenient to use a superposition of the field maps for each of the
four plates individually powered. This option is also available by chosing the appropriate lattice file. 

The kicker magnetic field can be represented as a field map or as a uniform vertical field. For the temporal profile it is standard practice to use the magnetometer measurement of the
time dependence of the kicker field. Alternatively one can specify a perfect rectangular pulse as well as other slightly more realistic versions.

The main dipole field is nominally uniform with field errors included as multipoles. 

The M5 line consists of nominally linear magnets (dipoles and quadrupoles). The tracking algorithm takes advantage of that linearity to propagate the phase space through those
elements using matrix multiplication.
\section{Apertures}   
Apertures are defined for each element in the lattice file. 
With a couple of important expections, particles with trajectories that intersect with the aperture are lost.
Consider for example the inflector. The inflector aperture is rectangular with width and height of 18mm and 56 mm resepectively. If a muon trajectory hits that aperture it is lost and
dropped from the distribution. The aperture of the quadrupoles is fixed by the quad plates. The collimator aperture is circular with radius 45mm.

Stored particles are confined within the quad and collimator aperture. But particles that exit the inflector en route to the ring must typically pass through the outer plate of Q1. 
In order to allow for this possibility the Q1 aperture is removed for injected particles, and multiple scattering is included for particles that pass through the quad plate.

In addition, multiple scattering can be 'turned on' for passage through the coils that overlap the beginning and end of the inflector.  

\section{Initialization of the Distribution}
\subsection{Origin}
The initial distribution of particles can be read from a file or generated at random according to an algorithm. There are a number of options available. 
\subsection{Pre existing distribution}
A distribution generated by
Dyktys Stratakis (see \href{https://gm2-docdb.fnal.gov/cgi-bin/sso/ShowDocument?docid=4461}{GM2-doc-4461-v1})
from the production target to the end of the M5 line is available with the following instruction in the input file:
\begin{itemize}
\item $\vev{\rm  muon\_file = 'particles\_M4M5End\_400\_mod.txt'}$ .                                                                           
\end{itemize}
The Stratakis distribution is generated by supposing that 8GeV protons are incident on the conversion target all at the same time. While all of the protons
strike the target at the same time, the  muons that survive transit around the
delivery ring and through the M4 and M5 lines emerge with a momentum dependent delay. That delay is on the order of a few nanoseconds. 
In fact, the length of the proton pulse on target is more than 120ns. That temporal distribution is imprinted on the phase space distribution in an
additional step. The parameters of the pulse shape are defined in the input file, most commonly by reference to a measurement. 
With the setting
\begin{itemize}
\item   $\vev{\rm tdistr    = "e989-P1"}$
\end{itemize}
the reference trace is that measured for the first bunch in the eight bunch train.                                                                                                 
\subsection{Generating a random distribution}
The phase space coordinates ($x,p_x$) of the distribution are correlated according to
\begin{eqnarray*}
x(s)&=&\sqrt{a\beta}\cos(\phi(s))\\
p_x = p\der{x(s)}{s}&=&p\frac{\sqrt{a}}{\beta}\alpha\cos\phi(s) +\frac{\sqrt{a}}{\beta}\sin\phi(s)
\end{eqnarray*}
where $a$ is the emittance,  $\alpha = \onehalf\der{\beta}{s}$ and $\der{\phi}{s} = \frac{1}{\beta}$.
A distribution with the appropriate correlation is created by
using a random gaussian distribution of horizonal and then vertical amplitudes ($a$), and then a random flat distributon from $0\rightarrow 2\pi$ of phase $\phi$.

\vskip 0.1in
\noindent
The parameters in the input file that specify initial phase space distribution are:

\vskip 0.1in
\begin{tabular}{lll}
  nmuons& = 100000&! number of muons\\
  create\_new\_distribution& = T/F &!Create a new distribution or read an existing file                                             \\
  seed& =0& !Random number seed.  If seed =0 then random number set to system clock.                                                  \\                                                        
  new\_file & = 'empty' &!If you choose to create a new distribution it will be saved in new\_file.                 \\                                                           
  muon\_file& = file\_name &! read the phase space distribution off the target from this file.\\
  tdistr &   = "e989-P10" &! Time distribution of the muons, choices are "flat", "gaus", "e821",\\
& &\ \ \ \   !"e989" or ``e989-P1'' through ``e989-P8'' or the average ``e989-P10''  \\
  tlength &  = 0.&! Width in time of distribution : (doesn't apply to tdistr="e821" or ``e989...'')                           \\
 tsigma & = 0. &! standard deviation (width) of time distribution if it is gaussian, \\
& & \ \ \ \ !(doesn't apply  tdistr="e821" or ``e989'')\\
  pzdistr&   = "gaus" &! ``gaus'' or ``flat''                                                                                    \\                                                      
  pz &       = 0.01 &cutoff of longitudinal momentum distribution, \\
& &\ \ \ \ !(fractional energy offset with respect to reference)            \\                                                          
  pzsigma &  = 0.013 &! width (sigma) when pzdistr="gaus"                                                                            \\                         
  epsdistr&  = "gaus" & !emittance distribution at production target: "delta", "flat", "gaus"               \\
  epsx  &    = 16.6e-6 & !horizontal emittance at inflector assuming no losses in injection channel (m-rad)   \\                                                                              
  epsy&      = 16.6e-6 & !vertical emittance at inflector assuming no losses in injection channel (m-rad)       \\
\end{tabular}                                                                                                                          
\vskip 0.1in

\subsection{Focusing}
The beam is focused by the quadrupoles in the M5 line to maximize transmission into the storage ring. The inflector is the most restrictive aperture in the injection
channel with full width and height of 18mm and 56mm respectively. By comparison the full width and height in the ring are both 90mm. The mismatch in the horizontal
plane is the most pronounced. To minimize beam size in the inflector we adjust the focusing in the M5 line so that the beta function in the inflector will be small.
But a small beta in the inflector results in a large beta in the ring. There is not much gained if all of the muons clear the inflector
only to be lost in the ring. We find that losses in the inflector and the ring are balanced if at the midpoint of the inflector $\beta_h\sim 2$m and $\beta_v\sim 10$m. 
In addition to the focusing in the M5 line quadrupoles, the beam is further horizontally defocused on passage through the fringe field of the
dipole in the injection channel. The distribution can be initialized with $\beta-$functions and emittance at the entrance to the start of the injection channel so
that it will have the desired $\beta_h=2$m, $\beta_v=10$m at the midpoint of the inflector.
\vskip 0.1in
\noindent
The parameters in the input file appear as follows:

\begin{table}[h]
\begin{tabular}{lll}
   twiss &    = $\beta_x,\beta_y,\alpha_x,\alpha_y,\eta_x,\eta_y, \eta^\prime_x,\eta^\prime_y,\phi_x,\phi_y,\gamma_x\gamma_y$&\phantom{space filleraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}\\
   twiss &    = 2.0, 10.0, 0., 0.0, 0.0, 0., 0., 0., 0.7, 0.7, 0., 0.& \\
\end{tabular}
\end{table}

\begin{table}[h]
\begin{tabular}{lll}
  twiss\_ref& ='end' &! calculate beta functions of distrubution so that it will take on specified 'twiss' values at inflector end\\
  twiss\_ref& = 'center' &! calculate beta functions of distrubution so that it will take on specified 'twiss' values midway through inflector\\
  twiss\_ref& = 'use\_as\_is' &! Use specified 'twiss' values directly for the distribution\\

\end{tabular}
\end{table}

\subsection{Offsets}
The offset and angle of the centroid of the distribution are likewise specified in the input file.
If the starting point of the tracking excercise is at the input to the injecton channel (end of the M5 line) then the trajectory for a magic momentum
muon with the following offsets at the start of the injection channel will exit the inflector at its horizontal and vertical midpoint with a horizontal angle of -2 mrad.

\begin{table}[h]
\begin{tabular}{lll}
  initial\_offsets\_ref& =  -2.9343E-02  0. 0. 1.6292E-02 0. 0. &! $x_{off},y_{off},z_{off},x^\prime_{off},y^\prime_{off},z^\prime_{off}$\\
  initial\_offsets& =  -2.9343E-02  0. 0. 1.6292E-02 0. 0. & \\
 \end{tabular}
\end{table}

There is a procedure to find offsets at the start of the injection channel that correspond to offsets specified at the inflector exit.
For example, suppose we want the offsets such that the trajectory of the magic momentum muon exit the inflector dead center with radial angle -2mrad.

%\begin{table}
\vskip 0.1in
\begin{tabular}{lll}
 opt\_incident& = T& T/F  \\
 inflector\_end\_target& = 0. 0. 0. -0.002 0. 0.& $x,y,z,x^\prime,y^\prime,\delta p/p$\\
\end{tabular}
\vskip 0.1in
%\end{table}

As written the program does not update 'initial\_offsets'. That step is done by hand.                                                                                                      

\subsection{Starting point}
The longitudinal coordinate of the start of tracking is either at the beginning of the 'line' as defined in the lattice file, or at the inflector exit according to
%\begin{table}[h]
\vskip 0.1in
\begin{tabular}{lll}
  start\_tracking\_at\_inflector\_exit& = F  &! F - tracking will start at the beginning of the lattice 'line', T- at the inflector exit\\
 \end{tabular}
\vskip 0.1in
%\end{table}
\noindent
The offsets and twiss parameters must be chosen consistent with the starting point. While the initial horizontal offset and angle indicated above (-29.3 mm and 16.3 mrad)
are a reasonable choice if tracking begins at the input to the injection channel, -77 mm and -2mrad would be the corresponding choice for tracking that begins at the inflector exit.
Likewise the choice of initial twiss parameters depend on the starting point for tracking. 

