Commit beabde14 authored by cvs2svn's avatar cvs2svn
Browse files

This commit was manufactured by cvs2svn to create tag

'GAUDI_v19r3-pre'.

git-svn-id: svn+ssh://svn.cern.ch/reps/gaudi/distribution/tags/GAUDI/GAUDI_v19r3-pre@3726 53ecefc3-a64d-0410-9bdf-b8581fa3287e
parent de6eb162
// $Id: AlgDecorators.h,v 1.2 2005/11/08 16:31:03 mato Exp $
// ============================================================================
#ifndef GAUDIPYTHON_ALGDECORATORS_H
#define GAUDIPYTHON_ALGDECORATORS_H 1
// ============================================================================
// Include files
// ============================================================================
// STD & STL
// ============================================================================
#include <string>
// ============================================================================
// GaudiAlg
// ============================================================================
#include "GaudiAlg/GaudiAlgorithm.h"
// ============================================================================
// GaudiPython
// ============================================================================
#include "GaudiPython/GaudiPython.h"
// ============================================================================
// forward declarations
// ============================================================================
class IInterface ;
class IAlgTool ;
class GaudiAlgorithm ;
// ============================================================================
/** @file
*
* Collection of "decorators" for python algorithms
*
* @author Vanya BELYAEV Ivan.Belyaev@lapp.in2p3.fr
* @date 2005-08-03
*/
namespace GaudiPython
{
/** @class Decorator
* simple class with allows to "decorate" the python
* algorithm with 'basic' funtionality from class GaudiAlgorithm
* @author Vanya BELYAEV Ivan.Belyaev@lapp.in2p3.fr
* @date 2007-08-03
*/
class AlgDecorator
{
public:
/** get the tool from GaudiAlgorithm
* @param alg GaudiAlgorithm
* @param type tool type
* @param name tool name
* @param parent tool parent
* @param create flag to create
* @return the tool
*/
static IAlgTool* tool_
( const GaudiAlgorithm* alg ,
const std::string& type ,
const std::string& name ,
const IInterface* parent = 0 ,
const bool create = true ) ;
/** get the tool from GaudiAlgorithm
* @param alg GaudiAlgorithm
* @param typeAndName tool type/name
* @param parent tool parent
* @param create flag to create
* @return the tool
*/
static IAlgTool* tool_
( const GaudiAlgorithm* alg ,
const std::string& typeAndName ,
const IInterface* parent = 0 ,
const bool create = true ) ;
/** get the service from GaudiAlgorithm
* @param alg GaudiAlgorithm
* @param name service name
* @param create flag to create
* @return the tool
*/
static IInterface* svc_
( const GaudiAlgorithm* alg ,
const std::string& name ,
const bool create = false ) ;
};
}; // end of namespace GaudiPython
#endif // GAUDIPYTHON_ALGDECORATORS_H
// $Id: Algorithm.h,v 1.18 2006/12/14 12:23:55 leggett Exp $
// ============================================================================
// ============================================================================
#ifndef GAUDIPYTHON_ALGORITHM_H
#define GAUDIPYTHON_ALGORITHM_H
// ============================================================================
// Include Files
// ============================================================================
// GaudiKernel
// ============================================================================
#include "GaudiKernel/Algorithm.h"
#include "GaudiKernel/Bootstrap.h"
// ============================================================================
// GaudiPython
// ============================================================================
#include "GaudiPython/GaudiPython.h"
// ============================================================================
// Python
// ============================================================================
#include "Python.h"
// ============================================================================
namespace GaudiPython
{
/// call the python method
StatusCode call_python_method ( PyObject* self , char* method ) ;
/// check the existence of the method
bool has_python_method ( PyObject* self , char* method ) ;
};
namespace GaudiPython
{
/** @class PyAlgorithm
* Python Algorithm base class
*/
class PyAlgorithm : public Algorithm
{
public:
/** constructor
* @param self python objects
* @param name name of algorithm instance
*/
PyAlgorithm
( PyObject* self ,
const std::string& name ) ;
public:
StatusCode initialize () ;
StatusCode beginRun () ;
StatusCode endRun () ;
StatusCode execute () ;
StatusCode finalize () ;
IAlgorithm* myself() { return this; }
private:
PyObject* m_self;
};
/** @class PyAlg
* general class to embedd the existing algorithm/base class
* into the python
* @author Vanya BELYAEV Ivan.Belyaev@lapp.in2p3.fr
* @date 2005-08-03
*/
template <class ALGORITHM>
class PyAlg :
public ALGORITHM ,
public virtual IAlgorithm ,
public virtual IProperty
{
public:
/** constructor from Python object and the name
* @param self python object
* @param name name of algorithm instance
*/
PyAlg
( PyObject* self ,
const std::string& name )
: ALGORITHM ( name , Gaudi::svcLocator() )
, m_self ( self )
{
// the printout of actual type for embedded algorithm has no sence
setProperty ( "TypePrint" , "false" ) ;
setProperty ( "StatPrint" , "true" ) ;
// The owner of the Algorithm is Python (as creator) therfore
// it should not be deleted by Gaudi (added an extra addRef()).
addRef() ;
addRef() ;
}
protected:
/// virtual destructor
virtual ~PyAlg() {}
PyObject* _obj() const { return m_self ; }
public:
virtual StatusCode initialize ()
{ return GaudiPython::call_python_method ( m_self , "initialize" ) ; }
virtual StatusCode beginRun ()
{ return GaudiPython::call_python_method ( m_self , "beginRun" ) ; }
virtual StatusCode endRun ()
{ return GaudiPython::call_python_method ( m_self , "endRun" ) ; }
virtual StatusCode execute ()
{ return GaudiPython::call_python_method ( m_self , "execute" ) ; }
virtual StatusCode finalize ()
{ return GaudiPython::call_python_method ( m_self , "finalize" ) ; }
virtual IAlgorithm* ialgorithm () { return this ; }
virtual IProperty* iproperty () { return this ; }
// preserve the existing methods
virtual StatusCode initialize_ () { return ALGORITHM::initialize () ; }
virtual StatusCode finalize_ () { return ALGORITHM::finalize () ; }
private:
// the default constructor is desabled
PyAlg() ;
// the copy constructor is desabled
PyAlg ( const PyAlg& );
// the assignement operator is desabled
PyAlg& operator=( const PyAlg& ) ;
private:
// "shadow" python class
PyObject* m_self;
} ;
} // namespace GaudiPython
// ============================================================================
// The END
// ============================================================================
#endif // GAUDIPYTHON_ALGORITHM_H
// ============================================================================
#ifndef GAUDIPYTHON_CALLBACKSTREAMBUF_H
#define GAUDIPYTHON_CALLBACKSTREAMBUF_H
// ============================================================================
#include <sstream> //included for stringbuf
// ============================================================================
#include "Python.h" //included for the python API
// ============================================================================
#include "GaudiPython/GaudiPython.h"
// ============================================================================
namespace GaudiPython
{
int call_python_method(PyObject* self , char* method, char* buf);
class CallbackStreamBuf : public std::stringbuf
{
private:
PyObject* m_self;
char* m_callbackBuff;
public:
CallbackStreamBuf(PyObject* self);
~CallbackStreamBuf();
virtual int sync() ;
}; //CallbackStreamBuf
}; //namespace GaudiPython
#endif
// $Id: GaudiPython.h,v 1.7 2005/11/08 16:31:03 mato Exp $
// ============================================================================
#ifndef GAUDIPYTHON_GAUDIPYTHON_H
#define GAUDIPYTHON_GAUDIPYTHON_H 1
// ============================================================================
// Include files
// ============================================================================
/** @namespace GaudiPython GaudiPython.h GaudiPython/GaudiPython.h
*
* The general namespace to hide all Gaudi-python related classes
*
* @author Vanya BELYAEV Ivan.Belyaev@lapp.in2p3.fr
* @date 2005-08-04
*/
namespace GaudiPython
{
} ; // end of namespace GaudiPython
#endif // GAUDIPYTHON_GAUDIPYTHON_H
#ifndef GAUDIPYTHON_HELPERS_H
#define GAUDIPYTHON_HELPERS_H
// Framework
#include "GaudiKernel/IAlgManager.h"
#include "GaudiKernel/IAlgorithm.h"
#include "GaudiKernel/IAlgTool.h"
#ifdef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#endif
#include "Python.h"
// the following is done instead of including Python.h becuase there is
// a clash with codecs.h defined in Python and the one in VC++ 7.1
//struct _object;
//typedef _object PyObject;
//extern "C" {
// PyObject* PyBuffer_FromMemory( void *ptr, int size);
//}
/** Namespace for all classes interfacing Gaudi to Python.
*/
namespace GaudiPython {
struct Helper {
// This is a number of static functions to overcome the current problem with PyLCGDict that
// does not allow to return instances of complex objects by calling arguments
// (reference to pointers)
Helper() {}
static IService* service(ISvcLocator* svcloc, const std::string& name, bool createif=false) {
IService* svc;
if ( svcloc->getService(name, svc, createif).isSuccess() ) return svc;
else return 0;
}
static IAlgorithm* algorithm(IAlgManager* algmgr, const std::string& name) {
IAlgorithm* alg;
if ( algmgr->getAlgorithm(name, alg ).isSuccess() ) return alg;
else return 0;
}
static DataObject* dataobject(IDataProviderSvc* dpsvc, const std::string& path ) {
DataObject* o;
if ( dpsvc->retrieveObject(path,o).isSuccess() ) return o;
else return 0;
}
static IAlgTool* tool(IToolSvc* toolsvc, const std::string& type, const std::string& name, IInterface* p, bool cif ) {
IAlgTool* o;
if ( toolsvc->retrieve(type, name, IAlgTool::interfaceID(), o, p, cif).isSuccess() ) return o;
else return 0;
}
static long loadDynamicLib(const std::string& name) {
void* h;
return System::loadDynamicLib(name, &h);
}
static IHistogram1D* histo1D( IHistogramSvc* hsvc, const std::string& path ) {
IHistogram1D* h;
if ( hsvc->findObject(path, h ).isSuccess() ) return h;
else return 0;
}
static IHistogram2D* histo2D( IHistogramSvc* hsvc, const std::string& path ) {
IHistogram2D* h;
if ( hsvc->findObject(path, h ).isSuccess() ) return h;
else return 0;
}
static IHistogram3D* histo3D( IHistogramSvc* hsvc, const std::string& path ) {
IHistogram3D* h;
if ( hsvc->findObject(path, h ).isSuccess() ) return h;
else return 0;
}
// Array support
private:
template <class T>
static int Array_length( PyObject* self ) {
const char* buf = 0;
int size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
return size/sizeof(T);
}
template <class T> static PyObject* toPython(T* o) { return 0; }
static PyObject* toPython(int* o) { return PyInt_FromLong((long)*o); }
static PyObject* toPython(short* o) { return PyInt_FromLong((long)*o); }
static PyObject* toPython(char* o) { return PyInt_FromLong((long)*o); }
static PyObject* toPython(long* o) { return PyInt_FromLong(*o); }
static PyObject* toPython(float* o) { return PyFloat_FromDouble((double)*o); }
static PyObject* toPython(double* o) { return PyFloat_FromDouble(*o); }
template <class T>
static PyObject* Array_item( PyObject* self, int idx ) {
const char* buf = 0;
int size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
if ( idx < 0 || idx >= size/int(sizeof(T)) ) {
PyErr_SetString( PyExc_IndexError, "buffer index out of range" );
return 0;
}
return toPython((T*)buf + idx);
}
public:
template <class T>
static PyObject* toArray( T* ptr, int size ) {
static PyTypeObject type = PyBuffer_Type;
static PySequenceMethods meth = *(PyBuffer_Type.tp_as_sequence);
meth.sq_item = (intargfunc) &Array_item<T>;
meth.sq_length = (inquiry) &Array_length<T>;
type.tp_as_sequence = &meth;
PyObject* buf = PyBuffer_FromReadWriteMemory( ptr, size*sizeof(T) );
Py_INCREF( &type );
buf->ob_type = &type;
return buf;
}
static PyObject* toIntArray( void* ptr, int size ) { return toArray( (int*)ptr, size ); }
static PyObject* toShortArray( void* ptr, int size ) { return toArray( (short*)ptr, size ); }
static PyObject* toFloatArray( void* ptr, int size ) { return toArray( (float*)ptr, size ); }
static PyObject* toDoubleArray( void* ptr, int size ) { return toArray( (double*)ptr, size ); }
template <class T>
static T* toAddress( std::vector<T>& v ) {
return &(*v.begin());
}
template <class T>
static T* toAddress( void * a) {
return (T*)a;
}
};
template PyObject* Helper::toArray(int*,int);
template PyObject* Helper::toArray(char*,int);
template PyObject* Helper::toArray(short*,int);
template PyObject* Helper::toArray(float*,int);
template PyObject* Helper::toArray(double*,int);
template int* Helper::toAddress(std::vector<int>&);
template float* Helper::toAddress(std::vector<float>&);
template double* Helper::toAddress(std::vector<double>&);
template int* Helper::toAddress<int>(void*);
template float* Helper::toAddress<float>(void*);
template double* Helper::toAddress<double>(void*);
} // namespace GaudiPython
#endif // !GAUDIPYTHON_HELPERS_H
// $Id: HistoDecorator.h,v 1.2 2005/09/12 15:29:13 mato Exp $
// ============================================================================
#ifndef GAUDIPYTHON_HISTODECORATOR_H
#define GAUDIPYTHON_HISTODECORATOR_H 1
// ============================================================================
// Include files
// ============================================================================
// GaudiAlg
// ============================================================================
#include "GaudiAlg/GaudiHistoAlg.h"
// ============================================================================
// GaudiPython
// ============================================================================
#include "GaudiPython/GaudiPython.h"
#include "GaudiPython/Vector.h"
// ============================================================================
namespace GaudiPython
{
/** @class HistoDecorator HistoDecorator.h GaudiPython/HistoDecorator.h
*
*
* @author Vanya BELYAEV Ivan.Belyaev@lapp.in2p3.fr
* @date 2005-08-04
*/
class HistoDecorator
{
public:
/** "plot"(book&fill) a sequence of data from the vector (implicit loop)
* (Expected to be more efficient)
* @param algo the algorihtm
* @param data data
* @param title histogram title
* @param low low edge
* @param high high edge
* @param bins number of bins
*/
static AIDA::IHistogram1D* plot1D
( const GaudiHistoAlg& algo ,
const double data ,
const std::string& title ,
const double low ,
const double high ,
const unsigned long bins = 100 ) ;
/** "plot"(book&fill) a sequence of data from the vector (implicit loop)
* (Expected to be more efficient)
* @param algo the algorihtm
* @param data data
* @param ID histogram ID
* @param title histogram title
* @param low low edge
* @param high high edge
* @param bins number of bins
*/
static AIDA::IHistogram1D* plot1D
( const GaudiHistoAlg& algo ,
const double data ,
const GaudiAlg::HistoID& ID ,
const std::string& title ,
const double low ,
const double high ,
const unsigned long bins = 100 ) ;
/** "plot"(book&fill) a sequence of data from the vector (implicit loop)
* (Expected to be more efficient)
* @param algo the algorihtm
* @param data data
* @param ID histogram ID
* @param title histogram title
* @param low low edge
* @param high high edge
* @param bins number of bins
*/
static AIDA::IHistogram1D* plot1D
( const GaudiHistoAlg& algo ,
const double data ,
const long ID ,
const std::string& title ,
const double low ,
const double high ,
const unsigned long bins = 100 ) ;
/** "plot"(book&fill) a sequence of data from the vector (implicit loop)
* (Expected to be more efficient)
* @param algo the algorihtm
* @param data data
* @param ID histogram ID
* @param title histogram title
* @param low low edge
* @param high high edge
* @param bins number of bins
*/
static AIDA::IHistogram1D* plot1D
( const GaudiHistoAlg& algo ,
const double data ,
const std::string& ID ,
const std::string& title ,
const double low ,
const double high ,
const unsigned long bins = 100 ) ;
/** "plot"(book&fill) a sequence of data from the vector (implicit loop)
* (Expected to be more efficient)
* @param algo the algorihtm
* @param data vector of data
* @param title histogram title
* @param low low edge
* @param high high edge
* @param bins number of bins
*/
static AIDA::IHistogram1D* plot1D
( const GaudiHistoAlg& algo ,
const GaudiPython::Vector& data ,
const std::string& title ,
const double low ,
const double high ,
const unsigned long bins = 100 ) ;
/** "plot"(book&fill) a sequence of data from the vector (implicit loop)
* (Expected to be more efficient)
* @param algo the algorihtm
* @param data vector of data
* @param ID histogram ID
* @param title histogram title
* @param low low edge
* @param high high edge
* @param bins number of bins
*/
static AIDA::IHistogram1D* plot1D
( const GaudiHistoAlg& algo ,
const GaudiPython::Vector& data ,
const GaudiAlg::HistoID& ID ,
const std::string& title ,
const double low ,
const double high ,
const unsigned long bins = 100 ) ;
/** "plot"(book&fill) a sequence of data from the vector (implicit loop)
* (Expected to be more efficient)
* @param algo the algorihtm
* @param data vector of data
* @param ID histogram ID
* @param title histogram title
* @param low low edge
* @param high high edge
* @param bins number of bins
*/
static AIDA::IHistogram1D* plot1D
( const GaudiHistoAlg& algo ,
const GaudiPython::Vector& data ,
const long ID ,
const std::string& title ,
const double low ,
const double high ,
const unsigned long bins = 100 ) ;
/** "plot"(book&fill) a sequence of data from the vector (implicit loop)
* (Expected to be more efficient)
* @param algo the algorihtm
* @param data vector of data
* @param ID histogram ID
* @param title histogram title
* @param low low edge
* @param high high edge
* @param bins number of bins
*/
static AIDA::IHistogram1D* plot1D
( const GaudiHistoAlg& algo ,
const GaudiPython::Vector& data ,
const std::string& ID ,
const std::string& title ,
const double low ,
const double high ,
const unsigned long bins = 100 ) ;
/** fill the 2D histogram (book on demand)
* @param valueX x value to be filled
* @param valueY y value to be filled
* @param title histogram title (must be unique within the algorithm)
* @param lowX low x limit for histogram
* @param highX high x limit for histogram
* @param lowY low y limit for histogram