################################################################################################### # Deployment specific global definitions - executed after startup.py ################################################################################################### from mathutils import estimate_peak_indexes, fit_gaussians, create_fit_point_list, Gaussian from mathutils import fit_polynomial,fit_gaussian, fit_harmonic, calculate_peaks from mathutils import PolynomialFunction, Gaussian, HarmonicOscillator import java.awt.Color as Color LASER_SETTLING_TIME = 3.0 def laser_on(): print "Laser On" caput("SIN-TIMAST-TMA:Beam-Las-Delay-Sel", 0) caput("SIN-TIMAST-TMA:Beam-Apply-Cmd.PROC", 1) time.sleep(LASER_SETTLING_TIME) def laser_off(): print "Laser Off" caput("SIN-TIMAST-TMA:Beam-Las-Delay-Sel", 1) caput("SIN-TIMAST-TMA:Beam-Apply-Cmd.PROC", 1) time.sleep(LASER_SETTLING_TIME) def is_laser_on(): return (caget ("SIN-TIMAST-TMA:Beam-Las-Delay-Sel",'d') == 0 ) # Switch off magnets def ccr(magnet): while caget(magnet+ ":I-COMP") > 0: sleep(0.5) def switch_off_magnets(magnets = None): if magnets is None: magnets = [ "SINEG01-MCRX120","SINEG01-MCRY120", "SINEG01-MQUA140", "SINEG01-MQUA150", "SINEG01-MCRX160","SINEG01-MCRY160", "SINEG01-MCRX180","SINEG01-MCRY180", "SINEG01-MCRX200","SINEG01-MCRY200", "SINEG01-MCRX220","SINEG01-MCRY220", "SINEG01-MQUA310", "SINEG01-MQUA320" ] magnets = to_list(magnets) for m in magnets: caput(m + ":I-SET", 0.0) sleep(0.5) for m in magnets: ccr(m) def fit(ydata, xdata = None): """ Gaussian fit """ if xdata is None: xdata = frange(0, len(ydata), 1) #ydata = to_list(ydata) #xdata = to_list(xdata) max_y= max(ydata) index_max = ydata.index(max_y) max_x= xdata[index_max] print "Max index:" + str(index_max), print " x:" + str(max_x), print " y:" + str(max_y) gaussians = fit_gaussians(ydata, xdata, [index_max,]) (norm, mean, sigma) = gaussians[0] p = plot([ydata],["data"],[xdata], title="Fit" )[0] fitted_gaussian_function = Gaussian(norm, mean, sigma) scale_x = [float(min(xdata)), float(max(xdata)) ] points = max((len(xdata)+1), 100) resolution = (scale_x[1]-scale_x[0]) / points fit_y = [] fit_x = frange(scale_x[0],scale_x[1],resolution, True) for x in fit_x: fit_y.append(fitted_gaussian_function.value(x)) p.addSeries(LinePlotSeries("fit")) p.getSeries(1).setData(fit_x, fit_y) if abs(mean - xdata[index_max]) < ((scale_x[0] + scale_x[1])/2): print "Mean -> " + str(mean) p.addMarker(mean, None, "Mean="+str(round(norm,2)), Color.MAGENTA.darker()) return (norm, mean, sigma) else: p.addMarker(max_x, None, "Max="+str(round(max_x,2)), Color.GRAY) print "Invalid gaussian fit: " + str(mean) return (None, None, None) def hfit(ydata, xdata = None): """ Harmonic fit """ if xdata is None: xdata = frange(0, len(ydata), 1) max_y= max(ydata) index_max = ydata.index(max_y) max_x= xdata[index_max] start,end = min(xdata), max(xdata) (amplitude, angular_frequency, phase) = fit_harmonic(ydata, xdata) fitted_harmonic_function = HarmonicOscillator(amplitude, angular_frequency, phase) print "amplitude = ", amplitude print "angular frequency = ", angular_frequency print "phase = ", phase f = angular_frequency/ (2* math.pi) print "frequency = ", f resolution = 4.00 # 1.00 fit_y = [] for x in frange(start,end,resolution, True): fit_y.append(fitted_harmonic_function.value(x)) fit_x = frange(start, end+resolution, resolution) p = plot(ydata,"data", xdata, title="HFit")[0] p.addSeries(LinePlotSeries("fit")) p.getSeries(1).setData(fit_x, fit_y) #m = (phase + math.pi)/ angular_frequency m = -phase / angular_frequency if (m0: plots[0].clear() def add_convex_hull_plot(title, x,y, name=None, clear = False, x_range = None, y_range = None): plots = get_plots(title = title) p = None if len(plots)==0: p = plot(None,name=name, title = title)[0] if x_range is not None: p.getAxis(p.AxisId.X).setRange(x_range[0], x_range[1]) if y_range is not None: p.getAxis(p.AxisId.Y).setRange(y_range[0], y_range[1]) p.setLegendVisible(True) else: p = plots[0] if clear: p.clear() p.addSeries(LinePlotSeries(name)) s = p.getSeries(name) s.setLinesVisible(False) s.setPointSize(3) x, y = to_array(x,'d') , to_array(y,'d') s.setData(x, y) #Convex Hull #In the first time the plot shows, it takes some time for the color to be assigned timeout = 0 while s.color is None and timeout<1000: time.sleep(0.001) timeout = timeout + 1 hull = LinePlotSeries(name + "Hull", s.color) p.addSeries(hull) #Bounding box #x1,x2,y1,y2 = min(x), max(x), min(y), max(y) #(hx,hy) = ([x1,x2, x2, x1, x1], [y1, y1, y2, y2, y1]) (hx,hy) = convex_hull(x=x, y=y) hx.append(hx[0]); hy.append(hy[0]) hull.setLineWidth(2) hull.setData(to_array(hx,'d') , to_array(hy,'d')) hull.setColor(s.color) return [hx,hy] import random class ComX(ReadonlyRegisterBase): def doRead(self): ret = gun_solenoid.read()/10.0 + random.random() print "X=",ret return ret class ComY(ReadonlyRegisterBase): def doRead(self): ret = gun_solenoid.read()/15.0 + random.random() print "Y=",ret return ret comx = ComX(); comx.initialize() comy = ComY(); comy.initialize() avx = create_averager(comx, 5, 0.001) avy = create_averager(comy, 5, 0.001) #CAS if get_context().isServerEnabled(): import ch.psi.pshell.epics.CAS as CAS #CAS.setServerPort(5062) class ServerUrl(ReadonlyRegisterBase): def doRead(self): return get_context().server.baseURL d = ServerUrl() d.initialize() cas5 = CAS("PSHELL_OP:SERVER_URL", d, 'string') #Managing local camtool server CAMTOOL_SERVER_SCRIPT = "camtool_pshell" #"cam_server" PRINT_CAMTOOL_OUTPUT = True #When process quit def _get_camtool_port(): return camtool.url[camtool.url.rfind(":")+1:] def is_camtool_running(): """ """ return int(exec_cmd("pgrep " + CAMTOOL_SERVER_SCRIPT + " | wc -l")) > 0 #return int(exec_cmd('pgrep -f "' + CAMTOOL_SERVER_SCRIPT + ' -p ' + _get_camtool_port() + '" | wc -l')) > 0 def kill_camtool(): """ Kill the private camtool instance and child pocesses """ #return exec_cmd("killall -9 " + CAMTOOL_SERVER_SCRIPT) return exec_cmd('pkill -f " \-p ' + _get_camtool_port() + '"') def _run_camtool(): try: #cmd = "source /opt/gfa/python\n" cmd = CAMTOOL_SERVER_SCRIPT + " -p " + _get_camtool_port() + " -b /afs/psi.ch/intranet/SF/Applications/config/camtool_n" if not PRINT_CAMTOOL_OUTPUT: cmd = cmd + " > /dev/null 2>&1" print "Executing: ", cmd print exec_cmd(cmd) except: import traceback print >> sys.stderr, traceback.format_exc() def check_camtool(): """ Checks if private camtool server is running, otherwise starts it. """ if is_camtool_running(): return False print "Staring Camtool" fork(_run_camtool) start =time.time() while not is_camtool_running(): if time.time() - start > 2.0: raise Exception("Error starting camtool process") time.sleep(3.0) def bsget(channel): """ """ st = Stream(None, dispatcher) try: st.addScalar(channel, channel, 10, 0) st.initialize() st.start(); st.waitValueNot(None, 5000) return st.getValue(channel) finally: st.close() #Machine utilities def is_timing_ok(): return caget("SIN-TIMAST-TMA:SOS-COUNT-CHECK") == 0 def get_repetition_rate(): return caget("SIN-TIMAST-TMA:Evt-15-Freq-I") #Device pool customization pbpg_mx.setTrustedWrite(False) pbpg_my.setTrustedWrite(False)