class MagnetSetup: """ GUI handler for field setup """ def __init__(self, main_window, parent): """Initialise the GUI window""" self.main_window = main_window self.parent = parent manipulators = {"magnet_setup_action":self.magnet_setup_action} self.window = Window(ROOT.gClient.GetRoot(), # pylint: disable = E1101 parent, data_file = self.share_dir+"magnet_setup.json", manipulator_dict = manipulators) self.window.set_button_action("&Okay", self.okay_action) self.window.set_button_action("&Cancel", self.cancel_action) def okay_action(self): """Handle okay button press""" field_list_out = [] field_frame = self.window.get_frame_dict("field_list", "vertical_frame") for frame in field_frame["children"]: try: name = frame["name"] scale_factor = float(frame["text_entry"].text_entry.GetText()) field_list_out.append({"field_name":name, "scale_factor":scale_factor}) except KeyError: pass self.main_window.lattice.set_fields(field_list_out) self.main_window.lattice.run_lattice() self.main_window.update_plot() self.window.close_window() self.main_window.magnet_setup = None def cancel_action(self): """Handle cancel button press""" self.window.close_window() self.main_window.magnet_setup = None def magnet_setup_action(self, item): # pylint: disable=W0613 """Iterate over magnets and add a line per magnet (special)""" field_list = self.main_window.lattice.get_field_list() geometry_list = { "type":"vertical_frame", "layout":"close", "name":"field_list", "children":[]} if len(field_list) == 0: geometry_list["children"].append({"type":"label", "name":"No fields in lattice", "label_length":30}) return geometry_list for field in field_list: geometry_list["children"].append({ "type":"named_text_entry", "default_text":str(field["scale_factor"]), "label_length":10, "name":str(field["field_name"]), "tool_tip":"Longitudinal beta function" }) return geometry_list share_dir = ""
class PlotSetup(): """ GUI handler for defining data to be plotted """ def __init__(self, main_window, parent, selected): """ Initialise the window """ self.main_window = main_window self.parent = parent self.selected = selected self.window = Window(ROOT.gClient.GetRoot(), # pylint: disable = E1101 parent, self.share_dir+"plot_setup.json") for i, item in enumerate(self.selected): self.window.set_action("variable_type_"+str(i), "drop_down", "Selected(Int_t)", self.select_action) type_frame = self.window.get_frame("variable_type_"+str(i), "drop_down") type_frame.Select(item["variable_type"]) self.window.set_button_action("&Okay", self.okay_action) self.window.set_button_action("&Cancel", self.cancel_action) def okay_action(self): """ Handle Okay button press; get the plot selection and push it to the main window """ type_int = self.window.get_frame_dict("variable_type_0", "drop_down")["frame"].GetSelected() first_int = self.window.get_frame_dict("first_var_0", "drop_down")["frame"].GetSelected() plot_apertures = self.window.get_frame("plot_apertures", "check_button").IsOn() if type_int != 0 and first_int == 0: raise GuiError("Please select plot variable") self.main_window.plot_setup_options = [{ "variable_type":type_int, "first_var":first_int, "plot_apertures":plot_apertures }] self.main_window.update_plot() self.window.close_window() self.main_window.plot_setup = None def cancel_action(self): """ Handle Cancel button press; just close the window """ self.window.close_window() self.main_window.plot_setup = None def select_action(self): """ Dynamically change the list of items in first_var depending on variable_type """ for i, item in enumerate(self.selected): type_select = self.window.get_frame_dict("variable_type_"+str(i), "drop_down")["frame"] first_var_select = self.window.get_frame_dict("first_var_"+str(i), "drop_down")["frame"] selected_type_int = type_select.GetSelected() selected_type_str = self.type_list[selected_type_int] var_list_name = self.type_variables_dict[selected_type_str][0] var_list = self.select_lists[var_list_name] first_var_select.RemoveAll() for i, entry in enumerate(var_list): first_var_select.AddEntry(entry, i) first_var_select.Select(item["first_var"]) @staticmethod def get_variable_type(options): """ Return a string corresponding to integer variable_type selected """ var_type_int = options[0]["variable_type"] return PlotSetup.type_list[var_type_int] @staticmethod def get_first_var(options): """ Return a string corresponding to integer first_var selected """ var_type_string = PlotSetup.get_variable_type(options) select_list_key = PlotSetup.type_variables_dict[var_type_string][0] select_list = PlotSetup.select_lists[select_list_key] first_var_int = options[0]["first_var"] return select_list[first_var_int] my_hit_get_variables = [var for var in Hit.get_variables() if len(var)] type_list = [ "<Select plot type>", "mean", "envelope", "RMS", "beta", "alpha", "gamma", "emittance", "dispersion", "dispersion_prime" ] select_lists = { "no_var":[ "", ], "optics_var":[ "<Select plot variable>", "x", "y", "transverse", "longitudinal" ], "physics_var":[ "<Select plot variable>" ]+my_hit_get_variables, "kinematic_var":[ "<Select plot variable>", "x", "y", "px", "py", "t", "energy" ], "disp_var":[ "<Select plot variable>", "x", "y" ] } type_variables_dict = { "<Select plot type>":["no_var"], "mean":["physics_var"], "envelope":["kinematic_var"], "RMS":["kinematic_var"], "beta":["optics_var"], "alpha":["optics_var"], "gamma":["optics_var"], "emittance":["optics_var"], "dispersion":["disp_var"], "dispersion_prime":["disp_var"], } share_dir = ""
class BeamSetup: """GUI window to setup a reference particle and beam ellipse""" def __init__(self, main_window, parent): """Initialise the window""" self.window = Window( ROOT.gClient.GetRoot(), # pylint: disable = E1101 parent, SHARE_DIR + "beam_setup.json") self.main_window = main_window self.parent = parent self.matrix_select = None self.matrix = None self.window.set_button_action("&Penn", self.penn_action) self.window.set_button_action("&Twiss", self.twiss_action) self.window.set_button_action("&Okay", self.okay_action) self.window.set_button_action("&Cancel", self.cancel_action) def twiss_action(self): """Handle Twiss button - open a window to set up Twiss parameters""" if self.matrix_select == None: self.matrix_select = TwissSetup(self, self.parent) def penn_action(self): """Handle Penn button - open a window to set up Penn parameters""" if self.matrix_select == None: self.matrix_select = PennSetup(self, self.parent) def okay_action(self): """ Handle Okay button - set the beam in lattice and then close the window """ if self.matrix_select != None: print "Close the matrix_select window" return self.main_window.lattice.set_beam(self.get_reference(), self.get_matrix()) self.main_window.lattice.run_lattice() self.main_window.update_plot() self.window.close_window() self.main_window.beam_setup = None def cancel_action(self): """ Handle Cancel button - just close the window """ if self.matrix_select != None: print "Close the matrix_select window" return self.window.close_window() self.main_window.beam_setup = None def set_matrix(self, matrix): """ Set the beam matrix (updating GUI elements) - matrix a maus_cpp.covariance_matrix.CovarianceMatrix object """ self.matrix = matrix for i, var_i in enumerate(["t", "e", "x", "px", "y", "py"]): for j, var_j in enumerate(["t", "e", "x", "px", "y", "py"]): value = matrix.get_element(i + 1, j + 1) self.window.set_text_entry(var_i + var_j, value) def get_matrix(self): """ Get the beam matrix (reading from GUI elements) Returns a maus_cpp.covariance_matrix.CovarianceMatrix object """ np_matrix = numpy.zeros([6, 6]) for i, var_i in enumerate(["t", "e", "x", "px", "y", "py"]): for j, var_j in enumerate(["t", "e", "x", "px", "y", "py"]): value1 = self.window.get_text_entry(var_i + var_j, type(1.)) value2 = self.window.get_text_entry(var_j + var_i, type(1.)) delta = abs(value1 - value2) if delta > 1e-6: err = "Matrix has non-symmetric element Cov("+\ var_i+","+var_j+") with delta "+str(delta)+"." err += "This element must be symmetric - upper "+\ "diagonal should be equal to lower diagonal." raise GuiError(err) else: np_matrix[i, j] = value1 evals = numpy.linalg.eigvals(np_matrix) if not numpy.all(evals > 0): evals = [i for i in evals] raise GuiError("Beam ellipse should be positive definite but "+\ " eigenvalues were not all positive: "+str(evals)) return maus_cpp.covariance_matrix.create_from_matrix(np_matrix) def set_reference(self, ref_hit): """ Get the reference particle (writing to GUI elements) - ref_hit an xboa.Hit.Hit object """ for var in ["x", "y", "z", "px", "py", "pz", "pid"]: self.window.set_text_entry(var, ref_hit[var]) def get_reference(self): """ Get the reference particle (reading from GUI elements) Returns an xboa.Hit.Hit object """ ref_dict = {} for var in ["x", "y", "z", "px", "py", "pz"]: var_dict = self.window.get_frame_dict(var, "named_text_entry") ref_dict[var] = float(var_dict["text_entry"].text_entry.GetText()) pid_dict = self.window.get_frame_dict("pid", "named_text_entry") try: ref_dict["pid"] = int(pid_dict["text_entry"].text_entry.GetText()) ref_dict["mass"] = common.pdg_pid_to_mass[abs(ref_dict["pid"])] ref_dict["charge"] = common.pdg_pid_to_charge[abs(ref_dict["pid"])] except KeyError: raise GuiError("Did not recognise reference particle pid") try: hit = Hit.new_from_dict(ref_dict, "energy") except Exception: raise GuiError("Failed to generate a reference particle") return hit
class PlotSetup(): """ GUI handler for defining data to be plotted """ def __init__(self, main_window, parent, selected): """ Initialise the window """ self.main_window = main_window self.parent = parent self.selected = selected self.window = Window( ROOT.gClient.GetRoot(), # pylint: disable = E1101 parent, self.share_dir + "plot_setup.json") for i, item in enumerate(self.selected): self.window.set_action("variable_type_" + str(i), "drop_down", "Selected(Int_t)", self.select_action) type_frame = self.window.get_frame("variable_type_" + str(i), "drop_down") type_frame.Select(item["variable_type"]) self.window.set_button_action("&Okay", self.okay_action) self.window.set_button_action("&Cancel", self.cancel_action) def okay_action(self): """ Handle Okay button press; get the plot selection and push it to the main window """ type_int = self.window.get_frame_dict( "variable_type_0", "drop_down")["frame"].GetSelected() first_int = self.window.get_frame_dict( "first_var_0", "drop_down")["frame"].GetSelected() plot_apertures = self.window.get_frame("plot_apertures", "check_button").IsOn() if type_int != 0 and first_int == 0: raise GuiError("Please select plot variable") self.main_window.plot_setup_options = [{ "variable_type": type_int, "first_var": first_int, "plot_apertures": plot_apertures }] self.main_window.update_plot() self.window.close_window() self.main_window.plot_setup = None def cancel_action(self): """ Handle Cancel button press; just close the window """ self.window.close_window() self.main_window.plot_setup = None def select_action(self): """ Dynamically change the list of items in first_var depending on variable_type """ for i, item in enumerate(self.selected): type_select = self.window.get_frame_dict("variable_type_" + str(i), "drop_down")["frame"] first_var_select = self.window.get_frame_dict( "first_var_" + str(i), "drop_down")["frame"] selected_type_int = type_select.GetSelected() selected_type_str = self.type_list[selected_type_int] var_list_name = self.type_variables_dict[selected_type_str][0] var_list = self.select_lists[var_list_name] first_var_select.RemoveAll() for i, entry in enumerate(var_list): first_var_select.AddEntry(entry, i) first_var_select.Select(item["first_var"]) @staticmethod def get_variable_type(options): """ Return a string corresponding to integer variable_type selected """ var_type_int = options[0]["variable_type"] return PlotSetup.type_list[var_type_int] @staticmethod def get_first_var(options): """ Return a string corresponding to integer first_var selected """ var_type_string = PlotSetup.get_variable_type(options) select_list_key = PlotSetup.type_variables_dict[var_type_string][0] select_list = PlotSetup.select_lists[select_list_key] first_var_int = options[0]["first_var"] return select_list[first_var_int] my_hit_get_variables = [var for var in Hit.get_variables() if len(var)] type_list = [ "<Select plot type>", "mean", "envelope", "RMS", "beta", "alpha", "gamma", "emittance", "dispersion", "dispersion_prime" ] select_lists = { "no_var": [ "", ], "optics_var": ["<Select plot variable>", "x", "y", "transverse", "longitudinal"], "physics_var": ["<Select plot variable>"] + my_hit_get_variables, "kinematic_var": ["<Select plot variable>", "x", "y", "px", "py", "t", "energy"], "disp_var": ["<Select plot variable>", "x", "y"] } type_variables_dict = { "<Select plot type>": ["no_var"], "mean": ["physics_var"], "envelope": ["kinematic_var"], "RMS": ["kinematic_var"], "beta": ["optics_var"], "alpha": ["optics_var"], "gamma": ["optics_var"], "emittance": ["optics_var"], "dispersion": ["disp_var"], "dispersion_prime": ["disp_var"], } share_dir = ""
class BeamSetup: """GUI window to setup a reference particle and beam ellipse""" def __init__(self, main_window, parent): """Initialise the window""" self.window = Window(ROOT.gClient.GetRoot(), # pylint: disable = E1101 parent, SHARE_DIR+"beam_setup.json") self.main_window = main_window self.parent = parent self.matrix_select = None self.matrix = None self.window.set_button_action("&Penn", self.penn_action) self.window.set_button_action("&Twiss", self.twiss_action) self.window.set_button_action("&Okay", self.okay_action) self.window.set_button_action("&Cancel", self.cancel_action) def twiss_action(self): """Handle Twiss button - open a window to set up Twiss parameters""" if self.matrix_select == None: self.matrix_select = TwissSetup(self, self.parent) def penn_action(self): """Handle Penn button - open a window to set up Penn parameters""" if self.matrix_select == None: self.matrix_select = PennSetup(self, self.parent) def okay_action(self): """ Handle Okay button - set the beam in lattice and then close the window """ if self.matrix_select != None: print "Close the matrix_select window" return self.main_window.lattice.set_beam(self.get_reference(), self.get_matrix()) self.main_window.lattice.run_lattice() self.main_window.update_plot() self.window.close_window() self.main_window.beam_setup = None def cancel_action(self): """ Handle Cancel button - just close the window """ if self.matrix_select != None: print "Close the matrix_select window" return self.window.close_window() self.main_window.beam_setup = None def set_matrix(self, matrix): """ Set the beam matrix (updating GUI elements) - matrix a maus_cpp.covariance_matrix.CovarianceMatrix object """ self.matrix = matrix for i, var_i in enumerate(["t", "e", "x", "px", "y", "py"]): for j, var_j in enumerate(["t", "e", "x", "px", "y", "py"]): value = matrix.get_element(i+1, j+1) self.window.set_text_entry(var_i+var_j, value) def get_matrix(self): """ Get the beam matrix (reading from GUI elements) Returns a maus_cpp.covariance_matrix.CovarianceMatrix object """ np_matrix = numpy.zeros([6, 6]) for i, var_i in enumerate(["t", "e", "x", "px", "y", "py"]): for j, var_j in enumerate(["t", "e", "x", "px", "y", "py"]): value1 = self.window.get_text_entry(var_i+var_j, type(1.)) value2 = self.window.get_text_entry(var_j+var_i, type(1.)) delta = abs(value1 - value2) if delta > 1e-6: err = "Matrix has non-symmetric element Cov("+\ var_i+","+var_j+") with delta "+str(delta)+"." err += "This element must be symmetric - upper "+\ "diagonal should be equal to lower diagonal." raise GuiError(err) else: np_matrix[i, j] = value1 evals = numpy.linalg.eigvals(np_matrix) if not numpy.all(evals > 0): evals = [i for i in evals] raise GuiError("Beam ellipse should be positive definite but "+\ " eigenvalues were not all positive: "+str(evals)) return maus_cpp.covariance_matrix.create_from_matrix(np_matrix) def set_reference(self, ref_hit): """ Get the reference particle (writing to GUI elements) - ref_hit an xboa.Hit.Hit object """ for var in ["x", "y", "z", "px", "py", "pz", "pid"]: self.window.set_text_entry(var, ref_hit[var]) def get_reference(self): """ Get the reference particle (reading from GUI elements) Returns an xboa.Hit.Hit object """ ref_dict = {} for var in ["x", "y", "z", "px", "py", "pz"]: var_dict = self.window.get_frame_dict(var, "named_text_entry") ref_dict[var] = float(var_dict["text_entry"].text_entry.GetText()) pid_dict = self.window.get_frame_dict("pid", "named_text_entry") try: ref_dict["pid"] = int(pid_dict["text_entry"].text_entry.GetText()) ref_dict["mass"] = common.pdg_pid_to_mass[abs(ref_dict["pid"])] ref_dict["charge"] = common.pdg_pid_to_charge[abs(ref_dict["pid"])] except KeyError: raise GuiError("Did not recognise reference particle pid") try: hit = Hit.new_from_dict(ref_dict, "energy") except Exception: raise GuiError("Failed to generate a reference particle") return hit
class MagnetSetup: """ GUI handler for field setup """ def __init__(self, main_window, parent): """Initialise the GUI window""" self.main_window = main_window self.parent = parent manipulators = {"magnet_setup_action": self.magnet_setup_action} self.window = Window( ROOT.gClient.GetRoot(), # pylint: disable = E1101 parent, data_file=self.share_dir + "magnet_setup.json", manipulator_dict=manipulators) self.window.set_button_action("&Okay", self.okay_action) self.window.set_button_action("&Cancel", self.cancel_action) def okay_action(self): """Handle okay button press""" field_list_out = [] field_frame = self.window.get_frame_dict("field_list", "vertical_frame") for frame in field_frame["children"]: try: name = frame["name"] scale_factor = float(frame["text_entry"].text_entry.GetText()) field_list_out.append({ "field_name": name, "scale_factor": scale_factor }) except KeyError: pass self.main_window.lattice.set_fields(field_list_out) self.main_window.lattice.run_lattice() self.main_window.update_plot() self.window.close_window() self.main_window.magnet_setup = None def cancel_action(self): """Handle cancel button press""" self.window.close_window() self.main_window.magnet_setup = None def magnet_setup_action(self, item): # pylint: disable=W0613 """Iterate over magnets and add a line per magnet (special)""" field_list = self.main_window.lattice.get_field_list() geometry_list = { "type": "vertical_frame", "layout": "close", "name": "field_list", "children": [] } if len(field_list) == 0: geometry_list["children"].append({ "type": "label", "name": "No fields in lattice", "label_length": 30 }) return geometry_list for field in field_list: geometry_list["children"].append({ "type": "named_text_entry", "default_text": str(field["scale_factor"]), "label_length": 10, "name": str(field["field_name"]), "tool_tip": "Longitudinal beta function" }) return geometry_list share_dir = ""