TupleUtils.py
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 """
00009 This module contains set of simple and useful utilities to booking and
00010 manipulation with N-Tuples (in the spirit of GaudiTuples<TYPE>)
00011
00012 """
00013
00014 __author__ = "Vanya BELYAEV ibelyaev@physics.syr.edu"
00015
00016 __all__ = (
00017 'nTuple' ,
00018 'getNTuple' ,
00019 'getNtuple' ,
00020 'getntuple' ,
00021 'gettuple' ,
00022 'activeTuples' ,
00023 'releaseTuples'
00024 )
00025
00026
00027 from GaudiPython.Bindings import gbl as cpp
00028 from GaudiPython.Bindings import AppMgr
00029 import GaudiPython.GaudiAlgs
00030
00031 _Tool = cpp.ITupleTool
00032 _Deco = cpp.GaudiPython.TupleToolDecorator
00033
00034
00035 _TOOLS_ = []
00036
00037
00038
00039 def _getToolSvc( **kwargs ) :
00040 """ Helper private auxillary utility to get Tool Service """
00041 svc = kwargs.get ( 'toolSvc' , None )
00042 if not svc : svc = kwargs.get ( 'toolsvc' , None )
00043 if not svc : svc = kwargs.get ( 'service' , None )
00044 if not svc : svc = kwargs.get ( 'svc' , None )
00045 else : return svc
00046 gaudi = kwargs.get ( 'gaudi' , None )
00047 if not gaudi : gaudi = AppMgr()
00048 return gaudi.toolsvc()
00049
00050
00051
00052
00053 def _nTuple_ ( s , *args ) :
00054 """ Retrive N-tuple ( book on demand ) """
00055 print 'ARGS:' , args
00056 return _Deco.nTuple ( s , *args)
00057
00058
00059 _nTuple_. __doc__ += "\n" + _Deco.nTuple . __doc__
00060 _Tool.nTuple = _nTuple_
00061 _Tool.ntuple = _nTuple_
00062
00063
00064
00065
00066 def nTuple ( dirpath , ID , ID2 = None , topdir = None , LUN = 'FILE1' ) :
00067 """
00068 Retrieve 'Smart'-N-tuple object.
00069 N-tuple is booked on-demand.
00070
00071 Atetntion !!
00072 The logical unit LUN must be configured by N-Tuple Service
00073
00074 Retrieve (book-n-demand) N-Tuple using
00075 the directory name and the title:
00076 >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
00077 'N-tuple title' , ## the title for N-Tuple
00078 LUN = 'FILE1' ) ## logical file unit
00079
00080 Retrieve (book-n-demand) N-Tuple using
00081 the directory name, literal ID and the title:
00082 >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
00083 'Tuple1' , ## the literal ID for N-Tuple
00084 'N-tuple title' , ## the title for N-Tuple
00085 LUN = 'FILE1' ) ## logical file unit
00086
00087 Retrieve (book-n-demand) N-Tuple using
00088 the directory name, numerical ID and the title:
00089 >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
00090 124 , ## the numerical ID for N-Tuple
00091 'N-tuple title' , ## the title for N-Tuple
00092 LUN = 'FILE1' ) ## logical file unit
00093
00094
00095 """
00096 toolSvc = _getToolSvc ()
00097
00098
00099 name = 'Tuple'+LUN+"/"
00100 if topdir : name += topdir
00101 name += dirpath
00102 name += "_%s"%ID
00103 if ID2 : name += "_%s"%ID2
00104 name=name.replace ( '.' , '_' )
00105 name=name.replace ( '/' , '_' )
00106 name=name.replace ( '\\' , '_' )
00107 name=name.replace ( ' ' , '_' )
00108
00109
00110 t0 = GaudiPython.iAlgTool( 'ToolSvc.'+name )
00111 t0.OutputLevel = 1
00112 t0.NTupleLUN = LUN
00113 t0.NTupleDir = dirpath
00114 t0.PropertiesPrint = False
00115 t0.OutputLevel = 4
00116 if topdir : t0.NTupleTopDir = topdir
00117
00118
00119 tool = toolSvc.create ( 'TupleTool' ,
00120 name ,
00121 interface = _Tool )
00122
00123
00124 t1 = GaudiPython.iAlgTool ( tool.name() , tool )
00125 if t1.NTupleLUN != LUN : t1.NTupleLUN = LUN
00126 if t1.NTupleDir != dirpath : t1.NTupleDir = dirpath
00127 if topdir and ( t1.NTupleTopDir != topdir ) :
00128 t1.NTupleTopDir = topdir
00129
00130 _TOOLS_.append ( tool )
00131 if not ID2 : return tool.nTuple ( ID )
00132
00133 return tool.nTuple ( ID , ID2 )
00134
00135
00136 nTuple . __doc__ += "\n\t help(ITupleTool.nTuple) : \n" \
00137 + _Tool.nTuple.__doc__
00138
00139 ntuple = nTuple
00140 getNTuple = nTuple
00141 getNtuple = nTuple
00142 getntuple = nTuple
00143 getTuple = nTuple
00144 gettuple = nTuple
00145
00146
00147
00148 def activeTuples () :
00149 """
00150 Return the list of active tools
00151 """
00152 return _TOOLS_
00153
00154
00155
00156 def releaseTuples () :
00157 """
00158 Release the active tool/tuples
00159 The method needs to be invoked explicitely at the end of the job
00160 """
00161 if not _TOOLS_ : return
00162 print ' %s/%s: release all pending ITupleTools: %s' % ( __file__ ,
00163 __name__ ,
00164 len(_TOOLS_) )
00165 from GaudiPython.Bindings import _gaudi
00166 if not _gaudi : return
00167
00168 toolSvc = _getToolSvc()
00169 if toolSvc.isValid() :
00170 while _TOOLS_ :
00171 t = _TOOLS_.pop()
00172 if not t : continue
00173 while 1 < t.refCount() :
00174 toolSvc._its.releaseTool( t )
00175
00176
00177 def _TupleUtils_AtExit_ () :
00178 """
00179 AtExit function for GaudiPython.TupleUtils module
00180 """
00181 if activeTuples() :
00182 print 'WARNING: the list of local TupleTools is not empty!'
00183 print 'WARNING: please use GaudiPython.TupleUtils.releaseTuples() at the end'
00184
00185 import atexit
00186 atexit.register ( _TupleUtils_AtExit_ )
00187
00188
00189 if "__main__" == __name__ :
00190 import sys
00191 print __doc__ , __all__
00192 for o in __all__ :
00193 print "\n\n\t",o,"\n"
00194 print sys.modules[__name__].__dict__[o].__doc__
00195
00196
00197
00198