Commit 6766e99f authored by Marco Clemencic's avatar Marco Clemencic
Browse files

GaudiPython: prototype of generic/extensible persistency configuration

From: Marco Clemencic <>

git-svn-id: svn+ssh:// 53ecefc3-a64d-0410-9bdf-b8581fa3287e
parent e1b09179
Package: GaudiPython
Package manager : Marco Clemencic
! 2011-12-12 - Marco Clemencic
- First prototype of generic/extensible persistency configuration.
- GaudiPython.Persistency: (new) module hosting the persistency helpers
- GaudiPython.Bindings: modified iNTuleSvc and iEventSelector to use the new
! 2011-11-01 - Marco Clemencic
- Moved core services out of GaudiSvc and into the new packages GaudiCoreSvc
and GaudiCommonSvc. (patch #1816)
......@@ -521,16 +521,17 @@ class iNTupleSvc(iDataSvc) :
iDataSvc.__init__(self, name, ints)
def book(self, *args) :
return apply(, args)
def defineOutput(self, files, typ='ROOT') :
""" Defines dthe mapping between logical names and the output file
def defineOutput(self, files, typ="Gaudi::RootCnvSvc"):
""" Defines the mapping between logical names and the output file
defineOutput({'LUN1':'MyFile1.root', 'LUN2':'Myfile2.root'}, typ='ROOT')
defineOutput({'LUN1':'MyFile1.root', 'LUN2':'Myfile2.root'}, svc='Gaudi::RootCnvSvc')
out = []
for o in files :
out.append( "%s DATAFILE='%s' OPT='RECREATE' TYP='%s'" % ( o, files[o], typ ) )
self.Output = out
if AppMgr().HistogramPersistency == 'NONE' : AppMgr().HistogramPersistency = typ
import Persistency as prs
helper = prs.get(typ)
self.Output = [helper.formatOutput(files[lun], lun=lun) for lun in files]
if AppMgr().HistogramPersistency == 'NONE':
AppMgr().HistogramPersistency = "ROOT"
def __getitem__ ( self, path ) :
return iDataSvc.__getitem__( self , path )
......@@ -612,41 +613,11 @@ class iEventSelector(iService):
def __init__(self):
iService.__init__(self, 'EventSelector', Helper.service(gbl.Gaudi.svcLocator(),'EventSelector'))
self.__dict__['g'] = AppMgr()
def open(self, stream, typ = 'POOL_ROOT', opt = 'READ', sel = None, fun = None, collection = None ):
if typ == 'ROOT' :
self.g.service('RootEvtCnvSvc').DbType = 'ROOT'
self.g.service('EventPersistencySvc').CnvServices = ['RootEvtCnvSvc']
elif typ == 'POOL_ROOT':
cacsvc = self.g.service('PoolDbCacheSvc')
if hasattr(cacsvc, 'Dlls') : cacsvc.Dlls += ['lcg_RootStorageSvc', 'lcg_XMLCatalog']
else : cacsvc.Dlls = ['lcg_RootStorageSvc', 'lcg_XMLCatalog']
cacsvc.OutputLevel = 4
cacsvc.DomainOpts = [ 'Domain[ROOT_All].CLASS_VERSION=2 TYP=int',
'Domain[ROOT_Key].CLASS_VERSION=2 TYP=int',
'Domain[ROOT_Tree].CLASS_VERSION=2 TYP=int' ]
cacsvc.DatabaseOpts = ['']
cacsvc.ContainerOpts = ['']
cnvSvcs = [('PoolRootEvtCnvSvc', 'POOL_ROOT'),
('PoolRootTreeEvtCnvSvc', 'POOL_ROOTTREE'),
('PoolRootKeyEvtCnvSvc', 'POOL_ROOTKEY')]
for svc in cnvSvcs :
self.g.declSvcType(svc[0], 'PoolDbCnvSvc')
cnvsvc = self.g.service(svc[0])
cnvsvc.DbType = svc[1]
self.g.service('EventPersistencySvc').CnvServices = [ svc[0] for svc in cnvSvcs ]
for svc in cnvSvcs :
self.g.service('EventDataSvc').RootCLID = 1
if type(stream) != list : stream = [stream]
fixpart = "TYP=\'%s\' OPT=\'%s\'" % ( typ, opt )
if sel : fixpart += " SEL=\'%s\'" % sel
if fun : fixpart += " FUN=\'%s\'" % fun
if collection : fixpart += " COLLECTION=\'%s\'" % collection
cstream = ["DATAFILE=\'%s\' %s" % ( s, fixpart) for s in stream]
self.Input = cstream
def open(self, stream, typ = 'Gaudi::RootCnvSvc', **kwargs):
import Persistency as prs
helper = prs.get(typ)
self.Input = helper.formatInput(stream, **kwargs)
def rewind(self):
# It is not possible to reinitialize EventSelector only
Module to configure the persistency type in GaudiPython.
__author__ = "Marco Clemencic <>"
class PersistencyError(RuntimeError):
Base class for exceptions in PersistencyHelper.
class UnknownPersistency(PersistencyError):
Exception raised if the persistency type is not known to the module.
def __init__(self, type_):
super(UnknownPersistency, self).__init__("Unknown persistency type %r" % type_)
self.type = type_
# internal storage for the persistency helpers
_implementations = []
def get(type_):
Return the PersistencyHerper implementing the given persistency type.
for i in _implementations:
if i.handle(type_):
return i
raise UnknownPersistency(type_)
def add(instance):
Function to extend the list of known helpers.
New helpers are added to the top of the list.
_implementations.insert(0, instance)
class FileDescription(object):
def __init__(self, filename, opt, svc, sel=None, collection=None, fun=None):
Class to hold/manipulate the file description.
@param filename: name of the file
@param opt: option (READ/CREATE/RECREATE/WRITE)
@param svc: conversion service (or selector)
@param sel: selection expression
@param collection: collection
@param fun: selection class
self.filename = filename
self.opt = opt
self.svc = svc
self.sel = sel
self.collection = collection = fun
def __data__(self):
Return a list of pairs describing the instance.
return [("DATAFILE", self.filename),
("OPT", self.opt),
("SVC", self.svc),
("SEL", self.sel),
("COLLECTION", self.collection),
def __str__(self):
Return the string representation of the file description to be passed
to the application.
return " ".join(["%s='%s'" % (k, v) for k, v in self.__data__() if v])
class PersistencyHelper(object):
Base class for extensions to persistency configuration in GaudiPython.
def __init__(self, types):
Define the type of persistencies supported by the instance.
self.types = set(types)
def handle(self, typ):
Returns True if the current instance understands the requested
persistency type.
return typ in self.types
class RootPersistency(PersistencyHelper):
Implementation of PersistencyHelper based on Gaudi::RootCnvSvc.
def __init__(self):
Declare the type of supported persistencies to the base class.
super(RootPersistency, self).__init__(["ROOT", "POOL_ROOT",
"RootCnvSvc", "Gaudi::RootCnvSvc"])
self.configured = False
def configure(self, appMgr):
Basic configuration.
if not self.configured:
# instantiate the required services
eps = appMgr.service ( 'EventPersistencySvc' )
eps.CnvServices += ['RootCnvSvc']
self.configured = True
def formatInput(self, filenames, **kwargs):
Translate a list of file names in a list of input descriptions.
The optional parameters 'collection', 'sel' and 'fun' should be used to
configure Event Tag Collection inputs.
@param filenames: the list of files
if not self.configured:
raise PersistencyError("Persistency not configured")
if type(filenames) is str:
filenames = [filenames]
fileargs = {}
# check if we are accessing a collection
fileargs = dict([(k, kwargs[k])
for k in ["collection", "sel", "fun"]
if k in kwargs])
if fileargs:
# is a collection
svc = 'Gaudi::RootCnvSvc'
svc = 'Gaudi::RootEvtSelector'
return map(str,
[FileDescription(f, 'READ', svc, **fileargs)
for f in filenames])
def formatOutput(self, filename, **kwargs):
Translate a filename in an output description.
@param filenames: the list of files
@param lun: Logical Unit for Event Tag Collection outputs (optional)
if not self.configured:
raise PersistencyError("Persistency not configured")
retval = str(FileDescription(filename, 'RECREATE', 'Gaudi::RootCnvSvc'))
if 'lun' in kwargs:
retval = "%s %s" % (kwargs['lun'], retval)
return retval
# Adding the know instances to the list of helpers
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment