Gaudi Framework, version v21r6

Home   Generated: 11 Nov 2009

TupleUtils.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # =============================================================================
00003 ## This module contains set of simple and useful utilities to booking and
00004 #  manipulation with N-Tuples (in the spirit of GaudiTuples<TYPE>)
00005 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
00006 #  @date 2007-08-04
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'        ,  ## function to book/retrieve N-tuple
00018     'getNTuple'     ,  ## ditto
00019     'getNtuple'     ,  ## ditto
00020     'getntuple'     ,  ## ditto
00021     'gettuple'      ,  ## ditto
00022     'activeTuples'  ,  ## get the list of all active n-tuples
00023     'releaseTuples'    ## release all actibe N-tuples
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 # the list of aquired tools (to be released)
00035 _TOOLS_ = []
00036 
00037 # =============================================================================
00038 ## Helper private auxillary utility to get Tool Service
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                                ## RETURN
00046     gaudi = kwargs.get ( 'gaudi' , None )
00047     if not gaudi : gaudi = AppMgr()
00048     return gaudi.toolsvc()                                 ## RETURN
00049 
00050 
00051 # =============================================================================
00052 ## Retrive N-Tuple ( book on demand )
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 ## Retrieve (book-on-demand) 'Smart'-N-tuple object.
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     # construct the name of the intermediate TupleTool
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     ## define tool properties
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     ## get the tool from Tool service
00119     tool = toolSvc.create ( 'TupleTool'       ,
00120                             name              ,
00121                             interface = _Tool )
00122 
00123     ## check the properties and redefine them if needed
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 )               ## RETURN
00134 
00135     return tool.nTuple ( ID , ID2 )                      ## RETURN
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 ## Return the list of active tools
00150 def activeTuples () :
00151     """
00152     Return the list of active tools
00153     """
00154     return _TOOLS_
00155 
00156 # =============================================================================
00157 ## Release the active tool/tuples
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 # The end
00194 # =============================================================================

Generated at Wed Nov 11 16:23:09 2009 for Gaudi Framework, version v21r6 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004