The Gaudi Framework  master (37c0b60a)
Helpers.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIPYTHON_HELPERS_H
12 #define GAUDIPYTHON_HELPERS_H
13 
14 #include <Python.h>
15 
16 // Python 3 compatibility
17 #if PY_MAJOR_VERSION >= 3
18 
19 # define PyInt_FromLong PyLong_FromLong
20 
21 # define PyBuffer_Type PyMemoryView_Type
22 
23 #endif
24 
25 #include <Gaudi/Property.h>
26 #include <GaudiKernel/DataObject.h>
28 #include <GaudiKernel/IAlgTool.h>
29 #include <GaudiKernel/IAlgorithm.h>
33 #include <GaudiKernel/IToolSvc.h>
34 
35 // FIXME: (MCl) workaround for ROOT-5850
36 namespace AIDA {
37  class IHistogram1D;
38  class IHistogram2D;
39  class IHistogram3D;
40  class IProfile1D;
41  class IProfile2D;
42 } // namespace AIDA
43 
44 #if PY_VERSION_HEX < 0x02050000
45 // Note (MCl):
46 // In Python 2.5, all the functions working with lenghts use the type PySsize_t
47 // instead of int. The also changed the name of the typedefs for those functions.
48 // Here we use:
49 // intargfunc -> ssizeargfunc
50 // inquiry -> lenfunc
51 //
53 typedef int Py_ssize_t;
54 #endif
55 
56 // the following is done instead of including Python.h becuase there is
57 // a clash with codecs.h defined in Python and the one in VC++ 7.1
58 // struct _object;
59 // typedef _object PyObject;
60 // extern "C" {
61 // PyObject* PyBuffer_FromMemory( void *ptr, int size);
62 //}
63 
67 namespace GaudiPython {
68 
69  struct Helper {
70  // Python 3 compatibility
71 #if PY_MAJOR_VERSION >= 3
72  // Taken from ROOT's TPyBufferFactory
73  static PyObject* PyBuffer_FromReadWriteMemory( void* ptr, int size ) {
74 # if PY_VERSION_HEX > 0x03000000
75  // Python 3 will set an exception if nullptr, just rely on size == 0
76  if ( !ptr ) {
77  static long dummy[1];
78  ptr = dummy;
79  size = 0;
80  }
81 # endif
82  Py_buffer bufinfo = {
83  ptr,
84  NULL,
85  size,
86  1,
87  0,
88  1,
89  NULL,
90  NULL,
91  NULL,
92  NULL,
93 # if PY_VERSION_HEX < 0x03030000
94  { 0, 0 },
95 # endif
96  NULL
97  };
98  return PyMemoryView_FromBuffer( &bufinfo );
99  }
100 #endif
101  // This is a number of static functions to overcome the current problem with PyLCGDict that
102  // does not allow to return instances of complex objects by calling arguments
103  // (reference to pointers)
104  Helper() {}
105  // Provided for backward compatibility
106  static IService* service( ISvcLocator* svcloc, const std::string& name, bool createif = false ) {
107  return svcloc->service( name, createif ).get();
108  }
109  // Provided for backward compatibility
110  static IAlgorithm* algorithm( IAlgManager* algmgr, const std::string& name, const bool createIf = false ) {
111  return algmgr->algorithm( name, createIf ).get();
112  }
113  // ==========================================================================
115  DataObject* o;
116  return dpsvc->retrieveObject( path, o ).isSuccess() ? o : nullptr;
117  }
118  // ==========================================================================
120  return dpsvc->registerObject( path, pObject );
121  }
122  // ==========================================================================
124  return dpsvc->unregisterObject( path );
125  }
126  // ==========================================================================
138  // ==========================================================================
151  const bool retrieve = true, const bool disableDoD = false );
152  // ==========================================================================
153  static IAlgTool* tool( IToolSvc* toolsvc, const std::string& type, const std::string& name, IInterface* p,
154  bool cif ) {
155  IAlgTool* o;
156  return toolsvc->retrieve( type, name, IAlgTool::interfaceID(), o, p, cif ).isSuccess() ? o : nullptr;
157  }
158  static long loadDynamicLib( const std::string& name ) {
159  void* h;
160  return System::loadDynamicLib( name, &h );
161  }
162  static IHistogram1D* histo1D( IHistogramSvc* hsvc, const std::string& path ) {
163  IHistogram1D* h;
164  return ( hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
165  }
166  static IHistogram2D* histo2D( IHistogramSvc* hsvc, const std::string& path ) {
167  IHistogram2D* h;
168  return ( hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
169  }
170  static IHistogram3D* histo3D( IHistogramSvc* hsvc, const std::string& path ) {
171  IHistogram3D* h;
172  return ( hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
173  }
174  static IProfile1D* profile1D( IHistogramSvc* hsvc, const std::string& path ) {
175  IProfile1D* h = 0;
176  return ( hsvc && hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
177  }
178  static IProfile2D* profile2D( IHistogramSvc* hsvc, const std::string& path ) {
179  IProfile2D* h = 0;
180  return ( hsvc && hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
181  }
182 
183  // Array support
184  private:
185  template <class T>
186  static Py_ssize_t Array_length( PyObject* self ) {
187 #if PY_MAJOR_VERSION >= 3
188  Py_buffer bufinfo;
189  ( *( self->ob_type->tp_as_buffer->bf_getbuffer ) )( self, &bufinfo, PyBUF_SIMPLE );
190  Py_ssize_t size = bufinfo.len;
191 #else
192 # if PY_VERSION_HEX < 0x02050000
193  const
194 # endif
195  char* buf = 0;
196  Py_ssize_t size = ( *( self->ob_type->tp_as_buffer->bf_getcharbuffer ) )( self, 0, &buf );
197 #endif
198  return size / sizeof( T );
199  }
200 
201  template <class T>
202  static PyObject* toPython( T* /*o*/ ) {
203  return 0;
204  }
205  static PyObject* toPython( int* o ) { return PyInt_FromLong( (long)*o ); }
206  static PyObject* toPython( short* o ) { return PyInt_FromLong( (long)*o ); }
207  static PyObject* toPython( char* o ) { return PyInt_FromLong( (long)*o ); }
208  static PyObject* toPython( long* o ) { return PyInt_FromLong( *o ); }
209  static PyObject* toPython( float* o ) { return PyFloat_FromDouble( (double)*o ); }
210  static PyObject* toPython( double* o ) { return PyFloat_FromDouble( *o ); }
211 
212  template <class T>
213  static PyObject* Array_item( PyObject* self, Py_ssize_t idx ) {
214 #if PY_VERSION_HEX < 0x02050000
215  const
216 #endif
217  char* buf = nullptr;
218 #if PY_MAJOR_VERSION >= 3
219  Py_buffer bufinfo;
220  ( *( self->ob_type->tp_as_buffer->bf_getbuffer ) )( self, &bufinfo, PyBUF_SIMPLE );
221  Py_ssize_t size = bufinfo.len;
222 #else
223  Py_ssize_t size = ( *( self->ob_type->tp_as_buffer->bf_getcharbuffer ) )( self, 0, &buf );
224 #endif
225  if ( idx < 0 || idx >= size / int( sizeof( T ) ) ) {
226  PyErr_SetString( PyExc_IndexError, "buffer index out of range" );
227  return nullptr;
228  }
229  return toPython( (T*)buf + idx );
230  }
231 
232  public:
233  template <class T>
234  static PyObject* toArray( T* ptr, Py_ssize_t size ) {
235  static PyTypeObject type = PyBuffer_Type;
236  static PySequenceMethods meth = *( PyBuffer_Type.tp_as_sequence );
237 #if PY_VERSION_HEX < 0x02050000
238  meth.sq_item = (intargfunc)&Array_item<T>;
239  meth.sq_length = (inquiry)&Array_length<T>;
240 #else
241  meth.sq_item = (ssizeargfunc)&Array_item<T>;
242  meth.sq_length = (lenfunc)&Array_length<T>;
243 #endif
244  type.tp_as_sequence = &meth;
245  PyObject* buf = PyBuffer_FromReadWriteMemory( ptr, size * sizeof( T ) );
246  buf->ob_type = &type;
247  Py_INCREF( buf->ob_type );
248  return buf;
249  }
250  static PyObject* toIntArray( void* ptr, Py_ssize_t size ) { return toArray( (int*)ptr, size ); }
251  static PyObject* toShortArray( void* ptr, Py_ssize_t size ) { return toArray( (short*)ptr, size ); }
252  static PyObject* toFloatArray( void* ptr, Py_ssize_t size ) { return toArray( (float*)ptr, size ); }
253  static PyObject* toDoubleArray( void* ptr, Py_ssize_t size ) { return toArray( (double*)ptr, size ); }
254 
255  template <class T>
256  static T* toAddress( std::vector<T>& v ) {
257  return v.data();
258  }
259  template <class T>
260  static T* toAddress( void* a ) {
261  return (T*)a;
262  }
263  };
264 
265  template PyObject* Helper::toArray( int*, Py_ssize_t );
266  template PyObject* Helper::toArray( char*, Py_ssize_t );
267  template PyObject* Helper::toArray( short*, Py_ssize_t );
268  template PyObject* Helper::toArray( float*, Py_ssize_t );
269  template PyObject* Helper::toArray( double*, Py_ssize_t );
270  template int* Helper::toAddress( std::vector<int>& );
271  template float* Helper::toAddress( std::vector<float>& );
272  template double* Helper::toAddress( std::vector<double>& );
273  template int* Helper::toAddress<int>( void* );
274  template float* Helper::toAddress<float>( void* );
275  template double* Helper::toAddress<double>( void* );
276 
277 } // namespace GaudiPython
278 
279 #endif // !GAUDIPYTHON_HELPERS_H
IService
Definition: IService.h:28
GaudiPython::Helper::toIntArray
static PyObject * toIntArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:250
GaudiPython::Helper::toPython
static PyObject * toPython(short *o)
Definition: Helpers.h:206
GaudiPython::Helper::toPython
static PyObject * toPython(float *o)
Definition: Helpers.h:209
GaudiPython::Helper::dataobject
static DataObject * dataobject(IDataProviderSvc *dpsvc, const std::string &path)
Definition: Helpers.h:114
AIDA
GaudiKernel.
Definition: Annotation.h:22
System::loadDynamicLib
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:150
IAlgManager.h
IDataProviderSvc::unregisterObject
virtual StatusCode unregisterObject(std::string_view fullPath)=0
Unregister object from the data store.
Py_ssize_t
int Py_ssize_t
For compatibility with Python 2.4 and 2.5.
Definition: Helpers.h:53
std::string
STL class.
IAlgTool
Definition: IAlgTool.h:33
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
IHistogramSvc
Definition: IHistogramSvc.h:56
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
GaudiPython::Helper::toDoubleArray
static PyObject * toDoubleArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:253
IHistogramSvc::findObject
virtual StatusCode findObject(const std::string &fullPath, AIDA::IHistogram1D *&h1dObj)=0
GaudiPython::Helper::toPython
static PyObject * toPython(char *o)
Definition: Helpers.h:207
GaudiPython::Helper::toPython
static PyObject * toPython(T *)
Definition: Helpers.h:202
GaudiPython::Helper::tool
static IAlgTool * tool(IToolSvc *toolsvc, const std::string &type, const std::string &name, IInterface *p, bool cif)
Definition: Helpers.h:153
std::vector
STL class.
GaudiPython::Helper::registerObject
static StatusCode registerObject(IDataProviderSvc *dpsvc, const std::string &path, DataObject *pObject)
Definition: Helpers.h:119
ISvcLocator
Definition: ISvcLocator.h:46
GaudiPython::Helper::getobject
static GAUDI_API DataObject * getobject(IDataProviderSvc *dpsvc, const std::string &path, const bool retrieve=true, const bool disableDoD=false)
the generic function to get object from TES
Definition: Helpers.cpp:119
GaudiPython::Helper
Definition: Helpers.h:69
GaudiPython::Helper::toPython
static PyObject * toPython(int *o)
Definition: Helpers.h:205
GaudiPython::Helper::toArray
static PyObject * toArray(T *ptr, Py_ssize_t size)
Definition: Helpers.h:234
GaudiPython::Helper::Array_item
static PyObject * Array_item(PyObject *self, Py_ssize_t idx)
Definition: Helpers.h:213
IDataProviderSvc.h
GaudiPython::Helper::histo3D
static IHistogram3D * histo3D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:170
IDataProviderSvc::registerObject
StatusCode registerObject(std::string_view fullPath, DataObject *pObject)
Register object with the data store.
Definition: IDataProviderSvc.h:72
GaudiPython::Helper::toAddress
static T * toAddress(std::vector< T > &v)
Definition: Helpers.h:256
IToolSvc.h
GaudiPython::Helper::toAddress
static T * toAddress(void *a)
Definition: Helpers.h:260
IAlgManager
Definition: IAlgManager.h:37
IAlgManager::algorithm
virtual SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
GaudiPython::Helper::toPython
static PyObject * toPython(long *o)
Definition: Helpers.h:208
StatusCode
Definition: StatusCode.h:65
IAlgorithm
Definition: IAlgorithm.h:38
IInterface::interfaceID
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:248
IDataProviderSvc::retrieveObject
virtual StatusCode retrieveObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
IAlgTool.h
AlgSequencer.h
h
Definition: AlgSequencer.py:31
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:98
GaudiPython::Helper::histo1D
static IHistogram1D * histo1D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:162
IToolSvc::retrieve
virtual StatusCode retrieve(std::string_view type, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent=0, bool createIf=true)=0
Retrieve tool with tool dependent part of the name automatically assigned.
GaudiPython::Helper::Helper
Helper()
Definition: Helpers.h:104
GaudiPython::Helper::loadDynamicLib
static long loadDynamicLib(const std::string &name)
Definition: Helpers.h:158
GaudiPython::Helper::toPython
static PyObject * toPython(double *o)
Definition: Helpers.h:210
SmartRefVectorImpl::PyObject
_object PyObject
Definition: SmartRefVector.h:32
GaudiPython::Helper::toShortArray
static PyObject * toShortArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:251
GaudiPython::Helper::service
static IService * service(ISvcLocator *svcloc, const std::string &name, bool createif=false)
Definition: Helpers.h:106
GaudiPython::Helper::Array_length
static Py_ssize_t Array_length(PyObject *self)
Definition: Helpers.h:186
gaudirun.type
type
Definition: gaudirun.py:160
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
DataObject.h
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
GaudiPython::Helper::profile2D
static IProfile2D * profile2D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:178
GaudiPython
Namespace for all classes interfacing Gaudi to Python.
Definition: Algorithm.h:30
IInterface
Definition: IInterface.h:239
DataObject
Definition: DataObject.h:36
Properties.v
v
Definition: Properties.py:122
GaudiPython::Helper::algorithm
static IAlgorithm * algorithm(IAlgManager *algmgr, const std::string &name, const bool createIf=false)
Definition: Helpers.h:110
GaudiPython::Helper::toFloatArray
static PyObject * toFloatArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:252
IDataProviderSvc
Definition: IDataProviderSvc.h:53
IAlgorithm.h
ISvcLocator.h
GaudiPython::Helper::findobject
static GAUDI_API DataObject * findobject(IDataProviderSvc *dpsvc, const std::string &path)
simple wrapper for IDataProviderSvc::findObject The methdod does NOT trigger the loading the object f...
Definition: Helpers.cpp:51
IToolSvc
Definition: IToolSvc.h:29
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
IHistogramSvc.h
GaudiPython::Helper::histo2D
static IHistogram2D * histo2D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:166
GaudiPython::Helper::profile1D
static IProfile1D * profile1D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:174
Property.h
GaudiPython::Helper::unregisterObject
static StatusCode unregisterObject(IDataProviderSvc *dpsvc, const std::string &path)
Definition: Helpers.h:123