00001
00002
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
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
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
00102 gbl.StatusCode.SUCCESS = 1
00103 gbl.StatusCode.FAILURE = 0
00104
00105
00106 if hasattr ( gbl.Gaudi.StringKey ,'__cpp_eq__' ) :
00107 _eq = gbl.Gaudi.StringKey.__cpp_eq__
00108 setattr ( gbl.Gaudi.StringKey ,'__eq__' , _eq )
00109
00110
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
00117 if gbl.gROOT.GetVersionInt() <= 51800 :
00118 import libPyROOT
00119 gbl.GaudiPython.PyROOTPickle.Initialize(libPyROOT, libPyROOT.ObjectProxy)
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
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
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
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
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
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
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
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
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
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
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
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
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
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
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
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
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
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
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
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 )