Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 451 of file PyROOTInspector.cxx.

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

◆ pyroot_inspect2()

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

Definition at line 594 of file PyROOTInspector.cxx.

598 {
599  PyObject *pystack = PyList_New(0);
600  ::recurse_pyinspect(pyobj, pyobj_name, pystack, persistentOnly, retvecs);
601  return pystack;
602 }

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:451
RootUtils::PyException
CPyCppyy::PyException PyException
Definition: Utility.h:24
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
parseMapping.v0
def v0
Definition: parseMapping.py:149
lumiFormat.i
int i
Definition: lumiFormat.py:85
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:132
PyObject
_object PyObject
Definition: IPyComponent.h:26