27 #include "TDirectory.h"
75 PyObject* attr = PyType_Type.tp_getattro( pyclass, pyname );
90 PyObject* attr = PyObject_GetItem( dict, pyname );
98 inline Bool_t IsTemplatedSTLClass(
const std::string& name,
const std::string& klass ) {
99 const int nsize = (int)name.size();
100 const int ksize = (int)klass.size();
102 return ( ( ksize < nsize && name.substr(0,ksize) == klass ) ||
103 ( ksize+5 < nsize && name.substr(5,ksize) == klass ) ) &&
104 name.find(
"::", name.find(
">" ) ) == std::string::npos;
112 PyObject* result = PyObject_CallMethod( obj, const_cast< char* >( meth ), const_cast< char* >(
"" ) );
123 PyObject* result = PyObject_CallMethod(
124 obj, const_cast< char* >( meth ), const_cast< char* >(
"O" ), arg1 );
136 PyObject* result = PyObject_CallMethod(
137 obj, const_cast< char* >( meth ), const_cast< char* >(
"OO" ), arg1, arg2 );
148 PyObject* result = PyObject_CallMethod(
149 obj, const_cast< char* >( meth ), const_cast< char* >(
"Oi" ), arg1, arg2 );
160 if ( idx == (
Py_ssize_t)-1 && PyErr_Occurred() )
164 if ( idx >= size || ( idx < 0 && idx < -size ) ) {
165 PyErr_SetString( PyExc_IndexError,
"index out of range" );
174 pyindex = PyLong_FromLong( size + idx );
192 Py_DECREF( pyindex );
202 if ( PyObject_IsTrue( value ) == 1 ) {
203 Py_INCREF( Py_False );
207 Py_INCREF( Py_True );
219 PyErr_SetString( PyExc_TypeError,
"getattr(): attribute name must be string" );
221 PyObject* pyptr = CallPyObjMethod(
self,
"__deref__" );
227 PyObject* val1 = PyObject_Str(
self );
228 PyObject* val2 = PyObject_Str( name );
229 PyErr_Format( PyExc_AttributeError,
"%s has no attribute \'%s\'",
238 PyObject* result = PyObject_GetAttr( pyptr, name );
250 PyErr_SetString( PyExc_TypeError,
"getattr(): attribute name must be string" );
252 PyObject* pyptr = CallPyObjMethod(
self,
"__follow__" );
256 PyObject* result = PyObject_GetAttr( pyptr, name );
266 return PyInt_FromLong( 0l );
268 PyObject* found = CallPyObjMethod(
self,
"FindObject", obj );
269 PyObject* result = PyInt_FromLong( PyObject_IsTrue( found ) );
280 return PyInt_FromLong( -1l );
282 return CallPyObjMethod(
self,
"Compare", obj );
293 return CallPyObjMethod(
self,
"IsEqual", obj );
304 return BoolNot( CallPyObjMethod(
self,
"IsEqual", obj ) );
315 PyObject* result = CallPyObjMethod(
self,
"__cpp_eq__", obj );
329 PyObject* result = CallPyObjMethod(
self,
"__cpp_ne__", obj );
344 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O:StaticCast" ),
353 PyErr_SetString( PyExc_TypeError,
"unbound method TClass::StaticCast "
354 "must be called with a TClass instance as first argument" );
359 PyErr_SetString( PyExc_TypeError,
"could not convert argument 1 (TClass* expected)" );
366 else if ( PyInt_Check( pyobject ) || PyLong_Check( pyobject ) ) address = (
void*)PyLong_AsLong( pyobject );
370 PyErr_SetString( PyExc_TypeError,
"could not convert argument 2 (void* expected)" );
378 TClass* tmp = to; to = from; from = tmp;
383 PyErr_Format( PyExc_TypeError,
"unable to cast %s to %s", from->
GetName(), to->
GetName() );
403 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O|l:DynamicCast" ),
409 PyObject* ptr = meth ? PyObject_Call( meth, args, 0 ) : 0;
419 else if ( PyInt_Check( pyobject ) || PyLong_Check( pyobject ) ) address = (
void*)PyLong_AsLong( pyobject );
422 if ( PyErr_Occurred() ) {
445 for (
Py_ssize_t i = 0; i < PySequence_Size( obj ); ++i ) {
446 PyObject* item = PySequence_GetItem( obj, i );
447 PyObject* result = CallPyObjMethod(
self,
"Add", item );
448 Py_XDECREF( result );
452 Py_INCREF( Py_None );
461 PyObject* result = CallPyObjMethod(
self,
"Remove", obj );
465 if ( ! PyObject_IsTrue( result ) ) {
467 PyErr_SetString( PyExc_ValueError,
"list.remove(x): x not in list" );
472 Py_INCREF( Py_None );
481 PyObject* l = CallPyObjMethod(
self,
"Clone" );
485 PyObject* result = CallPyObjMethod( l,
"extend", other );
499 Long_t imul = PyLong_AsLong( pymul );
500 if ( imul == -1 && PyErr_Occurred() )
503 if ( ! self->GetObject() ) {
504 PyErr_SetString( PyExc_TypeError,
"unsubscriptable object" );
511 for (
Long_t i = 0; i < imul; ++i ) {
524 Long_t imul = PyLong_AsLong( pymul );
525 if ( imul == -1 && PyErr_Occurred() )
528 PyObject* l = PySequence_List(
self );
530 for (
Long_t i = 0; i < imul - 1; ++i ) {
531 CallPyObjMethod(
self,
"extend", l );
544 for (
Py_ssize_t i = 0; i < PySequence_Size(
self ); ++i ) {
545 PyObject* item = PySequence_GetItem(
self, i );
546 PyObject* found = PyObject_RichCompare( item, obj, Py_EQ );
553 if ( PyObject_IsTrue( found ) )
565 if ( ! self->GetObject() ) {
566 PyErr_SetString( PyExc_TypeError,
"iteration over non-sequence" );
583 if ( PySlice_Check( index ) ) {
584 if ( ! self->GetObject() ) {
585 PyErr_SetString( PyExc_TypeError,
"unsubscriptable object" );
597 for (
Py_ssize_t i = start; i < stop; i += step ) {
604 return CallSelfIndex(
self, (
PyObject*)index,
"At" );
613 if ( ! PyArg_ParseTuple( args,
614 const_cast< char* >(
"OO:__setitem__" ), &index, &obj ) )
617 if ( PySlice_Check( index ) ) {
618 if ( ! self->GetObject() ) {
619 PyErr_SetString( PyExc_TypeError,
"unsubscriptable object" );
628 for (
Py_ssize_t i = stop - step; i >= start; i -= step ) {
632 for (
Py_ssize_t i = 0; i < PySequence_Size( obj ); ++i ) {
639 Py_INCREF( Py_None );
649 Py_DECREF( pyindex );
654 result = CallPyObjMethod( (
PyObject*)
self,
"AddAt", obj, pyindex );
655 Py_DECREF( pyindex );
664 if ( PySlice_Check( index ) ) {
665 if ( ! self->GetObject() ) {
666 PyErr_SetString( PyExc_TypeError,
"unsubscriptable object" );
675 for (
Py_ssize_t i = stop - step; i >= start; i -= step ) {
679 Py_INCREF( Py_None );
688 Py_INCREF( Py_None );
698 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"lO:insert" ), &idx, &obj ) )
704 else if ( size < idx )
707 return CallPyObjMethod(
self,
"AddAt", obj, idx );
715 int nArgs = PyTuple_GET_SIZE( args );
719 PyObject* result = CallSelfIndex(
self, index,
"RemoveAt" );
722 }
else if ( nArgs != 1 ) {
723 PyErr_Format( PyExc_TypeError,
724 "pop() takes at most 1 argument (%d given)", nArgs );
728 return CallSelfIndex(
self, PyTuple_GET_ITEM( args, 0 ),
"RemoveAt" );
736 PyObject* tup = PySequence_Tuple(
self );
740 PyObject* result = CallPyObjMethod(
self,
"Clear" );
741 Py_XDECREF( result );
743 for (
Py_ssize_t i = 0; i < PySequence_Size( tup ); ++i ) {
744 PyObject* retval = CallPyObjMethod(
self,
"AddAt", PyTuple_GET_ITEM( tup, i ), 0 );
745 Py_XDECREF( retval );
748 Py_INCREF( Py_None );
757 if ( PyTuple_GET_SIZE( args ) == 0 && ! kw ) {
759 return CallPyObjMethod(
self,
"Sort" );
762 PyObject* l = PySequence_List(
self );
764 if ( PyTuple_GET_SIZE( args ) == 1 )
765 result = CallPyObjMethod( l,
"sort", PyTuple_GET_ITEM( args, 0 ) );
767 PyObject* pymeth = PyObject_GetAttrString( l, const_cast< char* >(
"sort" ) );
768 result = PyObject_Call( pymeth, args, kw );
772 Py_XDECREF( result );
773 if ( PyErr_Occurred() ) {
778 result = CallPyObjMethod(
self,
"Clear" );
779 Py_XDECREF( result );
780 result = CallPyObjMethod(
self,
"extend", l );
781 Py_XDECREF( result );
784 Py_INCREF( Py_None );
794 PyObject* index = CallPyObjMethod(
self,
"IndexOf", obj );
798 if ( PyLong_AsLong( index ) < 0 ) {
800 PyErr_SetString( PyExc_ValueError,
"list.index(x): x not in list" );
811 PyObject* size = CallPyObjMethod(
self,
"GetLast" );
815 long lsize = PyLong_AsLong( size );
816 if ( lsize == -1 && PyErr_Occurred() )
820 return PyInt_FromLong( lsize + 1 );
831 if ( ! PyArg_ParseTuple( args,
832 const_cast< char* >(
"OO!:__setitem__" ), &idx, &
ObjectProxy_Type, &pyobj ) )
835 if ( ! self->GetObject() ) {
836 PyErr_SetString( PyExc_TypeError,
"unsubscriptable object" );
843 int index = (int)PyLong_AsLong( pyindex );
844 Py_DECREF( pyindex );
851 PyErr_SetString( PyExc_TypeError,
"attempt to call with null object" );
856 PyErr_Format( PyExc_TypeError,
"require object of type %s, but %s given",
867 TObject*
object = (*cla)[index];
873 Py_INCREF( Py_None );
888 static void vectoriter_dealloc( vectoriterobject* vi ) {
889 Py_XDECREF( vi->vi_vector );
890 delete vi->vi_converter;
891 PyObject_GC_Del( vi );
894 static int vectoriter_traverse( vectoriterobject* vi, visitproc visit,
void* arg ) {
895 Py_VISIT( vi->vi_vector );
899 static PyObject* vectoriter_iternext( vectoriterobject* vi ) {
900 if ( vi->vi_pos >= vi->vi_len )
905 if ( vi->vi_data && vi->vi_converter ) {
906 void* location = (
void*)((ptrdiff_t)vi->vi_data + vi->vi_stride * vi->vi_pos );
907 result = vi->vi_converter->FromMemory( location );
909 PyObject* pyindex = PyLong_FromLong( vi->vi_pos );
910 result = CallPyObjMethod( (
PyObject*)vi->vi_vector,
"_vector__at", pyindex );
911 Py_DECREF( pyindex );
918 PyTypeObject VectorIter_Type = {
920 (
char*)
"ROOT.vectoriter",
921 sizeof(vectoriterobject),
923 (destructor)vectoriter_dealloc,
924 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
928 (traverseproc)vectoriter_traverse,
931 (iternextfunc)vectoriter_iternext,
933 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
934 #
if PY_VERSION_HEX >= 0x02030000
937 #
if PY_VERSION_HEX >= 0x02060000
940 #
if PY_VERSION_HEX >= 0x03040000
946 vectoriterobject* vi = PyObject_GC_New( vectoriterobject, &VectorIter_Type );
947 if ( ! vi )
return NULL;
955 if ( pyvalue_type && pyvalue_size ) {
956 PyObject* pydata = CallPyObjMethod( v,
"data" );
958 vi->vi_data =
nullptr;
962 vi->vi_stride = PyLong_AsLong( pyvalue_size );
965 vi->vi_data =
nullptr;
966 vi->vi_converter =
nullptr;
970 Py_XDECREF( pyvalue_size );
971 Py_XDECREF( pyvalue_type );
973 vi->vi_len = vi->vi_pos = 0;
974 vi->vi_len = PySequence_Size( v );
976 _PyObject_GC_TRACK( vi );
984 if ( PySlice_Check( index ) ) {
985 if ( ! self->GetObject() ) {
986 PyErr_SetString( PyExc_TypeError,
"unsubscriptable object" );
992 Py_DECREF( pyclass );
996 for (
Py_ssize_t i = start; i < stop; i += step ) {
998 CallPyObjMethod( nseq,
"push_back", CallPyObjMethod( (
PyObject*)
self,
"_vector__at", pyidx ) );
1005 return CallSelfIndex(
self, (
PyObject*)index,
"_vector__at" );
1013 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"Oi:__setitem__" ), &idx, &bval ) )
1016 if ( ! self->GetObject() ) {
1017 PyErr_SetString( PyExc_TypeError,
"unsubscriptable object" );
1024 int index = (int)PyLong_AsLong( pyindex );
1025 Py_DECREF( pyindex );
1028 std::string::size_type pos = clName.find(
"vector<bool" );
1029 if ( pos != 0 && pos != 5 ) {
1030 PyErr_Format( PyExc_TypeError,
1031 "require object of type std::vector<bool>, but %s given",
1037 std::vector<bool>* vb = (std::vector<bool>*)self->GetObject();
1040 (*vb)[ index ] = (bool)bval;
1042 Py_INCREF( Py_None );
1052 PyObject* iter = CallPyObjMethod(
self,
"find", obj );
1054 PyObject* end = CallPyObjMethod(
self,
"end" );
1056 if ( ! PyObject_RichCompareBool( iter, end, Py_EQ ) ) {
1057 Py_INCREF( Py_True );
1067 Py_INCREF( Py_False );
1078 PyObject* iter = CallPyObjMethod(
self,
"begin" );
1080 PyObject* end = CallPyObjMethod(
self,
"end" );
1086 PyObject_SetAttr( iter, PyUnicode_FromString(
"_collection"),
self );
1100 if ( 0 <= idx && 0 <= size && idx < size )
1104 return CallPyObjMethod(
self,
"_getitem__unchecked", obj );
1105 }
else if ( PyErr_Occurred() ) {
1108 return CallPyObjMethod(
self,
"_getitem__unchecked", obj );
1110 PyErr_SetString( PyExc_IndexError,
"index out of range" );
1120 Long_t idx = PyLong_AsLong( pyindex );
1121 if ( idx == -1 && PyErr_Occurred() )
1125 PyErr_SetString( PyExc_TypeError,
"unsubscriptable object" );
1129 if ( (
int)idx == 0 )
1131 else if ( (
int)idx == 1 )
1135 PyErr_SetString( PyExc_IndexError,
"out of bounds" );
1140 #if PY_VERSION_HEX >= 0x03000000
1143 return ! PyObject_RichCompareBool( one, other, Py_EQ );
1146 static inline PyObject* PyROOT_PyString_FromCppString( std::string* s ) {
1158 #define PYROOT_IMPLEMENT_STRING_PYTHONIZATION( type, name ) \
1159 inline PyObject* name##GetData( PyObject* self ) { \
1160 if ( PyROOT::ObjectProxy_Check( self ) ) { \
1161 type* obj = ((type*)((ObjectProxy*)self)->GetObject()); \
1163 return PyROOT_PyString_FromCppString( obj ); \
1165 return ObjectProxy_Type.tp_str( self ); \
1168 PyErr_Format( PyExc_TypeError, "object mismatch (%s expected)", #type );\
1172 PyObject* name##StringRepr( PyObject* self ) \
1174 PyObject* data = name##GetData( self ); \
1176 PyObject* repr = PyROOT_PyUnicode_FromFormat( "\'%s\'", PyROOT_PyUnicode_AsString( data ) ); \
1177 Py_DECREF( data ); \
1183 PyObject* name##StringIsEqual( PyObject* self, PyObject* obj ) \
1185 PyObject* data = name##GetData( self ); \
1187 PyObject* result = PyObject_RichCompare( data, obj, Py_EQ ); \
1188 Py_DECREF( data ); \
1194 PyObject* name##StringIsNotEqual( PyObject* self, PyObject* obj ) \
1196 PyObject* data = name##GetData( self ); \
1198 PyObject* result = PyObject_RichCompare( data, obj, Py_NE ); \
1199 Py_DECREF( data ); \
1207 #define PYROOT_IMPLEMENT_STRING_PYTHONIZATION_CMP( type, name ) \
1208 PYROOT_IMPLEMENT_STRING_PYTHONIZATION( type, name ) \
1209 PyObject* name##StringCompare( PyObject* self, PyObject* obj ) \
1211 PyObject* data = name##GetData( self ); \
1214 result = PyObject_Compare( data, obj ); \
1215 Py_DECREF( data ); \
1217 if ( PyErr_Occurred() ) \
1219 return PyInt_FromLong( result ); \
1234 PyObject* data = CallPyObjMethod(
self,
"GetName" );
1245 PyObject* next = CallPyObjMethod(
self,
"Next" );
1250 if ( ! PyObject_IsTrue( next ) ) {
1252 PyErr_SetString( PyExc_StopIteration,
"" );
1269 if ( PyObject_RichCompareBool( last,
self, Py_EQ ) ) {
1270 PyErr_SetString( PyExc_StopIteration,
"" );
1272 PyObject* dummy = PyInt_FromLong( 1l );
1273 PyObject* iter = CallPyObjMethod(
self,
"__postinc__", dummy );
1276 if ( PyObject_RichCompareBool( last, iter, Py_EQ ) )
1277 PyErr_SetString( PyExc_StopIteration,
"" );
1279 next = CallPyObjMethod( iter,
"__deref__" );
1281 PyErr_SetString( PyExc_StopIteration,
"" );
1286 PyErr_SetString( PyExc_StopIteration,
"" );
1299 return PyErr_Format( PyExc_LookupError,
1300 "No operator==(const %s&, const %s&) available in the dictionary!",
1310 return PyErr_Format( PyExc_LookupError,
1311 "No operator!=(const %s&, const %s&) available in the dictionary!",
1321 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!:TDirectory::GetObject" ),
1329 PyErr_SetString( PyExc_TypeError,
1330 "TDirectory::GetObject must be called with a TDirectory instance as first argument" );
1336 ptr->
Set( address );
1338 Py_INCREF( Py_None );
1354 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!|O!i:TDirectory::WriteObject" ),
1363 PyErr_SetString( PyExc_TypeError,
1364 "TDirectory::WriteObject must be called with a TDirectory instance as first argument" );
1369 if ( option != 0 ) {
1377 return PyInt_FromLong( (
Long_t)result );
1398 PyErr_SetString( PyExc_ReferenceError,
"attempt to access a null-pointer" );
1403 const char* name = tree->
GetAlias( name1 );
1404 if ( ! name ) name = name1;
1410 branch = tree->
GetBranch( (std::string( name ) +
'.' ).c_str() );
1433 if ( klass && ! tree->
GetLeaf( name ) &&
1441 if ( branch && ! leaf ) {
1442 leaf = branch->
GetLeaf( name );
1447 leaf = (
TLeaf*)leaves->
At( 0 );
1482 PyErr_Format( PyExc_AttributeError,
1483 "\'%s\' object has no attribute \'%s\'", tree->IsA()->
GetName(),
name );
1489 class TTreeMemberFunction :
public PyCallable {
1492 TTreeMemberFunction(
const TTreeMemberFunction& t ) :
PyCallable( t )
1495 Py_INCREF( t.fOrg );
1498 TTreeMemberFunction&
operator=(
const TTreeMemberFunction& t )
1502 Py_INCREF( t.fOrg );
1508 ~TTreeMemberFunction() { Py_DECREF( fOrg ); fOrg = 0; }
1512 virtual PyObject* GetPrototype() {
return PyObject_GetAttrString( (
PyObject*)fOrg, (
char*)
"__doc__" ); }
1513 virtual Int_t GetPriority() {
return 100; }
1514 virtual PyObject* GetCoVarNames() {
1515 PyObject* co_varnames = PyTuple_New( 1 + 1 );
1529 class TTreeBranch :
public TTreeMemberFunction {
1534 virtual Int_t GetMaxArgs() {
return 5; }
1535 virtual PyCallable* Clone() {
return new TTreeBranch( *
this ); }
1544 int argc = PyTuple_GET_SIZE( args );
1551 PyErr_SetString( PyExc_TypeError,
1552 "TTree::Branch must be called with a TTree instance as first argument" );
1556 PyObject *name = 0, *clName = 0, *leaflist = 0;
1558 PyObject *bufsize = 0, *splitlevel = 0;
1561 if ( PyArg_ParseTuple( args, const_cast< char* >(
"O!OO!|O!:Branch" ),
1563 &leaflist, &PyInt_Type, &bufsize ) ) {
1590 if ( PyArg_ParseTuple( args, const_cast< char* >(
"O!O!O|O!O!:Branch" ),
1592 &PyInt_Type, &bufsize, &PyInt_Type, &splitlevel ) ) {
1595 PyErr_Clear(); clName = 0;
1596 if ( PyArg_ParseTuple( args, const_cast< char* >(
"O!O|O!O!" ),
1598 &PyInt_Type, &bufsize, &PyInt_Type, &splitlevel ) ) {
1604 if ( bIsMatch ==
kTRUE ) {
1609 if ( ((
ObjectProxy*)address)->fFlags & ObjectProxy::kIsReference )
1621 if ( buf != 0 && klName !=
"" ) {
1625 }
else if ( argc == 4 ) {
1627 PyInt_AS_LONG( bufsize ) );
1628 }
else if ( argc == 5 ) {
1630 PyInt_AS_LONG( bufsize ), PyInt_AS_LONG( splitlevel ) );
1651 class TTreeSetBranchAddress :
public TTreeMemberFunction {
1653 TTreeSetBranchAddress(
MethodProxy*
org ) : TTreeMemberFunction( org ) {}
1661 virtual Int_t GetMaxArgs() {
return 2; }
1662 virtual PyCallable* Clone() {
return new TTreeSetBranchAddress( *
this ); }
1669 int argc = PyTuple_GET_SIZE( args );
1676 PyErr_SetString( PyExc_TypeError,
1677 "TTree::SetBranchAddress must be called with a TTree instance as first argument" );
1684 if ( PyArg_ParseTuple( args, const_cast< char* >(
"SO:SetBranchAddress" ),
1685 &name, &address ) ) {
1689 if ( ((
ObjectProxy*)address)->fFlags & ObjectProxy::kIsReference )
1699 Py_INCREF( Py_None );
1716 virtual PyObject* ReportTypeError()
1718 PyErr_SetString( PyExc_TypeError,
1719 "TTree::SetBranchAddress must be called with a TTree instance as first argument" );
1727 class TChainSetBranchAddress :
public TTreeSetBranchAddress {
1729 TChainSetBranchAddress(
MethodProxy*
org ) : TTreeSetBranchAddress( org ) {}
1737 virtual Int_t GetMaxArgs() {
return 2; }
1738 virtual PyCallable* Clone() {
return new TChainSetBranchAddress( *
this ); }
1741 virtual PyObject* ReportTypeError()
1743 PyErr_SetString( PyExc_TypeError,
1744 "TChain::SetBranchAddress must be called with a TChain instance as first argument" );
1761 if ( ! (pya0 && pya1 && pya2 && pya3) ) {
1762 Py_XDECREF( pya3 ); Py_XDECREF( pya2 ); Py_XDECREF( pya1 ); Py_XDECREF( pya0 );
1767 PyObject* result = PyObject_CallFunction(
1768 pyfunc, (
char*)
"OOOOi", pya0, pya1, pya2, pya3, a4 );
1769 Py_DECREF( pya3 ); Py_DECREF( pya2 ); Py_DECREF( pya1 ); Py_DECREF( pya0 );
1773 throw std::runtime_error(
"TMinuit python fit function call failed" );
1776 Py_XDECREF( result );
1792 result = PyObject_CallFunction( pyfunc, (
char*)
"OO", pya0, pya1 );
1795 result = PyObject_CallFunction( pyfunc, (
char*)
"O", pya0 );
1803 throw std::runtime_error(
"TFN python function call failed" );
1805 d = PyFloat_AsDouble( result );
1806 Py_DECREF( result );
1824 PyObject* result = CallPyObjMethod(
self,
"Scale", scale );
1828 Py_DECREF( result );
1836 class TPretendInterpreted:
public PyCallable {
1838 TPretendInterpreted(
int nArgs ) : fNArgs( nArgs ) {}
1841 Int_t GetNArgs() {
return fNArgs; }
1842 virtual Int_t GetPriority() {
return 100; }
1843 virtual Int_t GetMaxArgs() {
return GetNArgs()+1; }
1844 virtual PyObject* GetCoVarNames() {
1845 PyObject* co_varnames = PyTuple_New( 1 + 1 );
1855 if ( ! pyobject || ! PyCallable_Check( pyobject ) ) {
1857 PyErr_Format( PyExc_ValueError,
1872 class TF1InitWithPyFunc :
public TPretendInterpreted {
1874 TF1InitWithPyFunc(
int ntf = 1 ) : TPretendInterpreted( 2 + 2*ntf ) {}
1881 "TF1::TF1(const char* name, PyObject* callable, "
1882 "Double_t xmin, Double_t xmax, Int_t npar = 0)" );
1885 virtual PyCallable* Clone() {
return new TF1InitWithPyFunc( *
this ); }
1891 int argc = PyTuple_GET_SIZE( args );
1892 const int reqNArgs = GetNArgs();
1893 if ( ! ( argc == reqNArgs || argc == reqNArgs+1 ) ) {
1894 PyErr_Format( PyExc_TypeError,
1895 "TFN::TFN(const char*, PyObject* callable, ...) =>\n"
1896 " takes at least %d and at most %d arguments (%d given)",
1897 reqNArgs, reqNArgs+1, argc );
1901 PyObject* pyfunc = PyTuple_GET_ITEM( args, 1 );
1905 if ( argc == reqNArgs+1 )
1906 npar = PyInt_AsLong( PyTuple_GET_ITEM( args, reqNArgs ) );
1909 std::vector<std::string> signature; signature.reserve( 2 );
1910 signature.push_back(
"double*" );
1911 signature.push_back(
"double*" );
1915 pyfunc, npar,
"double", signature,
"TFNPyCallback" );
1924 PyObject* newArgs = PyTuple_New( reqNArgs + 1 );
1926 for (
int iarg = 0; iarg < argc; ++iarg ) {
1927 PyObject* item = PyTuple_GET_ITEM( args, iarg );
1930 PyTuple_SET_ITEM( newArgs, iarg, item );
1936 if ( argc == reqNArgs )
1937 PyTuple_SET_ITEM( newArgs, reqNArgs, PyInt_FromLong( 0l ) );
1943 Py_DECREF( newArgs );
1944 Py_DECREF( method );
1951 class TF2InitWithPyFunc :
public TF1InitWithPyFunc {
1953 TF2InitWithPyFunc() : TF1InitWithPyFunc( 2 ) {}
1959 "TF2::TF2(const char* name, PyObject* callable, "
1960 "Double_t xmin, Double_t xmax, "
1961 "Double_t ymin, Double_t ymax, Int_t npar = 0)" );
1964 virtual PyCallable* Clone() {
return new TF2InitWithPyFunc( *
this ); }
1969 class TF3InitWithPyFunc :
public TF1InitWithPyFunc {
1971 TF3InitWithPyFunc() : TF1InitWithPyFunc( 3 ) {}
1977 "TF3::TF3(const char* name, PyObject* callable, "
1978 "Double_t xmin, Double_t xmax, "
1979 "Double_t ymin, Double_t ymax, "
1980 "Double_t zmin, Double_t zmax, Int_t npar = 0)" );
1983 virtual PyCallable* Clone() {
return new TF3InitWithPyFunc( *
this ); }
1993 class TMinuitSetFCN :
public TPretendInterpreted {
1995 TMinuitSetFCN(
int nArgs = 1 ) : TPretendInterpreted( nArgs ) {}
2002 "TMinuit::SetFCN(PyObject* callable)" );
2005 virtual PyCallable* Clone() {
return new TMinuitSetFCN( *
this ); }
2011 int argc = PyTuple_GET_SIZE( args );
2013 PyErr_Format( PyExc_TypeError,
2014 "TMinuit::SetFCN(PyObject* callable, ...) =>\n"
2015 " takes exactly 1 argument (%d given)", argc );
2019 PyObject* pyfunc = PyTuple_GET_ITEM( args, 0 );
2020 if ( ! IsCallable( pyfunc ) )
2024 std::vector<std::string> signature; signature.reserve( 5 );
2025 signature.push_back(
"Int_t&" );
2026 signature.push_back(
"Double_t*" );
2027 signature.push_back(
"Double_t&" );
2028 signature.push_back(
"Double_t*" );
2029 signature.push_back(
"Int_t" );
2033 pyfunc, 5,
"void", signature,
"TMinuitPyCallback" );
2046 for ( MethodProxy::Methods_t::const_iterator im = methods.begin(); im != methods.end(); ++im ) {
2047 PyObject* sig = (*im)->GetSignature();
2061 PyObject* newArgs = PyTuple_New( 1 );
2067 PyObject* result = setFCN->
Call(
self, newArgs, kwds, ctxt );
2071 Py_DECREF( newArgs );
2072 Py_DECREF( method );
2077 class TMinuitFitterSetFCN :
public TMinuitSetFCN {
2079 TMinuitFitterSetFCN() : TMinuitSetFCN( 1 ) {}
2085 "TMinuitFitter::SetFCN(PyObject* callable)" );
2088 virtual PyCallable* Clone() {
return new TMinuitFitterSetFCN( *
this ); }
2094 int argc = PyTuple_GET_SIZE( args );
2096 PyErr_Format( PyExc_TypeError,
2097 "TMinuitFitter::SetFCN(PyObject* callable, ...) =>\n"
2098 " takes exactly 1 argument (%d given)", argc );
2102 return TMinuitSetFCN::Call(
self, args, kwds, ctxt );
2109 void FitterPyCallback(
int& npar,
double* gin,
double& f,
double* u,
int flag )
2120 PyList_SetItem( arg3, 0, PyFloat_FromDouble( f ) );
2125 result = PyObject_CallFunction(
2126 gFitterPyCallback, (
char*)
"OOOOi", arg1, arg2, arg3, arg4, flag );
2127 f = PyFloat_AsDouble( PyList_GetItem( arg3, 0 ) );
2129 Py_DECREF( arg4 ); Py_DECREF( arg3 ); Py_DECREF( arg2 ); Py_DECREF( arg1 );
2133 throw std::runtime_error(
"TMinuit python fit function call failed" );
2136 Py_XDECREF( result );
2139 class TFitterFitFCN :
public TPretendInterpreted {
2141 TFitterFitFCN() : TPretendInterpreted( 2 ) {}
2147 "(PyObject* callable, int npar = 0, const double* params = 0, unsigned int dataSize = 0, bool chi2fit = false)" );
2152 "TFitter::FitFCN(PyObject* callable, int npar = 0, const double* params = 0, unsigned int dataSize = 0, bool chi2fit = false)" );
2155 virtual PyCallable* Clone() {
return new TFitterFitFCN( *
this ); }
2161 int argc = PyTuple_GET_SIZE( args );
2163 PyErr_Format( PyExc_TypeError,
2164 "TFitter::FitFCN(PyObject* callable, ...) =>\n"
2165 " takes at least 1 argument (%d given)", argc );
2169 PyObject* pyfunc = PyTuple_GET_ITEM( args, 0 );
2170 if ( ! IsCallable( pyfunc ) )
2174 Py_XDECREF( gFitterPyCallback );
2175 Py_INCREF( pyfunc );
2176 gFitterPyCallback = pyfunc;
2183 PyObject* newArgs = PyTuple_New( argc );
2185 for (
int iarg = 1; iarg < argc; ++iarg ) {
2186 PyObject* pyarg = PyTuple_GET_ITEM( args, iarg );
2188 PyTuple_SET_ITEM( newArgs, iarg, pyarg );
2195 Py_DECREF( newArgs );
2196 Py_DECREF( method );
2206 PyObject* result = CallPyObjMethod(
self,
"Get", attr );
2210 if ( !PyObject_IsTrue( result ) ) {
2211 PyObject* astr = PyObject_Str( attr );
2212 PyErr_Format( PyExc_AttributeError,
"TFile object has no attribute \'%s\'",
2215 Py_DECREF( result );
2229 PyErr_SetString( PyExc_TypeError,
2230 "TDirectoryFile::Get must be called with a TDirectoryFile instance as first argument" );
2237 PyErr_SetString( PyExc_ReferenceError,
"attempt to access a null-pointer" );
2253 void* addr = dirf->
Get( namecycle );
2260 return PyInt_FromLong( 3 );
2264 return PyInt_FromLong( 2 );
2302 Py_DECREF( pyfullname );
2324 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)StlSequenceIter;
2357 if ( name ==
"TObject" ) {
2368 else if ( name ==
"TClass" ) {
2378 else if ( name ==
"TCollection" ) {
2389 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)TCollectionIter;
2394 else if ( name ==
"TSeqCollection" ) {
2395 Utility::AddToClass( pyclass,
"__getitem__", (PyCFunction) TSeqCollectionGetItem, METH_O );
2397 Utility::AddToClass( pyclass,
"__delitem__", (PyCFunction) TSeqCollectionDelItem, METH_O );
2401 Utility::AddToClass( pyclass,
"reverse", (PyCFunction) TSeqCollectionReverse, METH_NOARGS );
2403 METH_VARARGS | METH_KEYWORDS );
2409 else if ( name ==
"TObjArray" ) {
2413 else if ( name ==
"TClonesArray" ) {
2417 Utility::AddToClass( pyclass,
"__getitem__", (PyCFunction) TSeqCollectionGetItem, METH_O );
2424 else if ( IsTemplatedSTLClass( name,
"vector" ) ) {
2436 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)vector_iter;
2439 TypedefInfo_t* ti =
gInterpreter->TypedefInfo_Factory( (name+
"::value_type").c_str() );
2442 PyObject_SetAttrString( pyclass,
"value_size", pyvalue_size );
2443 Py_DECREF( pyvalue_size );
2446 PyObject_SetAttrString( pyclass,
"value_type", pyvalue_type );
2447 Py_DECREF( pyvalue_type );
2456 std::string::size_type pos = name.find(
"vector<bool" );
2457 if ( pos == 0 || pos == 5 ) {
2463 else if ( IsTemplatedSTLClass( name,
"map" ) ) {
2468 else if ( IsTemplatedSTLClass( name,
"pair" ) ) {
2474 else if ( name.find(
"iterator" ) != std::string::npos ) {
2475 ((PyTypeObject*)pyclass)->tp_iternext = (iternextfunc)StlIterNext;
2486 else if ( name ==
"string" || name ==
"std::string" ) {
2495 else if ( name ==
"TString" ) {
2506 else if ( name ==
"TObjString" ) {
2517 else if ( name ==
"TIter" ) {
2518 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)PyObject_SelfIter;
2521 ((PyTypeObject*)pyclass)->tp_iternext = (iternextfunc)TIterNext;
2526 else if ( name ==
"TDirectory" ) {
2535 else if ( name ==
"TDirectoryFile" ) {
2542 else if ( name ==
"TTree" ) {
2550 Py_DECREF( original ); original = 0;
2552 PyObject_SetAttrString(
2553 pyclass, const_cast< char* >( method->
GetName().c_str() ), (
PyObject*)method );
2554 Py_DECREF( method ); method = 0;
2558 method =
MethodProxy_New(
"SetBranchAddress",
new TTreeSetBranchAddress( original ) );
2559 Py_DECREF( original ); original = 0;
2561 PyObject_SetAttrString(
2562 pyclass, const_cast< char* >( method->
GetName().c_str() ), (
PyObject*)method );
2563 Py_DECREF( method ); method = 0;
2567 else if ( name ==
"TChain" ) {
2572 Py_DECREF( original ); original = 0;
2574 PyObject_SetAttrString(
2575 pyclass, const_cast< char* >( method->
GetName().c_str() ), (
PyObject*)method );
2576 Py_DECREF( method ); method = 0;
2580 else if ( name ==
"TStyle" ) {
2586 else if ( name ==
"TH1" )
2589 else if ( name ==
"TF1" )
2592 else if ( name ==
"TF2" )
2595 else if ( name ==
"TF3" )
2598 else if ( name ==
"TFunction" )
2601 else if ( name ==
"TMinuit" )
2604 else if ( name ==
"TFitter" )
2607 else if ( name ==
"Fitter" )
2610 else if ( name ==
"TFile" ) {
2612 PyObject* attr = PyObject_GetAttrString( pyclass, (
char*)
"Open" );
2622 else if ( name.substr(0,8) ==
"TVector3" ) {
2629 else if ( name.substr(0,8) ==
"TVectorT" ) {
2636 else if ( name.substr(0,6) ==
"TArray" ) {
2643 else if ( name ==
"RooDataHist" )
2646 else if ( name ==
"RooSimultaneous" )
2652 PyObject* userPythonizations = PyObject_GetAttrString(
gRootModule,
"UserPythonizations" );
2653 PyObject* pythonizationScope = PyObject_GetAttrString(
gRootModule,
"PythonizationScope" );
2655 std::vector< std::string > pythonization_scopes;
2656 pythonization_scopes.push_back(
"__global__" );
2659 if ( user_scope !=
"__global__" ) {
2660 if ( PyDict_Contains( userPythonizations, pythonizationScope ) ) {
2661 pythonization_scopes.push_back( user_scope );
2667 for (
auto key = pythonization_scopes.cbegin(); key != pythonization_scopes.cend(); ++key ) {
2668 PyObject* tmp = PyDict_GetItemString( userPythonizations, key->c_str() );
2669 Py_ssize_t num_pythonizations = PyList_Size( tmp );
2671 if ( num_pythonizations )
2672 arglist = Py_BuildValue(
"O,s", pyclass, name.c_str() );
2673 for (
Py_ssize_t i = 0; i < num_pythonizations; ++i ) {
2674 PyObject* pythonizor = PyList_GetItem( tmp, i );
2676 PyObject* result = PyObject_CallObject( pythonizor, arglist );
2681 Py_DECREF( result );
2683 Py_XDECREF( arglist );
2686 Py_DECREF( userPythonizations );
2687 Py_DECREF( pythonizationScope );
R__EXTERN PyObject * gFirst
R__EXTERN PyObject * gCppEq
virtual void Add(TObject *obj)
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Bool_t AddBinaryOperator(PyObject *left, PyObject *right, const char *op, const char *label, const char *alt_label=NULL)
Install the named operator (op) into the left object's class if such a function exists as a global ov...
R__EXTERN PyObject * gInit
#define PyROOT_PyUnicode_FromString
R__EXTERN PyObject * gIter
R__EXTERN PyObject * gDict
R__EXTERN PyObject * gGetSize
virtual TLeaf * GetLeaf(const char *name) const
Return pointer to the 1st Leaf named name in thisBranch.
R__EXTERN Int_t gErrorIgnoreLevel
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
virtual TClass * GetTargetClass()
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
Collectable string class.
static Bool_t RegisterObject(ObjectProxy *pyobj, TObject *object)
start tracking
char * GetObject() const
Return a pointer to our object.
R__EXTERN PyObject * gTClassDynCast
R__EXTERN PyObject * gSize
virtual TLeaf * GetLeaf(const char *branchname, const char *leafname)
Return pointer to the 1st Leaf named name in any Branch of this Tree or any branch in the list of fri...
R__EXTERN PyObject * gFollow
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual TKey * GetKey(const char *name, Short_t cycle=9999) const
Return pointer to key with name,cycle.
MethodProxy * MethodProxy_New(const std::string &name, std::vector< PyCallable * > &methods)
R__EXTERN PyObject * gSetFCN
std::string GetFinalName(TCppType_t type)
R__EXTERN PyObject * gBranch
std::vector< PyCallable * > Methods_t
R__EXTERN PyObject * gFitFCN
virtual const char * GetClassName() const
R__EXTERN PyObject * gEnd
TBranch * GetBranch() const
#define PYROOT_IMPLEMENT_STRING_PYTHONIZATION(type, name)
R__EXTERN PyObject * gCppNe
PyObject * TTreeGetAttr(ObjectProxy *self, PyObject *pyname)
std::string GetReturnTypeNormalizedName() const
Get the normalized name of the return type.
R__EXTERN PyObject * gVectorAt
#define PyInt_FromSsize_t
TMethod * GetMethodAllAny(const char *method)
Return pointer to method without looking at parameters.
R__EXTERN PyObject * gDeref
virtual Int_t GetNdata() const
virtual TObject * RemoveAt(Int_t idx)
virtual void * GetObjectChecked(const char *namecycle, const char *classname)
See documentation of TDirectory::GetObjectCheck(const char *namecycle, const TClass *cl) ...
#define PyVarObject_HEAD_INIT(type, size)
const char * Data() const
Sequenceable collection abstract base class.
MethodProxy::Methods_t fMethods
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
R__EXTERN PyObject * gRootModule
TStreamerInfo * GetInfo() const
Get streamer info for the branch class.
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
std::map< std::string, std::string >::const_iterator iter
virtual PyObject * FromMemory(void *address)
virtual Int_t WriteObjectAny(const void *, const char *, const char *, Option_t *="", Int_t=0)
const std::string ClassName(PyObject *pyobj)
Retrieve the class name from the given python object (which may be just an instance of the class)...
#define PyROOT_PyUnicode_Type
#define PyROOT_PyUnicode_AsString
TObject * First() const
Return the object in the first slot.
Book space in a file, create I/O buffers, to fill them, (un)compress them.
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
#define PyROOT_PySliceCast
Cppyy::TCppType_t ObjectIsA() const
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
TConverter * CreateConverter(const std::string &fullType, Long_t size=-1)
PyTypeObject ObjectProxy_Type
virtual char * GetAddress() const
Bool_t ObjectProxy_Check(T *object)
R__EXTERN PyObject * gClass
R__EXTERN PyObject * gBegin
virtual const char * GetTypeName() const
TString GetString() const
TObjArray * GetElements() const
A ROOT file is structured in Directories (like a file system).
ClassInfo_t * GetClassInfo() const
Collection abstract base class.
virtual const char * GetAlias(const char *aliasName) const
Returns the expanded value of the alias. Search in the friends if any.
#define PyROOT_PyUnicode_FromStringAndSize
TClass * GetCurrentClass()
Return a pointer to the current type of the data member corresponding to branch element.
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, Bool_t isRef=kFALSE, Bool_t isValue=kFALSE)
only known or knowable objects will be bound (null object is ok)
virtual const char * GetName() const
Returns name of object.
The ROOT global object gROOT contains a list of all defined classes.
virtual TLeaf * GetLeafCount() const
PyObject * PyBuffer_FromMemory(Bool_t *buf, Py_ssize_t size=-1)
A Branch for the case of an object.
MethodInfo_t * fMethodInfo
virtual Int_t GetSize() const
virtual PyObject * Call(ObjectProxy *&self, PyObject *args, PyObject *kwds, TCallContext *ctxt=0)=0
PyObject * CreateScopeProxy(Cppyy::TCppScope_t)
Convenience function with a lookup first through the known existing proxies.
R__EXTERN PyObject * gGetItem
TCppScope_t GetScope(const std::string &scope_name)
R__EXTERN PyObject * gName
TClass * GetClass() const
Describe directory structure in memory.
static TClass * OP2TCLASS(PyROOT::ObjectProxy *pyobj)
R__EXTERN PyObject * gLen
Bool_t AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Add the given function to the class under name 'label'.
#define PyROOT_PyUnicode_Check
TObjArray * GetListOfLeaves()
virtual void * GetObjectChecked(const char *namecycle, const char *classname)
See documentation of TDirectoryFile::GetObjectCheck(const char *namecycle, const TClass *cl) ...
virtual void AddAt(TObject *obj, Int_t idx)=0
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
#define PYROOT_IMPLEMENT_STRING_PYTHONIZATION_CMP(type, name)
Bool_t AddUsingToClass(PyObject *pyclass, const char *method)
Helper to add base class methods to the derived class one (this covers the 'using' cases...
Binding & operator=(OUT(*fun)(void))
virtual TObject * At(Int_t idx) const =0
#define org(otri, vertexptr)
Mother of all ROOT objects.
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
const std::string & GetName() const
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
An array of clone (identical) objects.
virtual void * GetValuePointer() const
static TPyBufferFactory * Instance()
R__EXTERN PyObject * gSecond
virtual Int_t GetLenStatic() const
int GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, Bool_t check=kTRUE)
Retrieve a linear buffer pointer from the given pyobject.
PyObject * GetScopeProxy(Cppyy::TCppScope_t)
Retrieve scope proxy from the known ones.
Each ROOT class (see TClass) has a linked list of methods.
void * CreateWrapperMethod(PyObject *pyfunc, Long_t user, const char *retType, const std::vector< std::string > &signature, const char *callback)
Compile a function on the fly and return a function pointer for use on C-APIs.
Bool_t MethodProxy_Check(T *object)
double TFNPyCallback(void *vpyfunc, Long_t npar, double *a0, double *a1)
R__EXTERN PyObject * ggetSize
A TTree object has a header with a name and a title.
void * DynamicCast(const TClass *base, void *obj, Bool_t up=kTRUE)
Cast obj of this class type up to baseclass cl if up is true.
TCppObject_t Construct(TCppType_t type)
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
TObject * At(Int_t idx) const
static PyObject * PyROOT_PyCapsule_New(void *cobj, const char *, void(*destr)(void *))
R__EXTERN PyObject * gSetBranchAddress
A TTree is a list of TBranches.
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any. ...
void TMinuitPyCallback(void *vpyfunc, Long_t, Int_t &a0, Double_t *a1, Double_t &a2, Double_t *a3, Int_t a4)
PyObject * BindCppObject(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, Bool_t isRef=kFALSE)
if the object is a null pointer, return a typed one (as needed for overloading)
void Set(void *address, EFlags flags=kNone)
Int_t Size() const
Return size of object of this class.
Bool_t Pythonize(PyObject *pyclass, const std::string &name)