| # Gaudi Framework, version v21r11 |

Home |  | Generated: 30 Sep 2010

# Pythonizations.py

Go to the documentation of this file.

    00001 # File: GaudiPython/Pythonizations.py
    00002 # Author: Pere Mato (pere.mato@cern.ch)
    00003
    00004 """ This Pythonizations module provides a number of useful pythonizations
    00005     of adaptation of some classes.
    00006 """
    00007
    00008 __all__ = [ ]
    00009
    00010 import PyCintex
    00011 gbl = PyCintex.gbl
    00012
    00013 if not hasattr(gbl,'ostream') : gbl.gROOT.ProcessLine("#include <ostream>")
    00014 if not hasattr(gbl,'stringstream') : gbl.gROOT.ProcessLine("#include <sstream>")
    00015
    00016 #--- Hack to match the name scheme of dictionary on Linux ----------------------------
    00017 _loadDict_save = PyCintex.loadDict
    00018 def _loadDict(name):
    00019     import sys
    00020     if sys.platform != 'win32' and name[:3] != 'lib' : name = 'lib'+name
    00021     return _loadDict_save(name)
    00022 PyCintex.loadDict = _loadDict
    00023
    00024 #--- Adding extra functionality to C++ raw classes------------------------------------
    00025 def _printHisto1D(h) :
    00026     x = h.axis()
    00027     return  'Histogram 1D "%s" %d bins [%f,%f]' % (h.title(), x.bins(), x.lowerEdge(), x.upperEdge())
    00028 def _contentsHisto1D(h) :
    00029     x = h.axis()
    00030     return  map(h.binEntries, range(x.bins()))
    00031 def _printHisto2D(h) :
    00032     x,y = h.xAxis(),h.yAxis()
    00033     return  'Histogram 2D "%s" %d xbins [%f,%f], %d ybins [%f,%f]' % \
    00034     (h.title(), x.bins(), x.lowerEdge(), x.upperEdge(), y.bins(), y.lowerEdge(), y.upperEdge() )
    00035 def _printStatusCode(s) :
    00036     if s.isSuccess() : return 'SUCCESS'
    00037     else             : return 'FAILURE'
    00038 def _printBitReference(b) :
    00039     return str(1==b.bool())
    00040 def _printFillStream(o) :
    00041     if  o :
    00042         s = gbl.stringstream()
    00043         o.fillStream(s)
    00044         out = s.str()
    00045         if out == '' :
    00046             out = o.__class__.__name__ + ' object'
    00047             if hasattr( o, 'hasKey') and o.hasKey() :
    00048                 out += ' key = '+ str(o.key())
    00049     else :
    00050         out = o.__class__.__name__ + ' NULL object'
    00051     return out
    00052 def _container__getitem__(self, k) :
    00053     return self.containedObject(k)
    00054 def _container__len__(self) :
    00055     return self.numberOfObjects()
    00056 def _container__iter__(self) :
    00057     if hasattr(self,'containedObjects') : sequential = self.containedObjects()
    00058     else                                : sequential = self
    00059     count = 0
    00060     limit = self.__len__()
    00061     while count < limit :
    00062         yield sequential.__getitem__(count)
    00063         count += 1
    00064
    00065 def _draw_aida_ ( self , *args ) :
    00066     """
    00067     Draw AIDA histogram (through access to internal ROOT histogram
    00068
    00069     >>> aida = ...    # get the historgam
    00070     >>> aida.Draw()
    00071
    00072     """
    00073     _fun = PyCintex.gbl.Gaudi.Utils.Aida2ROOT.aida2root
    00074     _root = _fun ( self )
    00075     return _root.Draw( *args )
    00076
    00077 gbl.AIDA.IHistogram1D.__repr__ = _printHisto1D
    00078 gbl.AIDA.IHistogram1D.contents = _contentsHisto1D
    00079 gbl.AIDA.IHistogram2D.__repr__ = _printHisto2D
    00080 for h in (  gbl.AIDA.IHistogram   ,
    00081             gbl.AIDA.IHistogram1D ,
    00082             gbl.AIDA.IHistogram2D ,
    00083             gbl.AIDA.IHistogram3D ,
    00084             gbl.AIDA.IProfile1D   ,
    00085             gbl.AIDA.IProfile2D   ) :
    00086     h.Draw = _draw_aida_
    00087     h.plot = _draw_aida_
    00088
    00089 gbl.StatusCode.__repr__ = _printStatusCode
    00090 try: gbl._Bit_reference.__repr__ = _printBitReference
    00091 except: pass
    00092 gbl.ContainedObject.__repr__ = _printFillStream
    00093 gbl.DataObject.__repr__ = _printFillStream
    00094 gbl.ObjectContainerBase.__getitem__ = _container__getitem__
    00095 gbl.ObjectContainerBase.__len__ = _container__len__
    00096 gbl.ObjectContainerBase.__iter__ = _container__iter__
    00097
    00098 gbl.IUpdateManagerSvc.update = lambda self,obj: gbl.IUpdateManagerSvc.PythonHelper.update(self,obj)
    00099 gbl.IUpdateManagerSvc.invalidate = lambda self,obj: gbl.IUpdateManagerSvc.PythonHelper.invalidate(self,obj)
    00100
    00101 #---Globals--------------------------------------------------------------------
    00102 gbl.StatusCode.SUCCESS = 1
    00103 gbl.StatusCode.FAILURE = 0
    00104
    00105 # - string key, equality
    00106 if hasattr ( gbl.Gaudi.StringKey ,'__cpp_eq__' ) :
    00107     _eq = gbl.Gaudi.StringKey.__cpp_eq__
    00108     setattr ( gbl.Gaudi.StringKey ,'__eq__' , _eq )
    00109
    00110 # - string key, non-equality
    00111 if hasattr ( gbl.Gaudi.StringKey ,'__cpp_ne__' ) :
    00112     _ne = gbl.Gaudi.StringKey.__cpp_ne__
    00113     setattr ( gbl.Gaudi.StringKey ,'__ne__' , _ne )
    00114
    00115
    00116 #---Enabling Pickle support----------------------------------------------------
    00117 if  gbl.gROOT.GetVersionInt() <= 51800 :
    00118     import libPyROOT
    00119     gbl.GaudiPython.PyROOTPickle.Initialize(libPyROOT, libPyROOT.ObjectProxy)
    00120
    00121 # =============================================================================
    00122 ## decorate some map-like objects
    00123 # =============================================================================
    00124 ## The iterator for MapBase class
    00125 #
    00126 #  @code
    00127 #
    00128 #    >>> m = ...  ## the map
    00129 #    >>> for key in m : print key , m[key]
    00130 #
    00131 #  @endcode
    00132 #  @see Gaudi::Utils::MapBase
    00133 #  @see GaudiUtils::Map
    00134 #  @see GaudiUtils::HashMap
    00135 #  @see GaudiUtils::VectorMap
    00136 #  @see GaudiUtils::Map::key_at
    00137 #  @see GaudiUtils::HashMap::key_at
    00138 #  @see GaudiUtils::VectorMap::key_at
    00139 #  @author Vanya BELYAEV Ivan.Belyaev@itep.ru
    00140 #  @date 2010-02-20
    00141 def __mapbase_iter__ ( self ) :
    00142     """
    00143     The iterator for MapBase-based containers
    00144
    00145     >>> m = ...  ## the map
    00146     >>> for key in m : print key , m[key]
    00147
    00148     """
    00149     _size  = len ( self )
    00150     _index = 0
    00151     while _index < _size :
    00152         yield self.key_at ( _index )
    00153         _index +=1
    00154
    00155 # =============================================================================
    00156 ## The iterator for MapBase class
    00157 #
    00158 #  @code
    00159 #
    00160 #    >>> m = ...  ## the map
    00161 #    >>> for key,value in m.iteritems() : print key , value
    00162 #
    00163 #  @endcode
    00164 #  @see Gaudi::Utils::MapBase
    00165 #  @see GaudiUtils::Map
    00166 #  @see GaudiUtils::HashMap
    00167 #  @see GaudiUtils::VectorMap
    00168 #  @see GaudiUtils::Map::key_at
    00169 #  @see GaudiUtils::HashMap::key_at
    00170 #  @see GaudiUtils::VectorMap::key_at
    00171 #  @see GaudiUtils::Map::value_at
    00172 #  @see GaudiUtils::HashMap::value_at
    00173 #  @see GaudiUtils::VectorMap::value_at
    00174 #  @author Vanya BELYAEV Ivan.Belyaev@itep.ru
    00175 #  @date 2010-02-20
    00176 def __mapbase_iteritems__ ( self ) :
    00177     """
    00178     The iterator for MapBase-based containers
    00179
    00180     >>> m = ...  ## the map
    00181     >>> for key,value in m.iteritems() : print key, value
    00182
    00183     """
    00184     _size  = len ( self )
    00185     _index = 0
    00186     while _index < _size :
    00187         _key = self.key_at  ( _index )
    00188         yield ( _key , self.at ( _key ) )
    00189         _index +=1
    00190
    00191 # ============================================
    00192 ## Get the list of keys for the map
    00193 #
    00194 #  @code
    00195 #
    00196 #    >>> m    = ...        ## the map
    00197 #    >>> keys = m.keys()   ## get the list of keys
    00198 #
    00199 #  @endcode
    00200 #  @see Gaudi::Utils::MapBase
    00201 #  @see GaudiUtils::Map
    00202 #  @see GaudiUtils::HashMap
    00203 #  @see GaudiUtils::VectorMap
    00204 #  @see GaudiUtils::Map::key_at
    00205 #  @see GaudiUtils::HashMap::key_at
    00206 #  @ see GaudiUtils::VectorMap::key_at
    00207 #  @author Vanya BELYAEV Ivan.BElyaev@itep.ru
    00208 #  @date 2010-02-20
    00209 def __mapbase_keys__ ( self ) :
    00210     """
    00211     Get the list of keys
    00212
    00213     >>> m = ...           ## the map
    00214     >>> keys = m.keys()   ## get the list of keys
    00215
    00216     """
    00217     _size  = len ( self )
    00218     _keys  = []
    00219     for i in range ( 0 , _size ) : _keys.append ( self.key_at ( i ) )
    00220     return _keys
    00221
    00222 # ============================================
    00223 ## Get the list of items for the map
    00224 #
    00225 #  @code
    00226 #
    00227 #    >>> m     = ...        ## the map
    00228 #    >>> items = m.items()   ## get the list of items
    00229 #
    00230 #  @endcode
    00231 #  @see Gaudi::Utils::MapBase
    00232 #  @see GaudiUtils::Map
    00233 #  @see GaudiUtils::HashMap
    00234 #  @see GaudiUtils::VectorMap
    00235 #  @see GaudiUtils::Map::key_at
    00236 #  @see GaudiUtils::HashMap::key_at
    00237 #  @ see GaudiUtils::VectorMap::key_at
    00238 #  @author Vanya BELYAEV Ivan.BElyaev@itep.ru
    00239 #  @date 2010-02-20
    00240 def __mapbase_items__ ( self ) :
    00241     """
    00242     Get the list of items
    00243
    00244     >>> m     = ...        ## the map
    00245     >>> items = m.keys()   ## get the list of items
    00246
    00247     """
    00248     _size  = len ( self )
    00249     _items = []
    00250     for i in range ( 0 , _size )  :
    00251         _key   = self.key_at   ( i     )
    00252         _value = self.at       ( _key  )
    00253         _items.append ( ( _key , _value ) )
    00254     return _items
    00255
    00256 # ============================================
    00257 ## Get the list of values for the map
    00258 #
    00259 #  @code
    00260 #
    00261 #    >>> m      = ...           ## the map
    00262 #    >>> values = m.values ()   ## get the list of values
    00263 #
    00264 #  @endcode
    00265 #  @see Gaudi::Utils::MapBase
    00266 #  @see GaudiUtils::Map
    00267 #  @see GaudiUtils::HashMap
    00268 #  @see GaudiUtils::VectorMap
    00269 #  @see GaudiUtils::Map::value_at
    00270 #  @see GaudiUtils::HashMap::value_at
    00271 #  @ see GaudiUtils::VectorMap::value_at
    00272 #  @author Vanya BELYAEV Ivan.Belyaev@itep.ru
    00273 #  @date 2010-02-20
    00274 def __mapbase_values__ ( self ) :
    00275     """
    00276     Get the list of values
    00277
    00278     >>> m      = ...          ## the map
    00279     >>> values = m.values()   ## get the list of values
    00280
    00281     """
    00282     _size   = len ( self )
    00283     _values = []
    00284     for i in range ( 0 , _size ) :
    00285         _value = self.value_at ( i )
    00286         _values.append ( _value )
    00287     return _values
    00288
    00289 # ============================================
    00290 ## Check if the certain key is in the map
    00291 #
    00292 #  @code
    00293 #
    00294 #    >>> m      = ...        ## the map
    00295 #    >>> if 'a' in m : print 'key is in the map!'
    00296 #
    00297 #  @endcode
    00298 #  @see Gaudi::Utils::MapBase
    00299 #  @see GaudiUtils::Map
    00300 #  @see GaudiUtils::HashMap
    00301 #  @see GaudiUtils::VectorMap
    00302 #  @see GaudiUtils::Map::count
    00303 #  @see GaudiUtils::HashMap::count
    00304 #  @ see GaudiUtils::VectorMap::count
    00305 #  @author Vanya BELYAEV Ivan.BElyaev@itep.ru
    00306 #  @date 2010-02-20
    00307 def __mapbase_contains__ ( self , key ) :
    00308     """
    00309     Check if the certainkey is in the map
    00310
    00311     >>> m     = ...        ## the map
    00312     >>> if 'a' in m : ...  ##  chekc the presence of the key in the map
    00313
    00314     """
    00315     _num = self.count ( key )
    00316     return False if 0 == _num else True
    00317
    00318 # ============================================
    00319 ## Get the value for certain key,
    00320 #   return predefined value otherwise
    00321 #
    00322 #  @code
    00323 #
    00324 #    >>> m      = ...          ## the map
    00325 #    >>> v = m.get( key , 15 ) ## return the value[key] for existing key, else 15
    00326 #
    00327 #  @endcode
    00328 #  @see Gaudi::Utils::MapBase
    00329 #  @see GaudiUtils::Map
    00330 #  @see GaudiUtils::HashMap
    00331 #  @see GaudiUtils::VectorMap
    00332 #  @see GaudiUtils::Map::count
    00333 #  @see GaudiUtils::HashMap::count
    00334 #  @ see GaudiUtils::VectorMap::count
    00335 #  @author Vanya BELYAEV Ivan.BElyaev@itep.ru
    00336 #  @date 2010-02-20
    00337 def __mapbase_get__ ( self , key , value = None ) :
    00338     """
    00339     Get the value for the certain key, or 'value' otherwise
    00340
    00341     >>> m     = ...        ## the map
    00342     >>> v = m.get ( key , 15 )
    00343
    00344     """
    00345     if key in self : return self.at( key )
    00346     return value
    00347
    00348 # ============================================
    00349 ## Representation of MapBase-based maps
    00350 #
    00351 #  @code
    00352 #
    00353 #    >>> m      = ...        ## the map
    00354 #    >>> print m
    00355 #
    00356 #  @endcode
    00357 #  @see Gaudi::Utils::MapBase
    00358 #  @see GaudiUtils::Map
    00359 #  @see GaudiUtils::HashMap
    00360 #  @see GaudiUtils::VectorMap
    00361 #  @author Vanya BELYAEV Ivan.BElyaev@itep.ru
    00362 #  @date 2010-02-20
    00363 def __mapbase_str__ ( self  ) :
    00364     """
    00365     Representation of MapBase-based maps:
    00366
    00367     >>> m     = ...        ## the map
    00368     >>> print map
    00369
    00370     """
    00371     _result  = ' { '
    00372     _size  = len ( self )
    00373     for i in range ( 0 , _size ) :
    00374         _key = self.key_at   ( i    )
    00375         _val = self.at       ( _key )
    00376         if 0 != i : _result += ' , '
    00377         _result += " %s : %s " % ( str ( _key ) , str ( _val ) )
    00378     _result += ' } '
    00379     return _result
    00380
    00381 # ============================================
    00382 ## "Setitem" for MapBase-based maps:
    00383 #
    00384 #  @code
    00385 #
    00386 #    >>> m        = ...        ## the map
    00387 #    >>> m [ key] = value    ## set the item
    00388 #
    00389 #  @endcode
    00390 #  @see Gaudi::Utils::MapBase
    00391 #  @see GaudiUtils::Map
    00392 #  @see GaudiUtils::HashMap
    00393 #  @see GaudiUtils::VectorMap
    00394 #  @see GaudiUtils::Map::update
    00395 #  @see GaudiUtils::HashMap::update
    00396 #  @see GaudiUtils::VectorMap::update
    00397 #  @author Vanya BELYAEV Ivan.BElyaev@itep.ru
    00398 #  @date 2010-02-20
    00399 def __mapbase_setitem__ ( self , key , value ) :
    00400     """
    00401     'Set-item' for MapBase-based maps:
    00402
    00403     >>> m      = ...        ## the map
    00404     >>> m[key] = value     ## set the item
    00405
    00406     """
    00407     _replaced = True if key in self else False
    00408     self.update ( key , value )
    00409     return _replaced
    00410
    00411 # ============================================
    00412 ## "Del-item" for MapBase-based maps:
    00413 #
    00414 #  @code
    00415 #
    00416 #    >>> m        = ...   ## the map
    00417 #    >>> del m [ key]     ## del th eitem
    00418 #
    00419 #  @endcode
    00420 #
    00421 #  @see Gaudi::Utils::MapBase
    00422 #  @see GaudiUtils::Map
    00423 #  @see GaudiUtils::HashMap
    00424 #  @see GaudiUtils::VectorMap
    00425 #  @see GaudiUtils::Map::erase
    00426 #  @see GaudiUtils::HashMap::erase
    00427 #  @see GaudiUtils::VectorMap::erase
    00428 #  @author Vanya BELYAEV Ivan.BElyaev@itep.ru
    00429 #  @date 2010-02-20
    00430 def __mapbase_delitem__ ( self , key ) :
    00431     """
    00432     'Del-item' for MapBase-based maps:
    00433
    00434     >>> m      = ...        ## the map
    00435     >>> del m[key]
    00436
    00437     """
    00438     _erased = True if key in self else False
    00439     self.erase ( key )
    00440     return _erased
    00441
    00442 gbl.Gaudi.Utils.MapBase . __len__       = lambda s   : s.size()
    00443 gbl.Gaudi.Utils.MapBase . __iter__      = __mapbase_iter__
    00444 gbl.Gaudi.Utils.MapBase .   keys        = __mapbase_keys__
    00445 gbl.Gaudi.Utils.MapBase . __iteritems__ = __mapbase_iteritems__
    00446 gbl.Gaudi.Utils.MapBase .   iteritems   = __mapbase_iteritems__
    00447 gbl.Gaudi.Utils.MapBase .   items       = __mapbase_items__
    00448 gbl.Gaudi.Utils.MapBase .   values      = __mapbase_values__
    00449 gbl.Gaudi.Utils.MapBase . __contains__  = __mapbase_contains__
    00450 gbl.Gaudi.Utils.MapBase .   has_key     = __mapbase_contains__
    00451 gbl.Gaudi.Utils.MapBase .   get         = __mapbase_get__
    00452 gbl.Gaudi.Utils.MapBase . __str__       = __mapbase_str__
    00453 gbl.Gaudi.Utils.MapBase . __repr__      = __mapbase_str__
    00454 gbl.Gaudi.Utils.MapBase . __setitem__   = __mapbase_setitem__
    00455 gbl.Gaudi.Utils.MapBase . __delitem__   = __mapbase_delitem__
    00456 gbl.Gaudi.Utils.MapBase . __getitem__   = lambda s,key : s.at ( key )