| The Gaudi Framework v32r1 (f65d50dc)

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