ATLAS Offline Software
Static Public Member Functions | List of all members
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 428 of file PyROOTInspector.cxx.

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

◆ pyroot_inspect2()

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

Definition at line 571 of file PyROOTInspector.cxx.

575 {
576  PyObject *pystack = PyList_New(0);
577  ::recurse_pyinspect(pyobj, pyobj_name, pystack, persistentOnly, retvecs);
578  return pystack;
579 }

The documentation for this class was generated from the following files:
RootUtils::PyROOTInspector::pyroot_inspect
static PyObject * pyroot_inspect(PyObject *obj, bool persistentOnly=false)
Definition: PyROOTInspector.cxx:428
RootUtils::PyException
CPyCppyy::PyException PyException
Definition: Utility.h:24
parseMapping.v0
def v0
Definition: parseMapping.py:149
lumiFormat.i
int i
Definition: lumiFormat.py:92
RootUtils::objectIsA
TClass * objectIsA(PyObject *obj)
Definition: Utility.cxx:103
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
str
Definition: BTagTrackIpAccessor.cxx:11
python.PyAthena.obj
obj
Definition: PyAthena.py:135
PyObject
_object PyObject
Definition: IPyComponent.h:26