Gaudi Framework, version v25r1

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

Generated at Mon Mar 24 2014 18:27:45 for Gaudi Framework, version v25r1 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004