Files
sf-op/script/Alignment/Gun_solenoid_alignment.py
2017-11-02 08:53:16 +01:00

129 lines
5.0 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
do_elog = True
dry_run = False
is_panel = get_exec_pars().source != CommandSource.ui #Must be checked before callin "run"
camera_name = "SINEG01-DSCR190"
bpm_name = "SINEG01-DBPM340"
use_good_region=False
magnets = ["SINEG01-MCRX120","SINEG01-MCRY120",
"SINEG01-MQUA140","SINEG01-MQUA150",
"SINEG01-MCRX160","SINEG01-MCRY160",
"SINEG01-MCRX180","SINEG01-MCRY180"]
if not is_panel:
source = "server" # "server", "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)
positioner = DummyPositioner("positioner") if dry_run else gun_solenoid
original_gun_solenoid = positioner.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
setup_camera_scan()
if not dry_run:
#magnet_values = []
#for magnet in magnets:
# magnet_values.append(caget (magnet + ":I-SET"))
switch_off_magnets( magnets )
# add here gun phase setting see wiki page
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 in ["camtool", "server"]:
#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));
after_sample_camera_scan()
r = None
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)
finally:
end_camera_scan()
positioner.write(original_gun_solenoid)
# 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,])