ATLAS Offline Software
Loading...
Searching...
No Matches
RootUtils::PyROOTInspector Class Reference

#include <PyROOTInspector.h>

Collaboration diagram for RootUtils::PyROOTInspector:

Static Public Member Functions

static PyObjectpyroot_inspect (PyObject *obj, bool persistentOnly=false)
static PyObjectpyroot_inspect2 (PyObject *obj, PyObject *obj_name, bool persistentOnly=false, bool retvecs=false)

Detailed Description

Definition at line 21 of file PyROOTInspector.h.

Member Function Documentation

◆ pyroot_inspect()

PyObject * RootUtils::PyROOTInspector::pyroot_inspect ( PyObject * obj,
bool persistentOnly = false )
static

Definition at line 444 of file PyROOTInspector.cxx.

446{
447 // handle non-pyroot objects
448 if (!TPython::CPPInstance_Check(pyobj)) {
449 Py_XINCREF(pyobj);
450 return pyobj;
451 }
452
453 TClass *tcls = RootUtils::objectIsA(pyobj);
454 if (0 == tcls) {
455 Py_INCREF(Py_None);
456 return Py_None;
457 }
458 void *obj = TPython::CPPInstance_AsVoidPtr(pyobj);
459
460 if (!strcmp(tcls->GetName(), "string")) {
461 std::string *str = (std::string*)obj;
462 return PyUnicode_FromString(str->c_str());
463 }
464
465 TString tstring = tcls->GetName();
466 if (tstring.BeginsWith("pair<") ||
467 tstring.BeginsWith("std::pair<")) {
468 PyObject *val = PyTuple_New(2);
469 PyObject *v0 = PyObject_GetAttrString(pyobj, "first");
470 PyObject *v1 = PyObject_GetAttrString(pyobj, "second");
471 PyTuple_SET_ITEM(val, 0, pyroot_inspect(v0, persistentOnly));
472 PyTuple_SET_ITEM(val, 1, pyroot_inspect(v1, persistentOnly));
473 Py_DECREF(v0);
474 Py_DECREF(v1);
475 return val;
476 }
477
478 Int_t hdr = 0;
479 if (CPyCppyy::Sequence_Check(pyobj)) {
480 if (!strcmp(tcls->GetName(), "CLHEP::Hep3Vector")) {
481 hdr = 0;
482 } else {
483 hdr = 1;
484 }
485 } else {
486 hdr = 0;
487 }
488
489 TList *members = tcls->GetListOfDataMembers();
490 const Int_t nmembers = members->GetEntries();
491
492 PyObject *py_members = PyList_New(nmembers+hdr);
493#if PYROOT_INSPECTOR_DBG
494 std::cerr << "==[" << tcls->GetName() << "]== (#mbrs:"
495 << nmembers
496 << " #stl:" << hdr /*PySequence_Size(pyobj)*/
497 << " #py-sz:" << PyList_Size(py_members)
498 << ")...\n";
499#endif
500
501 if (hdr) {
502 // handle collection
503 const Py_ssize_t nelems = PySequence_Size(pyobj);
504#if PYROOT_INSPECTOR_DBG
505 std::cerr << "== sequence (" << nelems << ")...\n";
506#endif
507 PyObject *py_elems = PyList_New(nelems);
508 for (Py_ssize_t i = 0; i < nelems; ++i) {
509 PyObject *itr = PySequence_GetItem(pyobj, i);
510 PyObject *itr_pyroot = pyroot_inspect(itr, persistentOnly);
511 PyList_SET_ITEM(py_elems, i, itr_pyroot);
512 Py_DECREF(itr);
513 //Py_DECREF(itr_pyroot);
514 }
515 // add the elements to the "members" list
516 PyList_SET_ITEM(py_members, 0, py_elems);
517#if PYROOT_INSPECTOR_DBG
518 std::cerr << "== sequence (" << nelems << ")... content:\n"
519 << ::to_str(py_elems)
520 << "\n";
521 std::cerr << "== sequence (" << nelems << ")... [done]\n";
522#endif
523 }
524
525
526 for (Int_t j = 0; j<nmembers; ++j) {
527 TDataMember *mbr = (TDataMember*)(members->At(j));
528 Int_t offset = mbr->GetOffset();
529 char *ptr = (char*)obj + offset;
530
531#if PYROOT_INSPECTOR_DBG
532 TClass *mbr_cls = TClass::GetClass(mbr->GetTypeName());
533 std::cerr << "==[" << j << "] - [" << mbr->GetTypeName() << "] "
534 << "[" << mbr->GetName()
535 << "]"
536 << "[" << (mbr_cls ? mbr_cls->GetName() : "N/A") << "]\n";
537#endif
538
539 PyObject *py_mbr = 0;
540
541 if (persistentOnly && !mbr->IsPersistent())
542 continue;
543 if (mbr->IsaPointer())
544 continue;
545 if (mbr->IsBasic()) {
546 TDataType * mbr_type = mbr->GetDataType();
547 EDataType mbr_dtype = (EDataType)mbr_type->GetType();
548 py_mbr = to_pyobj(ptr, mbr_dtype);
549 } else if (mbr->IsEnum()) {
550#if PYROOT_INSPECTOR_DBG
551 std::cerr << "==[" << mbr->GetTypeName() << "]["
552 << mbr->GetDataType()->GetType() << "][val="
553 << (*(int*)ptr) << "]["
554 << mbr->GetName() << "] is an enum !!\n";
555#endif
556 py_mbr = to_pyobj(ptr, kInt_t);
557 } else {
558 PyObject *pyroot_obj = TPython::CPPInstance_FromVoidPtr
559 ((void*)ptr,
560 mbr->GetTypeName());
561 if (pyroot_obj) {
562 py_mbr = pyroot_inspect(pyroot_obj, persistentOnly);
563 }
564 Py_XDECREF(pyroot_obj);
565 }
566 if (!py_mbr) {
567 std::cerr << "could not create py-object of type ["
568 << mbr->GetTypeName() << "] !\n";
569 Py_DECREF(py_members);
571 }
572
573 PyObject *py_item = PyTuple_New(2);
574 PyTuple_SET_ITEM(py_item, 0,
575 PyUnicode_FromString(mbr->GetName()));
576 PyTuple_SET_ITEM(py_item, 1, py_mbr);
577 PyList_SET_ITEM(py_members, j+hdr, py_item);
578 }
579#if PYROOT_INSPECTOR_DBG
580 std::cerr << "==[" << tcls->GetName() << "]== (#mbrs:"
581 << nmembers << ")... [done]\n";
582#endif
583 return py_members;
584}
_object PyObject
static PyObject * pyroot_inspect(PyObject *obj, bool persistentOnly=false)
CPyCppyy::PyException PyException
Definition Utility.h:24
TClass * objectIsA(PyObject *obj)
Definition Utility.cxx:103
float j(const xAOD::IParticle &, const xAOD::TrackMeasurementValidation &hit, const Eigen::Matrix3d &jab_inv)
void * ptr(T *p)
Definition SGImplSvc.cxx:74

◆ pyroot_inspect2()

PyObject * RootUtils::PyROOTInspector::pyroot_inspect2 ( PyObject * obj,
PyObject * obj_name,
bool persistentOnly = false,
bool retvecs = false )
static

Definition at line 587 of file PyROOTInspector.cxx.

591{
592 PyObject *pystack = PyList_New(0);
593 ::recurse_pyinspect(pyobj, pyobj_name, pystack, persistentOnly, retvecs);
594 return pystack;
595}

The documentation for this class was generated from the following files: