~chris-rogers/maus/1312

« back to all changes in this revision

Viewing changes to src/output/OutputPySocket/OutputPySocket.py

  • Committer: Chris Rogers
  • Date: 2015-07-08 13:32:29 UTC
  • Revision ID: chris.rogers@stfc.ac.uk-20150708133229-2dcc61hkymee1m3x
sockets in framework

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:8080/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
Generate spills from a socket
 
18
"""
 
19
 
 
20
from docstore.root_document_store import RootDocumentStore
 
21
 
 
22
class OutputPySocket:
 
23
    """
 
24
    Generates a user defined number of empty spills, defined by the
 
25
    spill_generator_number_of_spills. Empty spill is just an empty dict.
 
26
    """
 
27
 
 
28
    def __init__(self):
 
29
        """
 
30
        Initialise InputPySpillGenerator to 0
 
31
        """
 
32
        self.doc_store = None
 
33
        self.collection = ""
 
34
 
 
35
    def birth(self, json_config):
 
36
        """
 
37
        birth() reads the number of spills from the configuration
 
38
        """
 
39
        host = ""
 
40
        port = 0
 
41
        timeout = 0
 
42
        poll_time = 0
 
43
        n_socket_retries = 0
 
44
        retry_time = 0
 
45
        n_docstore_retries = 0
 
46
        branch_name = ""
 
47
        self.last_doc = 0
 
48
        self.tree_names =  self.doc_store.default_tree_names()
 
49
        self.collection = ""
 
50
        self.doc_store = RootDocumentStore(timeout,
 
51
                                           poll_time,
 
52
                                           n_socket_retries,
 
53
                                           retry_time,
 
54
                                           n_docstore_retries)
 
55
        self.doc_store.connect({"host":host, "port":port})
 
56
        if not self.doc_store.has_collection(self.collection):
 
57
            raise RuntimeError("Could not find collection "+\
 
58
                               str(self.collection))
 
59
 
 
60
    def save(self, event):
 
61
        """
 
62
        Save an event
 
63
        """
 
64
        event_out = None
 
65
        if type(event) == type(""):
 
66
            event_out = json.loads(event)
 
67
        elif type(event) == type([]) or type(event) == type({}):
 
68
            event_out = event
 
69
        elif type(event) == type(ROOT.TObject):
 
70
            event_out = ROOT.TTree("OutputPySocket", "TTree") # pylint: disable = E1101
 
71
            for name, a_type in self.tree_names.iteritems():
 
72
                if type(event) == a_type:
 
73
                    event_out.Branch(name, event, event.GetSizeOf(), 1)
 
74
                    event_out.Fill()
 
75
        if event_out == None:
 
76
            raise TypeError("Could not unpack the type of event "+str(event))
 
77
        self.doc_store.put(self.collection, self.last_doc, event)
 
78
        self.last_doc += 1
 
79
 
 
80
    def death(self):
 
81
        """
 
82
        death() relinquishes the document store
 
83
        """
 
84
        self.doc_store.disconnect()
 
85
        return True
 
86