!+
! Subroutine track1_custom (start_orb, ele, param, end_orb, track, err_flag)
!
! For scattering in various markers 
!
! Note: This routine is not to be confused with track1_custom2.
! See the Bmad manual for more details.
!
! General rule: Your code may NOT modify any argument that is not listed as
! an output agument below."
!
! Modules Needed:
!   use bmad
!
! Input:
!   start_orb  -- Coord_struct: Starting position.
!   ele    -- Ele_struct: Element.
!   param  -- lat_param_struct: Lattice parameters.
!
! Output:
!   end_orb   -- Coord_struct: End position.
!   track     -- track_struct, optional: Structure holding the track information if the 
!                 tracking method does tracking step-by-step.
!   err_flag  -- Logical: Set true if there is an error. False otherwise.
!-

subroutine track1_custom (start_orb, ele, param, end_orb, track, err_flag)

use bmad_interface !, except_dummy => track1_custom
use materials_mod
use parameters_bmad   !includes eloss, us_scatter, ds_scatter, tilt

implicit none

type (coord_struct) :: start_orb
type (coord_struct) :: end_orb
type (coord_struct) temp_orb, delta,temp_orb2
type (ele_struct) :: ele
type (lat_param_struct) :: param
type (track_struct), optional :: track
logical err_flag
logical energy_loss
logical withinInflectorAperture
logical first/.true./
logical first1/.true./,first2/.true./,first3/.true./
real(rp) limit_in/6.905/,limit_out/7.010/ !radial limits for the trackers
real(rp) radius/7.112/                    !radius of the ring
real(rp) apRadius/0.045/                  !raidus of apeture

character(32) :: r_name = 'track1_custom'

integer lun

!

!call out_io (s_fatal$, r_name, 'THIS DUMMY ROUTINE SHOULD NOT HAVE BEEN CALLED IN THE FIRST PLACE.')
err_flag = .false.

! Remember to also set end_orb%t

withinInflectorAperture = .true.
energy_loss = eloss 
temp_orb = start_orb

if(first)then
  write(51,'(a)')'Change in coordinate vector due to upstream inflector scattering'
  write(51,'(6a12)')'x','xp','y','yp','z','zp'
  write(52,'(a)')'Change in coordinate vector due to downstream inflector scattering'
  write(52,'(6a12)')'x','xp','y','yp','z','zp'
endif
!print '(a,1x,2(a,2es12.4),2es12.4)',ele%name,'start_orb = ',start_orb%vec(1:2), ' end_orb = ', end_orb%vec(1:2), start_orb%s, ele%s
if(ele%name == 'MARK_CRYO_US')    call scatter( Al,                0.001_rp, temp_orb)
if(ele%name == 'MARK_INFLECTOR_US')then
     withinInflectorAperture = .false.
!     print '(a2,1x,6es12.4)','bs',temp_orb%vec(1:6)
     call inflector_scatter1(temp_orb, .true., .false., energy_loss)
     delta%vec = temp_orb%vec - start_orb%vec
     write(51,'(6es12.4)')delta%vec(1:6)
!     print '(a2,1x,6es12.4)','us',temp_orb%vec(1:6),delta%vec(1:6)
    call E821InflectorAperture(temp_orb%vec(1),temp_orb%vec(3),withinInflectorAperture,.true.,.false.)
    if(.not. withinInflectorAperture)temp_orb%state=lost$
endif
if(ele%name == 'MARK_INFLECTOR_DS')then
    withinInflectorAperture = .false.
    call inflector_scatter1(temp_orb, .false., .true., energy_loss)
     delta%vec = temp_orb%vec - start_orb%vec
     write(52,'(6es12.4)')delta%vec(1:6)
!     print '(a2,1x,6es12.4)','ds',delta%vec(1:6)
    call E821InflectorAperture(temp_orb%vec(1),temp_orb%vec(3),withinInflectorAperture, .false.,.true.)
    if(.not. withinInflectorAperture)temp_orb%state = lost$
endif

!include electron tracking

if (start_orb%species == electron$) then
if (limit_in-radius <= start_orb%vec(1) .and. limit_out-radius >= start_orb%vec(1) &
    .and. -apRadius <= start_orb%vec(3) .and. apRadius >=  start_orb%vec(3)) then     !electron goes into the tracker

write(54,'(a20)') ele%name
if (ele%name == 'TRACKER1') then

if (first1) then
write(61,'(a)') 'Electron Information for tracker1'
write(61,'(10a12)') 'time','s','e_x','e_px','e_y','e_py','e_z','e_pz' 
first1 = .false.
end if

write(61,'(8es12.4)') start_orb%t,start_orb%s,start_orb%vec 
temp_orb%state = lost$

end if

if (ele%name == 'TRACKER2') then

if (first2) then
write(62,'(a)') 'Electron Information for tracker2'
write(62,'(10a12)') 'time','s','e_x','e_px','e_y','e_py','e_z','e_pz' 
first2 = .false.
end if

write(62,'(8es12.4)') start_orb%t,start_orb%s,start_orb%vec 
temp_orb%state = lost$

end if

if (ele%name == 'TRACKER3') then

if (first3) then
write(63,'(a)') 'Electron Information for tracker3'
write(63,'(10a12)') 'time','s','e_x','e_px','e_y','e_py','e_z','e_pz' 
first3 = .false.
end if

write(63,'(8es12.4)') start_orb%t,start_orb%s,start_orb%vec 
temp_orb%state = lost$

end if

end if !if goes into the tracker

end if !if it is an electron


end_orb = temp_orb
end_orb%s = ele%s 
!if(end_orb%state == lost$)print '(a12,1x,a16,7es12.4)','Lost in',ele%name,end_orb%s,end_orb%vec(1:6)




if(first)then
 lun=lunget()
 open (unit=lun, file = "log.dat",access='append')
 write(lun, '(a)')'TRACK1_CUSTOM: Scattering in inflector ends'
 write(lun, '(1x,a15,l)')' energy_loss = ', energy_loss
 write(lun,'(1x,a16)')ele%name
 first=.false.
 close(unit=lun)
endif
!end_orb = start_orb
!end_orb%s = ele%s
end subroutine

