This commit is contained in:
gobbo_a
2017-09-22 10:58:17 +02:00
parent 0c1d63ae9a
commit 5a7afde16f
40 changed files with 975 additions and 483 deletions
-7
View File
@@ -1,7 +0,0 @@
class AnalogInput(ReadonlyRegisterBase):
def doRead(self):
return cam_server.getValue("x_rms")
add_device(AnalogInput("tst"), True)
tst.setPolling(10)
-13
View File
@@ -1,13 +0,0 @@
c = {"reference_marker":[0, 0, 100, 100]}
camtool.setCalibration("SLG-LCAM-C021", c)
camtool.getGeometry("SINEG01-DSCR350")
for c in camtool.getCameras():
if c not in ["example", "simulation"]:
try:
print c , " - ", camtool.getGeometry(c)
except:
print c
time.sleep(0.2)
+7 -8
View File
@@ -56,13 +56,12 @@ 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")
#Camera setup
cam_server.start(camera_name)
wait_cam_server_message()
x = cam_server.stream.getChild("x_fit_mean")
dx = cam_server.stream.getChild("x_fit_standard_deviation")
#Creating averagers
x_averager = create_averager(x, nb, -1) # -1 event based, waits for the next value
@@ -87,7 +86,7 @@ try:
finally:
phase.write(phase0)
phase.close()
camtool.stop() # stops camtool but does not close it camtool is a global object
cam_server.stop() # stops cam_server but does not close it cam_server is a global object
ph = r.getPositions(0)
p = [A * val.mean + B for val in r.getReadable(0)]
+20 -24
View File
@@ -2,9 +2,8 @@ import ch.psi.pshell.epics.Positioner as Positioner
import ch.psi.pshell.epics.ChannelDouble as ChannelDouble
dry_run = True
do_elog = 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]
@@ -15,18 +14,18 @@ if is_panel:
else:
start = -50.0
stop = 150.0
step = 10.0
step = 1.0
nb = 2
lat = 0.120
lat = 0.010
plt = plot(None, title="Output")[0]
#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("SINEG01-DICT215:B1_CHARGE")
plt.getAxis(plt.AxisId.Y).setLabel("SINEG01-DICT215:B1_CHARGE (pC)")
plt.setLegendVisible(True)
if dry_run:
@@ -55,7 +54,6 @@ bph.config.save()
bph.initialize()
rph.initialize()
rph.monitored=True
bph0 = bph.read()
rph0 = rph.read()
@@ -77,7 +75,6 @@ finally:
rph.close()
q.close()
#st.close()
beamphase = r.getPositions(0)
rfphase = [val.mean for val in r.getReadable(0)]
rfphaserms = [val.stdev for val in r.getReadable(0)]
@@ -85,22 +82,21 @@ charge = [val.mean for val in r.getReadable(1)]
chargerms = [val.stdev for val in r.getReadable(1)]
#Fitting and plotting
#try:
i, i_min, i_max = 0, 0, 0
while charge[i] < max(charge) * 0.10 : i_min = i; i = i + 1
while charge[i] < max(charge) * 0.60 : i_max = i; i = i + 1
readable = charge[i_min:i_max]
positions = beamphase[i_min:i_max]
pars_polynomial = (a0, a1) = fit_polynomial(readable, positions, 1)
fitted_polynomial_function = PolynomialFunction(pars_polynomial)
charge_fit = []
for x in beamphase: charge_fit.append(fitted_polynomial_function.value(x))
plt.addSeries(LinePlotSeries("Fit", plt.getSeries(0).color))
plt.getSeries(1).setData(beamphase, charge_fit)
i, a, b = 0, 0, 0
while charge[i] < (max(charge) * 0.20) : a = i; i = i + 1
while charge[i] < (max(charge) * 0.80) : 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[charge.index(max(charge))]
x_fit = frange(x1, x2, (x2-x1), True)
y_fit = [f.value(val) for val in x_fit]
plt.addSeries(LinePlotErrorSeries("Fit", Color.blue))
plt.getSeries(1).setData(to_array(x_fit, 'd'), y_fit, [0.0] * len(x_fit))
plt.getSeries(1).setPointsVisible(False)
#plt.addMarker(ph_p_max, plt.AxisId.X, "%3.2f" % ph_p_max, plt.getSeries(0).color)
#except:
# raise Exception("Fit failure")
plt.addMarker(x1, plt.AxisId.X, "%3.2f" % x1, plt.getSeries(1).color)
#Elog entry
if do_elog:
@@ -126,5 +122,5 @@ if do_elog:
elog("Schottky scan", log_msg, attachments)
#Setting the return value
bph_ref_guess = 0.0
bph_ref_guess = x1
set_return([bph_ref_guess])
+13 -61
View File
@@ -11,27 +11,27 @@
import datetime
import ch.psi.utils.Chrono as Chrono
mode = "camtool" # "camtool", "bpm" or "direct"
mode = "server" # "server", "camtool", "bpm" or "direct"
camera_name = "simulation"
#camera_name = "SLG-LCAM-C041"
use_good_region=False
dry_run = True
if True: #get_exec_pars().source == CommandSource.ui:
I1 = 95.0
I2 = 100.0
dI = 2.5
dI = 1.0
settling_time = 0.1
plot_image = False
number_images = 3
use_background = False
multiple_background = False
use_background = True
multiple_background = True
number_backgrounds = 3
do_elog = False
centroid_excursion_plot = True
else:
centroid_excursion_plot = False
check_camtool()
#laser_was_on = is_laser_on()
positioner = DummyPositioner("positioner")
@@ -44,69 +44,20 @@ print "Parameters: ", I1, I2, dI, settling_time, plot_image, number_images, use_
plot_name = datetime.datetime.fromtimestamp(time.time()).strftime('%H%M%S')
if mode == "camtool":
if use_background:
#laser_off()
if not multiple_background:
camtool.stop()
camtool.grabBackground(camera_name, number_backgrounds)
#camtool.stop()
#camtool.startPipeline(camera_name, 0, use_background, None, 0.0, None)
#camtool.startReceiver();
c = Chrono()
print "t1 = " , c.ellapsed
camtool.start(camera_name, 0, use_background, None, 0.0, None)
print "t2 = " , c.ellapsed
if camtool.value is not None: print "Started " , camtool.value.getTimestamp()
wait_camtool_message()
print "t3 = " , c.ellapsed
print "OK"
else:
if mode == "bpm":
add_device(BpmStats("image_stats", camera_name), True)
else:
add_device(ImageStats(PsiCamera("image_stats", camera_name)), True)
multiple_background = False
use_background = False
image_stats.setNumberOfImages(max(number_images,1))
setup_camera_scan()
#switch_off_magnets()
# add here gun phase setting see wiki page
def before_sample():
if mode == "camtool":
if multiple_background:
camtool.stop()
camtool.grabBackground(camera_name, number_backgrounds)
camtool.start(camera_name, 0, use_background, None, 0.0, None)
#laser_on()
wait_camtool_message(number_images) #Wait filing the averager cache
else:
image_stats.update()
def after_sample():
if multiple_background:
#laser_off()
pass
r = None
#if not multiple_background:
# laser_on()
try:
if mode == "camtool":
print "Getting sensors"
sensors = get_camtool_stats(number_images, good_region=use_good_region)
print "Sensors ok"
if plot_image:
sensors.append(camtool.getDataMatrix())
else:
sensors = [image_stats.com_x_mean, image_stats.com_y_mean, image_stats.com_x_stdev, image_stats.com_y_stdev]
r = lscan(positioner, sensors , I1, I2, dI, settling_time, before_read = before_sample, after_read = after_sample)
try:
sensors = get_camera_scan_sensors()
r = lscan(positioner, sensors , I1, I2, dI, settling_time, before_read = before_sample_camera_scan, after_read = after_sample_camera_scan)
finally:
if mode == "camtool": camtool.stop()
#pass
end_camera_scan()
positioner.write(original_gun_solenoid)
#if laser_was_on:
@@ -130,4 +81,5 @@ gsa_log_msg = gsa_log_msg + "\n\n" + r.print()
if do_elog:
elog("Gun solenoid current scan", gsa_log_msg , get_plot_snapshots())
print gsa_log_msg
set_return([r, hx, hy])
+40
View File
@@ -0,0 +1,40 @@
import ch.psi.pshell.epics.ChannelDouble as ChannelDouble
import ch.psi.pshell.epics.InvalidValueAction as InvalidValueAction
import java.lang.System as System
invalid = ChannelDouble("invalid", "SARUN15-UIND030:INVALID-PV", -1, True, InvalidValueAction.Nullify)
avg = create_averager(invalid, 5, interval =2.0, name = "avg")
invalid.monitored = True
add_device(invalid, True)
add_device(avg, True)
"""
invalid.write(100.0)
#caput ("SARUN15-UIND030:INVALID-PV", 100)
print invalid.read()
print invalid.severity
"""
"""
tspc = System.currentTimeMillis(); ts = invalid.timestamp; tsn = invalid.timestampNanos
print tspc
print ts
print tsn
print tspc - ts
#print invalid.takeTimestamped().severity
"""
"""
invalid.write(1000.0)
#caput ("SARUN15-UIND030:INVALID-PV", 1000)
print invalid.read()
print invalid.severity
"""
+110
View File
@@ -0,0 +1,110 @@
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
if is_panel:
station = args[0]
bpm_ch = args[1]
else:
station = "STEST01"
bpm_ch = "SINBC02-DBPM140"
start = caget(station + "-RSYS:SET-SCAN-START")
stop = caget(station + "-RSYS:SET-SCAN-STOP")
step = caget(station + "-RSYS:SET-SCAN-STEP")
lat = caget(station + "-RSYS:SET-SCAN-WAIT-TIME")
nb = caget(station + "-RSYS:SET-NUM-AVERAGE")
disp = caget(bpm_ch + ":DISPERSION")
energy0 = caget(bpm_ch + ":ENERGY")
A = energy0 / disp / 1e3
B = energy0
phase = Positioner("Phase", station + "-RSYS:SET-VSUM-PHASE", station + "-RSYS:GET-VSUM-PHASE")
phase.config.minValue =-360.0
phase.config.maxValue = 360.0
phase.config.precision = 4
phase.config.rotation = False
phase.config.resolution = 0.1
phase.initialize()
V = ChannelDouble("Amplitude Readback", station + "-RSYS:GET-VSUM-AMPLT")
V.initialize()
P = ChannelDouble("Power Readback", station + "-RSYS:GET-KLY-POWER")
P.initialize()
if dry_run:
x = ChannelDouble("BPM-X", bpm_ch + ":X1-SIMU")
else:
x = ChannelDouble("BPM-X", bpm_ch + ":X1")
x.initialize()
phase0 = phase.read()
caput(station + "-RSYS:GET-FIT-PHASE-ARRAY", to_array([0.0],'d'))
caput(station + "-RSYS:GET-FIT-ENERGY-ARRAY", to_array([0.0],'d'))
caput(station + "-RSYS:GET-ONCREST-VSUM-PHASE", float('nan'))
caput(station + "-RSYS:GET-ONCREST-VSUM-AMPLT", float('nan'))
caput(station + "-RSYS:GET-ONCREST-E-GAIN", float('nan'))
caput(station + "-RSYS:GET-ONCREST-KLY-POWER", float('nan'))
#update the plot dynamically
arr_phase,arr_energy = [],[]
def after(rec):
global A, B
arr_phase.append(rec.positions[0])
arr_energy.append(A * rec.values[0].mean + B)
caput(station + "-RSYS:GET-PHASE-ARRAY", to_array(arr_phase, 'd'))
caput(station + "-RSYS:GET-ENERGY-ARRAY", to_array(arr_energy,'d'))
#scan and plot
try:
phase.write(start)
time.sleep(1.0)
x_averager = create_averager(x, nb, 0.100)
r = lscan(phase, x_averager, start, stop, step, latency=lat, after_read = after)
rf_phase = r.getPositions(0)
energy = [A * val.mean + B for val in r.getReadable(0)]
caput(station + "-RSYS:GET-ENERGY-ARRAY", to_array(energy, 'd'))
caput(station + "-RSYS:GET-PHASE-ARRAY", to_array(rf_phase,'d'))
try:
run("CPython/wrapper")
(fit_amplitude, fit_phase_deg, fit_offset, ph_crest, fit_x, fit_y) = hfitoff(energy , rf_phase)
except:
raise Exception("Fit failure")
# This only works when pshell is visible not in server mode to be fixed
plt = plot(None,name="phase scan")[0]
plt.getSeries(0).setData(to_array(rf_phase,'d'), to_array(energy,'d'))
plt.getSeries(0).setPointSize(6)
plt.getSeries(0).setLinesVisible(False)
plt.addSeries(LinePlotSeries("fit"))
plt.getSeries(1).setData(fit_x, fit_y)
plt.getSeries(1).setPointsVisible(False)
plt.setLegendVisible(True)
phase.write(ph_crest)
time.sleep(lat)
Ampl = V.read()
Power = P.read()
caput(station + "-RSYS:GET-ONCREST-VSUM-PHASE", ph_crest)
caput(station + "-RSYS:GET-ONCREST-E-GAIN", fit_amplitude)
caput(station + "-RSYS:GET-ONCREST-VSUM-AMPLT", Ampl)
caput(station + "-RSYS:GET-ONCREST-KLY-POWER", Power)
caput(station + "-RSYS:GET-FIT-PHASE-ARRAY", fit_x)
caput(station + "-RSYS:GET-FIT-ENERGY-ARRAY", fit_y)
finally:
phase.write(phase0)
phase.close()
V.close()
P.close()
x.close()
phase_offset = 90.0 - ph_crest
amplitude_scale = fit_amplitude / Ampl
power_scale = Power / math.pow(fit_amplitude,2)
caput(station + "-RSYS:SET-VSUM-PHASE-OFFSET-BASE-CALC", phase_offset)
caput(station + "-RSYS:SET-VSUM-AMPLT-SCALE-CALC", amplitude_scale)
caput(station + "-RSYS:SET-VOLT-POWER-SCALE-CALC", power_scale)
if do_elog:
title = "Phase scan" + station
log_msg = "Data file: " + get_exec_pars().path + "\n"
log_msg = log_msg + "On-crest VS phase: %0.2f" % ph_crest + " deg \n"
log_msg = log_msg + "Energy Gain: %0.3f" % fit_amplitude + " MeV \n"
log_msg = log_msg + "VS -phase Offset: %0.2f" % phase_offset + " deg \n"
log_msg = log_msg + "Amplitude Scale: %0.3f" % amplitude_scale + " MV \n"
log_msg = log_msg + "Power Scale: %0.3f" % power_scale + " MW/MV^2"
attachments = get_plot_snapshots()
elog(title, log_msg, attachments)
+40
View File
@@ -0,0 +1,40 @@
do_elog = False
if get_exec_pars().source == CommandSource.ui:
station = "STEST01"
else:
station = args[0]
phase_set = caget(station + "-RSYS:PHASE-SET")
ampli_set = caget(station + "-RSYS:AMPLT-SET")
power_set = caget(station + "-RSYS:POWER-SET")
n = 0
if (phase_set == 'True'):
phase_offset = caget(station + "-RSYS:SET-VSUM-PHASE-OFFSET-BASE-CALC")
phase_offset_old = caget(station + "-RSYS:SET-VSUM-PHASE-OFFSET-BASE")
caput(station + "-RSYS:SET-VSUM-PHASE-OFFSET-BASE", phase_offset)
print phase_set
n = n + 1
if (ampli_set == 'True'):
amplitude_scale = caget(station + "-RSYS:SET-VSUM-AMPLT-SCALE-CALC")
amplitude_scale_old = caget(station + "-RSYS:SET-VSUM-AMPLT-SCALE")
caput(station + "-RSYS:SET-VSUM-AMPLT-SCALE", amplitude_scale)
print ampli_set
n = n + 1
if (power_set == 'True'):
power_scale = caget(station + "-RSYS:SET-VOLT-POWER-SCALE-CALC")
power_scale_old = caget(station + "-RSYS:SET-VOLT-POWER-SCALE")
caput(station + "-RSYS:SET-VOLT-POWER-SCALE", power_scale)
print power_set
n = n + 1
caput(station + "-RSYS:CMD-LOAD-CALIB-BEAM", 1)
if do_elog == True and n > 0:
title = "Set RF calibration:" + station
log_msg = ""
if (phase_set == 'True'): log_msg = log_msg + station + "-RSYS:SET-VSUM-PHASE-OFFSET-BASE: %0.2f" % phase_offset + " deg (was %0.2f" % phase_offset_old + " deg)\n"
if (ampli_set == 'True'): log_msg = log_msg + station + "-RSYS:SET-VSUM-AMPLT-SCALE: %0.3f" % amplitude_scale + " MV (was %0.3f" % amplitude_scale_old + " MV)\n"
if (power_set == 'True'): log_msg = log_msg + station + "-RSYS:SET-VOLT-POWER-SCALE: %0.5f" % power_scale + " MW/MV^2 (was %0.5f" % power_scale_old + " MW/MV^2)"
attachments = []
elog(title, log_msg, attachments)