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

184 lines
6.8 KiB
Python
Executable File

# Tool to align the solenoid on the gun.
# S. Bettoni, A. Gobbo, D. Voulot
# 06/06/2016
#Code ok with the GUI
# Procedure:
# switch off all the magnets between the gun solenoid and the screen or BPM used for the measurement
# change the current of the gun soleoid
# look at the centroid position (BPM or screen) downstream of the gun.
import datetime
is_panel = get_exec_pars().source != CommandSource.ui #Must be checked before callin "run"
camera_name = "SINEG01-DSCR190"
use_good_region=False
do_elog = True
if not is_panel:
source = "direct" # "camtool", "bpm" or "direct"
I1 = 100.0
I2 = 105.0
dI = 1.0
settling_time = 0.1
plot_image = False
number_images = 5
use_background = True
multiple_background = False
number_backgrounds = 5
plots = get_plots(title = "Centroid excursion")
if len(plots)==0:
centroid_plot = plot(None, title = "Centroid excursion")[0]
centroid_plot.clear()
else:
centroid_plot = plots[0]
centroid_plot_index = 1 if ((len(centroid_plot.getAllSeries()) == 0) or (not globals().has_key("centroid_plot_index"))) else (centroid_plot_index + 1)
print "Plot index: ", centroid_plot_index
centroid_plot.setLegendVisible(True)
centroid_plot.setStyle(centroid_plot.Style.ErrorXY)
centroid_plot.getAxis(centroid_plot.AxisId.Y).setLabel("")
centroid_plot.getAxis(centroid_plot.AxisId.X).setLabel("Centroid Excursion")
scan_series = LinePlotErrorSeries(str(centroid_plot_index))
centroid_plot.addSeries(scan_series)
scan_series.setLinesVisible(False)
scan_series.setPointSize(4)
#Testing
"""
camera_name = "SLG-LCAM-C041"
def laser_on():
print "Laser On"
def laser_off():
print "Laser Off"
def switch_off_magnets(magnets = None):
pass
add_device(DummyPositioner("gun_solenoid"), True)
do_elog = False
"""
laser_was_on = is_laser_on()
original_gun_solenoid = gun_solenoid.read()
multiple_background = multiple_background and use_background
print "Parameters: ", I1, I2, dI, settling_time, plot_image, number_images, use_background, multiple_background, number_backgrounds
if source == "camtool":
check_camtool()
if use_background:
laser_off()
if not multiple_background:
camtool.stop()
camtool.grabBackground(camera_name, number_backgrounds)
camtool.start(camera_name, 0, use_background, None, 0.0, None)
else:
if source == "bpm":
run("Devices/BpmStats")
add_device(BpmStats("image_stats", camera_name), True)
multiple_background = False
use_background = False
else:
run("Devices/ImageStats")
add_device(ImageStats("image_stats", camera_name), True)
add_device(image_stats.source, True)
image_stats.enableBackground(use_background)
if use_background:
laser_off()
if not multiple_background:
image_stats.grabBackground(number_backgrounds)
image_stats.setNumberOfImages(max(number_images,1))
#switch_off_magnets()
# add here gun phase setting see wiki page
def before_sample(position, scan):
if source == "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:
if multiple_background:
image_stats.grabBackground(number_backgrounds)
laser_on()
image_stats.update()
def after_sample(record, scan):
x = record.values[0]
y = record.values[1]
stdev_x = record.values[2]
stdev_y = record.values[3]
if source == "camtool":
#x, y, stdev_x, stdev_y = x.mean, y.mean, stdev_x.mean, stdev_y.mean
x, y, stdev_x, stdev_y = x.mean, y.mean, x.stdev, y.stdev
scan_series.appendData(x, y, abs(stdev_x), abs(stdev_y));
if multiple_background:
laser_off()
r = None
if not multiple_background:
laser_on()
try:
if source != "camtool":
sensors = [image_stats.com_x_mean, image_stats.com_y_mean, image_stats.com_x_stdev, image_stats.com_y_stdev]
if plot_image and (source == "direct"):
sensors.append(image_stats.source.getDataMatrix())
else:
sensors = get_camtool_stats(number_images, good_region=use_good_region)
if plot_image:
sensors.append(camtool.getDataMatrix())
r = lscan(gun_solenoid, sensors , I1, I2, dI, settling_time, before_read = before_sample, after_read = after_sample)
finally:
if source == "camtool":
camtool.stop()
else:
image_stats.stop()
gun_solenoid.write(original_gun_solenoid)
if laser_was_on:
laser_on()
else:
laser_off()
# take the result of the scan and generate convex hull plot
(hx,hy)= convex_hull(x=to_array(r.getReadable(0), 'd'), y=to_array(r.getReadable(1),'d'))
hx.append(hx[0]); hy.append(hy[0])
hx = Convert.toDouble(hx)
hy = Convert.toDouble(hy)
hull = LinePlotErrorSeries(scan_series.name + "H", scan_series.color);
centroid_plot.addSeries(hull);
hull.setData(hx, hy);
#Include metadata do hdf5
path = get_exec_pars().scanPath
set_attribute(path, "Settling time", settling_time)
set_attribute(path, "Images", number_images)
set_attribute(path, "Background enabled", use_background)
set_attribute(path, "Background multiple", multiple_background)
set_attribute(path, "Background images", number_backgrounds)
set_attribute(path, "Plot index", centroid_plot_index)
# save the entry in the logbook
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 + "\nI1: " + str(I1)
log_msg = log_msg + "\nI2: " + str(I2)
log_msg = log_msg + "\ndI: " + str(dI)
log_msg = log_msg + "\nSettling time: " + str(settling_time)
log_msg = log_msg + "\nImages: " + str(number_images)
log_msg = log_msg + "\nBackground: enabled=" + str(use_background) + " multiple=" + str(multiple_background) + " number=" + str(number_backgrounds)
log_msg = log_msg + "\nPlot index: " + str(centroid_plot_index)
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() + "/centroid_excursion.png")
centroid_plot.saveSnapshot(file_name , "png")
elog("Gun solenoid alignment", log_msg, [file_name,])