13 """ This Pythonizations module provides a number of useful pythonizations
14 of adaptation of some classes.
16 from __future__
import print_function
26 print(
"# WARNING: using PyCintex as cppyy implementation")
27 from PyCintex
import gbl
29 if not hasattr(gbl,
"ostream"):
30 gbl.gROOT.ProcessLine(
"#include <ostream>")
31 if not hasattr(gbl,
"stringstream"):
32 gbl.gROOT.ProcessLine(
"#include <sstream>")
39 return 'Histogram 1D "%s" %d bins [%f,%f]' % (
49 return map(h.binEntries,
range(x.bins()))
53 x, y = h.xAxis(), h.yAxis()
54 return 'Histogram 2D "%s" %d xbins [%f,%f], %d ybins [%f,%f]' % (
73 return str(1 == b.bool())
78 s = gbl.stringstream()
82 out = o.__class__.__name__ +
" object"
83 if hasattr(o,
"hasKey")
and o.hasKey():
84 out +=
" key = " + str(o.key())
86 out = o.__class__.__name__ +
" NULL object"
91 return self.containedObject(k)
95 return self.numberOfObjects()
99 if hasattr(self,
"containedObjects"):
100 sequential = self.containedObjects()
104 limit = self.__len__()
106 yield sequential.__getitem__(count)
112 Draw AIDA histogram (through access to internal ROOT histogram
114 >>> aida = ... # get the historgam
118 _fun = gbl.Gaudi.Utils.Aida2ROOT.aida2root
120 return _root.Draw(*args)
123 gbl.AIDA.IHistogram1D.__str__ = _printHisto1D
124 gbl.AIDA.IHistogram1D.contents = _contentsHisto1D
125 gbl.AIDA.IHistogram2D.__str__ = _printHisto2D
128 gbl.AIDA.IHistogram1D,
129 gbl.AIDA.IHistogram2D,
130 gbl.AIDA.IHistogram3D,
137 gbl.StatusCode.__repr__ = _printStatusCode
139 gbl._Bit_reference.__repr__ = _printBitReference
142 gbl.ContainedObject.__repr__ = _printFillStream
143 gbl.DataObject.__repr__ = _printFillStream
144 gbl.ObjectContainerBase.__getitem__ = _container__getitem__
145 gbl.ObjectContainerBase.__len__ = _container__len__
146 gbl.ObjectContainerBase.__iter__ = _container__iter__
148 gbl.IUpdateManagerSvc.update = (
149 lambda self, obj: gbl.IUpdateManagerSvc.PythonHelper.update(self, obj)
151 gbl.IUpdateManagerSvc.invalidate = (
152 lambda self, obj: gbl.IUpdateManagerSvc.PythonHelper.invalidate(self, obj)
156 if not hasattr(gbl.StatusCode,
"SUCCESS"):
158 gbl.StatusCode.SUCCESS = 1
159 gbl.StatusCode.FAILURE = 0
162 if hasattr(gbl.Gaudi.StringKey,
"__cpp_eq__"):
163 _eq = gbl.Gaudi.StringKey.__cpp_eq__
164 setattr(gbl.Gaudi.StringKey,
"__eq__", _eq)
167 if hasattr(gbl.Gaudi.StringKey,
"__cpp_ne__"):
168 _ne = gbl.Gaudi.StringKey.__cpp_ne__
169 setattr(gbl.Gaudi.StringKey,
"__ne__", _ne)
172 if gbl.gROOT.GetVersionInt() <= 51800:
175 gbl.GaudiPython.PyROOTPickle.Initialize(libPyROOT, libPyROOT.ObjectProxy)
201 The iterator for MapBase-based containers
203 >>> m = ... ## the map
204 >>> for key in m : print(key , m[key])
209 while _index < _size:
210 yield self.key_at(_index)
239 The iterator for MapBase-based containers
241 >>> m = ... ## the map
242 >>> for key,value in m.iteritems() : print(key, value)
247 while _index < _size:
248 _key = self.key_at(_index)
249 yield (_key, self.at(_key))
277 >>> m = ... ## the map
278 >>> keys = m.keys() ## get the list of keys
283 for i
in range(0, _size):
284 _keys.append(self.key_at(i))
310 Get the list of items
312 >>> m = ... ## the map
313 >>> items = m.keys() ## get the list of items
318 for i
in range(0, _size):
319 _key = self.key_at(i)
320 _value = self.at(_key)
321 _items.append((_key, _value))
347 Get the list of values
349 >>> m = ... ## the map
350 >>> values = m.values() ## get the list of values
355 for i
in range(0, _size):
356 _value = self.value_at(i)
357 _values.append(_value)
383 Check if the certainkey is in the map
385 >>> m = ... ## the map
386 >>> if 'a' in m : ... ## chekc the presence of the key in the map
389 _num = self.count(key)
390 return False if 0 == _num
else True
416 Get the value for the certain key, or 'value' otherwise
418 >>> m = ... ## the map
419 >>> v = m.get ( key , 15 )
446 Representation of MapBase-based maps:
448 >>> m = ... ## the map
454 for i
in range(0, _size):
455 _key = self.key_at(i)
459 _result +=
" %s : %s " % (str(_key), str(_val))
486 'Set-item' for MapBase-based maps:
488 >>> m = ... ## the map
489 >>> m[key] = value ## set the item
492 _replaced =
True if key
in self
else False
493 self.update(key, value)
520 'Del-item' for MapBase-based maps:
522 >>> m = ... ## the map
526 _erased =
True if key
in self
else False
531 gbl.Gaudi.Utils.MapBase.__len__ =
lambda s: s.size()
532 gbl.Gaudi.Utils.MapBase.__iter__ = __mapbase_iter__
533 gbl.Gaudi.Utils.MapBase.keys = __mapbase_keys__
534 gbl.Gaudi.Utils.MapBase.__iteritems__ = __mapbase_iteritems__
535 gbl.Gaudi.Utils.MapBase.values = __mapbase_values__
536 gbl.Gaudi.Utils.MapBase.__contains__ = __mapbase_contains__
537 gbl.Gaudi.Utils.MapBase.get = __mapbase_get__
538 gbl.Gaudi.Utils.MapBase.__str__ = __mapbase_str__
539 gbl.Gaudi.Utils.MapBase.__repr__ = __mapbase_str__
540 gbl.Gaudi.Utils.MapBase.__setitem__ = __mapbase_setitem__
541 gbl.Gaudi.Utils.MapBase.__delitem__ = __mapbase_delitem__
542 gbl.Gaudi.Utils.MapBase.__getitem__ =
lambda s, key: s.at(key)
545 gbl.Gaudi.Utils.MapBase.iteritems = __mapbase_iteritems__
546 gbl.Gaudi.Utils.MapBase.items = __mapbase_items__
547 gbl.Gaudi.Utils.MapBase.has_key = __mapbase_contains__
550 gbl.Gaudi.Utils.MapBase.items = __mapbase_iteritems__
558 gbl.gInterpreter.Declare(
560 #ifndef REENTINTERFACES_PYTHON_HELPERS
561 #define REENTINTERFACES_PYTHON_HELPERS
562 #include <GaudiKernel/IAlgorithm.h>
563 #include <GaudiKernel/IEventProcessor.h>
564 #include <GaudiKernel/ThreadLocalContext.h>
566 namespace GaudiPython::Helpers {
567 StatusCode executeEvent( IEventProcessor* self ) {
568 return self->executeEvent( self->createEventContext() );
570 bool isExecuted( IAlgorithm* self ) {
571 return self->execState( Gaudi::Hive::currentContext() ).state() == AlgExecState::State::Done;
573 bool filterPassed( IAlgorithm* self ) {
574 return self->execState( Gaudi::Hive::currentContext() ).filterPassed();
576 StatusCode ialg_execute( IAlgorithm* self ) {
577 return self->execute( Gaudi::Hive::currentContext() );
584 gbl.IEventProcessor.executeEvent = gbl.GaudiPython.Helpers.executeEvent
585 gbl.IAlgorithm.isExecuted = gbl.GaudiPython.Helpers.isExecuted
586 gbl.IAlgorithm.filterPassed = gbl.GaudiPython.Helpers.filterPassed
587 gbl.IAlgorithm._execute_orig = gbl.IAlgorithm.execute
588 gbl.IAlgorithm.execute =
lambda self, ctx=
None: (
589 gbl.GaudiPython.Helpers.ialg_execute(self)
591 else self._execute_orig(ctx)