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 while 1 < tool.refCount() : toolSvc._its.releaseTool ( tool )
00131
00132 _TOOLS_.append ( tool )
00133 if not ID2 : return tool.nTuple ( ID )
00134
00135 return tool.nTuple ( ID , ID2 )
00136
00137
00138 nTuple . __doc__ += "\n\t help(ITupleTool.nTuple) : \n" \
00139 + _Tool.nTuple.__doc__
00140
00141 ntuple = nTuple
00142 getNTuple = nTuple
00143 getNtuple = nTuple
00144 getntuple = nTuple
00145 getTuple = nTuple
00146 gettuple = nTuple
00147
00148
00149
00150 def activeTuples () :
00151 """
00152 Return the list of active tools
00153 """
00154 return _TOOLS_
00155
00156
00157
00158 def releaseTuples () :
00159 """
00160 Release the active tool/tuples
00161 The method needs to be invoked explicitely at the end of the job
00162 """
00163 if not _TOOLS_ : return
00164 print ' %s/%s: release all pending ITupleTools: %s' % ( __file__ ,
00165 __name__ ,
00166 len(_TOOLS_) )
00167 toolSvc = _getToolSvc()
00168 while _TOOLS_ and toolSvc.isValid() :
00169 t = _TOOLS_.pop()
00170 if t and 0 < t.refCount() : toolSvc._its.releaseTool( t )
00171
00172
00173 def _TupleUtils_AtExit_ () :
00174 """
00175 AtExit function for GaudiPython.TupleUtils module
00176 """
00177 if activeTuples() :
00178 print 'WARNING: the list of local TupleTools is not empty!'
00179 print 'WARNING: please use GaudiPython.TupleUtils.releaseTuples() at the end'
00180
00181 import atexit
00182 atexit.register ( _TupleUtils_AtExit_ )
00183
00184
00185 if "__main__" == __name__ :
00186 import sys
00187 print __doc__ , __all__
00188 for o in __all__ :
00189 print "\n\n\t",o,"\n"
00190 print sys.modules[__name__].__dict__[o].__doc__
00191
00192
00193
00194