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