Build a python module wrapping ROOT classes

Dear (py)ROOTers,

I have a code built on ROOT, let us call it MyPackage, that I would like to wrap in a python module, let us call it mypackage.

This topic

was very useful in importing the ROOT classes of the aforementioned code in a simple python script (even in a python jupyter notebook actually!).

I’d like to go beyond that and build a python interface to the code, such that a user can install the python module and use the underlying ROOT classes from python.
Fundamentally I do not want a user to do the gSystem/gInterpreter loading and then

from ROOT import MyPackageClass

I’d like him to be able to directly do

from mypackage import mypackageclass

What would be the proper way of doing it?
From my limited experience I can only think to create a series of python modules, each wrapping the corresponding classes in MyPackage/include/and then gather all of them in a module with __init__.py. Schematically:

.
β”œβ”€β”€ include
β”‚   β”œβ”€β”€ FirstClass.h
β”‚   β”œβ”€β”€ SecondClass.h
β”‚   └── ThirdClass.h
└── wrapper
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ firstclass.py
    β”œβ”€β”€ secondclass.py
    └── thirdclass.py

Additionally I do not know to which level I’d have to expose the class.
Is there a cleaner and more and efficient way to realise it?
Has somebody already done it and has suggestion?

Thanks for your guidance.

1 Like

Hi,

I would not advise to create Python wrappers for your C++ classes because that is actually what PyROOT automatically does, which is what is cool about PyROOT :slight_smile:

If you are concerned about the user having to load the headers / libraries and you want to hide that from them, why don’t you do it in the initialization of your module?

import mymodule # makes the header / library known to the ROOT interpreter
from ROOT import FirstClass
...

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.