parameter[lattice]                = g_2
parameter[particle]               = antimuon
parameter[p0c]                    = 3.094350E9
parameter[absolute_time_tracking] = .true.
parameter[aperture_limit_on]      = .true.
parameter[ran_seed]               = 123
parameter[custom_attribute2]      = "sbend::field_index"
parameter[custom_attribute1]      = "sbend::kick_width"

!*********************************************
! 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 ! Gap from injection point to first electric quadrupole
BFREE: FULLRING, angle = ThetaK1Start - ThetaQ1End
CFREE: FULLRING, angle = ThetaQ2Start - ThetaK3End
DFREE: FULLRING, angle = ThetaQ3Start - ThetaQ2End
EFREE: FULLRING, angle = ThetaQ4Start - ThetaQ3End
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)

! Kickers
KICKER1:         FULLRING, angle = dThetaK1,    hkick = 0.022, kick_width = 100.e-9
GAPK12:          FULLRING, angle = dThetaGapK12
KICKER2A:        FULLRING, angle = dThetaK2/2,  hkick = 0.022, kick_width = 100.e-9
MARK_CENTERLINE: FULLRING, angle = 0
KICKER2B:        FULLRING, angle = dThetaK2/2,  hkick = 0.022, kick_width = 100.e-9
GAPK23:          FULLRING, angle = dThetaGapK23
KICKER3:         FULLRING, angle = dThetaK3,    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.102 ! vac chmbr. better?
KICKER1[y_limit]  := 0.045
KICKER1[aperture_at] = continuous

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

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

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

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

cfree[x_limit] := 0.045
cfree[y_limit] := 0.045

dfree[x_limit] := 0.045
dfree[y_limit] := 0.045

efree[x_limit] := 0.045
efree[y_limit] := 0.045

ffree[x_limit] := 0.045
ffree[y_limit] := 0.045

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



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

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_to_ring: patch, x_offset=-0.077  
ring_br: fork, to_line = ring

!use, RING
use, inj