All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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
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  // ==========================================================================
66  const std::string& path )
67  {
68  DataObject* o;
69  if ( dpsvc->retrieveObject(path,o).isSuccess() ) return o;
70  else return 0;
71  }
72  // ==========================================================================
84  ( IDataProviderSvc* dpsvc ,
85  const std::string& path ) ;
86  // ==========================================================================
99  ( IDataProviderSvc* dpsvc ,
100  const std::string& path ,
101  const bool retrieve = true ,
102  const bool disableDoD = false ) ;
103  // ==========================================================================
104  static IAlgTool* tool(IToolSvc* toolsvc, const std::string& type, const std::string& name, IInterface* p, bool cif ) {
105  IAlgTool* o;
106  if ( toolsvc->retrieve(type, name, IAlgTool::interfaceID(), o, p, cif).isSuccess() ) return o;
107  else return 0;
108  }
109  static long loadDynamicLib(const std::string& name) {
110  void* h;
111  return System::loadDynamicLib(name, &h);
112  }
113  static IHistogram1D* histo1D( IHistogramSvc* hsvc, const std::string& path ) {
114  IHistogram1D* h;
115  if ( hsvc->findObject(path, h ).isSuccess() ) return h;
116  else return 0;
117  }
118  static IHistogram2D* histo2D( IHistogramSvc* hsvc, const std::string& path ) {
119  IHistogram2D* h;
120  if ( hsvc->findObject(path, h ).isSuccess() ) return h;
121  else return 0;
122  }
123  static IHistogram3D* histo3D( IHistogramSvc* hsvc, const std::string& path ) {
124  IHistogram3D* h;
125  if ( hsvc->findObject(path, h ).isSuccess() ) return h;
126  else return 0;
127  }
128  static IProfile1D*
129  profile1D
130  ( IHistogramSvc* hsvc ,
131  const std::string& path )
132  {
133  IProfile1D* h = 0 ;
134  if ( 0 != hsvc && hsvc->findObject ( path , h ).isSuccess() ) { return h ; }
135  return 0 ;
136  }
137  static IProfile2D*
138  profile2D
139  ( IHistogramSvc* hsvc ,
140  const std::string& path )
141  {
142  IProfile2D* h = 0 ;
143  if ( 0 != hsvc && hsvc->findObject ( path , h ).isSuccess() ) { return h ; }
144  return 0 ;
145  }
146 
147 // Array support
148 private:
149  template <class T>
150  static Py_ssize_t Array_length( PyObject* self ) {
151 #if PY_VERSION_HEX < 0x02050000
152  const
153 #endif
154  char* buf = 0;
155  Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
156  return size/sizeof(T);
157  }
158 
159  template <class T> static PyObject* toPython(T* /*o*/) { return 0; }
160  static PyObject* toPython(int* o) { return PyInt_FromLong((long)*o); }
161  static PyObject* toPython(short* o) { return PyInt_FromLong((long)*o); }
162  static PyObject* toPython(char* o) { return PyInt_FromLong((long)*o); }
163  static PyObject* toPython(long* o) { return PyInt_FromLong(*o); }
164  static PyObject* toPython(float* o) { return PyFloat_FromDouble((double)*o); }
165  static PyObject* toPython(double* o) { return PyFloat_FromDouble(*o); }
166 
167  template <class T>
168  static PyObject* Array_item( PyObject* self, Py_ssize_t idx ) {
169 #if PY_VERSION_HEX < 0x02050000
170  const
171 #endif
172  char* buf = 0;
173  Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
174  if ( idx < 0 || idx >= size/int(sizeof(T)) ) {
175  PyErr_SetString( PyExc_IndexError, "buffer index out of range" );
176  return 0;
177  }
178  return toPython((T*)buf + idx);
179  }
180 
181 public:
182  template <class T>
183  static PyObject* toArray( T* ptr, Py_ssize_t size ) {
184  static PyTypeObject type = PyBuffer_Type;
185  static PySequenceMethods meth = *(PyBuffer_Type.tp_as_sequence);
186 #if PY_VERSION_HEX < 0x02050000
187  meth.sq_item = (intargfunc) &Array_item<T>;
188  meth.sq_length = (inquiry) &Array_length<T>;
189 #else
190  meth.sq_item = (ssizeargfunc) &Array_item<T>;
191  meth.sq_length = (lenfunc) &Array_length<T>;
192 #endif
193  type.tp_as_sequence = &meth;
194  PyObject* buf = PyBuffer_FromReadWriteMemory( ptr, size*sizeof(T) );
195  buf->ob_type = &type;
196  Py_INCREF( buf->ob_type );
197  return buf;
198  }
199  static PyObject* toIntArray ( void* ptr, Py_ssize_t size ) { return toArray( (int*) ptr , size ); }
200  static PyObject* toShortArray ( void* ptr, Py_ssize_t size ) { return toArray( (short*) ptr , size ); }
201  static PyObject* toFloatArray ( void* ptr, Py_ssize_t size ) { return toArray( (float*) ptr , size ); }
202  static PyObject* toDoubleArray ( void* ptr, Py_ssize_t size ) { return toArray( (double*) ptr , size ); }
203 
204  template <class T>
205  static T* toAddress( std::vector<T>& v ) {
206  return &(*v.begin());
207  }
208  template <class T>
209  static T* toAddress( void * a) {
210  return (T*)a;
211  }
212 
213  // FIXME: (MCl) workaround for ROOT-6028, ROOT-6054, ROOT-6073
214  static StatusCode setPropertyFromString(Property& p, const std::string& s) {
215  return p.fromString(s);
216  }
217 };
218 
219 template PyObject* Helper::toArray(int*,Py_ssize_t);
220 template PyObject* Helper::toArray(char*,Py_ssize_t);
221 template PyObject* Helper::toArray(short*,Py_ssize_t);
222 template PyObject* Helper::toArray(float*,Py_ssize_t);
223 template PyObject* Helper::toArray(double*,Py_ssize_t);
224 template int* Helper::toAddress(std::vector<int>&);
225 template float* Helper::toAddress(std::vector<float>&);
226 template double* Helper::toAddress(std::vector<double>&);
227 template int* Helper::toAddress<int>(void*);
228 template float* Helper::toAddress<float>(void*);
229 template double* Helper::toAddress<double>(void*);
230 
231 } // namespace GaudiPython
232 
233 #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:17
static PyObject * toPython(long *o)
Definition: Helpers.h:163
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:104
static PyObject * toDoubleArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:202
int Py_ssize_t
For compatibility with Python 2.4 and 2.5.
Definition: Helpers.h:31
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
static PyObject * toIntArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:199
static PyObject * toPython(double *o)
Definition: Helpers.h:165
static PyObject * toPython(int *o)
Definition: Helpers.h:160
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 PyObject * toArray(T *ptr, Py_ssize_t size)
Definition: Helpers.h:183
static IProfile2D * profile2D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:139
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:62
static StatusCode setPropertyFromString(Property &p, const std::string &s)
Definition: Helpers.h:214
static PyObject * toPython(T *)
Definition: Helpers.h:159
The IAlgManager is the interface implemented by the Algorithm Factory in the Application Manager to s...
Definition: IAlgManager.h:28
static PyObject * Array_item(PyObject *self, Py_ssize_t idx)
Definition: Helpers.h:168
static PyObject * toPython(float *o)
Definition: Helpers.h:164
Data provider interface definition.
static PyObject * toFloatArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:201
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:82
string type
Definition: gaudirun.py:126
static IHistogram3D * histo3D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:123
static long loadDynamicLib(const std::string &name)
Definition: Helpers.h:109
static T * toAddress(void *a)
Definition: Helpers.h:209
General service interface definition.
Definition: IService.h:19
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Definition of the basic interface.
Definition: IInterface.h:160
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
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:62
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:134
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:20
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
static IHistogram2D * histo2D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:118
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:43
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:200
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:150
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
string s
Definition: gaudirun.py:210
static T * toAddress(std::vector< T > &v)
Definition: Helpers.h:205
static IProfile1D * profile1D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:130
#define GAUDI_API
Definition: Kernel.h:108
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
static IHistogram1D * histo1D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:113
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:171
static PyObject * toPython(char *o)
Definition: Helpers.h:162
static PyObject * toPython(short *o)
Definition: Helpers.h:161
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:123
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