Implementation of C++ Class for PyROOT with CMake

Hello,

I have a project where I want to use C++ classes in my python code using PyROOT. It is my understanding that I must create a dictionary for my class and a rootmap. After reading through the ROOT documentation and other forums, I still don’t quite understand this process. Below is my project structure.

/top
   CMakeLists.txt
   /python
      pythonscript.py
   /core
      /io
         LinkDef.h
         Test.h
         Test.cpp

In pythonscript.py I want to be able to run

from ROOT import space

where the Test class is under namespace space. Is it possible to setup my CMakeLists.txt file so that it will create the dictionary and rootmap?
Any help would be greatly appreciated.
CMakeLists.txt (540 Bytes)
LinkDef.h (197 Bytes)

Hi,

for the sake of completeness, the creation of a dictionary for interactivity with custom classes in PyROOT is not necessary. If you have built a shared library and the interfaces you’d like to use in Python is in a header (say, libFoo.so and foo.h), you can just inject the relevant information in the interpreter, like this:

ROOT.gInterpreter.Declare('#include "foo.h"')
ROOT.gSystem.Load("libFoo.so")
c = ROOT.MyClassInFoo()

Creating a dictionary and a rootmap adds the capability to perform IO with the classes selected and, in addition, more comfort thanks to the autoloading(parsing) capabilities of ROOT.
Once you generate your dictionary you can access the custom C++ entities in PyROOT as you have written above.
What is going wrong in the generation? What is the content of the rootmap?

Cheers,
D

Some cmake fragments to create dictionaries would be very handy, though. (And a documented rootmap format.)

Here’s the work contributed to cppyy for the use with KDE: https://bitbucket.org/wlav/cppyy-backend/src/0b0fbe1af34213df51245456ecac7b00e6e208f2/cling/python/cppyy_backend/cmake/FindCppyy.cmake?at=master&fileviewer=file-view-default

Probably overkill for the project here, but maybe it provides some inspiration.

Hi Wim,

great! I wonder if in this case the ROOT provided snippet, already in use in the CMakeLists.txt file attached in the previous post, is enough.

Cheers,
D

Thanks for the responses. I will checkout the KDE link.

When I run cmake in a build directory, it says it cannot find source file Test.cxx. To add to this, if I share this with another user, I’d like it to be set up so that the user can build the project and not have to load the libraries by hand, which I believe is exploiting the rootmap capabilities. Do I have to generate the rootmap files or can CMake do this?

Hi,

the macro should generate the rootmap too. Is this happening in your case?

Cheers,
D

Hi,

I’ve made some progress. It will build but running cmake does not generate the rootmap file. It only generates the ,so and .pcm files. I had to manually write the rootmap file. Besides this everything runs great.

ROOT offers the ROOT_GENERATE_DICTIONARY macro in CMake, that you can use to generate the dictionary sources and add that to the target_sources of your library to get dictionaries for your class. At the moment I don’t think that there is an easy example of how to use it, other than code in ROOT itself like in graf3d/eve/CMakeLists.txt. You can also have a look at the example here: https://root.cern.ch/faq-page#n1013. As part of the build system restructuring that is happening this year, we will create new macros to make it easier for dependent projects to create libraries with dictionaries.

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