65 ClassImp(TPySelector);
69 void TPySelector::SetupPySelf()
72 if ( fPySelf && fPySelf != Py_None )
76 std::string opt = GetOption();
77 std::string::size_type pos = opt.find(
'#' );
78 std::string module = opt.substr( 0, pos );
79 std::string user = (pos == std::string::npos) ?
"" : opt.substr( pos+1, std::string::npos );
81 TString impst = TString::Format(
"import %s", module.c_str() );
84 SetOption( user.c_str() );
87 if ( ! TPython::Exec( (
const char*)impst ) ) {
88 Abort(
"failed to load provided python module" );
93 PyObject* tpysel = PyObject_GetAttrString(
94 PyImport_AddModule( const_cast< char* >(
"libPyROOT" ) ),
95 const_cast< char* >(
"TPySelector" ) );
98 PyObject* pymod = PyImport_AddModule( const_cast< char* >( module.c_str() ) );
101 PyObject* dict = PyModule_GetDict( pymod );
105 PyObject* allvalues = PyDict_Values( dict );
107 PyObject* pyclass = 0;
108 for (
int i = 0; i < PyList_GET_SIZE( allvalues ); ++i ) {
109 PyObject* value = PyList_GET_ITEM( allvalues, i );
112 if ( PyType_Check( value ) && PyObject_IsSubclass( value, tpysel ) ) {
113 if ( PyObject_RichCompareBool( value, tpysel, Py_NE ) ) {
122 Py_DECREF( allvalues );
127 Abort(
"no TSelector derived class available in provided module" );
131 PyObject* args = PyTuple_New( 0 );
132 PyObject*
self = PyObject_Call( pyclass, args, 0 );
134 Py_DECREF( pyclass );
137 if ( !
self || ! PyROOT::ObjectProxy_Check(
self ) ) {
138 if ( ! PyErr_Occurred() )
139 PyErr_SetString( PyExc_RuntimeError,
"could not create python selector" );
146 Py_XDECREF( fPySelf );
151 TPySelector* oldselector = (TPySelector*)((PyROOT::ObjectProxy*)fPySelf)->fObject;
152 ((PyROOT::ObjectProxy*)fPySelf)->fObject =
this;
154 PyROOT::TMemoryRegulator::UnregisterObject( oldselector );
162 PyObject* TPySelector::CallSelf(
const char* method, PyObject* pyobject )
164 if ( ! fPySelf || fPySelf == Py_None ) {
165 Py_INCREF( Py_None );
169 PyObject* result = 0;
173 PyObject* pymethod = PyObject_GetAttrString( fPySelf, const_cast< char* >( method ) );
174 if ( ! PyROOT::MethodProxy_CheckExact( pymethod ) ) {
176 result = PyObject_CallFunction( pymethod, const_cast< char* >(
"O" ), pyobject );
178 result = PyObject_CallFunction( pymethod, const_cast< char* >(
"" ) );
183 Py_INCREF( Py_None );
187 Py_XDECREF( pymethod );
197 TPySelector::TPySelector( TTree*, PyObject*
self ) : fChain( 0 ), fPySelf( 0 )
205 Py_INCREF( Py_None );
213 TPySelector::~TPySelector()
215 if ( fPySelf == Py_None ) {
216 Py_DECREF( fPySelf );
222 Int_t TPySelector::Version()
const {
225 PyObject* result =
const_cast< TPySelector*
>( this )->CallSelf(
"Version" );
226 if ( result && result != Py_None ) {
227 Int_t ires = (Int_t)PyLong_AsLong( result );
230 }
else if ( result == Py_None ) {
239 Int_t TPySelector::GetEntry( Long64_t entry, Int_t getall )
241 return fChain ? fChain->GetTree()->GetEntry( entry, getall ) : 0;
248 void TPySelector::Init( TTree* tree )
257 PyObject* pytree = PyROOT::BindCppObject( (
void*)tree, tree->IsA()->GetName() );
258 PyObject* result = CallSelf(
"Init", pytree );
264 Py_XDECREF( result );
270 Bool_t TPySelector::Notify()
272 PyObject* result = CallSelf(
"Notify" );
277 Py_XDECREF( result );
288 void TPySelector::Begin( TTree* )
294 PyObject* result = CallSelf(
"Begin" );
299 Py_XDECREF( result );
306 void TPySelector::SlaveBegin( TTree* tree )
311 PyObject* result = 0;
313 PyObject* pytree = PyROOT::BindCppObject( (
void*)tree, tree->IsA()->GetName() );
314 result = CallSelf(
"SlaveBegin", pytree );
317 result = CallSelf(
"SlaveBegin", Py_None );
323 Py_XDECREF( result );
329 Bool_t TPySelector::Process( Long64_t entry )
331 if ( ! fPySelf || fPySelf == Py_None ) {
336 Abort(
"no python selector instance available" );
340 PyObject* result = PyObject_CallMethod( fPySelf,
341 const_cast< char* >(
"Process" ), const_cast< char* >(
"L" ), entry );
347 Bool_t bresult = (Bool_t)PyLong_AsLong( result );
355 void TPySelector::SlaveTerminate()
357 PyObject* result = CallSelf(
"SlaveTerminate" );
362 Py_XDECREF( result );
368 void TPySelector::Terminate()
370 PyObject* result = CallSelf(
"Terminate" );
375 Py_XDECREF( result );
381 void TPySelector::Abort(
const char* why, EAbort what )
383 if ( ! why && PyErr_Occurred() ) {
384 PyObject *pytype = 0, *pyvalue = 0, *pytrace = 0;
385 PyErr_Fetch( &pytype, &pyvalue, &pytrace );
388 PyObject* pystr = PyObject_Str( pyvalue );
389 Abort( PyROOT_PyUnicode_AsString( pystr ), what );
392 PyErr_Restore( pytype, pyvalue, pytrace );
394 TSelector::Abort( why ? why :
"", what );