#include "PyROOT.h"
#include "TPySelector.h"
#include "TPyReturn.h"
#include "ObjectProxy.h"
#include "MethodProxy.h"
#include "RootWrapper.h"
#include "TPython.h"
#include "TString.h"
ClassImp(TPySelector)
void TPySelector::SetupPySelf()
{
if ( fPySelf && fPySelf != Py_None )
return;
TString impst = TString::Format( "import %s", GetOption() );
if ( ! TPython::Exec( (const char*)impst ) ) {
Abort( "failed to load provided python module" );
return;
}
PyObject* tpysel = PyObject_GetAttrString(
PyImport_AddModule( const_cast< char* >( "libPyROOT" ) ),
const_cast< char* >( "TPySelector" ) );
PyObject* pymod = PyImport_AddModule( const_cast< char* >( GetOption() ) );
PyObject* dict = PyModule_GetDict( pymod );
Py_INCREF( dict );
PyObject* allvalues = PyDict_Values( dict );
PyObject* pyclass = 0;
for ( int i = 0; i < PyList_GET_SIZE( allvalues ); ++i ) {
PyObject* value = PyList_GET_ITEM( allvalues, i );
Py_INCREF( value );
if ( PyType_Check( value ) && PyObject_IsSubclass( value, tpysel ) ) {
if ( PyObject_Compare( value, tpysel ) ) {
pyclass = value;
break;
}
}
Py_DECREF( value );
}
Py_DECREF( allvalues );
Py_DECREF( dict );
Py_DECREF( tpysel );
if ( ! pyclass ) {
Abort( "no TSelector derived class available in provided module" );
return;
}
PyObject* args = PyTuple_New( 0 );
PyObject* self = PyObject_Call( pyclass, args, 0 );
Py_DECREF( args );
Py_DECREF( pyclass );
if ( ! self || ! PyROOT::ObjectProxy_Check( self ) ) {
if ( ! PyErr_Occurred() )
PyErr_SetString( PyExc_RuntimeError, "could not create python selector" );
Py_XDECREF( self );
Abort( 0 );
return;
}
Py_INCREF( self );
Py_DECREF( fPySelf );
fPySelf = self;
((PyROOT::ObjectProxy*)fPySelf)->fObject = this;
}
PyObject* TPySelector::CallSelf( const char* method, PyObject* pyobject )
{
if ( ! fPySelf || fPySelf == Py_None ) {
Py_INCREF( Py_None );
return Py_None;
}
PyObject* result = 0;
PyObject* pymethod = PyObject_GetAttrString( fPySelf, const_cast< char* >( method ) );
if ( ! PyROOT::MethodProxy_CheckExact( pymethod ) ) {
if ( pyobject )
result = PyObject_CallFunction( pymethod, const_cast< char* >( "O" ), pyobject );
else
result = PyObject_CallFunction( pymethod, const_cast< char* >( "" ) );
} else {
Py_INCREF( Py_None );
result = Py_None;
}
Py_XDECREF( pymethod );
if ( ! result )
Abort( 0 );
return result;
}
TPySelector::TPySelector( TTree*, PyObject* self ) : fChain( 0 ), fPySelf( 0 )
{
if ( self ) {
Py_INCREF( self );
fPySelf = self;
} else {
Py_INCREF( Py_None );
fPySelf = Py_None;
}
}
TPySelector::~TPySelector()
{
Py_DECREF( fPySelf );
}
Int_t TPySelector::Version() const {
PyObject* result = const_cast< TPySelector* >( this )->CallSelf( "Version" );
if ( result && result != Py_None ) {
Int_t ires = (Int_t)PyLong_AsLong( result );
Py_DECREF( result );
return ires;
} else if ( result == Py_None ) {
Py_DECREF( result );
}
return -99;
}
Int_t TPySelector::GetEntry( Long64_t entry, Int_t getall )
{
return fChain ? fChain->GetTree()->GetEntry( entry, getall ) : 0;
}
void TPySelector::Init( TTree* tree )
{
if ( ! tree )
return;
fChain = tree;
PyObject* pytree = PyROOT::BindRootObject( (void*)tree, tree->IsA() );
PyObject* result = CallSelf( "Init", pytree );
Py_DECREF( pytree );
if ( ! result )
Abort( 0 );
Py_XDECREF( result );
}
Bool_t TPySelector::Notify()
{
PyObject* result = CallSelf( "Notify" );
if ( ! result )
Abort( 0 );
Py_XDECREF( result );
return kTRUE;
}
void TPySelector::Begin( TTree* )
{
SetupPySelf();
PyObject* result = CallSelf( "Begin" );
if ( ! result )
Abort( 0 );
Py_XDECREF( result );
}
void TPySelector::SlaveBegin( TTree* tree )
{
SetupPySelf();
Init( tree );
PyObject* result = 0;
if ( tree ) {
PyObject* pytree = PyROOT::BindRootObject( (void*)tree, tree->IsA() );
result = CallSelf( "SlaveBegin", pytree );
Py_DECREF( pytree );
} else {
result = CallSelf( "SlaveBegin", Py_None );
}
if ( ! result )
Abort( 0 );
Py_XDECREF( result );
}
Bool_t TPySelector::Process( Long64_t entry )
{
if ( ! fPySelf || fPySelf == Py_None ) {
Abort( "no python selector instance available" );
return kFALSE;
}
PyObject* result = PyObject_CallMethod( fPySelf,
const_cast< char* >( "Process" ), const_cast< char* >( "L" ), entry );
if ( ! result ) {
Abort( 0 );
return kFALSE;
}
Bool_t bresult = (Bool_t)PyLong_AsLong( result );
Py_DECREF( result );
return bresult;
}
void TPySelector::SlaveTerminate()
{
PyObject* result = CallSelf( "SlaveTerminate" );
if ( ! result )
Abort( 0 );
Py_XDECREF( result );
}
void TPySelector::Terminate()
{
PyObject* result = CallSelf( "Terminate" );
if ( ! result )
Abort( 0 );
Py_XDECREF( result );
}
void TPySelector::Abort( const char* why, EAbort what )
{
if ( ! why && PyErr_Occurred() ) {
PyObject *pytype = 0, *pyvalue = 0, *pytrace = 0;
PyErr_Fetch( &pytype, &pyvalue, &pytrace );
PyObject* pystr = PyObject_Str( pyvalue );
Abort( PyString_AS_STRING( pystr ), what );
Py_DECREF( pystr );
PyErr_Restore( pytype, pyvalue, pytrace );
} else
TSelector::Abort( why ? why : "", what );
}
Last change: Fri Dec 12 09:06:31 2008
Last generated: 2008-12-12 09:06
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.