~durga/maus/rel709

« back to all changes in this revision

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

  • Committer: Durga Rajaram
  • Date: 2013-11-23 13:36:13 UTC
  • mfrom: (659.1.78 rc)
  • Revision ID: durga@fnal.gov-20131123133613-7t1s89cksp4x9bev
Tags: MAUS-v0.7.5
MAUS-v0.7.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
#  This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
 
4
 
5
#  MAUS is free software: you can redistribute it and/or modify
 
6
#  it under the terms of the GNU General Public License as published by
 
7
#  the Free Software Foundation, either version 3 of the License, or
 
8
#  (at your option) any later version.
 
9
 
10
#  MAUS is distributed in the hope that it will be useful,
 
11
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#  GNU General Public License for more details.
 
14
 
15
#  You should have received a copy of the GNU General Public License
 
16
#  along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
"""
 
19
The envelope tool enables the user to study the effects of lattice modifications
 
20
in an interactive environment. 
 
21
 
 
22
GUI tools provide functionality for setting up, propagating and plotting beam
 
23
envelopes through electromagnetic lattices.
 
24
"""
 
25
 
 
26
import time
 
27
import os
 
28
import sys
 
29
import ROOT
 
30
 
 
31
import gui.gui_exception_handler
 
32
from gui.window import Window
 
33
 
 
34
ENV_DIR = os.path.expandvars("${MAUS_ROOT_DIR}/bin/utilities/envelope_tool/")
 
35
sys.path.append(ENV_DIR+"lib")
 
36
 
 
37
# pylint: disable=F0401
 
38
import beam_setup 
 
39
from beam_setup import BeamSetup
 
40
from lattice import Lattice
 
41
from plotter import Plotter
 
42
from plot_setup import PlotSetup
 
43
from magnet_setup import MagnetSetup
 
44
 
 
45
SHARE_DIR = (ENV_DIR+"share/")
 
46
 
 
47
def set_share_dirs():
 
48
    """
 
49
    Setup share directories in submodules (used for loading aux files)
 
50
    """
 
51
    Lattice.share_dir = SHARE_DIR
 
52
    PlotSetup.share_dir = SHARE_DIR
 
53
    MagnetSetup.share_dir = SHARE_DIR
 
54
    beam_setup.SHARE_DIR = SHARE_DIR
 
55
 
 
56
class MainWindow():
 
57
    """
 
58
    Defines the main window for the envelope tool GUI
 
59
    """
 
60
    def __init__(self):
 
61
        """Initialise the main window"""
 
62
        self.window = Window(ROOT.gClient.GetRoot(), # pylint: disable=E1101
 
63
                             ROOT.gClient.GetRoot(), # pylint: disable=E1101
 
64
                             SHARE_DIR+"main_frame.json")
 
65
        self.lattice = Lattice()
 
66
        self.beam_setup = None
 
67
        self.magnet_setup = None
 
68
        self.plot_setup = None
 
69
        self.plot_setup_options = [{"variable_type":0,
 
70
                                    "first_var":0,
 
71
                                    "plot_apertures":True}]
 
72
        self.window.set_button_action("&Beam Setup", self.beam_button_action)
 
73
        self.window.set_button_action("&Magnet Setup",
 
74
                                                      self.magnet_button_action)
 
75
        self.window.set_button_action("&Plot Setup", self.plot_button_action)
 
76
        self.window.set_button_action("E&xit", self.exit_button_action)
 
77
        self.update_plot()
 
78
 
 
79
    def beam_button_action(self):
 
80
        """Handle a Beam Setup button press"""
 
81
        if self.beam_setup == None:
 
82
            self.beam_setup = BeamSetup(self, self.window.main_frame)
 
83
            ref, ellipse = self.lattice.get_beam()
 
84
            self.beam_setup.set_reference(ref)
 
85
            self.beam_setup.set_matrix(ellipse)
 
86
 
 
87
    def magnet_button_action(self):
 
88
        """Handle a Magnet Setup button press"""
 
89
        if self.magnet_setup == None:
 
90
            self.magnet_setup = MagnetSetup(self, self.window.main_frame)
 
91
 
 
92
    def plot_button_action(self):
 
93
        """Handle a Plot Setup button press"""
 
94
        if self.plot_setup == None:
 
95
            self.plot_setup = PlotSetup(self, self.window.main_frame,
 
96
                                        self.plot_setup_options)
 
97
 
 
98
    def exit_button_action(self):
 
99
        """Handle a Exit button press"""
 
100
        self.window.close_window()
 
101
 
 
102
    def update_plot(self):
 
103
        """Update the plot"""
 
104
        canvas_frame = self.window.get_frame("main_canvas", "canvas")
 
105
        canvas = canvas_frame.GetCanvas()
 
106
        Plotter(self.plot_setup_options, canvas, self.lattice.ref_list,
 
107
                self.lattice.ellipse_list, self.lattice.get_field_list())
 
108
 
 
109
def main():
 
110
    """
 
111
    The main function - build a MainWindow and poll to see if it has been closed
 
112
    """
 
113
    set_share_dirs()
 
114
    try:
 
115
        main_window = MainWindow()
 
116
        gui.gui_exception_handler.set_error_level("exceptions")
 
117
        while main_window.window.main_frame != None:
 
118
            time.sleep(1)
 
119
    except KeyboardInterrupt:
 
120
        print "Pressed Ctrl-C"
 
121
    finally:
 
122
        print "done"
 
123
        return 0 # pylint: disable=W0150
 
124
 
 
125
if __name__ == '__main__':
 
126
    main()