%\chapter{Field Map}
%\label{c:field-map}
%\section{Introduction}
\SECTION{Overview}
\index{Bmad|hyperbf}
\pdfbookmark[1]{Overview}{Overview}

%---------------------------------------------------------------------------
The g-2 storage ring simulation program is based on subroutines from the \bmad\footnote{http://www.lepp.cornell.edu/~dcs/bmad/}  library
of accelerator modeling routines. A set of specialized routines incorporates the peculiarities of the g-2 
storage ring and interfaces the \bmad code. The model is comprised of three branches: the M5 beam line, the injection channel, and the storage ring.
The program will propagate a distribution of muons through all three branches in series. Alternatively, it is possible to restrict tracking to the ring.
By default closed ring twiss parameters are computed. Twiss parameters specified at the start of M5 line or injection channel are propagated to the ring. 
Following the \bmad format, the layout of the beam lines is described in a {\tt lattice file} as a sequence of elements. Each element has a name, 
length, apertures, magnetic and/or electric fields. (i.e. The element named {\rm QUAD1\_SHORT} has length of 1.64 m, 
electric fields, magnetic field, walls at $\pm 0.05$m, etc.) The electromagnetic fields in each element are determined by multipole expansion or field map. 
The injection kickers and electrostatic quadrupole fields are time dependent, for the study of injection, scraping, and RF matching. Collimators
define the aperture. The program includes spin tracking, and muon decay so that it can be used to investigate systematics related to measurement of $\omega_a$,
detector acceptance, etc. 
The program is flexible, so that various configurations of the accelerator
hardware can be explored. The basic outline of model and functionality is as follows:
\begin{itemize}
\item The g-2 ring is described in a {\tt lattice filed} as a sequence of elements
\item Each element has a name, length, apertures, magnetic and/or electric fields. (i.e. The element named {\rm QUAD1\_SHORT} has length of 1.64 m, electric fields, magnetic field, walls at $\pm 0.05$m, etc.)
\item Read parameters and lattice layout from an input file, {\tt input.dat}, ~\sref{c:input-file}
\item Define the reference trajectory through the injection line ~\sref{c:injection-channel} by tracking with initial coordinates given in {\tt input.dat}
\item Compute the transfer matrices through the injection line 
\item Create (or read) a phase space distribution of muons
\item Transport the distribution to the entrance of the injection line and rotate (in phase space) so that the distribution, when propagated through the injection channel,
 will match the Twiss parameters in the inflector that are specified in {\tt input.dat}
\item Track the distribution (one muon at a time) through the elements of the injection channel. 
\item The injection line reference orbit is offset by $d$ with respect to the magic radius of the ring, where $d$ is the displacement of the center of the inflector from the magic radius. Track 
phase space coordinates and spin of particles into the ring.
\item The electrostatic quads are represented as maps or multipole expansions.
\item The kicker is represented as a map or expansion. The kicker field has a time dependence that is defined via parameters in {\tt input.dat}
\item Muons that strike collimators are assumed lost.
\item Electrons from muon decay (assuming muon decay is implemented) are allowed to penetrate collimators on their way to detectors. 
\item Muons are tracked for a number of turns defined in {\tt input.dat}
\item Diagnostics
\begin{enumerate}
\item The phase space distribution is written to a file with a subroutine call. The distribution is saved at the end of each element in the injection line.
\item Parameters summarizing the distribution, such as average and rms position and times, are recorded after each element and all turns
\item Multiple scattering in the inflector end coils, inflector cryo windows, quadrupole plates and kicker plates can be implemented with a switch in {\tt input.dat}
\item Calculation of the polarization of the distribution at any point in the ring on any turn can be implemented with a switch.
\end{enumerate}
\item There is an option to require that Twiss parameters be propgated backwards from the inflector (where they are defined in the input file) to the start of the injection line.
\item There is an option to propagate phase space coordinates at the inflector exit be propagated backwards to the start of the injection line.
\item There is an option to tilt the inflector, in the $r,\phi$ plane about the exit end of the inflector magnet. Tilting effects the location of the inflector apertures as well as the inflector field map. 
\end{itemize}
\subsection{Lattice File}
The lattice file, usually named ``bmad_...'' defines the layout of the accelerator components according to the \bmad syntax.
It will typically include standard beam line components in the M5 beam line, and field maps for the hole through the yoke, fringe and inflector.
Quadrupole and kicker fields are superimposed on the main dipole. Collimators and the scattering surfaces such as the coils overlapping the 
ends of the inflector and quadrupole plates are defined. \bmad routines propagate phase space coordinates or twiss parameters through one element at a time so 
that it is possibile to monitor distributions at the end of each element. ``Marker'' elements can be defined and inserted almost arbitrarily if it is of interest to 
keep track of the distribution at non-standard points in the lattice. \bmad defines the injection line and the ring as distinct branches so that particles
can be propagated once through the injection line and an arbitrary number of times around the ring.
\subsection{Input}
Other parameters are defined in the namelist formatted file {\tt input.dat} and summarized in Tables \ref{tab:input1} and \ref{tab:input2}.


\begin{table}[htdp]
\caption{\tt input.dat}
\begin{center}
\begin{tabular}{ p{4cm}p{4cm}p{8cm}}
Parameter & Example value & Description \\ \hline
   lat_file_name& 'bmad.lat.'& Lattice file \\
  nmuons& 10000& Number of muons to track\\
  nturns &  20& Number of turns to track\\
  create_new_distribution& .true.&                            Create a new phase space distribution off of the target  (or not)\\
  new_file &'new_file.dat'&                          Write the new distribution to this file, if the name is blank no file will be written\\
  muon_file& 'old_file.dat'&                          Read the phase space distribution off the target from this file. If blank, use freshly created distribution\\
  tdistr    &"e989"     &                                     Time/longitudinal distribution: "flat", "gaus", "e821", "e989"\\
  tlength  & 120.e-9     &                                    Base length of injected bunch (doesn't apply to tdistr="e821")\\
  tsigma   &   0.e-9        &                                 Sigma for tdistrgaus" (doesn't apply to tdistr={"uniform","e821","e989"})\\
  pzdistr  &  "gaus"         &                                 Momentum distribution: "flat", "gaus"\\
  pz        & 0.06             &                             Cutoff of longitudinal momentum distribution\\
  pzsigma & 0.0112     &                                    Sigma for pzdistr="gaus"\\
  epsdistr & "gaus"       &                                  Emittance distribution at target: "delta", "flat", "gaus"\\
  epsx    &  14.e-6       &                                  Emittance at inflector (m-rad)\\
  epsy    &14.e-6       &                                  Emittance at inflector (m-rad)\\
  twiss   &1.0 8.0 0. 0.0 0.0 0. 0. 0.7 0.7 0. 0. & Betax, betay, alphax, alphay, etax, etapx, etay, etapy, phix, phiy, gammax, gammay at inflector midpoint\\
 \\
  inf_aperture & "e821"&                                      Inflector aperture: "none", "rect", "e821", "e989"\\
  inflector_width&  0.009& \\
  inf_end_us  &  .true.     &                                Include scattering at  upstream  end of inflector (E821 coils)\\
  inf_end_ds  &  .true.     &                               Include scattering at downstream end of inflector (E821 coils)\\
  enerloss & .true.          &                             Include energy loss with scattering in inflector(E821 coils)\\
  inflector_angle&  -0.003&\\
 inflector_field&  1.012747&                                 Multiplier of Bmagic. 1.02747 * Bmagic = B_wuzeng\\
  initial_offsets & -0.04863 0. 0. 0.02247 0. 0. & Initial phase space coordinates of reference orbit (18mm,Wuzeng, -3mrad)\\
  opt_incident&  .true.&\\
  inj_matrix_tracking & .true.&                               If true, propagate trajectories through injection channel using matrices, otherwise step through field map\\
  ring_theta     &  0.                &                        Rotation of ring elements with respect to inflector exit\\
\end{tabular}
\end{center}
\label{tab:input1}
\end{table}%
\begin{table}[htdp]
\caption{\tt input.dat}
\begin{center}

\begin{tabular}{ p{4cm}p{4cm}p{8cm}}
Parameter & Example value & Description \\ \hline
 kickerPlates  &   989             &                         Controls shape of B-field: 1=uniform, 821, 989\\
  kickerCircuit &  989             &                           Controls shape of pulse: 1=square kick, 821=RLC, 989=Blumlein\\
  kickerFieldType& 2               &                         Controls B-field calculation: 1=analytic, 2=mapped\\
  kicker_params\%kicker\_field&      0.022   0.022   0.022 & Kick strength (Tesla) for 989 kick\\
 kicker_params\%kick\_width&    120.e-9   120.e-9   120.e-9&   Kick width (seconds) -- doesn't apply to kickerCircuit=821\\
  kicker_tStart       &      -1.0  -1.0   -1.0  & If kicker_tstart(i) = -1., automatically calculate kicker start time\\
\hline
  quad_plate   &  .false.                                  &   include scatter in Q1 quad plates\\
  quadPlates   &  1                                       &  821, 989 -- controls shape of E-field for Q1_Long\\
  quadCircuit  &  1                                        &   0=off at injection, ramped up with tauRC=5us; 1=storage potential at injection; todo:2=scraping at inj.\\
  quadFieldType&  1                                       &    1=analytic, 2=mapped\\
  quad_params  & 0.185 0.185 0.185 0.185    & field_index(4)  (For the time being quads 2,3 and 4 all have the value assigne to 2\\
\hline
  twiss_file & .false.                                       &  true => write twiss parameters every cm to "twiss_file.dat"\\
  vparam_id & 0					   &   0-nothing,  1-betax, 2-betay, 3-eta, 4-kick, 5-kick_width, 6-initial position offset, 7-initial angle offset is the parameter to vary\\
  vparam_min&  160.e-4&\\
  vparam_max & 300.e-4&					    maximum of parameter to vary\\
  delta_vparam &10.e-4&					    parameter step \\
  loop&  .false.&\\
\hline

\end{tabular}
\end{center}
\label{tab:input2}
\end{table}%

\subsection {Create a distribution of muons}
There are several possibilities: 
\begin{itemize}
\item Gaussian in 6D phase space coordinates
\item Temporal distribution consistent with that of the proton beam on the pion production target (FNAL ``W'')
\item Flat distribution
\item Gaussian in transverse coordinates and flat or ``W'' in longitudinal
\end{itemize}
Alternatively the program will read an exisiting distribution.
\subsection{Spin tracking and Muon decay}

%\section{Magnetic Field}
%\label{s:magneticfield}

%\begin{figure}[tb]
% \begin{center}
%    \includegraphics*[width=0.45\textwidth,angle=0]{/Users/dlr10/lepp/g-2/magneticfield/bfield_along_injection_axis.pdf}
%    \includegraphics*[width=0.45\textwidth,angle=0]{/Users/dlr10/lepp/g-2/magneticfield/bfield_total_along_injection_axis.pdf}
%\caption[Magnetic field along injection line]
% {
%(Left)Vertical component of the magnetic field along the trajectory through the hole
%in the backleg iron, between the inner coils, through the fringe field
%and finally along the axis of the inflector to the gap.(Right) Sum of inflector field and dipole fringe field.
% }
% \label{f:bfieldy}
% \end{center}
% \end{figure}
\index{Tao}
%---------------------------------------------------------------------------
%\section{Trajectory}
%through the fields using the BMAD\footnote{http://www.lepp.cornell.edu/~dcs/bmad/} code, and is shown in Figure \ref{f:trajectory}. In order
