Passing variable to python in compiled c++ program using root

Hi,
I am trying to write a C++ program using root and python. In particular i am interested in creating a c++/root variable (like a vector, or an int, or whatever the type doesn’t really matter) and then passing the said variable to a python script or block of code using pyroot.

In particular i have to do some data analysis and i would like to pass a vector of values to python in order to use numpy and scipy functions to ease the analysis. All of this must be done in a compiled c++ program.

Thank you

Hi,

may I ask where this constraint comes from? PyROOT makes it very easy to call C++ from Python, but calling Python from C++ is trickier.

Cheers,
Enrico

Hi Enrico, thank you for the prompt answer.

Basically I am trying to add a module in a root/c++ analysis framework. This framework reads data from a file (basically arrays of numbers) which are saved in a custom class. For now let’s consider that the data is inside an std::vector or any root container to make it simpler.
Basically i have this vector of numbers loaded in the c++ program and i would like it to transmit it to a python block of code in order to analyze it with python libraries and then i would also like to transmit it back to c++ in order for the framework I/O to do it’s job.

What i would like to do is use some kind of memory pointer or binding in order to operate on a variable on both python and c++, i vaguely recall this being an option in the first versions of pyroot/tpython but now i can’t seem to find any indication on how to do such a thing anymore. Obviously other solutions are also welcome, this is just what i was expecting to find.

Ok, so you have a C++ framework to which you want to add a Python module, or anyway functionality that is implemented in Python. So you really have to call Python from C++.

I’m not sure how passing variables to Python and back to C++ is done, @etejedor might have a suggestion.

Cheers,
Enrico

Hi,

What you can do is use Python as the steering language for this. You can have your main program in Python, and use ROOT to load a C++ library where you have your C++ code. You would call into that C++ library with PyROOT, e.g. passing an std::vector you created in Python to a C++ function that would fill that vector, or simply making the C++ function return an std::vector (for which PyROOT would automatically create a Python proxy). The changes made to the vector from C++ would be visible on the Python side once you’d return from the function call.

For information on how to dynamically load C++ code via PyROOT and call into it, please see:

Hi,
I am not sure i am understanding well, you are saying to write the c++ code in a separate library and then execute it from python ?

The problem in doing so is that all the code must be contained in a c++ class, which is then executed by the c++ framework, so i am forced to use c++ as the steering language. Another option i can explore is to import in the c++ code some custom python functions that accept c++ objects, that is also something i can’t really find any tutorial on, i only successfully imported a custom python class into root.

Yes, my suggestion was to steer everything from Python because that makes it possible to benefit from PyROOT - it would be PyROOT taking care of the type conversions between the languages.

You can also check whether TPython covers everything you want to do:

https://root.cern/doc/master/classTPython.html

Otherwise you’ll need to use the Python C API and manipulate Python modules at a lower level.

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