| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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