!!!  Energy Measurement at exit of rf photoinjector
assign,print=TERM

E_MeV   :=5.0
HR      :=E_MeV/0.3

Is_A    :=116.3
Ik_A    :=-0.6

KSc     :=213.8/150*Is_A/HR
VKc     :=0.0447*Ik_A/HR
Xso     :=20.0
Xst     :=22.1
!!!!!!!!!!!!!!!!!  Solenoid coil  !!!!!!!!!!!!!!!!!!!!!!!!
xSTilt:=0.0
s01: solenoid, l=0.01, Ks=KSc*exp(-((01.0-Xso)/10.7)^4)
s02: solenoid, l=0.01, Ks=KSc*exp(-((02.0-Xso)/10.7)^4)
s03: solenoid, l=0.01, Ks=KSc*exp(-((03.0-Xso)/10.7)^4)
s04: solenoid, l=0.01, Ks=KSc*exp(-((04.0-Xso)/10.7)^4)
s05: solenoid, l=0.01, Ks=KSc*exp(-((05.0-Xso)/10.7)^4)
s06: solenoid, l=0.01, Ks=KSc*exp(-((06.0-Xso)/10.7)^4)
s07: solenoid, l=0.01, Ks=KSc*exp(-((07.0-Xso)/10.7)^4)
s08: solenoid, l=0.01, Ks=KSc*exp(-((08.0-Xso)/10.7)^4)
s09: solenoid, l=0.01, Ks=KSc*exp(-((09.0-Xso)/10.7)^4)
s10: solenoid, l=0.01, Ks=KSc*exp(-((10.0-Xso)/10.7)^4)
s11: solenoid, l=0.01, Ks=KSc*exp(-((11.0-Xso)/10.7)^4)
s12: solenoid, l=0.01, Ks=KSc*exp(-((12.0-Xso)/10.7)^4)
s13: solenoid, l=0.01, Ks=KSc*exp(-((13.0-Xso)/10.7)^4)
s14: solenoid, l=0.01, Ks=KSc*exp(-((14.0-Xso)/10.7)^4)
s15: solenoid, l=0.01, Ks=KSc*exp(-((15.0-Xso)/10.7)^4)
s16: solenoid, l=0.01, Ks=KSc*exp(-((16.0-Xso)/10.7)^4)
s17: solenoid, l=0.01, Ks=KSc*exp(-((17.0-Xso)/10.7)^4)
s18: solenoid, l=0.01, Ks=KSc*exp(-((18.0-Xso)/10.7)^4)
s19: solenoid, l=0.01, Ks=KSc*exp(-((19.0-Xso)/10.7)^4)
s20: solenoid, l=0.01, Ks=KSc*exp(-((20.0-Xso)/10.7)^4)
s21: solenoid, l=0.01, Ks=KSc*exp(-((21.0-Xso)/10.7)^4)
s22: solenoid, l=0.01, Ks=KSc*exp(-((22.0-Xso)/10.7)^4)
s23: solenoid, l=0.01, Ks=KSc*exp(-((23.0-Xso)/10.7)^4)
s24: solenoid, l=0.01, Ks=KSc*exp(-((24.0-Xso)/10.7)^4)
s25: solenoid, l=0.01, Ks=KSc*exp(-((25.0-Xso)/10.7)^4)
s26: solenoid, l=0.01, Ks=KSc*exp(-((26.0-Xso)/10.7)^4)
s27: solenoid, l=0.01, Ks=KSc*exp(-((27.0-Xso)/10.7)^4)
s28: solenoid, l=0.01, Ks=KSc*exp(-((28.0-Xso)/10.7)^4)
s29: solenoid, l=0.01, Ks=KSc*exp(-((29.0-Xso)/10.7)^4)
s30: solenoid, l=0.01, Ks=KSc*exp(-((30.0-Xso)/10.7)^4)
s31: solenoid, l=0.01, Ks=KSc*exp(-((31.0-Xso)/10.7)^4)
s32: solenoid, l=0.01, Ks=KSc*exp(-((32.0-Xso)/10.7)^4)
s33: solenoid, l=0.01, Ks=KSc*exp(-((33.0-Xso)/10.7)^4)
s34: solenoid, l=0.01, Ks=KSc*exp(-((34.0-Xso)/10.7)^4)
s35: solenoid, l=0.01, Ks=KSc*exp(-((35.0-Xso)/10.7)^4)
s36: solenoid, l=0.01, Ks=KSc*exp(-((36.0-Xso)/10.7)^4)
s37: solenoid, l=0.01, Ks=KSc*exp(-((37.0-Xso)/10.7)^4)
s38: solenoid, l=0.01, Ks=KSc*exp(-((38.0-Xso)/10.7)^4)
s39: solenoid, l=0.01, Ks=KSc*exp(-((39.0-Xso)/10.7)^4)
s40: solenoid, l=0.01, Ks=KSc*exp(-((40.0-Xso)/10.7)^4)
!!!!!!!!!!!!!  Solenoid and trim coils  !!!!!!!!!!!!!!!
VKc01:=VKc*(exp(-((01.0-Xst)/6.3)^4)+0.3*exp(-((01.0-Xst)/6.3)^2))/1.3
k01:   kicker, l=0.00, HKICK=VKc01*COS(XSTILT), VKICK=VKc01*SIN(XSTILT)
VKc02:=VKc*(exp(-((02.0-Xst)/6.3)^4)+0.3*exp(-((02.0-Xst)/6.3)^2))/1.3
k02:   kicker, l=0.00, HKICK=VKc02*COS(XSTILT), VKICK=VKc02*SIN(XSTILT)
VKc03:=VKc*(exp(-((03.0-Xst)/6.3)^4)+0.3*exp(-((03.0-Xst)/6.3)^2))/1.3
k03:   kicker, l=0.00, HKICK=VKc03*COS(XSTILT), VKICK=VKc03*SIN(XSTILT)
VKc04:=VKc*(exp(-((04.0-Xst)/6.3)^4)+0.3*exp(-((04.0-Xst)/6.3)^2))/1.3
k04:   kicker, l=0.00, HKICK=VKc04*COS(XSTILT), VKICK=VKc04*SIN(XSTILT)
VKc05:=VKc*(exp(-((05.0-Xst)/6.3)^4)+0.3*exp(-((05.0-Xst)/6.3)^2))/1.3
k05:   kicker, l=0.00, HKICK=VKc05*COS(XSTILT), VKICK=VKc05*SIN(XSTILT)
VKc06:=VKc*(exp(-((06.0-Xst)/6.3)^4)+0.3*exp(-((06.0-Xst)/6.3)^2))/1.3
k06:   kicker, l=0.00, HKICK=VKc06*COS(XSTILT), VKICK=VKc06*SIN(XSTILT)
VKc07:=VKc*(exp(-((07.0-Xst)/6.3)^4)+0.3*exp(-((07.0-Xst)/6.3)^2))/1.3
k07:   kicker, l=0.00, HKICK=VKc07*COS(XSTILT), VKICK=VKc07*SIN(XSTILT)
VKc08:=VKc*(exp(-((08.0-Xst)/6.3)^4)+0.3*exp(-((08.0-Xst)/6.3)^2))/1.3
k08:   kicker, l=0.00, HKICK=VKc08*COS(XSTILT), VKICK=VKc08*SIN(XSTILT)
VKc09:=VKc*(exp(-((09.0-Xst)/6.3)^4)+0.3*exp(-((09.0-Xst)/6.3)^2))/1.3
k09:   kicker, l=0.00, HKICK=VKc09*COS(XSTILT), VKICK=VKc09*SIN(XSTILT)
VKc10:=VKc*(exp(-((10.0-Xst)/6.3)^4)+0.3*exp(-((10.0-Xst)/6.3)^2))/1.3
k10:  kicker, l=0.00, HKICK=VKc10*COS(XSTILT), VKICK=VKc10*SIN(XSTILT)

VKc11:=VKc*(exp(-((11.0-Xst)/6.3)^4)+0.3*exp(-((11.0-Xst)/6.3)^2))/1.3
k11:   kicker, l=0.00, HKICK=VKc11*COS(XSTILT), VKICK=VKc11*SIN(XSTILT)
VKc12:=VKc*(exp(-((12.0-Xst)/6.3)^4)+0.3*exp(-((12.0-Xst)/6.3)^2))/1.3
k12:   kicker, l=0.00, HKICK=VKc12*COS(XSTILT), VKICK=VKc12*SIN(XSTILT)
VKc13:=VKc*(exp(-((13.0-Xst)/6.3)^4)+0.3*exp(-((13.0-Xst)/6.3)^2))/1.3
k13:   kicker, l=0.00, HKICK=VKc13*COS(XSTILT), VKICK=VKc13*SIN(XSTILT)
VKc14:=VKc*(exp(-((14.0-Xst)/6.3)^4)+0.3*exp(-((14.0-Xst)/6.3)^2))/1.3
k14:   kicker, l=0.00, HKICK=VKc14*COS(XSTILT), VKICK=VKc14*SIN(XSTILT)
VKc15:=VKc*(exp(-((15.0-Xst)/6.3)^4)+0.3*exp(-((15.0-Xst)/6.3)^2))/1.3
k15:   kicker, l=0.00, HKICK=VKc15*COS(XSTILT), VKICK=VKc15*SIN(XSTILT)
VKc16:=VKc*(exp(-((16.0-Xst)/6.3)^4)+0.3*exp(-((16.0-Xst)/6.3)^2))/1.3
k16:   kicker, l=0.00, HKICK=VKc16*COS(XSTILT), VKICK=VKc16*SIN(XSTILT)
VKc17:=VKc*(exp(-((17.0-Xst)/6.3)^4)+0.3*exp(-((17.0-Xst)/6.3)^2))/1.3
k17:   kicker, l=0.00, HKICK=VKc17*COS(XSTILT), VKICK=VKc17*SIN(XSTILT)
VKc18:=VKc*(exp(-((18.0-Xst)/6.3)^4)+0.3*exp(-((18.0-Xst)/6.3)^2))/1.3
k18:   kicker, l=0.00, HKICK=VKc18*COS(XSTILT), VKICK=VKc18*SIN(XSTILT)
VKc19:=VKc*(exp(-((19.0-Xst)/6.3)^4)+0.3*exp(-((19.0-Xst)/6.3)^2))/1.3
k19:   kicker, l=0.00, HKICK=VKc19*COS(XSTILT), VKICK=VKc19*SIN(XSTILT)
VKc20:=VKc*(exp(-((20.0-Xst)/6.3)^4)+0.3*exp(-((20.0-Xst)/6.3)^2))/1.3
k20:  kicker, l=0.00, HKICK=VKc20*COS(XSTILT), VKICK=VKc20*SIN(XSTILT)

VKc21:=VKc*(exp(-((21.0-Xst)/6.3)^4)+0.3*exp(-((21.0-Xst)/6.3)^2))/1.3
k21:   kicker, l=0.00, HKICK=VKc21*COS(XSTILT), VKICK=VKc21*SIN(XSTILT)
VKc22:=VKc*(exp(-((22.0-Xst)/6.3)^4)+0.3*exp(-((22.0-Xst)/6.3)^2))/1.3
k22:   kicker, l=0.00, HKICK=VKc22*COS(XSTILT), VKICK=VKc22*SIN(XSTILT)
VKc23:=VKc*(exp(-((23.0-Xst)/6.3)^4)+0.3*exp(-((23.0-Xst)/6.3)^2))/1.3
k23:   kicker, l=0.00, HKICK=VKc23*COS(XSTILT), VKICK=VKc23*SIN(XSTILT)
VKc24:=VKc*(exp(-((24.0-Xst)/6.3)^4)+0.3*exp(-((24.0-Xst)/6.3)^2))/1.3
k24:   kicker, l=0.00, HKICK=VKc24*COS(XSTILT), VKICK=VKc24*SIN(XSTILT)
VKc25:=VKc*(exp(-((25.0-Xst)/6.3)^4)+0.3*exp(-((25.0-Xst)/6.3)^2))/1.3
k25:   kicker, l=0.00, HKICK=VKc25*COS(XSTILT), VKICK=VKc25*SIN(XSTILT)
VKc26:=VKc*(exp(-((26.0-Xst)/6.3)^4)+0.3*exp(-((26.0-Xst)/6.3)^2))/1.3
k26:   kicker, l=0.00, HKICK=VKc26*COS(XSTILT), VKICK=VKc26*SIN(XSTILT)
VKc27:=VKc*(exp(-((27.0-Xst)/6.3)^4)+0.3*exp(-((27.0-Xst)/6.3)^2))/1.3
k27:   kicker, l=0.00, HKICK=VKc27*COS(XSTILT), VKICK=VKc27*SIN(XSTILT)
VKc28:=VKc*(exp(-((28.0-Xst)/6.3)^4)+0.3*exp(-((28.0-Xst)/6.3)^2))/1.3
k28:   kicker, l=0.00, HKICK=VKc28*COS(XSTILT), VKICK=VKc28*SIN(XSTILT)
VKc29:=VKc*(exp(-((29.0-Xst)/6.3)^4)+0.3*exp(-((29.0-Xst)/6.3)^2))/1.3
k29:   kicker, l=0.00, HKICK=VKc29*COS(XSTILT), VKICK=VKc29*SIN(XSTILT)
VKc30:=VKc*(exp(-((30.0-Xst)/6.3)^4)+0.3*exp(-((30.0-Xst)/6.3)^2))/1.3
k30:  kicker, l=0.00, HKICK=VKc30*COS(XSTILT), VKICK=VKc30*SIN(XSTILT)



VKc31:=VKc*(exp(-((31.0-Xst)/6.3)^4)+0.3*exp(-((31.0-Xst)/6.3)^2))/1.3
k31:   kicker, l=0.00, HKICK=VKc31*COS(XSTILT), VKICK=VKc31*SIN(XSTILT)
VKc32:=VKc*(exp(-((32.0-Xst)/6.3)^4)+0.3*exp(-((32.0-Xst)/6.3)^2))/1.3
k32:   kicker, l=0.00, HKICK=VKc32*COS(XSTILT), VKICK=VKc32*SIN(XSTILT)
VKc33:=VKc*(exp(-((33.0-Xst)/6.3)^4)+0.3*exp(-((33.0-Xst)/6.3)^2))/1.3
k33:   kicker, l=0.00, HKICK=VKc33*COS(XSTILT), VKICK=VKc33*SIN(XSTILT)
VKc34:=VKc*(exp(-((34.0-Xst)/6.3)^4)+0.3*exp(-((34.0-Xst)/6.3)^2))/1.3
k34:   kicker, l=0.00, HKICK=VKc34*COS(XSTILT), VKICK=VKc34*SIN(XSTILT)
VKc35:=VKc*(exp(-((35.0-Xst)/6.3)^4)+0.3*exp(-((35.0-Xst)/6.3)^2))/1.3
k35:   kicker, l=0.00, HKICK=VKc35*COS(XSTILT), VKICK=VKc35*SIN(XSTILT)
VKc36:=VKc*(exp(-((36.0-Xst)/6.3)^4)+0.3*exp(-((36.0-Xst)/6.3)^2))/1.3
k36:   kicker, l=0.00, HKICK=VKc36*COS(XSTILT), VKICK=VKc36*SIN(XSTILT)
VKc37:=VKc*(exp(-((37.0-Xst)/6.3)^4)+0.3*exp(-((37.0-Xst)/6.3)^2))/1.3
k37:   kicker, l=0.00, HKICK=VKc37*COS(XSTILT), VKICK=VKc37*SIN(XSTILT)
VKc38:=VKc*(exp(-((38.0-Xst)/6.3)^4)+0.3*exp(-((38.0-Xst)/6.3)^2))/1.3
k38:   kicker, l=0.00, HKICK=VKc38*COS(XSTILT), VKICK=VKc38*SIN(XSTILT)
VKc39:=VKc*(exp(-((39.0-Xst)/6.3)^4)+0.3*exp(-((39.0-Xst)/6.3)^2))/1.3
k39:   kicker, l=0.00, HKICK=VKc39*COS(XSTILT), VKICK=VKc39*SIN(XSTILT)
VKc40:=VKc*(exp(-((40.0-Xst)/6.3)^4)+0.3*exp(-((40.0-Xst)/6.3)^2))/1.3
k40:  kicker, l=0.00, HKICK=VKc40*COS(XSTILT), VKICK=VKc40*SIN(XSTILT)

sol_st: line=( s01,k01, s02,k02, s03,k03, s04,k04, s05,k05,  &
               s06,k06, s07,k07, s08,k08, s09,k09, s10,k10,  &
               s11,k11, s12,k12, s13,k13, s14,k14, s15,k15,  &
               s16,k16, s17,k17, s18,k18, s19,k19, s20,k20,  &
               s21,k21, s22,k22, s23,k23, s24,k24, s25,k25,  &
               s26,k26, s27,k27, s28,k28, s29,k29, s30,k30,  &
               s31,k31, s32,k32, s33,k33, s34,k34, s35,k35,  &
               s36,k36, s37,k37, s38,k38, s39,k39, s40,k40)


d: drift, l=0.247

m:marker
Xstilt:=0.0
E_mev:=3.6
!!!!!!!!!!!!!! Currents of solenoid and trim coils !!!!!!!!!!!!!!!!!!
Is_A    :=142.5
Ik_A    :=0.6

use,period=(sol_st,d,m)

twiss,betx=10.0,bety=10.0, couple
match,betx=10.0,bety=10.0,x=0,y=0,orbit

vary,  e_mev,   Lower=2.0, Upper=7.0, step=0.0003
!!!!!!!!! Here it is assumed:                   !!!!!!!!!!!!!!!
!!!!!!!!! beam position w/o trim is (286, 248)  !!!!!!!!!!!!!!!
!!!!!!!!! beam position w   trim is (355, 161)  !!!!!!!!!!!!!!!
!!!!!!!!! calibration is (53.1, 57.7) um/pixel  !!!!!!!!!!!!!!!
!!!!!!!!! *change sign if necessary             !!!!!!!!!!!!!!!

constrain,  m ,   x=-1e-6*(286-355)*53.1,  y=-1e-6*(248-161)*57.7
   lmdif, calls=1000
endmatch