Files
sf-op/script/RFscan/GunScan.py
2017-06-13 14:30:43 +02:00

106 lines
3.5 KiB
Python

import ch.psi.pshell.epics.Positioner as Positioner
import ch.psi.pshell.epics.Camtool as Camtool
#Parameters
dry_run = False
do_elog = True
camera_name = "SINBD01-DSCR010"
if get_exec_pars().source == CommandSource.ui:
start = 45.0
stop = 55.0
step = 1.0
nb = 3
lat = 0.3
disp = -0.32
energy0 = 7.1
plt = plot(None, title="Output")[0]
else:
start = args[0]
stop = args[1]
step = args[2]
nb = int(args[3])
lat = args[4]
disp = args[5]
energy0 = args[6]
plt = args[7]
#Plot setup
plt.clear()
plt.setStyle(plt.Style.ErrorY)
plt.addSeries(LinePlotErrorSeries("Energy"))
plt.addSeries(LinePlotErrorSeries("Energy Spread", None, 2))
plt.getAxis(plt.AxisId.X).setLabel("Gun Phase")
plt.getAxis(plt.AxisId.Y).setLabel("Energy (MeV)")
plt.getAxis(plt.AxisId.Y2).setLabel("Energy Spread (MeV)")
plt.setLegendVisible(True)
#Creating Phase positioner
if dry_run:
phase = Positioner("Phase", "SINEG01-RSYS:SET-BEAM-PHASE-SIM", "SINEG01-RSYS:SET-BEAM-PHASE-SIM")
#phase = DummyPositioner("Phase")
camera_name = "SLG-LCAM-C041"
do_elog = False
else:
phase = Positioner("Phase", "SINEG01-RSYS:SET-BEAM-PHASE", "SINEG01-RSYS:GET-BEAM-PHASE")
phase.config.minValue = -180.0
phase.config.maxValue = 180.0
phase.config.precision = 3
phase.config.resolution = 1.0
phase.config.rotation = True
phase.config.save()
phase.initialize()
phase0 = phase.read()
#Camtool setup
#kill_camtool()
check_camtool()
camtool.start(camera_name)
wait_camtool_message()
x = camtool.stream.getChild("x_fit_mean")
dx = camtool.stream.getChild("x_fit_standard_deviation")
#Creating averagers
xb = create_averager(x, nb, -1) # -1 event based, waits for the next value
dxb = create_averager(dx, nb, -1)
dxb.monitored=True # not blocking, will return last nb values
#Record callback: uptate of output plot
def after_sample(record, scan):
global energy0, disp
x_fit_mean, x_fit_mean_sigma, = record.values[0].mean, record.values[0].stdev
x_fit_std, x_fit_std_sigma = record.values[1].mean, record.values[1].stdev
E_mean, E_std = energy0 * (1 + x_fit_mean / 1e6 / disp), abs(energy0 * (x_fit_mean_sigma / 1e6 / disp))
dE_mean, dE_std = abs(energy0 * (x_fit_std / 1e6 / disp)), abs(energy0 * (x_fit_std_sigma / 1e6 / disp))
plt.getSeries(0).appendData(record.positions[0], E_mean, E_std)
plt.getSeries(1).appendData(record.positions[0], dE_mean, dE_std)
#The scan loop
try:
r = lscan(phase, [xb, dxb], start, stop, step , latency=lat, after_read = after_sample)
finally:
phase.write(phase0)
phase.close()
camtool.stop() # stops camtool but does not close it camtool is a global object
#Saving metadata
E = [energy0 * (1 + val.mean / 1e6 / disp) for val in r.getReadable(0)]
dE = [abs(energy0 * (val.mean / 1e6 / disp)) for val in r.getReadable(1)]
save_dataset(get_exec_pars().group + "/E", E)
save_dataset(get_exec_pars().group + "/dE", dE)
if do_elog:
if get_option("Generated data file:\n" + get_exec_pars().path +"\n\n" + "Save to ELOG?", "YesNo") == "Yes":
log_msg = "Data file: " + get_exec_pars().path
#log_msg = log_msg + "\n\n" + r.print()
sleep(0.1) #Give some time to plot to be finished - it is not sync with acquisition
file_name = os.path.abspath(get_context().setup.getContextPath() + "/GunScanPlot.png")
plt.saveSnapshot(file_name , "png")
elog("Gun scan", log_msg, [file_name,])