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* >(
"" ) );
124 obj, const_cast< char* >( meth ), const_cast< char* >(
"O" ), arg1 );
137 obj, const_cast< char* >( meth ), const_cast< char* >(
"OO" ), arg1, arg2 );
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\'",
250 PyErr_SetString( PyExc_TypeError,
"getattr(): attribute name must be string" );
252 PyObject* pyptr = CallPyObjMethod(
self,
"__follow__" );
266 return PyInt_FromLong( 0
l );
268 PyObject* found = CallPyObjMethod(
self,
"FindObject", obj );
280 return PyInt_FromLong( -1
l );
282 return CallPyObjMethod(
self,
"Compare", obj );
293 return CallPyObjMethod(
self,
"IsEqual", obj );
304 return BoolNot( CallPyObjMethod(
self,
"IsEqual", 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 );
448 Py_XDECREF( result );
452 Py_INCREF( Py_None );
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" );
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() )
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 );
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 );
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" );
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;
959 Py_XDECREF( pydata );
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 ) {
1150 static inline PyObject* PyROOT_PyString_FromCppString( TString* 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 ); \ 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,
"" );
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;
1407 TBranch* branch = tree->GetBranch( name );
1410 branch = tree->GetBranch( (std::string( name ) +
'.' ).c_str() );
1433 if ( klass && ! tree->GetLeaf( name ) &&
1440 TLeaf* leaf = 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" );
1558 PyObject *bufsize = 0, *splitlevel = 0;
1561 if ( PyArg_ParseTuple( args, const_cast< char* >(
"O!OO!|O!:Branch" ),
1563 &leaflist, &PyInt_Type, &bufsize ) ) {
1567 buf = (
void*)((
ObjectProxy*)address)->GetObject();
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 ) {
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 ) ) {
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 );
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 );
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( 0
l ) );
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 );
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 );
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 );
2221 PyObject_SetAttr(
self, attr, result );
2233 PyErr_SetString( PyExc_TypeError,
2234 "TDirectoryFile::Get must be called with a TDirectoryFile instance as first argument" );
2241 PyErr_SetString( PyExc_ReferenceError,
"attempt to access a null-pointer" );
2257 void* addr = dirf->
Get( namecycle );
2264 return PyInt_FromLong( 3 );
2268 return PyInt_FromLong( 2 );
2305 if ( ! pyfullname ) pyfullname = PyObject_GetAttr( pyclass,
PyStrings::gName );
2307 Py_DECREF( pyfullname );
2329 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)StlSequenceIter;
2362 if ( name ==
"TObject" ) {
2373 else if ( name ==
"TClass" ) {
2383 else if ( name ==
"TCollection" ) {
2394 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)TCollectionIter;
2399 else if ( name ==
"TSeqCollection" ) {
2400 Utility::AddToClass( pyclass,
"__getitem__", (PyCFunction) TSeqCollectionGetItem, METH_O );
2402 Utility::AddToClass( pyclass,
"__delitem__", (PyCFunction) TSeqCollectionDelItem, METH_O );
2406 Utility::AddToClass( pyclass,
"reverse", (PyCFunction) TSeqCollectionReverse, METH_NOARGS );
2408 METH_VARARGS | METH_KEYWORDS );
2414 else if ( name ==
"TObjArray" ) {
2418 else if ( name ==
"TClonesArray" ) {
2422 Utility::AddToClass( pyclass,
"__getitem__", (PyCFunction) TSeqCollectionGetItem, METH_O );
2429 else if ( IsTemplatedSTLClass( name,
"vector" ) ) {
2441 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)vector_iter;
2444 TypedefInfo_t* ti =
gInterpreter->TypedefInfo_Factory( (name+
"::value_type").c_str() );
2447 PyObject_SetAttrString( pyclass,
"value_size", pyvalue_size );
2448 Py_DECREF( pyvalue_size );
2451 PyObject_SetAttrString( pyclass,
"value_type", pyvalue_type );
2452 Py_DECREF( pyvalue_type );
2461 std::string::size_type pos = name.find(
"vector<bool" );
2462 if ( pos == 0 || pos == 5 ) {
2468 else if ( IsTemplatedSTLClass( name,
"map" ) ) {
2473 else if ( IsTemplatedSTLClass( name,
"pair" ) ) {
2479 else if ( name.find(
"iterator" ) != std::string::npos ) {
2480 ((PyTypeObject*)pyclass)->tp_iternext = (iternextfunc)StlIterNext;
2491 else if ( name ==
"string" || name ==
"std::string" ) {
2500 else if ( name ==
"TString" ) {
2511 else if ( name ==
"TObjString" ) {
2522 else if ( name ==
"TIter" ) {
2523 ((PyTypeObject*)pyclass)->tp_iter = (getiterfunc)PyObject_SelfIter;
2526 ((PyTypeObject*)pyclass)->tp_iternext = (iternextfunc)TIterNext;
2531 else if ( name ==
"TDirectory" ) {
2540 else if ( name ==
"TDirectoryFile" ) {
2547 else if ( name ==
"TTree" ) {
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 method =
MethodProxy_New(
"SetBranchAddress",
new TTreeSetBranchAddress( original ) );
2564 Py_DECREF( original ); original = 0;
2566 PyObject_SetAttrString(
2567 pyclass, const_cast< char* >( method->
GetName().c_str() ), (
PyObject*)method );
2568 Py_DECREF( method ); method = 0;
2572 else if ( name ==
"TChain" ) {
2577 Py_DECREF( original ); original = 0;
2579 PyObject_SetAttrString(
2580 pyclass, const_cast< char* >( method->
GetName().c_str() ), (
PyObject*)method );
2581 Py_DECREF( method ); method = 0;
2585 else if ( name ==
"TStyle" ) {
2591 else if ( name ==
"TH1" )
2594 else if ( name ==
"TF1" )
2597 else if ( name ==
"TF2" )
2600 else if ( name ==
"TF3" )
2603 else if ( name ==
"TFunction" )
2606 else if ( name ==
"TMinuit" )
2609 else if ( name ==
"TFitter" )
2612 else if ( name ==
"Fitter" )
2615 else if ( name ==
"TFile" ) {
2617 PyObject* attr = PyObject_GetAttrString( pyclass, (
char*)
"Open" );
2627 else if ( name.substr(0,8) ==
"TVector3" ) {
2634 else if ( name.substr(0,8) ==
"TVectorT" ) {
2641 else if ( name.substr(0,6) ==
"TArray" && name !=
"TArray" ) {
2648 else if ( name ==
"RooDataHist" )
2651 else if ( name ==
"RooSimultaneous" )
2657 PyObject* userPythonizations = PyObject_GetAttrString(
gRootModule,
"UserPythonizations" );
2658 PyObject* pythonizationScope = PyObject_GetAttrString(
gRootModule,
"PythonizationScope" );
2660 std::vector< std::string > pythonization_scopes;
2661 pythonization_scopes.push_back(
"__global__" );
2664 if ( user_scope !=
"__global__" ) {
2665 if ( PyDict_Contains( userPythonizations, pythonizationScope ) ) {
2666 pythonization_scopes.push_back( user_scope );
2672 for (
auto key = pythonization_scopes.cbegin(); key != pythonization_scopes.cend(); ++key ) {
2673 PyObject* tmp = PyDict_GetItemString( userPythonizations, key->c_str() );
2674 Py_ssize_t num_pythonizations = PyList_Size( tmp );
2676 if ( num_pythonizations )
2677 arglist = Py_BuildValue(
"O,s", pyclass, name.c_str() );
2678 for (
Py_ssize_t i = 0; i < num_pythonizations; ++i ) {
2679 PyObject* pythonizor = PyList_GetItem( tmp, i );
2686 Py_DECREF( result );
2688 Py_XDECREF( arglist );
2691 Py_DECREF( userPythonizations );
2692 Py_DECREF( pythonizationScope );
R__EXTERN PyObject * gFirst
R__EXTERN PyObject * gCppEq
virtual const char * GetName() const
Returns name of object.
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any. ...
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
R__EXTERN Int_t gErrorIgnoreLevel
virtual TClass * GetTargetClass()
Collectable string class.
static Bool_t RegisterObject(ObjectProxy *pyobj, TObject *object)
start tracking <object> proxied by <pyobj>
R__EXTERN PyObject * gTClassDynCast
R__EXTERN PyObject * gSize
virtual const char * GetClassName() const
virtual TKey * GetKey(const char *name, Short_t cycle=9999) const
Return pointer to key with name,cycle.
virtual const char * GetTypeName() const
R__EXTERN PyObject * gFollow
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
MethodProxy * MethodProxy_New(const std::string &name, std::vector< PyCallable * > &methods)
TStreamerInfo * GetInfo() const
Get streamer info for the branch class.
R__EXTERN PyObject * gSetFCN
std::string GetFinalName(TCppType_t type)
R__EXTERN PyObject * gBranch
std::vector< PyCallable *> Methods_t
R__EXTERN PyObject * gFitFCN
R__EXTERN PyObject * gEnd
#define PYROOT_IMPLEMENT_STRING_PYTHONIZATION(type, name)
R__EXTERN PyObject * gCppNe
PyObject * TTreeGetAttr(ObjectProxy *self, PyObject *pyname)
R__EXTERN PyObject * gVectorAt
TObject * At(Int_t idx) const
#define PyInt_FromSsize_t
TMethod * GetMethodAllAny(const char *method)
Return pointer to method without looking at parameters.
R__EXTERN PyObject * gDeref
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)
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
Sequenceable collection abstract base class.
MethodProxy::Methods_t fMethods
R__EXTERN PyObject * gRootModule
virtual TLeaf * GetLeaf(const char *name) const
Return pointer to the 1st Leaf named name in thisBranch.
std::string GetReturnTypeNormalizedName() const
Get the normalized name of the return type.
virtual PyObject * FromMemory(void *address)
ClassInfo_t * GetClassInfo() const
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
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
TObject * First() const
Return the object in the first slot.
TConverter * CreateConverter(const std::string &fullType, Long_t size=-1)
PyTypeObject ObjectProxy_Type
const TString & GetString() const
Bool_t ObjectProxy_Check(T *object)
R__EXTERN PyObject * gClass
R__EXTERN PyObject * gBegin
A ROOT file is structured in Directories (like a file system).
char * GetObject() const
Return a pointer to our object.
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Collection abstract base class.
#define PyROOT_PyUnicode_FromStringAndSize
Int_t Size() const
Return size of object of this class.
virtual TLeaf * GetLeafCount() const
TClass * GetCurrentClass()
Return a pointer to the current type of the data member corresponding to branch element.
virtual void * GetValuePointer() const
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)
The ROOT global object gROOT contains a list of all defined classes.
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
virtual Int_t GetLenStatic() const
PyObject * PyBuffer_FromMemory(Bool_t *buf, Py_ssize_t size=-1)
A Branch for the case of an object.
MethodInfo_t * fMethodInfo
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)
const std::string & GetName() const
R__EXTERN PyObject * gName
Describe directory structure in memory.
static TClass * OP2TCLASS(PyROOT::ObjectProxy *pyobj)
static RooMathCoreReg dummy
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...
R__EXTERN PyObject * gCppName
TClass * GetClass() const
Binding & operator=(OUT(*fun)(void))
virtual TObject * At(Int_t idx) const =0
#define org(otri, vertexptr)
Mother of all ROOT objects.
TObjArray * GetElements() const
An array of clone (identical) objects.
static TPyBufferFactory * Instance()
R__EXTERN PyObject * gSecond
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)
virtual char * GetAddress() const
double TFNPyCallback(void *vpyfunc, Long_t npar, double *a0, double *a1)
R__EXTERN PyObject * ggetSize
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)
static PyObject * PyROOT_PyCapsule_New(void *cobj, const char *, void(*destr)(void *))
R__EXTERN PyObject * gSetBranchAddress
TBranch * GetBranch() const
virtual Int_t GetSize() const
A TTree is a list of TBranches.
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)
Cppyy::TCppType_t ObjectIsA() const
Bool_t Pythonize(PyObject *pyclass, const std::string &name)
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
virtual Int_t GetNdata() const