\section{Examples}
\subsection{Create Distributions at Inflector Exit}
We propagate a distribution from the end of the M5 line and through the injection channel to the inflector exit. The centroid of the distribution corresponds to
the trajectory of the magic momentum muon. Here we choose the initial offsets so that the magic momentum muon exits the inflector with 
$x_{inf}=0,x^\prime_{inf}=-1 {\rm mrad}, y_{inf}=y^\prime_{inf}=0$.  
\begin{enumerate}
\item The first step is to find the initial phase space coordinates of the magic momentum muon.
The directory
 \href{http://www.classe.cornell.edu/~dlr/g-2/bmad_tracking_code/g-2/examples/example11/}{/home/dlr/development9\_linux/g-2/examples/example11}
contains the necessary files including
\begin{enumerate}
\item  \href{http://www.classe.cornell.edu/~dlr/g-2/bmad_tracking_code/g-2/examples/example11/bmad_esquad_grid.}{/home/dlr/development9\_linux/g-2/examples/example11/bmad\_esquad\_grid.} - lattice file. 
\item  \href{http://www.classe.cornell.edu/~dlr/g-2/bmad_tracking_code/g-2/examples/example11/input.dat}{/home/dlr/development9\_linux/g-2/examples/example11/input.dat} - run specific parameters. Note that 
\vskip 0.1in
\begin{tabular}{lll}
inflector\_end\_target = &0. 0. 0. -0.001 0. 0. & !x,y,z,xp,yp,dp/p\\
opt\_incident = &  T  &!find initial\_offsets that yield inflector\_end\_target\\
\end{tabular}
\item All of the soft links to the reference files are set with the command  \\  
\href{http://www.classe.cornell.edu/~dlr/g-2/bmad_tracking_code/g-2/softlink_lnx}{\tt /home/dlr/development9\_linux/g-2/softlink\_lnx}
\item Not to be forgotten, quad parameters are set in 'quad\_input.dat' and 'quad\_plate\_misalign.bmad'
\item
To execute the latest version of the program type {\tt /home/dlr/development9\_linux/production/bin/g2\_tracking}
\item The search for the vector of {\tt initial\_offsets} that leads to the desired {\tt inflector\_end\_target} vector begins with
the {\tt initial\_offsets} that are read from the 'input.dat' file. If the search fails the choice of {\tt initial\_offsets} may need to be adjusted.
\item The output is written to {\tt subdir/optimize\_incident\_out.dat} ({\tt subdir} is the subdirectory generated for the run and named with date and time {\tt yearmmdd\_hhmmss}

Here is an example of the out relevant to fixing the initial offsets.

\begin{verbatim}
[dlr10@lnx6186 example11]$ more 20220225_144619/optimize_incident_out.dat 
  element at end of injection line = MARK_INFLECTOR_DS                       
 iteration       x_init     xp_init     x_final    xp_final
         1 -2.9343E-02  1.6292E-02  4.2847E-07 -1.9998E-03
 end_orb%vec(1:2) =   3.8743E-10 -1.0000E-03
 iteration      x_init     xp_init     x_final    xp_final     Delta x    Delta xp
         2  -3.7440E-02  1.8816E-02 -3.3623E-04 -1.1726E-03 -8.0968E-03  2.5240E-03
         3  -3.7549E-02  1.8892E-02 -2.3286E-07 -9.9990E-04 -1.0870E-04  7.5782E-05
         4  -3.7547E-02  1.8891E-02 -1.6138E-08 -1.0000E-03  1.6958E-06 -5.1047E-07
         5  -3.7547E-02  1.8891E-02 -2.1031E-08 -1.0000E-03  7.8237E-09 -4.1334E-10
         6  -3.7547E-02  1.8891E-02  9.9876E-09 -9.9999E-04 -5.2470E-08  1.9456E-08
         7  -3.7547E-02  1.8891E-02  2.8900E-09 -1.0000E-03  4.4945E-08 -1.5309E-08
         7  -3.7547E-02  1.8891E-02  2.8900E-09 -1.0000E-03  4.4945E-08 -1.5309E-08

initial_offsets = -3.7547E-02 0.0 0.0  1.8891E-02 0.0 0.0
initial_offsets_ref = -3.7547E-02 0.0 0.0  1.8891E-02 0.0 0.0

\end{verbatim} 

\item Replace the 'initial\_offsets' and 'initial\_offsets\_ref' in the 'input.dat' file with the last two lines of 'optimize\_incident\_out.dat' 
Then rerun {\tt g2\_tracking}. The output to the monitor will include the following:

\begin{verbatim}

--------------------------------------------------------------------------------
Fractional Tune:        Qx = 0.94917   Qy = 0.31776
RING PARAMS:        Qx = 0.94918   Qy = 0.31776   betax = 7.64553   betay =22.02755   field_index = 0.10097
ELE(0)%beta:        betax = 7.64553   betay =22.02755
 circumference =   4.46860139E+01 radius =   7.11200000E+00
start_orb -3.7547E-02  1.8891E-02  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00
start_orb ref -3.7547E-02  1.8891E-02  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00
  element at end of injection line = MARK_INFLECTOR_DS                       
 Optimize_Incident: err_flag =   F  track_state =          -9
 iteration       x_init     xp_init     x_final    xp_final
         1 -3.7547E-02  1.8891E-02 -2.7141E-06 -1.0013E-03
 end_orb%vec(1:2) =   3.8743E-10 -1.0000E-03
         2  -3.7547E-02  1.8891E-02 -1.4181E-08 -1.0000E-03  1.4369E-07  2.8932E-07
         3  -3.7547E-02  1.8891E-02  3.6762E-08 -9.9998E-04 -1.4435E-08  6.3270E-09
         4  -3.7547E-02  1.8891E-02 -3.7868E-08 -1.0000E-03  1.7425E-08 -9.9100E-09
         5  -3.7547E-02  1.8891E-02  4.3686E-08 -9.9997E-04 -2.3444E-08  1.2164E-08
         6  -3.7547E-02  1.8891E-02 -3.2143E-12 -1.0000E-03  3.6329E-08 -1.6623E-08
         6  -3.7547E-02  1.8891E-02 -3.2143E-12 -1.0000E-03  3.6329E-08 -1.6623E-08
  trajectory through backleg and inflector
  0.0000E+00 -3.7547E-02  1.8891E-02  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00 BACKLEG_START                           
  2.4801E+00  8.0807E-03 -5.6106E-03  0.0000E+00  0.0000E+00 -4.5804E-04  0.0000E+00 FRINGE                                  
  2.4801E+00  8.0807E-03 -5.6106E-03  0.0000E+00  0.0000E+00 -4.5804E-04  0.0000E+00 MARK_CRYO_US                            
  2.6000E+00  6.7930E-03 -1.5562E-02  0.0000E+00  0.0000E+00 -4.6550E-04  0.0000E+00 CRYO_US                                 
  2.6000E+00  6.7930E-03 -1.5562E-02  0.0000E+00  0.0000E+00 -4.6550E-04  0.0000E+00 MARK_INFLECTOR_US                       
  4.3000E+00 -2.7141E-06 -1.0013E-03  0.0000E+00  0.0000E+00 -4.8822E-04  0.0000E+00 INFLECTOR                               
  4.3000E+00 -2.7141E-06 -1.0013E-03  0.0000E+00  0.0000E+00 -4.8822E-04  0.0000E+00 MARK_INFLECTOR_DS                       
  4.3000E+00  7.6997E-02 -1.0013E-03  0.0000E+00  0.0000E+00 -4.8822E-04  0.0000E+00 INJ_TO_RING                             
  4.3000E+00  7.6997E-02 -1.0013E-03  0.0000E+00  0.0000E+00 -4.8822E-04  0.0000E+00 RING_BR                                 
  4.3000E+00  7.6997E-02 -1.0013E-03  0.0000E+00  0.0000E+00 -4.8822E-04  0.0000E+00 END                                     
\end{verbatim}

The very last line  $s,x,x',y,y',z,dp/p$ is the phase space of the trajectory at the end of the inflector. 
The trajectory exits the inflector at the midpoint of its aperture (77mm from the magic radius), with an angle of -1mrad.

\end{enumerate}

\item The next step is to track each particle in a distribution through the injection channel. Probably the most straightforward way to proceed is
to read an existing distribution. Diktys, Volodya, and Eremery have generated such distributions, some of which are accessed by the preset soft links.
But as those files have a limited number of particles, and as the efficiency of capturing such a particle in the ring is on the order of 1\%, that
number is often insufficient. Alternatively we can generate our own particle list assuming gaussian distributions as discussed earlier.   
The files in directory 
 \href{http://www.classe.cornell.edu/~dlr/g-2/bmad_tracking_code/g-2/examples/example12/}{/home/dlr/development9\_linux/g-2/examples/example12}
are set up to:
\begin{enumerate}
\item create a distribution of 100,000 muons at the start of the injection channel
\item propagate the particles in that distribution into the ring
\item write the phase space coordinates of the particles that arrive at the inflector exit to a file
\end{enumerate}

The program has been executed in this directory ({\tt example12/}), and written output to {\tt subdir} = 20220225\_152556.
The phase space coordinates of the particles at the end of each of the elements in the 'line' are written to files named
$\vev{\rm ELEMENT\_NAME}${\tt \_phase\_space.dat}. The file {\tt INJ\_TO\_RING\_phase\_space.dat} is the distribution at the inflector exit.

Multiple runs can be combined as necessary.
\end{enumerate}

\subsection{Momentum Capture}
The distribution {\tt INJ\_TO\_RING\_phase\_space.dat} is propagated into the ring where each particle
receives a kick from the pulsed kicker. As the kicker is not uniform, the received kick depends on arrival time of the particle. 
The momentum bite that is
captured depends on the kick. The result is that the captured momentum bite depends on the arrival time.
We define capture as survival for at least 4 microseconds (about 28 revolutions).
We delete all of the particles that are lost in those 4 microseconds from the initial distribution. The result is the momentum and time
of the particles that survive.
The files in  \href{http://www.classe.cornell.edu/~dlr/g-2/bmad_tracking_code/g-2/examples/example13/}{/home/dlr/development9\_linux/g-2/examples/example13} are configured to that end.
Note that in the {\tt input.dat} file
\begin{verbatim}

nturns  = 100	                                                                                                        
nmuons = 43000
create_new_distribution = F                                                                                           
muon_file = 'INJ_TO_RING_phase_space.dat'   !The intitial distribution at the inflector exit 
                                                   ! is from our calculation in {\tt example12/}
twiss_ref = 'use_as_is'   !Use the distribution as is, without modification of beta functions                                                                                                     
start_tracking_at_inflector_exit = T        !Start tracking at the inflector exit                                                                          
initial_offsets_ref =  0. 0. 0. 0. 0. 0.    !Use the distribution as it is at the inflector exit
initial_offsets =  0. 0. 0. 0. 0. 0.                                                                               
kickerPulseFile = 'v0/traces-Ir.txt'            !kicker pulse trace                                                                      
kicker_params%kicker_field      =   204.2e-4   204.2e-4  204.2e-4 ! Peak field on each of the 3 kickers
kicker_tStart            =   120.0e-9    120.0e-9   120.0e-9   ! kicker relative time                
scraping_on = T             !quad scraping is turned on                                                                                           
write_phase_space_every_n_turns = 28  !The phase space of the distribution after 28 turns

\end{verbatim}

The files generated by that last line are, among others, 
                                                                                                                       
\noindent
\href{http://www.classe.cornell.edu/~dlr/g-2/bmad_tracking_code/g-2/examples/example13/20220226_204947/END028_phase_space.dat}{/home/dlr/development9\_linux/g-2/examples/example13/20220226\_204947/END028\_phase\_space.dat} and\\
\href{http://www.classe.cornell.edu/~dlr/g-2/bmad_tracking_code/g-2/examples/example13/20220226_204947/END000_phase_space.dat}{/home/dlr/development9\_linux/g-2/examples/example13/20220226\_204947/END000\_phase\_space.dat}

\subsection{Combining g2\_tracking output}
Another program {\tt /home/dlr/development9\_linux/production/bin/energy\_vs\_time} reads the file with the distribution that has survived 28 (END028\_ . . .) turns and the starting distribution (END00\_...) to generate
the captured time-momentum distribution as follows:

In directory /example13 type on the command line
\begin{verbatim}
{\tt /home/dlr/development9\_linux/production/bin/energy\_vs\_time}
\end{verbatim}

The program will look for the phase space files named {\tt END028\_phase\_space.dat} and {\tt END000\_phase\_space.dat} in all of the date named subdirectories (here there is only one), 
combine them and write \href{http://www.classe.cornell.edu/~dlr/g-2/bmad_tracking_code/g-2/examples/example13/all_Energy_vs_time_0.dat} {\tt /example13/all\_Energy\_vs\_time\_0.dat}

\section{Batch submission}

It is often convenient to take advantage of the CLASSE compute farm.
If the input file is configured to create a new distribution, and {\tt seed=0}, then we simply submit the job on multiple computers and then combine the results.
An example batch submission is shown in \href{http://www.classe.cornell.edu/~dlr/g-2/gm22/mytest/energy_vs_time_vs_turn/test}{\tt /nfs/gm2/data2/dlr10/g-2/mytest/energy\_vs\_time\_vs\_turn/test/} 
Note that the input file \href{http://www.classe.cornell.edu/~dlr/g-2/gm22/mytest/energy_vs_time_vs_turn/test}{\tt test/input.dat} is configured to generate a new distribution (as in {\tt /example12}
and that tracking starts at the start of the injection channel.
See the Classe computer group \href{https://wiki.classe.cornell.edu/Computing/GridEngine}{grid engine} wiki page for more about batch submission, handy commands, etc. 
\vskip 0.1in
\noindent
With the command

\noindent
\href{http://www.classe.cornell.edu/~dlr/g-2/gm22/mytest/energy_vs_time_vs_turn/test/submit_g2_10.sh}{\tt ./submit\_g2\_10.sh} \ \ 
 \href{http://www.classe.cornell.edu/~dlr/g-2/gm22/mytest/energy_vs_time_vs_turn/test/g2.sh}{\tt g2.sh}

\noindent
ten jobs are submitted to the grid.
\vskip 0.1in
\noindent
Finally run {\tt energy\_vs\_time} to read the data from all of the date named subdirectories and write {\tt all\_Energy\_vs\_time\_0.dat}.
