Beispiel #1
0
 def run():
     self.app.ui.set_status("Searching for servers...")
     servers = Interface.get_desk_unity_servers()
     self.this_computer = Computer()
     if len(servers) == 0:
         self.app.ui.set_status("No server founds.")
         logging.info("No server found.")
         self.app.ui.set_status("Starting as server.")
         self.this_computer.start_server()
         self.app.ui.set_status("Server Started.")
         return True
     else:
         lan_server = servers[0]
         if len(servers) > 1:
             self.app.ui.set_status(
                 "Found more than one server, choosing best interface..."
             )
             for server in servers:
                 interface = Interface.get_interface_by_ip(server)
                 if interface["lan"]:
                     lan_server = server
             self.app.ui.set_status("Connecting to server...")
             self.this_computer.connect_to_server(lan_server)
         else:
             server = servers[0]
             self.app.ui.set_status("Connecting to server...")
             self.this_computer.connect_to_server(server)
         self.app.ui.set_status("Connected to server.")
         return False
Beispiel #2
0
    def __init__(self, iface, wlan=None):
        Deauth.__init__(self)
        Eviltwin.__init__(self)
        DnsServer.__init__(self)
        SiteHandler.__init__(self)
        NetworkManager.__init__(self)
        InterfaceManager.__init__(self, iface)

        self.iface = iface
        self.wlan = wlan if wlan else iface

        self.out = '.data-01.out'
        self.csv = 'data-01.csv'
        self.cap = 'data-01.cap'

        self.ap = None
        self.atk = False
        self.alive = True
        self._exit = False
        self.bssid = None
        self.essid = None
        self.loadAp = True
        self.channel = None
        self.handshake = None
        self.monitorNetwork = False
Beispiel #3
0
    def __init__(self, calorimeters, beam):
        """
        Constructor
        """

        ## Configure the parent class
        window_config = Config(sample_buffers=1,
                               samples=4,
                               depth_size=16,
                               double_buffer=True)
        super(Display, self).__init__(resizable=True, config=window_config)

        ## Initial point of view
        self.yaw = -90.0
        self.pitch = -20.0
        self.zoom = 15.0

        ## Automatic rotation
        self.omega = config.camera_rotation_speed
        self.rotating = True

        ## Window size
        self.width = 800
        self.height = 600

        ## 3D objects
        self.calorimeters = calorimeters
        self.particles = []
        self.beam = beam

        ## 2D objects
        self.interface = Interface(self.width, self.height)

        ## Control when to allow modification of calorimeter openGL primitives
        self.allow_calo_update = False

        ## Time control
        self.refresh_rate = 30

        ## Mini text editor for cuts
        self.text_input_mode = False
        self.text_editor = MiniText(70)

        ## pyglet time control
        pyglet.clock.schedule_interval(self.update, 1.0 / self.refresh_rate)

        ## input ROOT file management
        if not config.cut:
            self.reader = reader_module.Custom_Reader(config.filename,
                                                      config.treename)
        else:
            self.reader = reader_module.Custom_Reader(
                config.filename,
                config.treename,
                initial_cut_string=config.cut)
Beispiel #4
0
    def __init__(self, calorimeters, beam):
        """
        Constructor
        """

        ## Configure the parent class
        window_config = Config(sample_buffers=1, samples=4, depth_size=16, double_buffer=True)
        super(Display, self).__init__(resizable=True,
                                      config=window_config)

        ## Initial point of view
        self.yaw = -90.0
        self.pitch = -20.0
        self.zoom = 15.0

        ## Automatic rotation
        self.omega = config.camera_rotation_speed
        self.rotating = True

        ## Window size
        self.width=800
        self.height=600

        ## 3D objects
        self.calorimeters = calorimeters
        self.particles = []
        self.beam = beam

        ## 2D objects
        self.interface = Interface(self.width, self.height)

        ## Control when to allow modification of calorimeter openGL primitives
        self.allow_calo_update = False

        ## Time control
        self.refresh_rate = 30

        ## Mini text editor for cuts
        self.text_input_mode = False
        self.text_editor = MiniText(70)

        ## pyglet time control
        pyglet.clock.schedule_interval(self.update, 1.0/self.refresh_rate)

        ## input ROOT file management
        if not config.cut:
            self.reader = reader_module.Custom_Reader(config.filename,
                                                      config.treename)
        else:
            self.reader = reader_module.Custom_Reader(config.filename,
                                                      config.treename,
                                                      initial_cut_string=config.cut)
Beispiel #5
0
from core.interface import Interface

if __name__ == "__main__":
    interface = Interface()
    interface.show()
Beispiel #6
0
    cfg = json.load(json_data_file)

# getting data
f = gzip.open('../data/mnist.pkl.gz', 'rb')
training_set, validation_set, test_set = cPickle.load(f, encoding='latin1')
f.close()

training_label = np.zeros((training_set[1].size, training_set[1].max() + 1))
training_label[np.arange(training_set[1].size), training_set[1]] = 1

validation_label = np.zeros(
    (validation_set[1].size, validation_set[1].max() + 1))
validation_label[np.arange(validation_set[1].size), validation_set[1]] = 1

model = Model(cfg,
              input_layer={
                  "dtype": training_set[0].dtype,
                  "size": 784
              },
              output_layer={
                  "dtype": training_label.dtype,
                  "size": 10
              })
interface = Interface(
    cfg=cfg,
    model=model,
    train=(training_set[0], training_label),
    test=(validation_set[0], validation_label),
)
interface.start()
Beispiel #7
0
raw_seq = raw_df['Close'].tolist()
raw_seq = np.array(raw_seq)

seq = [
    np.array(raw_seq[i * cfg['n_feature']:(i + 1) * cfg['n_feature']])
    for i in range(len(raw_seq) // cfg['n_feature'])
]
seq = [seq[0] / seq[0][0] - 1.0
       ] + [curr / seq[i][-1] - 1.0 for i, curr in enumerate(seq[1:])]

X = np.array(
    [seq[i:i + cfg['n_step']] for i in range(len(seq) - cfg['n_step'])])
y = np.array([seq[i + cfg['n_step']] for i in range(len(seq) - cfg['n_step'])])

train_size = int(len(X) * (1.0 - 0.05))
train_X, test_X = X[:train_size], X[train_size:]
train_y, test_y = y[:train_size], y[train_size:]

model = RNN(cfg)
# model.dry_run(train_x, train_y)
#
interface = Interface(
    cfg=cfg,
    model=model,
    train=(train_X, train_y),
    test=(test_X, test_y),
)
# interface.historical = [y for x in validation_y for y in x]
interface.start()
Beispiel #8
0
#!.venv/bin/python
from core.interface import Interface

Interface.launch()
Beispiel #9
0
class Display(pyglet.window.Window):

    ## --------------------------------------- ##
    def __init__(self, calorimeters, beam):
        """
        Constructor
        """

        ## Configure the parent class
        window_config = Config(sample_buffers=1,
                               samples=4,
                               depth_size=16,
                               double_buffer=True)
        super(Display, self).__init__(resizable=True, config=window_config)

        ## Initial point of view
        self.yaw = -90.0
        self.pitch = -20.0
        self.zoom = 15.0

        ## Automatic rotation
        self.omega = config.camera_rotation_speed
        self.rotating = True

        ## Window size
        self.width = 800
        self.height = 600

        ## 3D objects
        self.calorimeters = calorimeters
        self.particles = []
        self.beam = beam

        ## 2D objects
        self.interface = Interface(self.width, self.height)

        ## Control when to allow modification of calorimeter openGL primitives
        self.allow_calo_update = False

        ## Time control
        self.refresh_rate = 30

        ## Mini text editor for cuts
        self.text_input_mode = False
        self.text_editor = MiniText(70)

        ## pyglet time control
        pyglet.clock.schedule_interval(self.update, 1.0 / self.refresh_rate)

        ## input ROOT file management
        if not config.cut:
            self.reader = reader_module.Custom_Reader(config.filename,
                                                      config.treename)
        else:
            self.reader = reader_module.Custom_Reader(
                config.filename,
                config.treename,
                initial_cut_string=config.cut)

    ## ---------------------------------------- ##
    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        """
        Mouse control 1: click and drag to rotate around the scene
        """

        ## Click
        if buttons == mouse.LEFT:

            ## translate mouse movement into rotation of point of view
            self.yaw += dx * config.yaw_speed
            self.pitch += dy * config.pitch_speed

            ## Do not allow periodicity on pitch
            if self.pitch > config.max_pitch:
                self.pitch = config.max_pitch

            if self.pitch < config.min_pitch:
                self.pitch = config.min_pitch

            ## Do not allow yaw to grow arbitrarily large
            if self.yaw > 180.0:
                self.yaw = self.yaw - 360.0

            if self.yaw < -180.0:
                self.yaw = self.yaw + 360.0

    # ---------------------------------------- ##
    def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
        """
        Mouse control 2: scroll up and down to zoom in and out
        """

        ## Translate mouse scroll into zoom
        self.zoom += scroll_y * config.zoom_speed

        ## Only allow zoom in specified range
        if self.zoom < config.max_zoom:
            self.zoom = config.max_zoom
        if self.zoom > config.min_zoom:
            self.zoom = config.min_zoom

    ## ---------------------------------------- ##
    def on_text(self, text):
        """
        Keyboard control 1: typing in the text editor
        """

        ## Must be in text input mode, otherwise keys for control
        if self.text_input_mode:
            ## Prevent 'c' (used to trigger text input mode) to be included in text editor
            if self.negate_key:
                self.negate_key = False
            else:
                ## Collect character into text editor and pass formatted output to
                ## 2D inteface
                self.text_editor.insert(text)
                self.interface.set_text(self.text_editor.full_output)

    ## ---------------------------------------- ##
    def on_key_press(self, symbol, modifiers):
        """
        Keyboard control 2: main controls
        """

        ## Main controls
        if not self.text_input_mode:

            ## Turn on text input mode, enter cut
            if symbol == key.C:
                self.text_input_mode = True
                self.negate_key = True
                ## Display cut message
                self.interface.toggle_cut()
                ## Cut history navigator
                self.history_index = -1

            ## Reset the cut, go back to full ROOT tree
            if symbol == key.R:
                if self.reader.current_cut != CUT_NO_SELECTION:
                    self.reader.reset_cut()
                    self.interface.reset_cut()

            ## Transverse view
            if symbol == key.A:
                ## Stop automatic rotation
                self.rotating = False

                self.yaw = 0.0
                self.pitch = 0.0
                self.zoom = 15.0

            ## Longitudinal view
            if symbol == key.S:
                ## Stop automatic rotation
                self.rotating = False

                self.yaw = -90.0
                self.pitch = 0.0
                self.zoom = 15.0

            ## Toggle automatic rotation
            if symbol == key.D:
                if not self.rotating:
                    self.rotating = True
                elif self.rotating:
                    self.rotating = False

            ## Toggle help screen
            if symbol == key.H:
                self.interface.toggle_help()

            ## Event navigation
            if symbol == key.LEFT or symbol == key.RIGHT or symbol == key.UP or symbol == key.DOWN:

                ## Load particles from previous event
                if symbol == key.LEFT:
                    new_particles = self.reader.previous()

                ## Load particles from next event
                if symbol == key.RIGHT:
                    new_particles = self.reader.next()

                ## Load particles from a random event
                if symbol == key.UP or symbol == key.DOWN:
                    new_particles = self.reader.random()

                if new_particles is not None:

                    ## Prepare for new event, remove particles from old event
                    for particle in self.particles:
                        particle.delete()

                    self.particles = new_particles

                    ## Remove calorimeter energy
                    for calo in self.calorimeters:
                        calo.reset()
                        calo.energize(self.particles)

                    ## Print out event information to terminal
                    self.reader.print_event()

                    ## Beam collision animation
                    self.beam.start()

                    ## Allow modification of calorimeter openGL primitives
                    self.allow_calo_update = True

        ## Text editor controls
        if self.text_input_mode:

            ## Backspace expected behaviour
            if symbol == key.BACKSPACE:
                self.text_editor.backspace()
                self.interface.set_text(self.text_editor.full_output)

            ## Delete expected behaviour
            if symbol == key.DELETE:
                self.text_editor.delete()
                self.interface.set_text(self.text_editor.full_output)

            ## Move cursor left
            if symbol == key.LEFT:
                self.text_editor.cursor_left()
                self.interface.set_text(self.text_editor.full_output)

            ## Move cursor right
            if symbol == key.RIGHT:
                self.text_editor.cursor_right()
                self.interface.set_text(self.text_editor.full_output)

            ## Move back one step in history
            if symbol == key.UP:
                if self.history_index == -1:
                    self.current_input = self.text_editor.text_output
                self.history_index += 1
                n = len(self.reader.history)
                if self.history_index >= n:
                    self.history_index = n - 1
                else:
                    self.text_editor.set(
                        self.reader.history[self.history_index])
                    self.interface.set_text(self.text_editor.full_output)

            ## Move forward one step in history
            if symbol == key.DOWN:
                self.history_index -= 1

                if self.history_index < 0:
                    self.history_index = -1
                    self.text_editor.set(self.current_input)
                else:
                    self.text_editor.set(
                        self.reader.history[self.history_index])
                    self.interface.set_text(self.text_editor.full_output)

            ## Move cursor to beginning of text
            if symbol == key.A and modifiers & key.MOD_CTRL:
                self.text_editor.goto_begin()

            ## Move cursor to end of text
            if symbol == key.E and modifiers & key.MOD_CTRL:
                self.text_editor.goto_end()

            ## Kill the text following te cursor
            if symbol == key.K and modifiers & key.MOD_CTRL:
                self.text_editor.kill()

            ## Yank the text back at the cursor position
            if symbol == key.Y and modifiers & key.MOD_CTRL:
                self.text_editor.yank()

        ## Terminate text input mode and pass cut string to ROOT file reader
        if symbol == key.ENTER:
            ## Terminate text input mode
            if self.text_input_mode:
                self.text_input_mode = False
                ## Empty text editor and interface text, make cut message disappear
                if self.interface.cut.opacity > 0:
                    self.text_editor.reset()
                    self.interface.set_text('')
                    self.interface.toggle_cut()
                ## Pass cut string to ROOT file reader
                if self.reader.cut(self.text_editor.text_output):

                    ## Prepare for new event, remove particles from old event
                    for particle in self.particles:
                        particle.delete()
#
                    self.particles = []

                    ## Remove calorimeter energy
                    for calo in self.calorimeters:
                        calo.reset()
                        calo.energize(self.particles)

                    ## Select a random event that passes the cut, print and display
                    self.particles = self.reader.random()
                    self.reader.print_event()
                    self.beam.start()
                    self.allow_calo_update = True

        ## Quit CDER
        if symbol == key.ESCAPE:
            self.dispatch_event('on_close')

    ## ---------------------------------------- ##
    def on_resize(self, width, height):
        """
        Behaviour of displayed objects when resizing the window
        """

        ## Avoid displaying artefacts in memory
        self.clear()

        ## Protect against vanishing window
        if height == 0:
            height = 1

        ## Adjust openGL 3D perspective
        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45.0, float(width) / float(height), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)

        ## Adjust positioning and size of 2D objects
        self.interface.resize(width, height)

    ## ---------------------------------------- ##
    def mode_3D(self):
        """
        Set openGL to draw objects in the 3D scene
        """

        ## Rendering options
        glDisable(GL_DEPTH_TEST)
        glEnable(GL_BLEND)
        glShadeModel(GL_SMOOTH)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE)
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

        ## Perspective definition
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45.0,
                       float(self.width) / float(self.height), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

    ## ---------------------------------------- ##
    def mode_2D(self):
        """
        Set openGL to draw objects in the 2D scene in front of the 3D scene
        """

        ## Rendering options
        #glDisable(GL_DEPTH_TEST)

        ## Perspective options
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluOrtho2D(0, self.width, 0, self.height)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

    ## ---------------------------------------- ##
    def update(self, dt):
        """
        Update the displayed 3D and 2D scenes
        """

        ## Automatic rotation
        if self.rotating:
            self.yaw += self.omega * dt
            ## Do not allow yaw to grow arbitrarily large
            if self.yaw > 180.0:
                self.yaw = self.yaw - 360.0
            if self.yaw < -180.0:
                self.yaw = self.yaw + 360.0

        ## Update beam positions (only during beam animation)
        if self.beam.incoming:
            self.beam.update(dt)

        ## Update calorimeters
        for calo in self.calorimeters:
            calo.update(dt)

        ## Update interface
        if self.text_input_mode:
            self.text_editor.update(dt)
            self.interface.set_text(self.text_editor.full_output)
        self.interface.update(dt)

        ## Redraw the scene
        self.draw()

    ## ---------------------------------------- ##
    def draw(self):
        """
        Draw the 3D and 2D scenes
        """

        ## Clear openGL buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()

        ## Draw the 3D scene first
        self.mode_3D()

        ## Point of view
        gluLookAt(0.0, 0.0, -self.zoom, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)

        ## Rotate the scene to emulate yaw
        glRotatef(self.yaw, 0.0, 1.0, 0.0)

        ## Determine the pitch axis
        phi_camera = self.yaw * math.pi / 180.0
        theta_camera = self.pitch * math.pi / 180.0

        ## Rotate the scene to emulate pitch
        glRotatef(self.pitch, math.cos(phi_camera), 0.0, math.sin(phi_camera))

        ## Determine camera position w.r.t. calorimeter coordinate system
        ## This allow ordering of cell drawing in case of disabled depth test
        theta_camera += math.pi / 2
        theta_calo, phi_calo = utils.sphy_to_sphz(theta_camera, phi_camera)
        if phi_camera > 0:
            theta_camera = -theta_camera

        ## Draw calorimeters
        for calo in self.calorimeters:
            calo.theta_camera = theta_calo
            calo.r_camera = self.zoom
            calo.phi_camera = theta_camera - math.pi / 2
            calo.draw()

        ## Draw particles
        for particle in self.particles:
            particle.draw()

        ## Draw the 2D scene, delegate to interface
        self.mode_2D()
        self.interface.draw()

        ## Switch back to 3D scene to allow for manipulation
        self.mode_3D()
Beispiel #10
0
class Display(pyglet.window.Window):

    ## --------------------------------------- ##
    def __init__(self, calorimeters, beam):
        """
        Constructor
        """

        ## Configure the parent class
        window_config = Config(sample_buffers=1, samples=4, depth_size=16, double_buffer=True)
        super(Display, self).__init__(resizable=True,
                                      config=window_config)

        ## Initial point of view
        self.yaw = -90.0
        self.pitch = -20.0
        self.zoom = 15.0

        ## Automatic rotation
        self.omega = config.camera_rotation_speed
        self.rotating = True

        ## Window size
        self.width=800
        self.height=600

        ## 3D objects
        self.calorimeters = calorimeters
        self.particles = []
        self.beam = beam

        ## 2D objects
        self.interface = Interface(self.width, self.height)

        ## Control when to allow modification of calorimeter openGL primitives
        self.allow_calo_update = False

        ## Time control
        self.refresh_rate = 30

        ## Mini text editor for cuts
        self.text_input_mode = False
        self.text_editor = MiniText(70)

        ## pyglet time control
        pyglet.clock.schedule_interval(self.update, 1.0/self.refresh_rate)

        ## input ROOT file management
        if not config.cut:
            self.reader = reader_module.Custom_Reader(config.filename,
                                                      config.treename)
        else:
            self.reader = reader_module.Custom_Reader(config.filename,
                                                      config.treename,
                                                      initial_cut_string=config.cut)

    


    ## ---------------------------------------- ##
    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        """
        Mouse control 1: click and drag to rotate around the scene
        """

        ## Click
        if buttons == mouse.LEFT:

            ## translate mouse movement into rotation of point of view
            self.yaw += dx * config.yaw_speed
            self.pitch += dy * config.pitch_speed

            ## Do not allow periodicity on pitch
            if self.pitch > config.max_pitch:
                self.pitch = config.max_pitch

            if self.pitch < config.min_pitch:
                self.pitch = config.min_pitch

            ## Do not allow yaw to grow arbitrarily large
            if self.yaw > 180.0:
                self.yaw = self.yaw - 360.0

            if self.yaw < -180.0:
                self.yaw = self.yaw + 360.0

                

    # ---------------------------------------- ##
    def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
        """
        Mouse control 2: scroll up and down to zoom in and out
        """

        ## Translate mouse scroll into zoom
        self.zoom += scroll_y * config.zoom_speed

        ## Only allow zoom in specified range
        if self.zoom < config.max_zoom:
            self.zoom = config.max_zoom
        if self.zoom > config.min_zoom:
            self.zoom = config.min_zoom



    ## ---------------------------------------- ##
    def on_text(self, text):
        """
        Keyboard control 1: typing in the text editor
        """

        ## Must be in text input mode, otherwise keys for control
        if self.text_input_mode:
            ## Prevent 'c' (used to trigger text input mode) to be included in text editor
            if self.negate_key:
                self.negate_key = False
            else:
                ## Collect character into text editor and pass formatted output to
                ## 2D inteface
                self.text_editor.insert(text)
                self.interface.set_text(self.text_editor.full_output)



    ## ---------------------------------------- ##
    def on_key_press(self, symbol, modifiers):
        """
        Keyboard control 2: main controls
        """

        ## Main controls
        if not self.text_input_mode:

            ## Turn on text input mode, enter cut
            if symbol == key.C:
                self.text_input_mode = True
                self.negate_key = True
                ## Display cut message
                self.interface.toggle_cut()
                ## Cut history navigator
                self.history_index = -1

            ## Reset the cut, go back to full ROOT tree 
            if symbol == key.R:
                if self.reader.current_cut != CUT_NO_SELECTION:
                    self.reader.reset_cut()
                    self.interface.reset_cut()

            ## Transverse view
            if symbol == key.A:
                ## Stop automatic rotation
                self.rotating = False
                
                self.yaw = 0.0
                self.pitch = 0.0
                self.zoom = 15.0

            ## Longitudinal view
            if symbol == key.S:
                ## Stop automatic rotation
                self.rotating = False
                
                self.yaw = -90.0
                self.pitch = 0.0
                self.zoom = 15.0

            ## Toggle automatic rotation
            if symbol == key.D:
                if not self.rotating:
                    self.rotating = True
                elif self.rotating:
                    self.rotating = False

            ## Toggle help screen
            if symbol == key.H:
                self.interface.toggle_help()

            ## Event navigation
            if symbol == key.LEFT or symbol == key.RIGHT or symbol == key.UP or symbol == key.DOWN:

                ## Load particles from previous event
                if symbol == key.LEFT:
                    new_particles = self.reader.previous()

                ## Load particles from next event
                if symbol == key.RIGHT:
                    new_particles = self.reader.next()

                ## Load particles from a random event
                if symbol == key.UP or symbol == key.DOWN:
                    new_particles = self.reader.random()

                if new_particles is not None:
                    
                    ## Prepare for new event, remove particles from old event
                    for particle in self.particles:
                        particle.delete()

                    self.particles = new_particles

                    ## Remove calorimeter energy
                    for calo in self.calorimeters:
                        calo.reset()
                        calo.energize(self.particles)

                    ## Print out event information to terminal
                    self.reader.print_event()

                    ## Beam collision animation
                    self.beam.start()

                    ## Allow modification of calorimeter openGL primitives
                    self.allow_calo_update = True


        ## Text editor controls
        if self.text_input_mode:

            ## Backspace expected behaviour
            if symbol == key.BACKSPACE:
                self.text_editor.backspace()
                self.interface.set_text(self.text_editor.full_output)

            ## Delete expected behaviour
            if symbol == key.DELETE:
                self.text_editor.delete()
                self.interface.set_text(self.text_editor.full_output)

            ## Move cursor left
            if symbol == key.LEFT:
                self.text_editor.cursor_left()
                self.interface.set_text(self.text_editor.full_output)

            ## Move cursor right
            if symbol == key.RIGHT:
                self.text_editor.cursor_right()
                self.interface.set_text(self.text_editor.full_output)

            ## Move back one step in history
            if symbol == key.UP:
                if self.history_index == -1:
                    self.current_input = self.text_editor.text_output
                self.history_index +=1
                n = len(self.reader.history)
                if self.history_index >= n:
                    self.history_index = n-1
                else:
                    self.text_editor.set(self.reader.history[self.history_index])
                    self.interface.set_text(self.text_editor.full_output)

            ## Move forward one step in history
            if symbol == key.DOWN:
                self.history_index -=1
                
                if self.history_index < 0:
                    self.history_index = -1
                    self.text_editor.set(self.current_input)
                else:
                    self.text_editor.set(self.reader.history[self.history_index])
                    self.interface.set_text(self.text_editor.full_output)
                

            ## Move cursor to beginning of text
            if symbol == key.A and modifiers & key.MOD_CTRL:
                self.text_editor.goto_begin()

            ## Move cursor to end of text
            if symbol == key.E and modifiers & key.MOD_CTRL:
                self.text_editor.goto_end()

            ## Kill the text following te cursor
            if symbol == key.K and modifiers & key.MOD_CTRL:
                self.text_editor.kill()

            ## Yank the text back at the cursor position
            if symbol == key.Y and modifiers & key.MOD_CTRL:
                self.text_editor.yank()

        ## Terminate text input mode and pass cut string to ROOT file reader
        if symbol == key.ENTER:
            ## Terminate text input mode
            if self.text_input_mode:
                self.text_input_mode = False
                ## Empty text editor and interface text, make cut message disappear
                if self.interface.cut.opacity > 0:
                    self.text_editor.reset()
                    self.interface.set_text('')
                    self.interface.toggle_cut()
                ## Pass cut string to ROOT file reader
                if self.reader.cut(self.text_editor.text_output):

                    ## Prepare for new event, remove particles from old event
                    for particle in self.particles:
                        particle.delete()
# 
                    self.particles = []
                    
                    ## Remove calorimeter energy
                    for calo in self.calorimeters:
                        calo.reset()
                        calo.energize(self.particles)
                        
                
                    ## Select a random event that passes the cut, print and display
                    self.particles = self.reader.random()
                    self.reader.print_event()
                    self.beam.start()
                    self.allow_calo_update = True
                

        ## Quit CDER
        if symbol == key.ESCAPE:
            self.dispatch_event('on_close')



    ## ---------------------------------------- ##
    def on_resize(self,width, height):
        """
        Behaviour of displayed objects when resizing the window
        """

        ## Avoid displaying artefacts in memory
        self.clear()
        
        ## Protect against vanishing window
        if height == 0:
            height=1

        ## Adjust openGL 3D perspective
        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45.0, float(width) / float(height), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)

        ## Adjust positioning and size of 2D objects
        self.interface.resize(width, height)



    ## ---------------------------------------- ##
    def mode_3D(self):
        """
        Set openGL to draw objects in the 3D scene
        """

        ## Rendering options
        glDisable(GL_DEPTH_TEST)
        glEnable(GL_BLEND)
        glShadeModel(GL_SMOOTH)
        glBlendFunc(GL_SRC_ALPHA,GL_ONE)
        glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

        ## Perspective definition
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45.0, float(self.width) / float(self.height), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()



    ## ---------------------------------------- ##
    def mode_2D(self):
        """
        Set openGL to draw objects in the 2D scene in front of the 3D scene
        """

        ## Rendering options
        #glDisable(GL_DEPTH_TEST) 

        ## Perspective options
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluOrtho2D(0, self.width, 0, self.height)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        


    ## ---------------------------------------- ##
    def update(self, dt):
        """
        Update the displayed 3D and 2D scenes
        """

        ## Automatic rotation
        if self.rotating:
            self.yaw += self.omega * dt
            ## Do not allow yaw to grow arbitrarily large
            if self.yaw > 180.0:
                self.yaw = self.yaw - 360.0
            if self.yaw < -180.0:
                self.yaw = self.yaw + 360.0
        
        ## Update beam positions (only during beam animation)
        if self.beam.incoming:
            self.beam.update(dt)

        ## Update calorimeters
        for calo in self.calorimeters:
            calo.update(dt)

        ## Update interface
        if self.text_input_mode:
            self.text_editor.update(dt)
            self.interface.set_text(self.text_editor.full_output)
        self.interface.update(dt)

        ## Redraw the scene
        self.draw()
    
        
        
    ## ---------------------------------------- ##
    def draw(self):
        """
        Draw the 3D and 2D scenes
        """

        ## Clear openGL buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()

        ## Draw the 3D scene first
        self.mode_3D()
        
        ## Point of view
        gluLookAt( 0.0,  0.0, -self.zoom,
                   0.0,  0.0,  0.0,
                   0.0,  1.0,  0.0 )

        ## Rotate the scene to emulate yaw
        glRotatef(self.yaw, 0.0, 1.0, 0.0)

        ## Determine the pitch axis
        phi_camera = self.yaw*math.pi / 180.0
        theta_camera = self.pitch*math.pi / 180.0

        ## Rotate the scene to emulate pitch
        glRotatef(self.pitch, math.cos(phi_camera), 0.0, math.sin(phi_camera))

        ## Determine camera position w.r.t. calorimeter coordinate system
        ## This allow ordering of cell drawing in case of disabled depth test
        theta_camera += math.pi/2
        theta_calo, phi_calo = utils.sphy_to_sphz(theta_camera, phi_camera) 
        if phi_camera > 0:
            theta_camera = -theta_camera

        ## Draw calorimeters
        for calo in self.calorimeters:
            calo.theta_camera = theta_calo
            calo.r_camera = self.zoom
            calo.phi_camera = theta_camera - math.pi/2
            calo.draw()

        ## Draw particles
        for particle in self.particles:
            particle.draw()

        ## Draw the 2D scene, delegate to interface
        self.mode_2D()
        self.interface.draw()

        ## Switch back to 3D scene to allow for manipulation
        self.mode_3D()