13 """ This Pythonizations module provides a number of useful pythonizations
14 of adaptation of some classes.
16 from __future__
import print_function
25 print(
"# WARNING: using PyCintex as cppyy implementation")
26 from PyCintex
import gbl
28 if not hasattr(gbl,
'ostream'):
29 gbl.gROOT.ProcessLine(
"#include <ostream>")
30 if not hasattr(gbl,
'stringstream'):
31 gbl.gROOT.ProcessLine(
"#include <sstream>")
38 return 'Histogram 1D "%s" %d bins [%f,%f]' % (h.title(), x.bins(),
39 x.lowerEdge(), x.upperEdge())
44 return map(h.binEntries,
range(x.bins()))
48 x, y = h.xAxis(), h.yAxis()
49 return 'Histogram 2D "%s" %d xbins [%f,%f], %d ybins [%f,%f]' % \
50 (h.title(), x.bins(), x.lowerEdge(), x.upperEdge(),
51 y.bins(), y.lowerEdge(), y.upperEdge())
62 return str(1 == b.bool())
67 s = gbl.stringstream()
71 out = o.__class__.__name__ +
' object'
72 if hasattr(o,
'hasKey')
and o.hasKey():
73 out +=
' key = ' + str(o.key())
75 out = o.__class__.__name__ +
' NULL object'
80 return self.containedObject(k)
84 return self.numberOfObjects()
88 if hasattr(self,
'containedObjects'):
89 sequential = self.containedObjects()
93 limit = self.__len__()
95 yield sequential.__getitem__(count)
101 Draw AIDA histogram (through access to internal ROOT histogram
103 >>> aida = ... # get the historgam
107 _fun = gbl.Gaudi.Utils.Aida2ROOT.aida2root
109 return _root.Draw(*args)
112 gbl.AIDA.IHistogram1D.__str__ = _printHisto1D
113 gbl.AIDA.IHistogram1D.contents = _contentsHisto1D
114 gbl.AIDA.IHistogram2D.__str__ = _printHisto2D
115 for h
in (gbl.AIDA.IHistogram, gbl.AIDA.IHistogram1D, gbl.AIDA.IHistogram2D,
116 gbl.AIDA.IHistogram3D, gbl.AIDA.IProfile1D, gbl.AIDA.IProfile2D):
120 gbl.StatusCode.__repr__ = _printStatusCode
122 gbl._Bit_reference.__repr__ = _printBitReference
125 gbl.ContainedObject.__repr__ = _printFillStream
126 gbl.DataObject.__repr__ = _printFillStream
127 gbl.ObjectContainerBase.__getitem__ = _container__getitem__
128 gbl.ObjectContainerBase.__len__ = _container__len__
129 gbl.ObjectContainerBase.__iter__ = _container__iter__
131 gbl.IUpdateManagerSvc.update =
lambda self, obj: gbl.IUpdateManagerSvc.PythonHelper.update(
133 gbl.IUpdateManagerSvc.invalidate =
lambda self, obj: gbl.IUpdateManagerSvc.PythonHelper.invalidate(
137 if not hasattr(gbl.StatusCode,
'SUCCESS'):
139 gbl.StatusCode.SUCCESS = 1
140 gbl.StatusCode.FAILURE = 0
143 if hasattr(gbl.Gaudi.StringKey,
'__cpp_eq__'):
144 _eq = gbl.Gaudi.StringKey.__cpp_eq__
145 setattr(gbl.Gaudi.StringKey,
'__eq__', _eq)
148 if hasattr(gbl.Gaudi.StringKey,
'__cpp_ne__'):
149 _ne = gbl.Gaudi.StringKey.__cpp_ne__
150 setattr(gbl.Gaudi.StringKey,
'__ne__', _ne)
153 if gbl.gROOT.GetVersionInt() <= 51800:
155 gbl.GaudiPython.PyROOTPickle.Initialize(libPyROOT, libPyROOT.ObjectProxy)
181 The iterator for MapBase-based containers
183 >>> m = ... ## the map
184 >>> for key in m : print(key , m[key])
189 while _index < _size:
190 yield self.key_at(_index)
219 The iterator for MapBase-based containers
221 >>> m = ... ## the map
222 >>> for key,value in m.iteritems() : print(key, value)
227 while _index < _size:
228 _key = self.key_at(_index)
229 yield (_key, self.at(_key))
257 >>> m = ... ## the map
258 >>> keys = m.keys() ## get the list of keys
263 for i
in range(0, _size):
264 _keys.append(self.key_at(i))
290 Get the list of items
292 >>> m = ... ## the map
293 >>> items = m.keys() ## get the list of items
298 for i
in range(0, _size):
299 _key = self.key_at(i)
300 _value = self.at(_key)
301 _items.append((_key, _value))
327 Get the list of values
329 >>> m = ... ## the map
330 >>> values = m.values() ## get the list of values
335 for i
in range(0, _size):
336 _value = self.value_at(i)
337 _values.append(_value)
363 Check if the certainkey is in the map
365 >>> m = ... ## the map
366 >>> if 'a' in m : ... ## chekc the presence of the key in the map
369 _num = self.count(key)
370 return False if 0 == _num
else True
396 Get the value for the certain key, or 'value' otherwise
398 >>> m = ... ## the map
399 >>> v = m.get ( key , 15 )
426 Representation of MapBase-based maps:
428 >>> m = ... ## the map
434 for i
in range(0, _size):
435 _key = self.key_at(i)
439 _result +=
" %s : %s " % (str(_key), str(_val))
466 'Set-item' for MapBase-based maps:
468 >>> m = ... ## the map
469 >>> m[key] = value ## set the item
472 _replaced =
True if key
in self
else False
473 self.update(key, value)
500 'Del-item' for MapBase-based maps:
502 >>> m = ... ## the map
506 _erased =
True if key
in self
else False
511 gbl.Gaudi.Utils.MapBase.__len__ =
lambda s: s.size()
512 gbl.Gaudi.Utils.MapBase.__iter__ = __mapbase_iter__
513 gbl.Gaudi.Utils.MapBase.keys = __mapbase_keys__
514 gbl.Gaudi.Utils.MapBase.__iteritems__ = __mapbase_iteritems__
515 gbl.Gaudi.Utils.MapBase.values = __mapbase_values__
516 gbl.Gaudi.Utils.MapBase.__contains__ = __mapbase_contains__
517 gbl.Gaudi.Utils.MapBase.get = __mapbase_get__
518 gbl.Gaudi.Utils.MapBase.__str__ = __mapbase_str__
519 gbl.Gaudi.Utils.MapBase.__repr__ = __mapbase_str__
520 gbl.Gaudi.Utils.MapBase.__setitem__ = __mapbase_setitem__
521 gbl.Gaudi.Utils.MapBase.__delitem__ = __mapbase_delitem__
522 gbl.Gaudi.Utils.MapBase.__getitem__ =
lambda s, key: s.at(key)
525 gbl.Gaudi.Utils.MapBase.iteritems = __mapbase_iteritems__
526 gbl.Gaudi.Utils.MapBase.items = __mapbase_items__
527 gbl.Gaudi.Utils.MapBase.has_key = __mapbase_contains__
530 gbl.Gaudi.Utils.MapBase.items = __mapbase_iteritems__
538 gbl.gInterpreter.Declare(
"""
539 #ifndef REENTINTERFACES_PYTHON_HELPERS
540 #define REENTINTERFACES_PYTHON_HELPERS
541 #include <GaudiKernel/IAlgorithm.h>
542 #include <GaudiKernel/IEventProcessor.h>
543 #include <GaudiKernel/ThreadLocalContext.h>
545 namespace GaudiPython::Helpers {
546 StatusCode executeEvent( IEventProcessor* self ) {
547 return self->executeEvent( self->createEventContext() );
549 bool isExecuted( IAlgorithm* self ) {
550 return self->execState( Gaudi::Hive::currentContext() ).state() == AlgExecState::State::Done;
552 bool filterPassed( IAlgorithm* self ) {
553 return self->execState( Gaudi::Hive::currentContext() ).filterPassed();
555 StatusCode ialg_execute( IAlgorithm* self ) {
556 return self->execute( Gaudi::Hive::currentContext() );
562 gbl.IEventProcessor.executeEvent = gbl.GaudiPython.Helpers.executeEvent
563 gbl.IAlgorithm.isExecuted = gbl.GaudiPython.Helpers.isExecuted
564 gbl.IAlgorithm.filterPassed = gbl.GaudiPython.Helpers.filterPassed
565 gbl.IAlgorithm._execute_orig = gbl.IAlgorithm.execute
566 gbl.IAlgorithm.execute =
lambda self, ctx=
None: (gbl.GaudiPython.Helpers.ialg_execute(self)
if ctx
is None else self._execute_orig(ctx))