parameter[lattice]                = g_2
parameter[particle]               = antimuon
parameter[geometry] = open
parameter[p0c]                    = 3.094353005E9
parameter[absolute_time_tracking] = .true.
bmad_com[aperture_limit_on]       = .true.
beginning[beta_a]   = 3.5
beginning[beta_b]   = 22.

!parameter[ran_seed]               = 123
parameter[custom_attribute2]      = "sbend::field_index"
parameter[custom_attribute1]      = "sbend::kick_width"

parameter[custom_attribute1]      = "sbend::kick_width"
parameter[custom_attribute2]      = "sbend::field_index"
parameter[custom_attribute3]      = "taylor::x_angle"


!====================================================================================================
!  INJECTION LINE  ==================================================================================
!====================================================================================================

 ! Here are the Taylor maps from Dave Rubin's simulations.  Each Taylor map has a custom attribute 
 ! "taylor::x_angle" representing the reference orbit's angle with respect to the downstream direction
 ! (in the horizontal midplane).  This information is used in the scattering/energy-loss subroutines 
 ! in "materials_mod.f90".
 call, file = 'taylor_maps/BACKLEG_START_taylor.dat'
 call, file = 'taylor_maps/BACKLEG_taylor.dat'
 call, file = 'taylor_maps/CRYO_US_taylor.dat'
 call, file = 'taylor_maps/INFA_taylor.dat'   !l = 1.5
 call, file = 'taylor_maps/INFB_taylor.dat'   !l = 0.2
 call, file = 'taylor_maps/INF_EXIT_taylor.dat'
 call, file = 'taylor_maps/END_taylor.dat'
 call, file = 'taylor_maps/DR_taylor.dat'



 ! Here are the places we'd like to compute scattering/energy-loss.
 MARK_CRYO_US:      marker  !  upstream  inflector cryo window
 MARK_INFLECTOR_US: marker !, tracking_method = custom  !  upstream  inflector end
 MARK_INFLECTOR_DS: marker !, tracking_method = custom  ! downstream inflector end
! MARK_INFLECTOR_US: drift, l=0.001 , tracking_method = custom  !  upstream  inflector end
! MARK_INFLECTOR_DS: drift, l=0.001 , tracking_method = custom  ! downstream inflector end
 MARK_CRYO_DS:      marker  ! downstream inflector cryo window

!INFA_Taylor[wall]:= { superimpose = .true.,       &
!                 section = { type = wall_start, s = 0., v(1)={0.009,-0.028}, v(2)={0.009, 0.028}, v(3)={-0.009, 0.028}, v(4)={-0.009,-0.028} }, &
!                 section = { type = wall_end,   s = INFA_TAYLOR[l], v(1)={0.009,-0.028}, v(2)={0.009, 0.028}, v(3)={-0.009, 0.028}, v(4)={-0.009,-0.028} }}
! Walls don't work with taylor elements

! Here is the injection line
 injinf : line = (         &
  BACKLEG_START_TAYLOR, &
  BACKLEG_TAYLOR,       &
  MARK_CRYO_US,         &
  CRYO_US_TAYLOR,       &
  MARK_INFLECTOR_US,    &
  INFA_TAYLOR,          &
  INFB_TAYLOR,          &
  MARK_INFLECTOR_DS,    &
  INJ_TO_RING,          &
  RING_BR               )
injinf[e_tot]  = 3.094350E9

!

!*********************************************
! ELECTRIC QUADRUPOLES GEOMETRICAL PARAMETERS
!*********************************************

! The following come from the E821 Quad NIM 
! paper and the E821 Design Report, pp. 227.

!==================================
! ELECTRODE SPANNING ANGLES
!==================================
 dThetaQShort = 13.0*pi/180.
 dThetaQGap   =  4.0*pi/180.
 dThetaQLong  = 26.0*pi/180.

!==================================
! ELECTRODE START ANGLES
!==================================
!ThetaQ1Start = 21.900*pi/180. ! design report (old)
 ThetaQ1Start = 22.075*pi/180. ! geometry -- no overlap with kickers
 ThetaQ2Start = ThetaQ1Start + pi/2.
 ThetaQ3Start = ThetaQ1Start + pi
 ThetaQ4Start = ThetaQ2Start + pi

!==================================
! ELECTRODE END ANGLES
!==================================
 ThetaQ1End   = ThetaQ1Start + dThetaQShort + dThetaQGap + dThetaQLong
 ThetaQ2End   = ThetaQ1End   + pi/2.
 ThetaQ3End   = ThetaQ1End   + pi
 ThetaQ4End   = ThetaQ2End   + pi

 width2 = 0.045*0.045

!=================================
! MAGIC RADIUS
!=================================
 radius = 7.112

!*********************************************
! KICKER GEOMETRICAL PARAMETERS
!*********************************************

 kickerArrayCenterline = pi/2.

!==================================
! KICKER SPANNING ANGLES
!==================================
 dThetaK1     = 1.76e0/7.112
 dThetaGapK12 = 0.10e0/7.112
 dThetaK2     = 1.76e0/7.112
 dThetaGapK23 = 0.10e0/7.112
 dThetaK3     = 1.76e0/7.112

!==================================
! KICKER START ANGLES
!==================================
 ThetaK1Start = kickerArrayCenterline - dThetaK2/2. - dThetaGapK12 - dThetaK1
 ThetaK2Start = kickerArrayCenterline - dThetaK2/2.
 ThetaK3Start = kickerArrayCenterline + dThetaK2/2. + dThetaGapK23

!==================================
! KICKER END ANGLES
!==================================
 ThetaK1End   = kickerArrayCenterline - dThetaK2/2. - dThetaGapK12
 ThetaK2End   = kickerArrayCenterline + dThetaK2/2.
 ThetaK3End   = kickerArrayCenterline + dThetaK2/2. + dThetaGapK23 + dThetaK3



!*********************************************
! LATTICE ELEMENTS
!*********************************************

!FULLRING: sbend, rho = 7.112, field_calc = custom, tracking_method = runge_kutta, num_steps = 100 , mat6_calc_method = tracking, ds_step=0.01 !, angle = twopi
 FULLRING: sbend, rho = 7.112, field_calc = custom, tracking_method = runge_kutta, num_steps = 1000, mat6_calc_method = tracking, ds_step=0.001 !, angle = twopi


! These elements are the "gaps" between the various quads/kickers
AFREE: FULLRING, angle = ThetaQ1Start, aperture_type=elliptical ! Gap from injection point to first electric quadrupole
BFREE: FULLRING, angle = ThetaK1Start - ThetaQ1End, aperture_type = elliptical
CFREE: FULLRING, angle = ThetaQ2Start - ThetaK3End, aperture_type = elliptical
DFREE: FULLRING, angle = ThetaQ3Start - ThetaQ2End, aperture_type = elliptical
EFREE: FULLRING, angle = ThetaQ4Start - ThetaQ3End, aperture_type = elliptical
FFREE: FULLRING, angle = twopi        - ThetaQ4End

! This element is the small gap between the "short" and "long" sections of each "single" electric quadrupole
QFREE: FULLRING, angle = dThetaQGap

! The first electric quadrupole's field is unique, and so Q1 must be handled separately
! The "wall" represents the Q1 short and long outer quadrupole plates that some muons will scatter through
!QUAD1_SHORT: FULLRING, angle = dThetaQShort, field_index=0.141, wall = {superimpose = .true., thickness = 0.0005, &
!                 section = {type = wall_start, s = 0.,                   v(1) = {0.05,-0.0235}, v(2) = {0.05,0.0235}}, &
!                 section = {type = wall_end,   s =  dThetaQShort*radius, v(1) = {0.05,-0.0235}, v(2) = {0.05,0.0235}}}

!QUAD1_LONG:  FULLRING, angle = dThetaQLong,  field_index=0.141, wall = {superimpose = .true., thickness = 0.0005, & 
!                             section={s =  0, v(1) = {0.07,-0.025}, v(2) = {0.07,0.025}}, &
!                             section={s =  dThetaQLong*radius, v(1) = {0.07,-0.025}, v(2) = {0.07,0.025}}}

! TODO: Implement scattering in "wall"
QUAD1_SHORT: FULLRING, angle = dThetaQShort, field_index=0.141, wall = { superimpose = .true., thickness = 0.0005,       &
                 section = { type = wall_start, s = 0.,                  v(1)={0.05025,-0.0235}, v(2)={0.05025, 0.0235} }, &
                 section = { type = wall_end,   s = dThetaQShort*radius, v(1)={0.05025,-0.0235}, v(2)={0.05025, 0.0235} }}

QUAD1_LONG:  FULLRING, angle = dThetaQLong,  field_index=0.141, wall = { superimpose = .true., thickness = 0.0005,       &
                 section = { type = wall_start, s = 0.,                  v(1)={0.05025,-0.0235}, v(2)={0.05025, 0.0235} }, &
                 section = { type = wall_end,   s = dThetaQLong*radius,  v(1)={0.05025,-0.0235}, v(2)={0.05025, 0.0235} }}

QUAD1:       LINE = (QUAD1_SHORT, QFREE, QUAD1_LONG)

! The E-fields of Q2, Q3, and Q4 are the same, and so can be handled identically
QUAD_SHORT:  FULLRING, angle = dThetaQShort, field_index = 0.141
QUAD_LONG:   FULLRING, angle = dThetaQLong, field_index=0.141
QUAD:        LINE = (QUAD_SHORT, QFREE, QUAD_LONG)

collimator: ecollimator, l=0.00001, x_limit=0.045, y_limit=0.045

! Kickers
KICKER1:         FULLRING, angle = dThetaK1, descrip = "KICKER_E989_NoRolledEdges.dat", &
                        hkick = 0.022, kick_width = 100.e-9, hkick = 0.022, kick_width = 100.e-9                     
GAPK12:          FULLRING, angle = dThetaGapK12
KICKER2A:        FULLRING, angle = dThetaK2/2,  descrip = "KICKER_E989_NoRolledEdges.dat", hkick = 0.022, kick_width = 100.e-9
MARK_CENTERLINE: FULLRING, angle = 0
KICKER2B:        FULLRING, angle = dThetaK2/2, descrip = "KICKER_E989_NoRolledEdges.dat", hkick = 0.022, kick_width = 100.e-9
GAPK23:          FULLRING, angle = dThetaGapK23
KICKER3:         FULLRING, angle = dThetaK3,descrip = "KICKER_E989_NoRolledEdges.dat", hkick = 0.022, kick_width = 100.e-9
KICKERS: LINE = (KICKER1, GAPK12, KICKER2A, MARK_CENTERLINE, KICKER2B, GAPK23, KICKER3)

!*********************************************
! APERTURE LIMITS
!*********************************************

! Elements just downstream of the injection point should NOT have storage-region aperture limits!
!afree[x_limit] := 0.045
!afree[y_limit] := 0.045

QUAD1_SHORT[x_limit] := 0.102
QUAD1_SHORT[y_limit] := 0.05
QUAD1_SHORT[aperture_at] = continuous


QUAD1_LONG[x_limit]  := 0.102
QUAD1_LONG[y_limit]  := 0.050
QUAD1_LONG[aperture_at] = continuous

QUAD_SHORT[x_limit] := 0.05
QUAD_SHORT[y_limit] := 0.05
QUAD_SHORT[aperture_at] = continuous


QUAD_LONG[x_limit]  := 0.05
QUAD_LONG[y_limit]  := 0.050
QUAD_LONG[aperture_at] = continuous

KICKER1[x_limit]  := 0.05 !0.102 ! vac chmbr. better?
KICKER1[y_limit]  := 0.056
KICKER1[aperture_at] = continuous

KICKER2A[x_limit] := 0.102
KICKER2A[y_limit] := 0.056
KICKER2A[aperture_at] = continuous

KICKER2B[x_limit] := 0.102
KICKER2B[y_limit] := 0.056
KICKER2B[aperture_at] = continuous

KICKER3[x_limit]  := 0.102
KICKER3[y_limit]  := 0.056
KICKER3[aperture_at] = continuous

freelimit=0.045
bfree[x_limit] := freelimit
bfree[y_limit] := freelimit

cfree[x_limit] := freelimit
cfree[y_limit] := freelimit

dfree[x_limit] := freelimit
dfree[y_limit] := freelimit

efree[x_limit] := freelimit
efree[y_limit] := freelimit

ffree[x_limit] := freelimit
ffree[y_limit] := freelimit

QUAD_SHORT[x_limit] := freelimit
QUAD_LONG[y_limit]  := freelimit

inf_x_ap = 0.009
inf_y_ap = 0.028

!MARK_CRYO_US[x_limit] := inf_x_ap
!MARK_CRYO_US[y_limit] := inf_y_ap      

MARK_INFLECTOR_US[x_limit]:=inf_x_ap
MARK_INFLECTOR_US[y_limit]:=inf_y_ap

MARK_INFLECTOR_DS[x_limit]:=inf_x_ap
MARK_INFLECTOR_DS[y_limit]:=inf_y_ap

!call, file = x_injection.lat
dr: drift, l=0.00001

backleg_start: marker
backhole_x_ap = 0.04
backhole_y_ap = 0.05
backleg_start[x_limit]:= backhole_x_ap
backleg_start[y_limit]:= backhole_y_ap


backleg:  em_field, descrip = 'injec_fld.dat', field_calc = custom, l=2.480114, & 
          tracking_method = runge_kutta, num_steps = 100 , mat6_calc_method = tracking, ds_step=0.0001

cryo_us:  em_field, descrip = 'injec_fld.dat', field_calc = custom, l=0.119886, & 
          tracking_method = runge_kutta, num_steps = 100 , mat6_calc_method = tracking, ds_step=0.0001

inf:      em_field, descrip = 'injec_fld.dat', field_calc = custom, l=1.7000,    &
          tracking_method = runge_kutta, num_steps = 1000 , mat6_calc_method = tracking, ds_step=0.001
infa:      em_field, descrip = 'injec_fld.dat', field_calc = custom, l=1.5000,    &
          tracking_method = runge_kutta, num_steps = 1000 , mat6_calc_method = tracking, ds_step=0.001
infb:      em_field, descrip = 'injec_fld.dat', field_calc = custom, l=0.200,    &
          tracking_method = runge_kutta, num_steps = 1000 , mat6_calc_method = tracking, ds_step=0.001

inf_exit: em_field, descrip = 'injec_fld.dat', field_calc = custom, l=0.095,    &
          tracking_method = runge_kutta, num_steps = 1000 , mat6_calc_method = tracking, ds_step=0.001

inject: line=(backleg_start,backleg, mark_cryo_us, cryo_us, mark_inflector_us, infa,infb,mark_inflector_ds)

!*********************************************
! g-2 STORAGE RING
!*********************************************

!RING: LINE = (afree,collimator, QUAD1, bfree,collimator, KICKERS, cfree,collimator, QUAD, dfree,collimator, QUAD, efree, QUAD, ffree)
RING: LINE = (afree, QUAD1, bfree, KICKERS, cfree, QUAD, dfree, QUAD, efree, QUAD, ffree)

inflector: drift, l=0.017 !l=1.7
!inj: line =(inflector, inj_to_ring, ring_br)
!inj: line =(injinf, inj_to_ring, ring_br)
inj: line = (inject, inj_to_ring, ring_br)
inj_to_ring: patch, x_offset=-0.077  
ring_br: fork, to_line = ring

!use, RING
use, inj