Files
sf-op/script/RFscan/SchottkyScan.py
gobbo_a 8beeb7dbc7
2023-08-17 15:16:51 +02:00

134 lines
5.1 KiB
Python

import ch.psi.pshell.epics.Positioner as Positioner
import ch.psi.pshell.epics.ChannelDouble as ChannelDouble
dry_run = False
do_elog = True
is_panel = get_exec_pars().source != CommandSource.ui #must be check before run
if is_panel:
start = args[0]
stop = args[1]
step = args[2]
nb = int(args[3])
lat = args[4]
plt = args[5]
bunch = args[6]
else:
start = -10.0
stop = 150.0
step = 5.0
nb = 1
lat = 0.110 # lat is applied after each step (but not between samples)
plt = plot(None, title="Output")[0]
bunch = 1
#Plot setup
plt.clear()
plt.removeMarker(None)
plt.setStyle(plt.Style.ErrorY)
plt.addSeries(LinePlotErrorSeries("Charge", Color.red))
plt.getAxis(plt.AxisId.X).setLabel("Gun Beam Phase (deg)")
plt.getAxis(plt.AxisId.Y).setLabel("Bunch Charge (pC)")
plt.setLegendVisible(True)
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")
q = ChannelDouble("Charge", "SINEG01-DICT215:B" + str(bunch) + "_CHARGE")
q.initialize()
q.monitored=True
bph.config.minValue = -90.0
bph.config.maxValue = 360.0
bph.config.precision = 4 # nb of digit to display
bph.config.resolution = 0.5 # set and get have to be within resolution
bph.config.rotation = True # will go back to zero if > 360 deg
bph.config.save()
bph.initialize()
bph.update()
bph0 = bph.read() % 360.0
rph.initialize()
rph.monitored=True
bph0 = bph.read()
#Creating averagers
rph_averager = create_averager(rph, nb, 0.1)
q_averager = create_averager(q, nb, 0.1)
q_averager.monitored=True
#Record callback: uptate of output plot
def after_sample(record, scan):
plt.getSeries(0).appendData((record.positions[0] + 90) % 360 - 90, record.readables[1].mean, record.readables[1].stdev)
#The scan loop
try:
bph.write(start)
time.sleep(1.0)
r = lscan(bph, (rph_averager, q_averager), start, stop, step, latency=lat, after_read = after_sample)
finally:
bph.write(bph0)
bph.close()
rph.close()
q.close()
beamphase = [(val + 90) % 360 - 90 for val in r.getPositions(0)]
rfphase = [(val.mean + 90) % 360 - 90 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)]
#Fitting and plotting
i, a, b = 0, 0, 0
while charge[i] < (max(charge) * 0.20) : a = i; i = i + 1
while charge[i] < (max(charge) * 0.60) : b = i; i = i + 1
x = beamphase[a:b+1]
y = charge[a:b+1]
p = (a0, a1) = fit_polynomial(y, x, 1)
f = PolynomialFunction(p)
x1 = -a0 / a1 if a1 != 0 else 0.0
x2 = beamphase[b+1]
x_fit = frange(x1, x2, (x2-x1), True)
y_fit = [f.value(val) for val in x_fit]
plt.addSeries(LinePlotErrorSeries("Fit", Color.green))
plt.getSeries(1).setData(to_array(x_fit, 'd'), y_fit, [0.0] * len(x_fit))
plt.getSeries(1).setPointsVisible(False)
plt.addMarker(x1, plt.AxisId.X, "%0.2f" % x1, Color.green)
#Setting the return value
bph_ref_guess = x1
set_return([bph_ref_guess])
#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("SLG-LGEN:WHICH_LASER"))
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("SLAUV01-LATN100:MOT.RBV") + " deg \n"
elif Laser == "Jaguar":
log_msg = log_msg + "Energy plate: %0.2f" % caget("SLJUV01-LATN100:MOT.RBV") + " deg \n"
if caget("SLAUV01-LAPT100:IRIS-POS-GET") == "IRIS":
log_msg = log_msg + "Collimator Alcor: IRIS %0.2f" % caget("SLAUV01-LAPT100:SIZE-GET") + " mm \n"
else:
log_msg = log_msg + "Collimator Alcor: " + str(caget("SLAUV01-LAPT100:IRIS-POS-GET")) + "\n"
if caget("SLMUV01-LAPT100:IRIS-POS-GET") == "IRIS":
log_msg = log_msg + "Collimator Mizar: IRIS %0.2f" % caget("SLMUV01-LAPT100:SIZE-GET") + " mm \n"
else:
log_msg = log_msg + "Collimator Mizar: " + str(caget("SLMUV01-LAPT100:IRIS-POS-GET")) + "\n"
log_msg = log_msg + "Charge: %0.2f" % caget("SINEG01-DICT215:B1_CHARGE-OP") + " pC at %0.2f" % bph0 + " deg beam phase\n"
log_msg = log_msg + "Charge onset: %0.2f" % x1 + " deg \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)