| # Gaudi Framework, version v20r4
Generated: 8 Jan 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__ = ( 'nTuple' , 'getNTuple' , 'getNtuple'    , 'getntuple'     ,
    00017             'ntuple' , 'gettuple'  , 'activeTuples' , 'releaseTuples' )
    00018
    00019 import GaudiPython
    00020 import GaudiPython.GaudiAlgs
    00021
    00022 _gbl  = GaudiPython.gbl
    00023 _Tool = _gbl.ITupleTool
    00024 _Deco = _gbl.GaudiPython.TupleToolDecorator
    00025
    00026 # the list of aquired tools (to be released)
    00027 _TOOLS_ = []
    00028
    00029 # =============================================================================
    00030 ## Helper private auxillary utility to get Tool Service
    00031 def _getToolSvc( **kwargs ) :
    00032     """ Helper private auxillary utility to get Tool Service """
    00033     svc = kwargs.get ( 'toolSvc' , None )
    00034     if not svc : svc = kwargs.get ( 'toolsvc' , None )
    00035     if not svc : svc = kwargs.get ( 'service' , None )
    00036     if not svc : svc = kwargs.get ( 'svc'     , None )
    00037     else       : return svc                                ## RETURN
    00038     gaudi = kwargs.get ( 'gaudi' , None )
    00039     if not gaudi : gaudi = GaudiPython.AppMgr()
    00040     return gaudi.toolsvc()                                 ## RETURN
    00041
    00042
    00043 # =============================================================================
    00044 ## Retrive N-Tuple ( book on demand )
    00045 def _nTuple_ ( s , *args ) :
    00046     """ Retrive N-tuple ( book on demand )  """
    00047     print 'ARGS:' , args
    00048     return _Deco.nTuple ( s , *args)
    00049
    00050 # =============================================================================
    00051 _nTuple_. __doc__ += "\n" + _Deco.nTuple . __doc__
    00052 _Tool.nTuple = _nTuple_
    00053 _Tool.ntuple = _nTuple_
    00054
    00055
    00056
    00057 # =============================================================================
    00058 ## Retrieve (book-on-demand) 'Smart'-N-tuple object.
    00059 def nTuple ( dirpath , ID , ID2 = None , topdir = None , LUN = 'FILE1' ) :
    00060     """
    00061     Retrieve 'Smart'-N-tuple object.
    00062     N-tuple is booked on-demand.
    00063
    00064     Atetntion !!
    00065     The logical unit LUN must be configured by N-Tuple Service
    00066
    00067     Retrieve (book-n-demand) N-Tuple using
    00068     the  directory name and the title:
    00069     >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
    00070                      'N-tuple title'         , ## the title for N-Tuple
    00071                       LUN = 'FILE1'          ) ## logical file unit
    00072
    00073     Retrieve (book-n-demand) N-Tuple using
    00074     the  directory name, literal ID and the title:
    00075     >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
    00076                      'Tuple1'                , ## the literal ID for N-Tuple
    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, numerical ID and the title:
    00082     >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
    00083                      124                     , ## the numerical ID for N-Tuple
    00084                      'N-tuple title'         , ## the title for N-Tuple
    00085                       LUN = 'FILE1'          ) ## logical file unit
    00086
    00087
    00088     """
    00089     toolSvc = _getToolSvc ()
    00090
    00091     # construct the name of the intermediate TupleTool
    00092     name = 'Tuple'+LUN+"/"
    00093     if topdir : name += topdir
    00094     name += dirpath
    00095     name += "_%s"%ID
    00096     if  ID2 : name += "_%s"%ID2
    00097     name=name.replace ( '.'  , '_' )
    00098     name=name.replace ( '/'  , '_' )
    00099     name=name.replace ( '\\' , '_' )
    00100     name=name.replace ( ' '  , '_' )
    00101
    00102     ## define tool properties
    00103     t0 = GaudiPython.iAlgTool( 'ToolSvc.'+name )
    00104     t0.OutputLevel      = 1
    00105     t0.NTupleLUN        = LUN
    00106     t0.NTupleDir        = dirpath
    00107     t0.PropertiesPrint  = False
    00108     t0.OutputLevel      = 4
    00109     if topdir : t0.NTupleTopDir = topdir
    00110
    00111     ## get the tool from Tool service
    00112     tool = toolSvc.create ( 'TupleTool'       ,
    00113                             name              ,
    00114                             interface = _Tool )
    00115
    00116     ## check the properties and redefine them if needed
    00117     t1 = GaudiPython.iAlgTool ( tool.name() , tool )
    00118     if t1.NTupleLUN != LUN     : t1.NTupleLUN = LUN
    00119     if t1.NTupleDir != dirpath : t1.NTupleDir = dirpath
    00120     if topdir and ( t1.NTupleTopDir != topdir ) :
    00121         t1.NTupleTopDir = topdir
    00122
    00123     while 1 < tool.refCount() : toolSvc._its.releaseTool ( tool )
    00124
    00125     _TOOLS_.append ( tool )
    00126     if not ID2 : return tool.nTuple ( ID )               ## RETURN
    00127
    00128     return tool.nTuple ( ID , ID2 )                      ## RETURN
    00129
    00130
    00131 nTuple . __doc__ += "\n\t help(ITupleTool.nTuple) : \n" \
    00132                     + _Tool.nTuple.__doc__
    00133
    00134 ntuple    = nTuple
    00135 getNTuple = nTuple
    00136 getNtuple = nTuple
    00137 getntuple = nTuple
    00138 getTuple  = nTuple
    00139 gettuple  = nTuple
    00140
    00141 # =============================================================================
    00142 ## Return the list of active tools
    00143 def activeTuples () :
    00144     """
    00145     Return the list of active tools
    00146     """
    00147     return _TOOLS_
    00148
    00149 # =============================================================================
    00150 ## Release the active tool/tuples
    00151 def releaseTuples () :
    00152     """
    00153     Release the active tool/tuples
    00154     The method needs to be invoked explicitely at the end of the job
    00155     """
    00156     if not _TOOLS_ : return
    00157     print ' %s/%s: release all pending ITupleTools: %s' % ( __file__     ,
    00158                                                            __name__     ,
    00159                                                            len(_TOOLS_) )
    00160     toolSvc = _getToolSvc()
    00161     while _TOOLS_ and toolSvc.isValid() :
    00162         t = _TOOLS_.pop()
    00163         if t and 0 < t.refCount() : toolSvc._its.releaseTool( t )
    00164
    00165 if "__main__" == __name__ :
    00166     import sys
    00167     print __doc__ , __all__
    00168     for o in __all__ :
    00169         print "\n\n\t",o,"\n"
    00170         print sys.modules[__name__].__dict__[o].__doc__
    00171
    00172 # =============================================================================
    00173 # The end
    00174 # =============================================================================