The Gaudi Framework  master (53dee381)
Loading...
Searching...
No Matches
Persistency.py
Go to the documentation of this file.
11"""
12Module to configure the persistency type in GaudiPython.
13"""
14
15__author__ = "Marco Clemencic <marco.clemencic@cern.ch>"
16
17
18class PersistencyError(RuntimeError):
19 """
20 Base class for exceptions in PersistencyHelper.
21 """
22
23 pass
24
25
27 """
28 Exception raised if the persistency type is not known to the module.
29 """
30
31 def __init__(self, type_):
32 super().__init__(f"Unknown persistency type {type_!r}")
33 self.type = type_
34
35
36# internal storage for the persistency helpers
37_implementations = []
38
39
40def get(type_):
41 """
42 Return the PersistencyHerper implementing the given persistency type.
43 """
44 for i in _implementations:
45 if i.handle(type_):
46 return i
47 raise UnknownPersistency(type_)
48
49
50def add(instance):
51 """
52 Function to extend the list of known helpers.
53
54 New helpers are added to the top of the list.
55 """
56 _implementations.insert(0, instance)
57
58
60 def __init__(self, filename, opt, svc, sel=None, collection=None, fun=None):
61 """
62 Class to hold/manipulate the file description.
63
64 @param filename: name of the file
65 @param opt: option (READ/CREATE/RECREATE/WRITE)
66 @param svc: conversion service (or selector)
67 @param sel: selection expression
68 @param collection: collection
69 @param fun: selection class
70 """
71 self.filename = filename
72 self.opt = opt
73 self.svc = svc
74 self.sel = sel
75 self.collection = collection
76 self.fun = fun
77
78 def __data__(self):
79 """
80 Return a list of pairs describing the instance.
81 """
82 return [
83 ("DATAFILE", self.filename),
84 ("OPT", self.opt),
85 ("SVC", self.svc),
86 ("SEL", self.sel),
87 ("COLLECTION", self.collection),
88 ("FUN", self.fun),
89 ]
90
91 def __str__(self):
92 """
93 Return the string representation of the file description to be passed
94 to the application.
95 """
96 return " ".join([f"{k}='{v}'" for k, v in self.__data__() if v])
97
98
100 """
101 Base class for extensions to persistency configuration in GaudiPython.
102 """
103
104 def __init__(self, types):
105 """
106 Define the type of persistencies supported by the instance.
107 """
108 self.types = set(types)
109
110 def handle(self, typ):
111 """
112 Returns True if the current instance understands the requested
113 persistency type.
114 """
115 return typ in self.types
116
117
119 """
120 Implementation of PersistencyHelper based on Gaudi::RootCnvSvc.
121 """
122
123 def __init__(self):
124 """
125 Constructor.
126
127 Declare the type of supported persistencies to the base class.
128 """
129 super().__init__(["ROOT", "RootCnvSvc", "Gaudi::RootCnvSvc"])
130 self.configured = False
131
132 def configure(self, appMgr):
133 """
134 Basic configuration.
135 """
136 if not self.configured:
137 # instantiate the required services
138 appMgr.service("Gaudi::RootCnvSvc/RootCnvSvc")
139 eps = appMgr.service("EventPersistencySvc")
140 eps.CnvServices += ["RootCnvSvc"]
141 self.configured = True
142
143 def formatInput(self, filenames, **kwargs):
144 """
145 Translate a list of file names in a list of input descriptions.
146
147 The optional parameters 'collection', 'sel' and 'fun' should be used to
148 configure Event Tag Collection inputs.
149
150 @param filenames: the list of files
151 """
152 if not self.configured:
153 raise PersistencyError("Persistency not configured")
154 if isinstance(filenames, str):
155 filenames = [filenames]
156 fileargs = {}
157 # check if we are accessing a collection
158 fileargs = dict(
159 [(k, kwargs[k]) for k in ["collection", "sel", "fun"] if k in kwargs]
160 )
161 if fileargs:
162 # is a collection
163 svc = "Gaudi::RootCnvSvc"
164 else:
165 svc = "Gaudi::RootEvtSelector"
166 return [str(FileDescription(f, "READ", svc, **fileargs)) for f in filenames]
167
168 def formatOutput(self, filename, **kwargs):
169 """
170 Translate a filename in an output description.
171
172 @param filenames: the list of files
173 @param lun: Logical Unit for Event Tag Collection outputs (optional)
174 """
175 if not self.configured:
176 raise PersistencyError("Persistency not configured")
177 retval = str(FileDescription(filename, "RECREATE", "Gaudi::RootCnvSvc"))
178 if "lun" in kwargs:
179 retval = "{} {}".format(kwargs["lun"], retval)
180 return retval
181
182
183# Adding the know instances to the list of helpers
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
__init__(self, filename, opt, svc, sel=None, collection=None, fun=None)
formatOutput(self, filename, **kwargs)
formatInput(self, filenames, **kwargs)