2 Module to configure the persistency type in GaudiPython.
4 __author__ =
"Marco Clemencic <marco.clemencic@cern.ch>"
6 class PersistencyError(RuntimeError):
8 Base class for exceptions in PersistencyHelper.
12 class UnknownPersistency(PersistencyError):
14 Exception raised if the persistency type is not known to the module.
16 def __init__(self, type_):
17 super(UnknownPersistency, self).__init__(
"Unknown persistency type %r" % type_)
25 Return the PersistencyHerper implementing the given persistency type.
27 for i
in _implementations:
30 raise UnknownPersistency(type_)
34 Function to extend the list of known helpers.
36 New helpers are added to the top of the list.
38 _implementations.insert(0, instance)
40 class FileDescription(object):
41 def __init__(self, filename, opt, svc, sel=None, collection=None, fun=None):
43 Class to hold/manipulate the file description.
45 @param filename: name of the file
46 @param opt: option (READ/CREATE/RECREATE/WRITE)
47 @param svc: conversion service (or selector)
48 @param sel: selection expression
49 @param collection: collection
50 @param fun: selection class
52 self.filename = filename
56 self.collection = collection
60 Return a list of pairs describing the instance.
62 return [(
"DATAFILE", self.filename),
66 (
"COLLECTION", self.collection),
70 Return the string representation of the file description to be passed
73 return " ".join([
"%s='%s'" % (k, v)
for k, v
in self.__data__()
if v])
75 class PersistencyHelper(object):
77 Base class for extensions to persistency configuration in GaudiPython.
79 def __init__(self, types):
81 Define the type of persistencies supported by the instance.
83 self.types = set(types)
84 def handle(self, typ):
86 Returns True if the current instance understands the requested
89 return typ
in self.types
91 class RootPersistency(PersistencyHelper):
93 Implementation of PersistencyHelper based on Gaudi::RootCnvSvc.
99 Declare the type of supported persistencies to the base class.
101 super(RootPersistency, self).__init__([
"ROOT",
"POOL_ROOT",
102 "RootCnvSvc",
"Gaudi::RootCnvSvc"])
103 self.configured =
False
105 def configure(self, appMgr):
109 if not self.configured:
111 appMgr.service(
'Gaudi::RootCnvSvc/RootCnvSvc')
112 eps = appMgr.service (
'EventPersistencySvc' )
113 eps.CnvServices += [
'RootCnvSvc']
114 self.configured =
True
116 def formatInput(self, filenames, **kwargs):
118 Translate a list of file names in a list of input descriptions.
120 The optional parameters 'collection', 'sel' and 'fun' should be used to
121 configure Event Tag Collection inputs.
123 @param filenames: the list of files
125 if not self.configured:
126 raise PersistencyError(
"Persistency not configured")
127 if type(filenames)
is str:
128 filenames = [filenames]
131 fileargs = dict([(k, kwargs[k])
132 for k
in [
"collection",
"sel",
"fun"]
136 svc =
'Gaudi::RootCnvSvc'
138 svc =
'Gaudi::RootEvtSelector'
140 [FileDescription(f,
'READ', svc, **fileargs)
143 def formatOutput(self, filename, **kwargs):
145 Translate a filename in an output description.
147 @param filenames: the list of files
148 @param lun: Logical Unit for Event Tag Collection outputs (optional)
150 if not self.configured:
151 raise PersistencyError(
"Persistency not configured")
152 retval = str(FileDescription(filename,
'RECREATE',
'Gaudi::RootCnvSvc'))
154 retval =
"%s %s" % (kwargs[
'lun'], retval)
158 add(RootPersistency())
struct GAUDI_API map
Parametrisation class for map-like implementation.