68 using namespace PyROOT;
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" );
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" );
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" );
1097 if ( 0 <= idx && 0 <= size && idx < size )
1101 return CallPyObjMethod(
self,
"_getitem__unchecked", obj );
1102 }
else if ( PyErr_Occurred() ) {
1105 return CallPyObjMethod(
self,
"_getitem__unchecked", obj );
1107 PyErr_SetString( PyExc_IndexError,
"index out of range" );
1117 Long_t idx = PyLong_AsLong( pyindex );
1118 if ( idx == -1 && PyErr_Occurred() )
1122 PyErr_SetString( PyExc_TypeError,
"unsubscriptable object" );
1126 if ( (
int)idx == 0 )
1128 else if ( (
int)idx == 1 )
1132 PyErr_SetString( PyExc_IndexError,
"out of bounds" );
1137 #if PY_VERSION_HEX >= 0x03000000
1140 return ! PyObject_RichCompareBool( one, other, Py_EQ );
1143 static inline PyObject* PyROOT_PyString_FromCppString( std::string* s ) {
1155 #define PYROOT_IMPLEMENT_STRING_PYTHONIZATION( type, name ) \
1156 inline PyObject* name##GetData( PyObject* self ) { \
1157 if ( PyROOT::ObjectProxy_Check( self ) ) { \
1158 type* obj = ((type*)((ObjectProxy*)self)->GetObject()); \
1160 return PyROOT_PyString_FromCppString( obj ); \
1162 return ObjectProxy_Type.tp_str( self ); \
1165 PyErr_Format( PyExc_TypeError, "object mismatch (%s expected)", #type );\
1169 PyObject* name##StringRepr( PyObject* self ) \
1171 PyObject* data = name##GetData( self ); \
1173 PyObject* repr = PyROOT_PyUnicode_FromFormat( "\'%s\'", PyROOT_PyUnicode_AsString( data ) ); \
1174 Py_DECREF( data ); \
1180 PyObject* name##StringIsEqual( PyObject* self, PyObject* obj ) \
1182 PyObject* data = name##GetData( self ); \
1184 PyObject* result = PyObject_RichCompare( data, obj, Py_EQ ); \
1185 Py_DECREF( data ); \
1191 PyObject* name##StringIsNotEqual( PyObject* self, PyObject* obj ) \
1193 PyObject* data = name##GetData( self ); \
1195 PyObject* result = PyObject_RichCompare( data, obj, Py_NE ); \
1196 Py_DECREF( data ); \
1204 #define PYROOT_IMPLEMENT_STRING_PYTHONIZATION_CMP( type, name ) \
1205 PYROOT_IMPLEMENT_STRING_PYTHONIZATION( type, name ) \
1206 PyObject* name##StringCompare( PyObject* self, PyObject* obj ) \
1208 PyObject* data = name##GetData( self ); \
1211 result = PyObject_Compare( data, obj ); \
1212 Py_DECREF( data ); \
1214 if ( PyErr_Occurred() ) \
1216 return PyInt_FromLong( result ); \
1231 PyObject* data = CallPyObjMethod(
self,
"GetName" );
1242 PyObject* next = CallPyObjMethod(
self,
"Next" );
1247 if ( ! PyObject_IsTrue( next ) ) {
1249 PyErr_SetString( PyExc_StopIteration,
"" );
1266 if ( PyObject_RichCompareBool( last,
self, Py_EQ ) ) {
1267 PyErr_SetString( PyExc_StopIteration,
"" );
1269 PyObject* dummy = PyInt_FromLong( 1l );
1270 PyObject* iter = CallPyObjMethod(
self,
"__postinc__", dummy );
1273 if ( PyObject_RichCompareBool( last, iter, Py_EQ ) )
1274 PyErr_SetString( PyExc_StopIteration,
"" );
1276 next = CallPyObjMethod( iter,
"__deref__" );
1278 PyErr_SetString( PyExc_StopIteration,
"" );
1283 PyErr_SetString( PyExc_StopIteration,
"" );
1296 return PyErr_Format( PyExc_LookupError,
1297 "No operator==(const %s&, const %s&) available in the dictionary!",
1307 return PyErr_Format( PyExc_LookupError,
1308 "No operator!=(const %s&, const %s&) available in the dictionary!",
1318 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!:TDirectory::GetObject" ),
1326 PyErr_SetString( PyExc_TypeError,
1327 "TDirectory::GetObject must be called with a TDirectory instance as first argument" );
1333 ptr->
Set( address );
1335 Py_INCREF( Py_None );
1351 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!|O!i:TDirectory::WriteObject" ),
1360 PyErr_SetString( PyExc_TypeError,
1361 "TDirectory::WriteObject must be called with a TDirectory instance as first argument" );
1366 if ( option != 0 ) {
1374 return PyInt_FromLong( (
Long_t)result );
1395 PyErr_SetString( PyExc_ReferenceError,
"attempt to access a null-pointer" );
1400 const char* name = tree->
GetAlias( name1 );
1401 if ( ! name ) name = name1;
1407 branch = tree->
GetBranch( (std::string( name ) +
'.' ).c_str() );
1430 if ( klass && ! tree->
GetLeaf( name ) &&
1438 if ( branch && ! leaf ) {
1439 leaf = branch->
GetLeaf( name );
1444 leaf = (
TLeaf*)leaves->
At( 0 );
1479 PyErr_Format( PyExc_AttributeError,
1480 "\'%s\' object has no attribute \'%s\'", tree->IsA()->
GetName(),
name );
1486 class TTreeMemberFunction :
public PyCallable {
1489 TTreeMemberFunction(
const TTreeMemberFunction& t ) :
PyCallable( t )
1492 Py_INCREF( t.fOrg );
1495 TTreeMemberFunction&
operator=(
const TTreeMemberFunction& t )
1499 Py_INCREF( t.fOrg );
1505 ~TTreeMemberFunction() { Py_DECREF( fOrg ); fOrg = 0; }
1509 virtual PyObject* GetPrototype() {
return PyObject_GetAttrString( (
PyObject*)fOrg, (
char*)
"__doc__" ); }
1510 virtual Int_t GetPriority() {
return 100; }
1511 virtual PyObject* GetCoVarNames() {
1512 PyObject* co_varnames = PyTuple_New( 1 + 1 );
1526 class TTreeBranch :
public TTreeMemberFunction {
1531 virtual Int_t GetMaxArgs() {
return 5; }
1532 virtual PyCallable* Clone() {
return new TTreeBranch( *
this ); }
1541 int argc = PyTuple_GET_SIZE( args );
1548 PyErr_SetString( PyExc_TypeError,
1549 "TTree::Branch must be called with a TTree instance as first argument" );
1553 PyObject *name = 0, *clName = 0, *leaflist = 0;
1555 PyObject *bufsize = 0, *splitlevel = 0;
1558 if ( PyArg_ParseTuple( args, const_cast< char* >(
"O!OO!|O!:Branch" ),
1560 &leaflist, &PyInt_Type, &bufsize ) ) {
1587 if ( PyArg_ParseTuple( args, const_cast< char* >(
"O!O!O|O!O!:Branch" ),
1589 &PyInt_Type, &bufsize, &PyInt_Type, &splitlevel ) ) {
1592 PyErr_Clear(); clName = 0;
1593 if ( PyArg_ParseTuple( args, const_cast< char* >(
"O!O|O!O!" ),
1595 &PyInt_Type, &bufsize, &PyInt_Type, &splitlevel ) ) {
1601 if ( bIsMatch ==
kTRUE ) {
1606 if ( ((
ObjectProxy*)address)->fFlags & ObjectProxy::kIsReference )
1618 if ( buf != 0 && klName !=
"" ) {
1622 }
else if ( argc == 4 ) {
1624 PyInt_AS_LONG( bufsize ) );
1625 }
else if ( argc == 5 ) {
1627 PyInt_AS_LONG( bufsize ), PyInt_AS_LONG( splitlevel ) );
1648 class TTreeSetBranchAddress :
public TTreeMemberFunction {
1650 TTreeSetBranchAddress(
MethodProxy*
org ) : TTreeMemberFunction( org ) {}
1658 virtual Int_t GetMaxArgs() {
return 2; }
1659 virtual PyCallable* Clone() {
return new TTreeSetBranchAddress( *
this ); }
1666 int argc = PyTuple_GET_SIZE( args );
1673 PyErr_SetString( PyExc_TypeError,
1674 "TTree::SetBranchAddress must be called with a TTree instance as first argument" );
1681 if ( PyArg_ParseTuple( args, const_cast< char* >(
"SO:SetBranchAddress" ),
1682 &name, &address ) ) {
1686 if ( ((
ObjectProxy*)address)->fFlags & ObjectProxy::kIsReference )
1696 Py_INCREF( Py_None );
1713 virtual PyObject* ReportTypeError()
1715 PyErr_SetString( PyExc_TypeError,
1716 "TTree::SetBranchAddress must be called with a TTree instance as first argument" );
1724 class TChainSetBranchAddress :
public TTreeSetBranchAddress {
1726 TChainSetBranchAddress(
MethodProxy*
org ) : TTreeSetBranchAddress( org ) {}
1734 virtual Int_t GetMaxArgs() {
return 2; }
1735 virtual PyCallable* Clone() {
return new TChainSetBranchAddress( *
this ); }
1738 virtual PyObject* ReportTypeError()
1740 PyErr_SetString( PyExc_TypeError,
1741 "TChain::SetBranchAddress must be called with a TChain instance as first argument" );
1758 if ( ! (pya0 && pya1 && pya2 && pya3) ) {
1759 Py_XDECREF( pya3 ); Py_XDECREF( pya2 ); Py_XDECREF( pya1 ); Py_XDECREF( pya0 );
1764 PyObject* result = PyObject_CallFunction(
1765 pyfunc, (
char*)
"OOOOi", pya0, pya1, pya2, pya3, a4 );
1766 Py_DECREF( pya3 ); Py_DECREF( pya2 ); Py_DECREF( pya1 ); Py_DECREF( pya0 );
1770 throw std::runtime_error(
"TMinuit python fit function call failed" );
1773 Py_XDECREF( result );
1789 result = PyObject_CallFunction( pyfunc, (
char*)
"OO", pya0, pya1 );
1792 result = PyObject_CallFunction( pyfunc, (
char*)
"O", pya0 );
1800 throw std::runtime_error(
"TFN python function call failed" );
1802 d = PyFloat_AsDouble( result );
1803 Py_DECREF( result );
1815 using namespace PyROOT;
1821 PyObject* result = CallPyObjMethod(
self,
"Scale", scale );
1825 Py_DECREF( result );
1833 class TPretendInterpreted:
public PyCallable {
1835 TPretendInterpreted(
int nArgs ) : fNArgs( nArgs ) {}
1838 Int_t GetNArgs() {
return fNArgs; }
1839 virtual Int_t GetPriority() {
return 100; }
1840 virtual Int_t GetMaxArgs() {
return GetNArgs()+1; }
1841 virtual PyObject* GetCoVarNames() {
1842 PyObject* co_varnames = PyTuple_New( 1 + 1 );
1852 if ( ! pyobject || ! PyCallable_Check( pyobject ) ) {
1854 PyErr_Format( PyExc_ValueError,
1869 class TF1InitWithPyFunc :
public TPretendInterpreted {
1871 TF1InitWithPyFunc(
int ntf = 1 ) : TPretendInterpreted( 2 + 2*ntf ) {}
1878 "TF1::TF1(const char* name, PyObject* callable, "
1879 "Double_t xmin, Double_t xmax, Int_t npar = 0)" );
1882 virtual PyCallable* Clone() {
return new TF1InitWithPyFunc( *
this ); }
1888 int argc = PyTuple_GET_SIZE( args );
1889 const int reqNArgs = GetNArgs();
1890 if ( ! ( argc == reqNArgs || argc == reqNArgs+1 ) ) {
1891 PyErr_Format( PyExc_TypeError,
1892 "TFN::TFN(const char*, PyObject* callable, ...) =>\n"
1893 " takes at least %d and at most %d arguments (%d given)",
1894 reqNArgs, reqNArgs+1, argc );
1898 PyObject* pyfunc = PyTuple_GET_ITEM( args, 1 );
1902 if ( argc == reqNArgs+1 )
1903 npar = PyInt_AsLong( PyTuple_GET_ITEM( args, reqNArgs ) );
1906 std::vector<std::string> signature; signature.reserve( 2 );
1907 signature.push_back(
"double*" );
1908 signature.push_back(
"double*" );
1912 pyfunc, npar,
"double", signature,
"TFNPyCallback" );
1921 PyObject* newArgs = PyTuple_New( reqNArgs + 1 );
1923 for (
int iarg = 0; iarg < argc; ++iarg ) {
1924 PyObject* item = PyTuple_GET_ITEM( args, iarg );
1927 PyTuple_SET_ITEM( newArgs, iarg, item );
1933 if ( argc == reqNArgs )
1934 PyTuple_SET_ITEM( newArgs, reqNArgs, PyInt_FromLong( 0l ) );
1940 Py_DECREF( newArgs );
1941 Py_DECREF( method );
1948 class TF2InitWithPyFunc :
public TF1InitWithPyFunc {
1950 TF2InitWithPyFunc() : TF1InitWithPyFunc( 2 ) {}
1956 "TF2::TF2(const char* name, PyObject* callable, "
1957 "Double_t xmin, Double_t xmax, "
1958 "Double_t ymin, Double_t ymax, Int_t npar = 0)" );
1961 virtual PyCallable* Clone() {
return new TF2InitWithPyFunc( *
this ); }
1966 class TF3InitWithPyFunc :
public TF1InitWithPyFunc {
1968 TF3InitWithPyFunc() : TF1InitWithPyFunc( 3 ) {}
1974 "TF3::TF3(const char* name, PyObject* callable, "
1975 "Double_t xmin, Double_t xmax, "
1976 "Double_t ymin, Double_t ymax, "
1977 "Double_t zmin, Double_t zmax, Int_t npar = 0)" );
1980 virtual PyCallable* Clone() {
return new TF3InitWithPyFunc( *
this ); }
1990 class TMinuitSetFCN :
public TPretendInterpreted {
1992 TMinuitSetFCN(
int nArgs = 1 ) : TPretendInterpreted( nArgs ) {}
1999 "TMinuit::SetFCN(PyObject* callable)" );
2002 virtual PyCallable* Clone() {
return new TMinuitSetFCN( *
this ); }
2008 int argc = PyTuple_GET_SIZE( args );
2010 PyErr_Format( PyExc_TypeError,
2011 "TMinuit::SetFCN(PyObject* callable, ...) =>\n"
2012 " takes exactly 1 argument (%d given)", argc );
2016 PyObject* pyfunc = PyTuple_GET_ITEM( args, 0 );
2017 if ( ! IsCallable( pyfunc ) )
2021 std::vector<std::string> signature; signature.reserve( 5 );
2022 signature.push_back(
"Int_t&" );
2023 signature.push_back(
"Double_t*" );
2024 signature.push_back(
"Double_t&" );
2025 signature.push_back(
"Double_t*" );
2026 signature.push_back(
"Int_t" );
2030 pyfunc, 5,
"void", signature,
"TMinuitPyCallback" );
2043 for ( MethodProxy::Methods_t::const_iterator im = methods.begin(); im != methods.end(); ++im ) {
2044 PyObject* sig = (*im)->GetSignature();
2058 PyObject* newArgs = PyTuple_New( 1 );
2064 PyObject* result = setFCN->
Call(
self, newArgs, kwds, ctxt );
2068 Py_DECREF( newArgs );
2069 Py_DECREF( method );
2074 class TMinuitFitterSetFCN :
public TMinuitSetFCN {
2076 TMinuitFitterSetFCN() : TMinuitSetFCN( 1 ) {}
2082 "TMinuitFitter::SetFCN(PyObject* callable)" );
2085 virtual PyCallable* Clone() {
return new TMinuitFitterSetFCN( *
this ); }
2091 int argc = PyTuple_GET_SIZE( args );
2093 PyErr_Format( PyExc_TypeError,
2094 "TMinuitFitter::SetFCN(PyObject* callable, ...) =>\n"
2095 " takes exactly 1 argument (%d given)", argc );
2099 return TMinuitSetFCN::Call(
self, args, kwds, ctxt );
2106 void FitterPyCallback(
int& npar,
double* gin,
double& f,
double* u,
int flag )
2117 PyList_SetItem( arg3, 0, PyFloat_FromDouble( f ) );
2122 result = PyObject_CallFunction(
2123 gFitterPyCallback, (
char*)
"OOOOi", arg1, arg2, arg3, arg4, flag );
2124 f = PyFloat_AsDouble( PyList_GetItem( arg3, 0 ) );
2126 Py_DECREF( arg4 ); Py_DECREF( arg3 ); Py_DECREF( arg2 ); Py_DECREF( arg1 );
2130 throw std::runtime_error(
"TMinuit python fit function call failed" );
2133 Py_XDECREF( result );
2136 class TFitterFitFCN :
public TPretendInterpreted {
2138 TFitterFitFCN() : TPretendInterpreted( 2 ) {}
2144 "(PyObject* callable, int npar = 0, const double* params = 0, unsigned int dataSize = 0, bool chi2fit = false)" );
2149 "TFitter::FitFCN(PyObject* callable, int npar = 0, const double* params = 0, unsigned int dataSize = 0, bool chi2fit = false)" );
2152 virtual PyCallable* Clone() {
return new TFitterFitFCN( *
this ); }
2158 int argc = PyTuple_GET_SIZE( args );
2160 PyErr_Format( PyExc_TypeError,
2161 "TFitter::FitFCN(PyObject* callable, ...) =>\n"
2162 " takes at least 1 argument (%d given)", argc );
2166 PyObject* pyfunc = PyTuple_GET_ITEM( args, 0 );
2167 if ( ! IsCallable( pyfunc ) )
2171 Py_XDECREF( gFitterPyCallback );
2172 Py_INCREF( pyfunc );
2173 gFitterPyCallback = pyfunc;
2180 PyObject* newArgs = PyTuple_New( argc );
2182 for (
int iarg = 1; iarg < argc; ++iarg ) {
2183 PyObject* pyarg = PyTuple_GET_ITEM( args, iarg );
2185 PyTuple_SET_ITEM( newArgs, iarg, pyarg );
2192 Py_DECREF( newArgs );
2193 Py_DECREF( method );
2203 PyObject* result = CallPyObjMethod(
self,
"Get", attr );
2207 if ( !PyObject_IsTrue( result ) ) {
2208 PyObject* astr = PyObject_Str( attr );
2209 PyErr_Format( PyExc_AttributeError,
"TFile object has no attribute \'%s\'",
2212 Py_DECREF( result );
2226 PyErr_SetString( PyExc_TypeError,
2227 "TDirectoryFile::Get must be called with a TDirectoryFile instance as first argument" );
2234 PyErr_SetString( PyExc_ReferenceError,
"attempt to access a null-pointer" );
2250 void* addr = dirf->
Get( namecycle );
2257 return PyInt_FromLong( 3 );
2261 return PyInt_FromLong( 2 );
2299 Py_DECREF( pyfullname );
2321 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)StlSequenceIter;
2354 if ( name ==
"TObject" ) {
2365 else if ( name ==
"TClass" ) {
2375 else if ( name ==
"TCollection" ) {
2386 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)TCollectionIter;
2391 else if ( name ==
"TSeqCollection" ) {
2392 Utility::AddToClass( pyclass,
"__getitem__", (PyCFunction) TSeqCollectionGetItem, METH_O );
2394 Utility::AddToClass( pyclass,
"__delitem__", (PyCFunction) TSeqCollectionDelItem, METH_O );
2398 Utility::AddToClass( pyclass,
"reverse", (PyCFunction) TSeqCollectionReverse, METH_NOARGS );
2400 METH_VARARGS | METH_KEYWORDS );
2406 else if ( name ==
"TObjArray" ) {
2410 else if ( name ==
"TClonesArray" ) {
2414 Utility::AddToClass( pyclass,
"__getitem__", (PyCFunction) TSeqCollectionGetItem, METH_O );
2421 else if ( IsTemplatedSTLClass( name,
"vector" ) ) {
2433 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)vector_iter;
2436 TypedefInfo_t* ti =
gInterpreter->TypedefInfo_Factory( (name+
"::value_type").c_str() );
2439 PyObject_SetAttrString( pyclass,
"value_size", pyvalue_size );
2440 Py_DECREF( pyvalue_size );
2443 PyObject_SetAttrString( pyclass,
"value_type", pyvalue_type );
2444 Py_DECREF( pyvalue_type );
2452 std::string::size_type pos = name.find(
"vector<bool" );
2453 if ( pos == 0 || pos == 5 ) {
2459 else if ( IsTemplatedSTLClass( name,
"map" ) ) {
2464 else if ( IsTemplatedSTLClass( name,
"pair" ) ) {
2470 else if ( name.find(
"iterator" ) != std::string::npos ) {
2471 ((PyTypeObject*)pyclass)->tp_iternext = (iternextfunc)StlIterNext;
2482 else if ( name ==
"string" || name ==
"std::string" ) {
2491 else if ( name ==
"TString" ) {
2502 else if ( name ==
"TObjString" ) {
2513 else if ( name ==
"TIter" ) {
2514 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)PyObject_SelfIter;
2517 ((PyTypeObject*)pyclass)->tp_iternext = (iternextfunc)TIterNext;
2522 else if ( name ==
"TDirectory" ) {
2531 else if ( name ==
"TDirectoryFile" ) {
2538 else if ( name ==
"TTree" ) {
2546 Py_DECREF( original ); original = 0;
2548 PyObject_SetAttrString(
2549 pyclass, const_cast< char* >( method->
GetName().c_str() ), (
PyObject*)method );
2550 Py_DECREF( method ); method = 0;
2554 method =
MethodProxy_New(
"SetBranchAddress",
new TTreeSetBranchAddress( original ) );
2555 Py_DECREF( original ); original = 0;
2557 PyObject_SetAttrString(
2558 pyclass, const_cast< char* >( method->
GetName().c_str() ), (
PyObject*)method );
2559 Py_DECREF( method ); method = 0;
2563 else if ( name ==
"TChain" ) {
2568 Py_DECREF( original ); original = 0;
2570 PyObject_SetAttrString(
2571 pyclass, const_cast< char* >( method->
GetName().c_str() ), (
PyObject*)method );
2572 Py_DECREF( method ); method = 0;
2576 else if ( name ==
"TStyle" ) {
2582 else if ( name ==
"TH1" )
2585 else if ( name ==
"TF1" )
2588 else if ( name ==
"TF2" )
2591 else if ( name ==
"TF3" )
2594 else if ( name ==
"TFunction" )
2597 else if ( name ==
"TMinuit" )
2600 else if ( name ==
"TFitter" )
2603 else if ( name ==
"Fitter" )
2606 else if ( name ==
"TFile" ) {
2608 PyObject* attr = PyObject_GetAttrString( pyclass, (
char*)
"Open" );
2618 else if ( name.substr(0,8) ==
"TVector3" ) {
2625 else if ( name.substr(0,8) ==
"TVectorT" ) {
2632 else if ( name.substr(0,6) ==
"TArray" ) {
2639 else if ( name ==
"RooDataHist" )
2642 else if ( name ==
"RooSimultaneous" )
2648 PyObject* userPythonizations = PyObject_GetAttrString(
gRootModule,
"UserPythonizations" );
2649 PyObject* pythonizationScope = PyObject_GetAttrString(
gRootModule,
"PythonizationScope" );
2651 std::vector< std::string > pythonization_scopes;
2652 pythonization_scopes.push_back(
"__global__" );
2655 if ( user_scope !=
"__global__" ) {
2656 if ( PyDict_Contains( userPythonizations, pythonizationScope ) ) {
2657 pythonization_scopes.push_back( user_scope );
2663 for (
auto key = pythonization_scopes.cbegin(); key != pythonization_scopes.cend(); ++key ) {
2664 PyObject* tmp = PyDict_GetItemString( userPythonizations, key->c_str() );
2665 Py_ssize_t num_pythonizations = PyList_Size( tmp );
2667 if ( num_pythonizations )
2668 arglist = Py_BuildValue(
"O,s", pyclass, name.c_str() );
2669 for (
Py_ssize_t i = 0; i < num_pythonizations; ++i ) {
2670 PyObject* pythonizor = PyList_GetItem( tmp, i );
2672 PyObject* result = PyObject_CallObject( pythonizor, arglist );
2677 Py_DECREF( result );
2679 Py_XDECREF( arglist );
2682 Py_DECREF( userPythonizations );
2683 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 <object> proxied by <pyobj>
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.
TEllipse dict(9, 20, 3, 1.5)
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)