~chris-rogers/maus/emr_mc_digitization

« back to all changes in this revision

Viewing changes to bin/utilities/envelope_tool/lib/plot_setup.py

  • Committer: Chris Rogers
  • Date: 2014-04-16 11:48:45 UTC
  • mfrom: (707 merge)
  • mto: This revision was merged to the branch mainline in revision 711.
  • Revision ID: chris.rogers@stfc.ac.uk-20140416114845-h3u3q7pdcxkxvovs
Update to trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#  This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
 
2
 
3
#  MAUS is free software: you can redistribute it and/or modify
 
4
#  it under the terms of the GNU General Public License as published by
 
5
#  the Free Software Foundation, either version 3 of the License, or
 
6
#  (at your option) any later version.
 
7
 
8
#  MAUS is distributed in the hope that it will be useful,
 
9
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
#  GNU General Public License for more details.
 
12
 
13
#  You should have received a copy of the GNU General Public License
 
14
#  along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
"""
 
17
GUI handler for setting up a plot
 
18
"""
 
19
 
 
20
# Currently hands around a dictionary that defines the setup options. This needs
 
21
# to be a class
 
22
# Currently only handles one plot element; plan is to support multiple plots on
 
23
# the same canvas
 
24
 
 
25
import ROOT
 
26
 
 
27
from xboa.Hit import Hit
 
28
 
 
29
from gui.window import Window
 
30
from gui.window import GuiError
 
31
 
 
32
class PlotSetup():
 
33
    """
 
34
    GUI handler for defining data to be plotted
 
35
    """
 
36
    def __init__(self, main_window, parent, selected):
 
37
        """
 
38
        Initialise the window
 
39
        """
 
40
        self.main_window = main_window
 
41
        self.parent = parent
 
42
        self.selected = selected
 
43
        self.window = Window(ROOT.gClient.GetRoot(), # pylint: disable = E1101
 
44
                             parent,
 
45
                             self.share_dir+"plot_setup.json")
 
46
        for i, item in enumerate(self.selected):
 
47
            self.window.set_action("variable_type_"+str(i), "drop_down",
 
48
                                  "Selected(Int_t)", self.select_action)
 
49
            type_frame = self.window.get_frame("variable_type_"+str(i),
 
50
                                               "drop_down")
 
51
            type_frame.Select(item["variable_type"])
 
52
        self.window.set_button_action("&Okay", self.okay_action)
 
53
        self.window.set_button_action("&Cancel", self.cancel_action)
 
54
 
 
55
    def okay_action(self):
 
56
        """
 
57
        Handle Okay button press; get the plot selection and push it to the
 
58
        main window
 
59
        """
 
60
        type_int = self.window.get_frame_dict("variable_type_0",
 
61
                                             "drop_down")["frame"].GetSelected()
 
62
        first_int = self.window.get_frame_dict("first_var_0",
 
63
                                            "drop_down")["frame"].GetSelected()
 
64
        plot_apertures = self.window.get_frame("plot_apertures",
 
65
                                               "check_button").IsOn()
 
66
        if type_int != 0 and first_int == 0:
 
67
            raise GuiError("Please select plot variable")
 
68
        self.main_window.plot_setup_options = [{
 
69
            "variable_type":type_int,
 
70
            "first_var":first_int,
 
71
            "plot_apertures":plot_apertures
 
72
        }]
 
73
        self.main_window.update_plot()
 
74
        self.window.close_window()
 
75
        self.main_window.plot_setup = None
 
76
    
 
77
    def cancel_action(self):
 
78
        """
 
79
        Handle Cancel button press; just close the window
 
80
        """
 
81
        self.window.close_window()
 
82
        self.main_window.plot_setup = None
 
83
 
 
84
    def select_action(self):
 
85
        """
 
86
        Dynamically change the list of items in first_var depending on
 
87
        variable_type
 
88
        """
 
89
        for i, item in enumerate(self.selected):
 
90
            type_select = self.window.get_frame_dict("variable_type_"+str(i),
 
91
                                                     "drop_down")["frame"]
 
92
            first_var_select = self.window.get_frame_dict("first_var_"+str(i),
 
93
                                                    "drop_down")["frame"]
 
94
            selected_type_int = type_select.GetSelected()
 
95
            selected_type_str = self.type_list[selected_type_int]
 
96
            var_list_name = self.type_variables_dict[selected_type_str][0]
 
97
            var_list = self.select_lists[var_list_name]
 
98
            first_var_select.RemoveAll()
 
99
            for i, entry in enumerate(var_list):
 
100
                first_var_select.AddEntry(entry, i)
 
101
            first_var_select.Select(item["first_var"])
 
102
 
 
103
    @staticmethod
 
104
    def get_variable_type(options):
 
105
        """
 
106
        Return a string corresponding to integer variable_type selected
 
107
        """
 
108
        var_type_int = options[0]["variable_type"]
 
109
        return PlotSetup.type_list[var_type_int]
 
110
 
 
111
    @staticmethod
 
112
    def get_first_var(options):
 
113
        """
 
114
        Return a string corresponding to integer first_var selected
 
115
        """
 
116
        var_type_string = PlotSetup.get_variable_type(options)
 
117
        select_list_key = PlotSetup.type_variables_dict[var_type_string][0]
 
118
        select_list = PlotSetup.select_lists[select_list_key]
 
119
        first_var_int = options[0]["first_var"]
 
120
        return select_list[first_var_int]
 
121
 
 
122
    my_hit_get_variables = [var for var in Hit.get_variables() if len(var)]
 
123
 
 
124
    type_list = [
 
125
        "<Select plot type>",
 
126
        "mean",
 
127
        "envelope",
 
128
        "RMS",
 
129
        "beta",
 
130
        "alpha",
 
131
        "gamma",
 
132
        "emittance",
 
133
        "dispersion",
 
134
        "dispersion_prime"
 
135
    ]
 
136
 
 
137
    select_lists = {
 
138
        "no_var":[
 
139
            "",
 
140
        ], "optics_var":[
 
141
            "<Select plot variable>",
 
142
            "x",
 
143
            "y",
 
144
            "transverse",
 
145
            "longitudinal"
 
146
        ], "physics_var":[
 
147
            "<Select plot variable>"
 
148
        ]+my_hit_get_variables, "kinematic_var":[
 
149
            "<Select plot variable>",
 
150
            "x",
 
151
            "y",
 
152
            "px",
 
153
            "py",
 
154
            "t",
 
155
            "energy"
 
156
        ], "disp_var":[
 
157
            "<Select plot variable>",
 
158
            "x",
 
159
            "y"
 
160
        ]
 
161
    }
 
162
 
 
163
    type_variables_dict = {
 
164
        "<Select plot type>":["no_var"],
 
165
        "mean":["physics_var"],
 
166
        "envelope":["kinematic_var"],
 
167
        "RMS":["kinematic_var"],
 
168
        "beta":["optics_var"],
 
169
        "alpha":["optics_var"],
 
170
        "gamma":["optics_var"],
 
171
        "emittance":["optics_var"],
 
172
        "dispersion":["disp_var"],
 
173
        "dispersion_prime":["disp_var"],
 
174
    }
 
175
 
 
176
    share_dir = ""