Files
sf-op/script/test/SchottkyScan_dv.py
2017-08-25 14:56:39 +02:00

132 lines
4.8 KiB
Python

import ch.psi.pshell.epics.Positioner as Positioner
import ch.psi.pshell.epics.ChannelDouble as ChannelDouble
dry_run = True
do_elog = False
is_panel = get_exec_pars().source != CommandSource.ui #must be check before run
run("CPython/wrapper")
if is_panel:
start = args[0]
stop = args[1]
step = args[2]
nb = int(args[3])
lat = args[4]
plt = args[5]
else:
start = -50.0
stop = 150.0
step = 2.0
nb = 2
lat = 0.120
plt = None
if plt is not None:
plt.clear()
plt.addSeries(LinePlotErrorSeries("Values"))
plt.getAxis(plt.AxisId.X).setLabel("Gun Beam Phase (deg)")
plt.getAxis(plt.AxisId.Y).setLabel("SINEG01-DICT215:B1_CHARGE")
if dry_run:
bph = Positioner("Beam phase", "SINEG01-RSYS:SET-BEAM-PHASE-SIM", "SINEG01-RSYS:SET-BEAM-PHASE-SIM")
rph = ChannelDouble("RF phase", "SINEG01-RSYS:SET-VSUM-PHASE-SIM")
q = ChannelDouble("Charge", "SINEG01-DICT215:B1_CHARGE-SIM")
q.initialize()
q.monitored=True
else:
bph = Positioner("Beam phase", "SINEG01-RSYS:SET-BEAM-PHASE", "SINEG01-RSYS:GET-BEAM-PHASE")
rph = ChannelDouble("RF phase", "SINEG01-RSYS:SET-VSUM-PHASE")
#st = Stream("ICTstream", dispatcher)
#q = st.addScalar("Charge", "SINEG01-DICT215:B1_CHARGE", 1, 0)
#st.initialize()
#st.start()
#st.waitValueChange(10000)
q = ChannelDouble("Charge", "SINEG01-DICT215:B1_CHARGE")
q.initialize()
q.monitored=True
bph.config.minValue = -360.0
bph.config.maxValue = 360.0
bph.config.precision = 3
bph.config.rotation = False
bph.config.resolution = 0.01
bph.config.save()
bph.initialize()
rph.initialize()
rph.monitored=True
bph0 = bph.read()
rph0 = rph.read()
#Record callback: uptate of output plot
def after_sample(record, scan):
if plt is not None:
plt.getSeries(0).appendData(record.positions[0], record.values[1].mean, record.values[1].stdev)
try:
rph_averager = create_averager(rph, nb, 0.1)
q_averager = create_averager(q, nb, 0.1)
q_averager.monitored=True
r = lscan(bph, (rph_averager, q_averager), start, stop, step, latency=lat, after_read = after_sample)
beamphase = r.getPositions(0)
rfphase = [val.mean for val in r.getReadable(0)]
rfphaserms = [val.stdev for val in r.getReadable(0)]
charge = [val.mean for val in r.getReadable(1)]
chargerms = [val.stdev for val in r.getReadable(1)]
finally:
rph.write(rph0)
bph.close()
rph.close()
q.close()
#st.close()
#Fitting and plotting
try:
i_max = charge.index(max(charge))
min_i = charge.index(max(charge) * 0.10)
max_i = charge.index(min(charge) * 0.60)
readable = charge[min_i:max_i]
positions = beamphase[min_i:max_i]
pars_polynomial = (a0, a1) = fit_polynomial(readable, positions, 1)
fitted_polynomial_function = PolynomialFunction(pars_polynomial)
plt.addSeries(LinePlotErrorSeries("Momentum Fit", plt.getSeries(0).color))
plt.addSeries(LinePlotErrorSeries("Momentum Spread Fit", plt.getSeries(1).color, 2))
plt.getSeries(2).setData(ph_p_fit, p_fit)
plt.getSeries(3).setData(ph_dp_fit, dp_fit)
plt.getSeries(2).setPointsVisible(False)
plt.getSeries(3).setPointsVisible(False)
plt.addMarker(ph_p_max, plt.AxisId.X, "%3.2f" % ph_p_max, plt.getSeries(0).color)
plt.addMarker(ph_dp_min, plt.AxisId.X, "%3.2f" % ph_dp_min, plt.getSeries(1).color)
except:
raise Exception("Fit failure")
#Elog entry
if do_elog:
if get_option("Generated data file:\n" + get_exec_pars().path +"\n\n" + "Save to ELOG?", "YesNo") == "Yes":
Laser = str(caget("SLGTV-LMTO-M055:MOT-KNOWN-POS"))
log_msg = "Data file: " + get_exec_pars().path + "\n\n"
log_msg = log_msg + "Laser: " + Laser + "\n"
if Laser == "Alcor":
log_msg = log_msg + "Energy plate: %0.2f" % caget("SLGTH01-LMRM-M074:MOT.RBV") + " deg \n"
else:
log_msg = log_msg + "Energy plate: %0.2f" % caget("SLGJG-LMRM-M031:MOT.RBV") + " deg \n"
if caget("SLGTV-LMTO-M053:MOT-ACT-POS") == "IRIS":
log_msg = log_msg + "Collimator: IRIS %0.2f" % caget("SLGTV-LAPP:SIZE-GET") + " mm \n"
else:
log_msg = log_msg + "Collimator: " + str(caget("SLGTV-LMTO-M053:MOT-ACT-POS")) + "\n"
log_msg = log_msg + "Charge: %0.2f" % caget("SINEG01-DICT215:B1_CHARGE-OP") + " pC at %0.2f" % bph0 + " deg beam phase\n"
attachments = []
if plt is not None:
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() + "/SchottkyScanPlot.png")
plt.saveSnapshot(file_name , "png")
attachments = [file_name]
elog("Schottky scan", log_msg, attachments)
#Setting the return value
bph_ref_guess = 0.0
set_return([bph_ref_guess])