The Gaudi Framework  v32r2 (46d42edc)
Helpers.h
Go to the documentation of this file.
1 #ifndef GAUDIPYTHON_HELPERS_H
2 #define GAUDIPYTHON_HELPERS_H
3 
4 #include "Python.h"
5 
6 // Python 3 compatibility
7 #if PY_MAJOR_VERSION >= 3
8 
9 # define PyInt_FromLong PyLong_FromLong
10 
11 # define PyBuffer_Type PyMemoryView_Type
12 
13 // Taken from ROOT's TPyBufferFactory
14 static PyObject* PyBuffer_FromReadWriteMemory( void* ptr, int size ) {
15 # if PY_VERSION_HEX > 0x03000000
16  // Python 3 will set an exception if nullptr, just rely on size == 0
17  if ( !ptr ) {
18  static long dummy[1];
19  ptr = dummy;
20  size = 0;
21  }
22 # endif
23  Py_buffer bufinfo = {
24  ptr,
25  NULL,
26  size,
27  1,
28  0,
29  1,
30  NULL,
31  NULL,
32  NULL,
33  NULL,
34 # if PY_VERSION_HEX < 0x03030000
35  {0, 0},
36 # endif
37  NULL
38  };
39  return PyMemoryView_FromBuffer( &bufinfo );
40 }
41 #endif
42 
43 #include "GaudiKernel/DataObject.h"
45 #include "GaudiKernel/IAlgTool.h"
46 #include "GaudiKernel/IAlgorithm.h"
50 #include "GaudiKernel/IToolSvc.h"
51 #include "GaudiKernel/Property.h"
52 
53 // FIXME: (MCl) workaround for ROOT-5850
54 namespace AIDA {
55  class IHistogram1D;
56  class IHistogram2D;
57  class IHistogram3D;
58  class IProfile1D;
59  class IProfile2D;
60 } // namespace AIDA
61 
62 #if PY_VERSION_HEX < 0x02050000
63 // Note (MCl):
64 // In Python 2.5, all the functions working with lenghts use the type PySsize_t
65 // instead of int. The also changed the name of the typedefs for those functions.
66 // Here we use:
67 // intargfunc -> ssizeargfunc
68 // inquiry -> lenfunc
69 //
71 typedef int Py_ssize_t;
72 #endif
73 
74 // the following is done instead of including Python.h becuase there is
75 // a clash with codecs.h defined in Python and the one in VC++ 7.1
76 // struct _object;
77 // typedef _object PyObject;
78 // extern "C" {
79 // PyObject* PyBuffer_FromMemory( void *ptr, int size);
80 //}
81 
85 namespace GaudiPython {
86 
87  struct Helper {
88  // This is a number of static functions to overcome the current problem with PyLCGDict that
89  // does not allow to return instances of complex objects by calling arguments
90  // (reference to pointers)
91  Helper() {}
92  // Provided for backward compatibility
93  static IService* service( ISvcLocator* svcloc, const std::string& name, bool createif = false ) {
94  return svcloc->service( name, createif ).get();
95  }
96  // Provided for backward compatibility
97  static IAlgorithm* algorithm( IAlgManager* algmgr, const std::string& name, const bool createIf = false ) {
98  return algmgr->algorithm( name, createIf ).get();
99  }
100  // ==========================================================================
102  DataObject* o;
103  return dpsvc->retrieveObject( path, o ).isSuccess() ? o : nullptr;
104  }
105  // ==========================================================================
107  return dpsvc->registerObject( path, pObject );
108  }
109  // ==========================================================================
111  return dpsvc->unregisterObject( path );
112  }
113  // ==========================================================================
125  // ==========================================================================
138  const bool retrieve = true, const bool disableDoD = false );
139  // ==========================================================================
140  static IAlgTool* tool( IToolSvc* toolsvc, const std::string& type, const std::string& name, IInterface* p,
141  bool cif ) {
142  IAlgTool* o;
143  return toolsvc->retrieve( type, name, IAlgTool::interfaceID(), o, p, cif ).isSuccess() ? o : nullptr;
144  }
145  static long loadDynamicLib( const std::string& name ) {
146  void* h;
147  return System::loadDynamicLib( name, &h );
148  }
149  static IHistogram1D* histo1D( IHistogramSvc* hsvc, const std::string& path ) {
150  IHistogram1D* h;
151  return ( hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
152  }
153  static IHistogram2D* histo2D( IHistogramSvc* hsvc, const std::string& path ) {
154  IHistogram2D* h;
155  return ( hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
156  }
157  static IHistogram3D* histo3D( IHistogramSvc* hsvc, const std::string& path ) {
158  IHistogram3D* h;
159  return ( hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
160  }
161  static IProfile1D* profile1D( IHistogramSvc* hsvc, const std::string& path ) {
162  IProfile1D* h = 0;
163  return ( hsvc && hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
164  }
165  static IProfile2D* profile2D( IHistogramSvc* hsvc, const std::string& path ) {
166  IProfile2D* h = 0;
167  return ( hsvc && hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
168  }
169 
170  // Array support
171  private:
172  template <class T>
173  static Py_ssize_t Array_length( PyObject* self ) {
174 #if PY_MAJOR_VERSION >= 3
175  Py_buffer bufinfo;
176  ( *( self->ob_type->tp_as_buffer->bf_getbuffer ) )( self, &bufinfo, PyBUF_SIMPLE );
177  Py_ssize_t size = bufinfo.len;
178 #else
179 # if PY_VERSION_HEX < 0x02050000
180  const
181 # endif
182  char* buf = 0;
183  Py_ssize_t size = ( *( self->ob_type->tp_as_buffer->bf_getcharbuffer ) )( self, 0, &buf );
184 #endif
185  return size / sizeof( T );
186  }
187 
188  template <class T>
189  static PyObject* toPython( T* /*o*/ ) {
190  return 0;
191  }
192  static PyObject* toPython( int* o ) { return PyInt_FromLong( (long)*o ); }
193  static PyObject* toPython( short* o ) { return PyInt_FromLong( (long)*o ); }
194  static PyObject* toPython( char* o ) { return PyInt_FromLong( (long)*o ); }
195  static PyObject* toPython( long* o ) { return PyInt_FromLong( *o ); }
196  static PyObject* toPython( float* o ) { return PyFloat_FromDouble( (double)*o ); }
197  static PyObject* toPython( double* o ) { return PyFloat_FromDouble( *o ); }
198 
199  template <class T>
200  static PyObject* Array_item( PyObject* self, Py_ssize_t idx ) {
201 #if PY_VERSION_HEX < 0x02050000
202  const
203 #endif
204  char* buf = nullptr;
205 #if PY_MAJOR_VERSION >= 3
206  Py_buffer bufinfo;
207  ( *( self->ob_type->tp_as_buffer->bf_getbuffer ) )( self, &bufinfo, PyBUF_SIMPLE );
208  Py_ssize_t size = bufinfo.len;
209 #else
210  Py_ssize_t size = ( *( self->ob_type->tp_as_buffer->bf_getcharbuffer ) )( self, 0, &buf );
211 #endif
212  if ( idx < 0 || idx >= size / int( sizeof( T ) ) ) {
213  PyErr_SetString( PyExc_IndexError, "buffer index out of range" );
214  return nullptr;
215  }
216  return toPython( (T*)buf + idx );
217  }
218 
219  public:
220  template <class T>
221  static PyObject* toArray( T* ptr, Py_ssize_t size ) {
222  static PyTypeObject type = PyBuffer_Type;
223  static PySequenceMethods meth = *( PyBuffer_Type.tp_as_sequence );
224 #if PY_VERSION_HEX < 0x02050000
225  meth.sq_item = (intargfunc)&Array_item<T>;
226  meth.sq_length = (inquiry)&Array_length<T>;
227 #else
228  meth.sq_item = (ssizeargfunc)&Array_item<T>;
229  meth.sq_length = (lenfunc)&Array_length<T>;
230 #endif
231  type.tp_as_sequence = &meth;
232  PyObject* buf = PyBuffer_FromReadWriteMemory( ptr, size * sizeof( T ) );
233  buf->ob_type = &type;
234  Py_INCREF( buf->ob_type );
235  return buf;
236  }
237  static PyObject* toIntArray( void* ptr, Py_ssize_t size ) { return toArray( (int*)ptr, size ); }
238  static PyObject* toShortArray( void* ptr, Py_ssize_t size ) { return toArray( (short*)ptr, size ); }
239  static PyObject* toFloatArray( void* ptr, Py_ssize_t size ) { return toArray( (float*)ptr, size ); }
240  static PyObject* toDoubleArray( void* ptr, Py_ssize_t size ) { return toArray( (double*)ptr, size ); }
241 
242  template <class T>
243  static T* toAddress( std::vector<T>& v ) {
244  return v.data();
245  }
246  template <class T>
247  static T* toAddress( void* a ) {
248  return (T*)a;
249  }
250 
251  // FIXME: (MCl) workaround for ROOT-6028, ROOT-6054, ROOT-6073
253  return p.fromString( s );
254  }
255  };
256 
257  template PyObject* Helper::toArray( int*, Py_ssize_t );
258  template PyObject* Helper::toArray( char*, Py_ssize_t );
259  template PyObject* Helper::toArray( short*, Py_ssize_t );
260  template PyObject* Helper::toArray( float*, Py_ssize_t );
261  template PyObject* Helper::toArray( double*, Py_ssize_t );
262  template int* Helper::toAddress( std::vector<int>& );
263  template float* Helper::toAddress( std::vector<float>& );
264  template double* Helper::toAddress( std::vector<double>& );
265  template int* Helper::toAddress<int>( void* );
266  template float* Helper::toAddress<float>( void* );
267  template double* Helper::toAddress<double>( void* );
268 
269 } // namespace GaudiPython
270 
271 #endif // !GAUDIPYTHON_HELPERS_H
virtual SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
constexpr auto size(const T &, Args &&...) noexcept
static PyObject * toPython(long *o)
Definition: Helpers.h:195
virtual StatusCode retrieve(const std::string &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.
static IAlgTool * tool(IToolSvc *toolsvc, const std::string &type, const std::string &name, IInterface *p, bool cif)
Definition: Helpers.h:140
static PyObject * toDoubleArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:240
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
static PyObject * toIntArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:237
static PyObject * toPython(double *o)
Definition: Helpers.h:197
static PyObject * toPython(int *o)
Definition: Helpers.h:192
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:41
static PyObject * toArray(T *ptr, Py_ssize_t size)
Definition: Helpers.h:221
static IProfile2D * profile2D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:165
static PyObject * toPython(T *)
Definition: Helpers.h:189
The IAlgManager is the interface implemented by the Algorithm Factory in the Application Manager to s...
Definition: IAlgManager.h:27
static PyObject * Array_item(PyObject *self, Py_ssize_t idx)
Definition: Helpers.h:200
static PyObject * toPython(float *o)
Definition: Helpers.h:196
int Py_ssize_t
For compatibility with Python 2.4 and 2.5.
Definition: Helpers.h:71
Data provider interface definition.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
static PyObject * toFloatArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:239
GaudiKernel.
Definition: Fill.h:10
virtual StatusCode fromString(const std::string &value)=0
string -> value
STL class.
virtual StatusCode unregisterObject(std::string_view fullPath)=0
Unregister object from the data store.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
static StatusCode setPropertyFromString(Gaudi::Details::PropertyBase &p, const std::string &s)
Definition: Helpers.h:252
T data(T... args)
static IHistogram3D * histo3D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:157
static long loadDynamicLib(const std::string &name)
Definition: Helpers.h:145
static T * toAddress(void *a)
Definition: Helpers.h:247
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Definition of the basic interface.
Definition: IInterface.h:244
static IAlgorithm * algorithm(IAlgManager *algmgr, const std::string &name, const bool createIf=false)
Definition: Helpers.h:97
Definition of the IHistogramSvc interface class.
Definition: IHistogramSvc.h:46
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
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:109
bool isSuccess() const
Definition: StatusCode.h:267
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:28
GaudiPython.h GaudiPython/GaudiPython.h.
Definition: AlgDecorators.h:37
static IHistogram2D * histo2D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:153
static StatusCode unregisterObject(IDataProviderSvc *dpsvc, const std::string &path)
Definition: Helpers.h:110
static IService * service(ISvcLocator *svcloc, const std::string &name, bool createif=false)
Definition: Helpers.h:93
STL class.
virtual StatusCode retrieveObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
static PyObject * toShortArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:238
static DataObject * dataobject(IDataProviderSvc *dpsvc, const std::string &path)
Definition: Helpers.h:101
StatusCode registerObject(std::string_view fullPath, DataObject *pObject)
Register object with the data store.
virtual StatusCode findObject(const std::string &fullPath, AIDA::IHistogram1D *&h1dObj)=0
static Py_ssize_t Array_length(PyObject *self)
Definition: Helpers.h:173
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
string s
Definition: gaudirun.py:318
static T * toAddress(std::vector< T > &v)
Definition: Helpers.h:243
static IProfile1D * profile1D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:161
#define GAUDI_API
Definition: Kernel.h:71
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
static IHistogram1D * histo1D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:149
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:253
static PyObject * toPython(char *o)
Definition: Helpers.h:194
static PyObject * toPython(short *o)
Definition: Helpers.h:193
static StatusCode registerObject(IDataProviderSvc *dpsvc, const std::string &path, DataObject *pObject)
Definition: Helpers.h:106
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:135