All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 // Framework
7 #include "GaudiKernel/IAlgManager.h"
8 #include "GaudiKernel/IAlgorithm.h"
9 #include "GaudiKernel/IAlgTool.h"
10 
11 // FIXME: (MCl) workaround for ROOT-5847
12 class Property;
13 // FIXME: (MCl) workaround for ROOT-5850
14 namespace AIDA {
15  class IHistogram1D;
16  class IHistogram2D;
17  class IHistogram3D;
18  class IProfile1D;
19  class IProfile2D;
20 }
21 
22 #if PY_VERSION_HEX < 0x02050000
23 // Note (MCl):
24 // In Python 2.5, all the functions working with lenghts use the type PySsize_t
25 // instead of int. The also changed the name of the typedefs for those functions.
26 // Here we use:
27 // intargfunc -> ssizeargfunc
28 // inquiry -> lenfunc
29 //
31 typedef int Py_ssize_t;
32 #endif
33 
34 // the following is done instead of including Python.h becuase there is
35 // a clash with codecs.h defined in Python and the one in VC++ 7.1
36 //struct _object;
37 //typedef _object PyObject;
38 //extern "C" {
39 // PyObject* PyBuffer_FromMemory( void *ptr, int size);
40 //}
41 
45 namespace GaudiPython {
46 
47 struct Helper {
48  // This is a number of static functions to overcome the current problem with PyLCGDict that
49  // does not allow to return instances of complex objects by calling arguments
50  // (reference to pointers)
51  Helper() {}
52  // Provided for backward compatibility
53  static IService* service(ISvcLocator* svcloc, const std::string& name, bool createif=false) {
54  return svcloc->service(name, createif).get();
55  }
56  // Provided for backward compatibility
57  static IAlgorithm* algorithm
58  ( IAlgManager* algmgr ,
59  const std::string& name ,
60  const bool createIf = false )
61  {
62  return algmgr->algorithm(name, createIf).get();
63  }
64  // ==========================================================================
65  static DataObject* dataobject ( IDataProviderSvc* dpsvc ,
66  const std::string& path )
67  {
68  DataObject* o;
69  return dpsvc->retrieveObject(path,o).isSuccess() ? o : nullptr;
70  }
71  // ==========================================================================
83  ( IDataProviderSvc* dpsvc ,
84  const std::string& path ) ;
85  // ==========================================================================
98  ( IDataProviderSvc* dpsvc ,
99  const std::string& path ,
100  const bool retrieve = true ,
101  const bool disableDoD = false ) ;
102  // ==========================================================================
103  static IAlgTool* tool(IToolSvc* toolsvc, const std::string& type, const std::string& name, IInterface* p, bool cif ) {
104  IAlgTool* o;
105  return toolsvc->retrieve(type, name, IAlgTool::interfaceID(), o, p, cif).isSuccess() ?
106  o : nullptr ;
107  }
108  static long loadDynamicLib(const std::string& name) {
109  void* h;
110  return System::loadDynamicLib(name, &h);
111  }
112  static IHistogram1D* histo1D( IHistogramSvc* hsvc, const std::string& path ) {
113  IHistogram1D* h;
114  return ( hsvc->findObject(path, h ).isSuccess() ) ? h : nullptr;
115  }
116  static IHistogram2D* histo2D( IHistogramSvc* hsvc, const std::string& path ) {
117  IHistogram2D* h;
118  return ( hsvc->findObject(path, h ).isSuccess() ) ? h : nullptr;
119  }
120  static IHistogram3D* histo3D( IHistogramSvc* hsvc, const std::string& path ) {
121  IHistogram3D* h;
122  return ( hsvc->findObject(path, h ).isSuccess() ) ? h : nullptr;
123  }
124  static IProfile1D*
125  profile1D
126  ( IHistogramSvc* hsvc ,
127  const std::string& path )
128  {
129  IProfile1D* h = 0 ;
130  return ( hsvc && hsvc->findObject ( path , h ).isSuccess() ) ? h : nullptr;
131  }
132  static IProfile2D*
133  profile2D
134  ( IHistogramSvc* hsvc ,
135  const std::string& path )
136  {
137  IProfile2D* h = 0 ;
138  return ( hsvc && hsvc->findObject ( path , h ).isSuccess() ) ? h : nullptr;
139  }
140 
141 // Array support
142 private:
143  template <class T>
144  static Py_ssize_t Array_length( PyObject* self ) {
145 #if PY_VERSION_HEX < 0x02050000
146  const
147 #endif
148  char* buf = 0;
149  Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
150  return size/sizeof(T);
151  }
152 
153  template <class T> static PyObject* toPython(T* /*o*/) { return 0; }
154  static PyObject* toPython(int* o) { return PyInt_FromLong((long)*o); }
155  static PyObject* toPython(short* o) { return PyInt_FromLong((long)*o); }
156  static PyObject* toPython(char* o) { return PyInt_FromLong((long)*o); }
157  static PyObject* toPython(long* o) { return PyInt_FromLong(*o); }
158  static PyObject* toPython(float* o) { return PyFloat_FromDouble((double)*o); }
159  static PyObject* toPython(double* o) { return PyFloat_FromDouble(*o); }
160 
161  template <class T>
162  static PyObject* Array_item( PyObject* self, Py_ssize_t idx ) {
163 #if PY_VERSION_HEX < 0x02050000
164  const
165 #endif
166  char* buf = nullptr;
167  Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
168  if ( idx < 0 || idx >= size/int(sizeof(T)) ) {
169  PyErr_SetString( PyExc_IndexError, "buffer index out of range" );
170  return nullptr;
171  }
172  return toPython((T*)buf + idx);
173  }
174 
175 public:
176  template <class T>
177  static PyObject* toArray( T* ptr, Py_ssize_t size ) {
178  static PyTypeObject type = PyBuffer_Type;
179  static PySequenceMethods meth = *(PyBuffer_Type.tp_as_sequence);
180 #if PY_VERSION_HEX < 0x02050000
181  meth.sq_item = (intargfunc) &Array_item<T>;
182  meth.sq_length = (inquiry) &Array_length<T>;
183 #else
184  meth.sq_item = (ssizeargfunc) &Array_item<T>;
185  meth.sq_length = (lenfunc) &Array_length<T>;
186 #endif
187  type.tp_as_sequence = &meth;
188  PyObject* buf = PyBuffer_FromReadWriteMemory( ptr, size*sizeof(T) );
189  buf->ob_type = &type;
190  Py_INCREF( buf->ob_type );
191  return buf;
192  }
193  static PyObject* toIntArray ( void* ptr, Py_ssize_t size ) { return toArray( (int*) ptr , size ); }
194  static PyObject* toShortArray ( void* ptr, Py_ssize_t size ) { return toArray( (short*) ptr , size ); }
195  static PyObject* toFloatArray ( void* ptr, Py_ssize_t size ) { return toArray( (float*) ptr , size ); }
196  static PyObject* toDoubleArray ( void* ptr, Py_ssize_t size ) { return toArray( (double*) ptr , size ); }
197 
198  template <class T>
199  static T* toAddress( std::vector<T>& v ) {
200  return v.data();
201  }
202  template <class T>
203  static T* toAddress( void * a) {
204  return (T*)a;
205  }
206 
207  // FIXME: (MCl) workaround for ROOT-6028, ROOT-6054, ROOT-6073
208  static StatusCode setPropertyFromString(Property& p, const std::string& s) {
209  return p.fromString(s);
210  }
211 };
212 
213 template PyObject* Helper::toArray(int*,Py_ssize_t);
214 template PyObject* Helper::toArray(char*,Py_ssize_t);
215 template PyObject* Helper::toArray(short*,Py_ssize_t);
216 template PyObject* Helper::toArray(float*,Py_ssize_t);
217 template PyObject* Helper::toArray(double*,Py_ssize_t);
218 template int* Helper::toAddress(std::vector<int>&);
219 template float* Helper::toAddress(std::vector<float>&);
220 template double* Helper::toAddress(std::vector<double>&);
221 template int* Helper::toAddress<int>(void*);
222 template float* Helper::toAddress<float>(void*);
223 template double* Helper::toAddress<double>(void*);
224 
225 } // namespace GaudiPython
226 
227 #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:18
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:103
static PyObject * toDoubleArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:196
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:193
static PyObject * toArray(T *ptr, Py_ssize_t size)
Definition: Helpers.h:177
#define GAUDI_API
Definition: Kernel.h:107
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:42
static IProfile2D * profile2D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:134
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
static StatusCode setPropertyFromString(Property &p, const std::string &s)
Definition: Helpers.h:208
list path
Definition: __init__.py:15
static PyObject * toPython(T *)
Definition: Helpers.h:153
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:162
Data provider interface definition.
static PyObject * toFloatArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:195
GaudiKernel.
Definition: Fill.h:8
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
int Py_ssize_t
For compatibility with Python 2.4 and 2.5.
Definition: Helpers.h:31
static IHistogram3D * histo3D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:120
static long loadDynamicLib(const std::string &name)
Definition: Helpers.h:108
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Definition of the basic interface.
Definition: IInterface.h:234
static IAlgorithm * algorithm(IAlgManager *algmgr, const std::string &name, const bool createIf=false)
Definition: Helpers.h:58
Definition of the IHistogramSvc interface class.
Definition: IHistogramSvc.h:47
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:124
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:23
GaudiPython.h GaudiPython/GaudiPython.h.
Definition: AlgDecorators.h:37
static IHistogram2D * histo2D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:116
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
static IService * service(ISvcLocator *svcloc, const std::string &name, bool createif=false)
Definition: Helpers.h:53
static PyObject * toShortArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:194
static DataObject * dataobject(IDataProviderSvc *dpsvc, const std::string &path)
Definition: Helpers.h:65
virtual StatusCode findObject(const std::string &fullPath, AIDA::IHistogram1D *&h1dObj)=0
static Py_ssize_t Array_length(PyObject *self)
Definition: Helpers.h:144
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
string s
Definition: gaudirun.py:245
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:124
static T * toAddress(std::vector< T > &v)
Definition: Helpers.h:199
static IProfile1D * profile1D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:126
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:112
string type
Definition: gaudirun.py:151
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
virtual StatusCode fromString(const std::string &value)=0
string -> value