Gaudi Framework, version v24r2

Home   Generated: Wed Dec 4 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Pythonizations.py
Go to the documentation of this file.
1 # File: GaudiPython/Pythonizations.py
2 # Author: Pere Mato (pere.mato@cern.ch)
3 
4 """ This Pythonizations module provides a number of useful pythonizations
5  of adaptation of some classes.
6 """
7 
8 __all__ = [ ]
9 
10 import PyCintex
11 gbl = PyCintex.gbl
12 
13 if not hasattr(gbl,'ostream') : gbl.gROOT.ProcessLine("#include <ostream>")
14 if not hasattr(gbl,'stringstream') : gbl.gROOT.ProcessLine("#include <sstream>")
15 
16 #--- Hack to match the name scheme of dictionary on Linux ----------------------------
17 _loadDict_save = PyCintex.loadDict
18 def _loadDict(name):
19  import sys
20  if sys.platform != 'win32' and name[:3] != 'lib' : name = 'lib'+name
21  return _loadDict_save(name)
22 PyCintex.loadDict = _loadDict
23 
24 #--- Adding extra functionality to C++ raw classes------------------------------------
25 def _printHisto1D(h) :
26  x = h.axis()
27  return 'Histogram 1D "%s" %d bins [%f,%f]' % (h.title(), x.bins(), x.lowerEdge(), x.upperEdge())
29  x = h.axis()
30  return map(h.binEntries, range(x.bins()))
31 def _printHisto2D(h) :
32  x,y = h.xAxis(),h.yAxis()
33  return 'Histogram 2D "%s" %d xbins [%f,%f], %d ybins [%f,%f]' % \
34  (h.title(), x.bins(), x.lowerEdge(), x.upperEdge(), y.bins(), y.lowerEdge(), y.upperEdge() )
36  if s.isSuccess() : return 'SUCCESS'
37  else : return 'FAILURE'
39  return str(1==b.bool())
41  if o :
42  s = gbl.stringstream()
43  o.fillStream(s)
44  out = s.str()
45  if out == '' :
46  out = o.__class__.__name__ + ' object'
47  if hasattr( o, 'hasKey') and o.hasKey() :
48  out += ' key = '+ str(o.key())
49  else :
50  out = o.__class__.__name__ + ' NULL object'
51  return out
52 def _container__getitem__(self, k) :
53  return self.containedObject(k)
54 def _container__len__(self) :
55  return self.numberOfObjects()
56 def _container__iter__(self) :
57  if hasattr(self,'containedObjects') : sequential = self.containedObjects()
58  else : sequential = self
59  count = 0
60  limit = self.__len__()
61  while count < limit :
62  yield sequential.__getitem__(count)
63  count += 1
64 
65 def _draw_aida_ ( self , *args ) :
66  """
67  Draw AIDA histogram (through access to internal ROOT histogram
68 
69  >>> aida = ... # get the historgam
70  >>> aida.Draw()
71 
72  """
73  _fun = PyCintex.gbl.Gaudi.Utils.Aida2ROOT.aida2root
74  _root = _fun ( self )
75  return _root.Draw( *args )
76 
77 gbl.AIDA.IHistogram1D.__repr__ = _printHisto1D
78 gbl.AIDA.IHistogram1D.contents = _contentsHisto1D
79 gbl.AIDA.IHistogram2D.__repr__ = _printHisto2D
80 for h in ( gbl.AIDA.IHistogram ,
81  gbl.AIDA.IHistogram1D ,
82  gbl.AIDA.IHistogram2D ,
83  gbl.AIDA.IHistogram3D ,
84  gbl.AIDA.IProfile1D ,
85  gbl.AIDA.IProfile2D ) :
86  h.Draw = _draw_aida_
87  h.plot = _draw_aida_
88 
89 gbl.StatusCode.__repr__ = _printStatusCode
90 try: gbl._Bit_reference.__repr__ = _printBitReference
91 except: pass
92 gbl.ContainedObject.__repr__ = _printFillStream
93 gbl.DataObject.__repr__ = _printFillStream
94 gbl.ObjectContainerBase.__getitem__ = _container__getitem__
95 gbl.ObjectContainerBase.__len__ = _container__len__
96 gbl.ObjectContainerBase.__iter__ = _container__iter__
97 
98 gbl.IUpdateManagerSvc.update = lambda self,obj: gbl.IUpdateManagerSvc.PythonHelper.update(self,obj)
99 gbl.IUpdateManagerSvc.invalidate = lambda self,obj: gbl.IUpdateManagerSvc.PythonHelper.invalidate(self,obj)
100 
101 #---Globals--------------------------------------------------------------------
102 gbl.StatusCode.SUCCESS = 1
103 gbl.StatusCode.FAILURE = 0
104 
105 # - string key, equality
106 if hasattr ( gbl.Gaudi.StringKey ,'__cpp_eq__' ) :
107  _eq = gbl.Gaudi.StringKey.__cpp_eq__
108  setattr ( gbl.Gaudi.StringKey ,'__eq__' , _eq )
109 
110 # - string key, non-equality
111 if hasattr ( gbl.Gaudi.StringKey ,'__cpp_ne__' ) :
112  _ne = gbl.Gaudi.StringKey.__cpp_ne__
113  setattr ( gbl.Gaudi.StringKey ,'__ne__' , _ne )
114 
115 
116 #---Enabling Pickle support----------------------------------------------------
117 if gbl.gROOT.GetVersionInt() <= 51800 :
118  import libPyROOT
119  gbl.GaudiPython.PyROOTPickle.Initialize(libPyROOT, libPyROOT.ObjectProxy)
120 
121 # =============================================================================
122 ## decorate some map-like objects
123 # =============================================================================
124 ## The iterator for MapBase class
125 #
126 # @code
127 #
128 # >>> m = ... ## the map
129 # >>> for key in m : print key , m[key]
130 #
131 # @endcode
132 # @see Gaudi::Utils::MapBase
133 # @see GaudiUtils::Map
134 # @see GaudiUtils::HashMap
135 # @see GaudiUtils::VectorMap
136 # @see GaudiUtils::Map::key_at
137 # @see GaudiUtils::HashMap::key_at
138 # @see GaudiUtils::VectorMap::key_at
139 # @author Vanya BELYAEV Ivan.Belyaev@itep.ru
140 # @date 2010-02-20
141 def __mapbase_iter__ ( self ) :
142  """
143  The iterator for MapBase-based containers
144 
145  >>> m = ... ## the map
146  >>> for key in m : print key , m[key]
147 
148  """
149  _size = len ( self )
150  _index = 0
151  while _index < _size :
152  yield self.key_at ( _index )
153  _index +=1
154 
155 # =============================================================================
156 ## The iterator for MapBase class
157 #
158 # @code
159 #
160 # >>> m = ... ## the map
161 # >>> for key,value in m.iteritems() : print key , value
162 #
163 # @endcode
164 # @see Gaudi::Utils::MapBase
165 # @see GaudiUtils::Map
166 # @see GaudiUtils::HashMap
167 # @see GaudiUtils::VectorMap
168 # @see GaudiUtils::Map::key_at
169 # @see GaudiUtils::HashMap::key_at
170 # @see GaudiUtils::VectorMap::key_at
171 # @see GaudiUtils::Map::value_at
172 # @see GaudiUtils::HashMap::value_at
173 # @see GaudiUtils::VectorMap::value_at
174 # @author Vanya BELYAEV Ivan.Belyaev@itep.ru
175 # @date 2010-02-20
176 def __mapbase_iteritems__ ( self ) :
177  """
178  The iterator for MapBase-based containers
179 
180  >>> m = ... ## the map
181  >>> for key,value in m.iteritems() : print key, value
182 
183  """
184  _size = len ( self )
185  _index = 0
186  while _index < _size :
187  _key = self.key_at ( _index )
188  yield ( _key , self.at ( _key ) )
189  _index +=1
190 
191 # ============================================
192 ## Get the list of keys for the map
193 #
194 # @code
195 #
196 # >>> m = ... ## the map
197 # >>> keys = m.keys() ## get the list of keys
198 #
199 # @endcode
200 # @see Gaudi::Utils::MapBase
201 # @see GaudiUtils::Map
202 # @see GaudiUtils::HashMap
203 # @see GaudiUtils::VectorMap
204 # @see GaudiUtils::Map::key_at
205 # @see GaudiUtils::HashMap::key_at
206 # @ see GaudiUtils::VectorMap::key_at
207 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
208 # @date 2010-02-20
209 def __mapbase_keys__ ( self ) :
210  """
211  Get the list of keys
212 
213  >>> m = ... ## the map
214  >>> keys = m.keys() ## get the list of keys
215 
216  """
217  _size = len ( self )
218  _keys = []
219  for i in range ( 0 , _size ) : _keys.append ( self.key_at ( i ) )
220  return _keys
221 
222 # ============================================
223 ## Get the list of items for the map
224 #
225 # @code
226 #
227 # >>> m = ... ## the map
228 # >>> items = m.items() ## get the list of items
229 #
230 # @endcode
231 # @see Gaudi::Utils::MapBase
232 # @see GaudiUtils::Map
233 # @see GaudiUtils::HashMap
234 # @see GaudiUtils::VectorMap
235 # @see GaudiUtils::Map::key_at
236 # @see GaudiUtils::HashMap::key_at
237 # @ see GaudiUtils::VectorMap::key_at
238 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
239 # @date 2010-02-20
240 def __mapbase_items__ ( self ) :
241  """
242  Get the list of items
243 
244  >>> m = ... ## the map
245  >>> items = m.keys() ## get the list of items
246 
247  """
248  _size = len ( self )
249  _items = []
250  for i in range ( 0 , _size ) :
251  _key = self.key_at ( i )
252  _value = self.at ( _key )
253  _items.append ( ( _key , _value ) )
254  return _items
255 
256 # ============================================
257 ## Get the list of values for the map
258 #
259 # @code
260 #
261 # >>> m = ... ## the map
262 # >>> values = m.values () ## get the list of values
263 #
264 # @endcode
265 # @see Gaudi::Utils::MapBase
266 # @see GaudiUtils::Map
267 # @see GaudiUtils::HashMap
268 # @see GaudiUtils::VectorMap
269 # @see GaudiUtils::Map::value_at
270 # @see GaudiUtils::HashMap::value_at
271 # @ see GaudiUtils::VectorMap::value_at
272 # @author Vanya BELYAEV Ivan.Belyaev@itep.ru
273 # @date 2010-02-20
274 def __mapbase_values__ ( self ) :
275  """
276  Get the list of values
277 
278  >>> m = ... ## the map
279  >>> values = m.values() ## get the list of values
280 
281  """
282  _size = len ( self )
283  _values = []
284  for i in range ( 0 , _size ) :
285  _value = self.value_at ( i )
286  _values.append ( _value )
287  return _values
288 
289 # ============================================
290 ## Check if the certain key is in the map
291 #
292 # @code
293 #
294 # >>> m = ... ## the map
295 # >>> if 'a' in m : print 'key is in the map!'
296 #
297 # @endcode
298 # @see Gaudi::Utils::MapBase
299 # @see GaudiUtils::Map
300 # @see GaudiUtils::HashMap
301 # @see GaudiUtils::VectorMap
302 # @see GaudiUtils::Map::count
303 # @see GaudiUtils::HashMap::count
304 # @ see GaudiUtils::VectorMap::count
305 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
306 # @date 2010-02-20
307 def __mapbase_contains__ ( self , key ) :
308  """
309  Check if the certainkey is in the map
310 
311  >>> m = ... ## the map
312  >>> if 'a' in m : ... ## chekc the presence of the key in the map
313 
314  """
315  _num = self.count ( key )
316  return False if 0 == _num else True
317 
318 # ============================================
319 ## Get the value for certain key,
320 # return predefined value otherwise
321 #
322 # @code
323 #
324 # >>> m = ... ## the map
325 # >>> v = m.get( key , 15 ) ## return the value[key] for existing key, else 15
326 #
327 # @endcode
328 # @see Gaudi::Utils::MapBase
329 # @see GaudiUtils::Map
330 # @see GaudiUtils::HashMap
331 # @see GaudiUtils::VectorMap
332 # @see GaudiUtils::Map::count
333 # @see GaudiUtils::HashMap::count
334 # @ see GaudiUtils::VectorMap::count
335 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
336 # @date 2010-02-20
337 def __mapbase_get__ ( self , key , value = None ) :
338  """
339  Get the value for the certain key, or 'value' otherwise
340 
341  >>> m = ... ## the map
342  >>> v = m.get ( key , 15 )
343 
344  """
345  if key in self : return self.at( key )
346  return value
347 
348 # ============================================
349 ## Representation of MapBase-based maps
350 #
351 # @code
352 #
353 # >>> m = ... ## the map
354 # >>> print m
355 #
356 # @endcode
357 # @see Gaudi::Utils::MapBase
358 # @see GaudiUtils::Map
359 # @see GaudiUtils::HashMap
360 # @see GaudiUtils::VectorMap
361 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
362 # @date 2010-02-20
363 def __mapbase_str__ ( self ) :
364  """
365  Representation of MapBase-based maps:
366 
367  >>> m = ... ## the map
368  >>> print map
369 
370  """
371  _result = ' { '
372  _size = len ( self )
373  for i in range ( 0 , _size ) :
374  _key = self.key_at ( i )
375  _val = self.at ( _key )
376  if 0 != i : _result += ' , '
377  _result += " %s : %s " % ( str ( _key ) , str ( _val ) )
378  _result += ' } '
379  return _result
380 
381 # ============================================
382 ## "Setitem" for MapBase-based maps:
383 #
384 # @code
385 #
386 # >>> m = ... ## the map
387 # >>> m [ key] = value ## set the item
388 #
389 # @endcode
390 # @see Gaudi::Utils::MapBase
391 # @see GaudiUtils::Map
392 # @see GaudiUtils::HashMap
393 # @see GaudiUtils::VectorMap
394 # @see GaudiUtils::Map::update
395 # @see GaudiUtils::HashMap::update
396 # @see GaudiUtils::VectorMap::update
397 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
398 # @date 2010-02-20
399 def __mapbase_setitem__ ( self , key , value ) :
400  """
401  'Set-item' for MapBase-based maps:
402 
403  >>> m = ... ## the map
404  >>> m[key] = value ## set the item
405 
406  """
407  _replaced = True if key in self else False
408  self.update ( key , value )
409  return _replaced
410 
411 # ============================================
412 ## "Del-item" for MapBase-based maps:
413 #
414 # @code
415 #
416 # >>> m = ... ## the map
417 # >>> del m [ key] ## del th eitem
418 #
419 # @endcode
420 #
421 # @see Gaudi::Utils::MapBase
422 # @see GaudiUtils::Map
423 # @see GaudiUtils::HashMap
424 # @see GaudiUtils::VectorMap
425 # @see GaudiUtils::Map::erase
426 # @see GaudiUtils::HashMap::erase
427 # @see GaudiUtils::VectorMap::erase
428 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
429 # @date 2010-02-20
430 def __mapbase_delitem__ ( self , key ) :
431  """
432  'Del-item' for MapBase-based maps:
433 
434  >>> m = ... ## the map
435  >>> del m[key]
436 
437  """
438  _erased = True if key in self else False
439  self.erase ( key )
440  return _erased
441 
442 gbl.Gaudi.Utils.MapBase . __len__ = lambda s : s.size()
443 gbl.Gaudi.Utils.MapBase . __iter__ = __mapbase_iter__
444 gbl.Gaudi.Utils.MapBase . keys = __mapbase_keys__
445 gbl.Gaudi.Utils.MapBase . __iteritems__ = __mapbase_iteritems__
446 gbl.Gaudi.Utils.MapBase . iteritems = __mapbase_iteritems__
447 gbl.Gaudi.Utils.MapBase . items = __mapbase_items__
448 gbl.Gaudi.Utils.MapBase . values = __mapbase_values__
449 gbl.Gaudi.Utils.MapBase . __contains__ = __mapbase_contains__
450 gbl.Gaudi.Utils.MapBase . has_key = __mapbase_contains__
451 gbl.Gaudi.Utils.MapBase . get = __mapbase_get__
452 gbl.Gaudi.Utils.MapBase . __str__ = __mapbase_str__
453 gbl.Gaudi.Utils.MapBase . __repr__ = __mapbase_str__
454 gbl.Gaudi.Utils.MapBase . __setitem__ = __mapbase_setitem__
455 gbl.Gaudi.Utils.MapBase . __delitem__ = __mapbase_delitem__
456 gbl.Gaudi.Utils.MapBase . __getitem__ = lambda s,key : s.at ( key )

Generated at Wed Dec 4 2013 14:33:10 for Gaudi Framework, version v24r2 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004