Wrapping ROOT classes for Python

Hello rooters,
I would like to create a python wrapper for a C++ class derived from TNamed.
According to this: root.cern.ch/pyroot
"PyROOT is a Python extension module that allows the user to interact with any ROOT class from the Python interpreter. This is done generically using the ROOT dictionary, therefore there is no need to generate any Python wrapper code to include new ROOT classes. […]"
Since my class has a dictionary generated with rootcling and it can be used directly from the ROOT interpreter, I guess that it should be also possible to use PyROOT to call my class from python.
However I cannot find any simple example to learn how to do it.
Can you please recommend some method to do this?
Thanks and cheers!

1 Like

Wow! It seems to be much more easier than I thought.
From a python script just doing:
from ROOT import myclass
Seems to do the trick… :smiley:
I guess that it is because the dictionary, right?
This is so great!

Yes, the dictionary do the trick in your case. But without dictionary also works [with ROOT 6] with just a bit more extra work. Imagine that you have an external library and the corresponding header file(s). Lets use the fastjet library for example.

# Load the library and include the header file
from ROOT import gInterpreter, gSystem
gInterpreter.ProcessLine('#include "fastjet/PseudoJet.hh"')
gSystem.Load('libfastjet')

# then you can starting the fastjet class directly....
from ROOT import fastjet
jet = fastjet.PseudoJet( 99.0, 0.1, 0, 100.0)
print jet.E()
1 Like

I was asking this in a wrong thread (thread concerning GDML), found this thread that seems appropriate so posting my sub-question here:

I am using pre-compiled root binaries (stored in /dir/to/ROOT/version/), how do I import ROOT to python so that I can install and use software like GeGeDe, this installation guide suggests doing:

$ sudo apt-et install libroot-bindings-python5.34
$ export PYTHONPATH=/usr/lib/x86_64-linux-gnu/root5.34
$ python -c 'import ROOT'  # <-- just a test...

With pre-compiled binaries, what I did is, exported:

export PYTHONPATH="dir/to/ROOT/version/lib/"

of course thisroot.sh has been sourced before this.

Then

python -c 'import ROOT'
Segmentation fault (core dumped)

It segfaults!
I am attaching the verbose of 2 python versions.

2.7: python2.7_import_ROOT_verbose.txt (6.6 KB)
3.6: python3.6m_import_ROOT_verbose.txt (42.1 KB)

Originally my question was a part of this thread.

SYSTEM DETAILS

_ROOT Versions: ROOT 6.18/04, 6.20/04 [pre-compiled bianries]
_Platform: Ubuntu 18.04.4 LTS
_Compiler: g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0