I’m trying to use a C++ class in my PyRoot code with Cint via gROOT.ProcessLine(). The strange thing is that the result of the public member function of this class is sometimes passed to Python, but sometimes not. This seems to depend on whether there’s a non-constant value in the parameter list of the method that I’m calling or not.
To illustrate what I’m doing here’s a minimal example:
So, the moment I set parameter/parameter1 to a constant value (i.e. uncomment the first line in the for loop), the result is properly passed to Python, but otherwise not.
I already tried a lot of things like using a shared library, compiling the C++ code or not, using casts etc. but I couldn’t really find any solution and I’m still totally puzzled …
I don’t follow what you’re trying to accomplish … The global result1 and result2 from the global CINT scope are found because of the “from ROOT import*”. But they are only found once, then transferred, and then exist on the python side and are afterwards never looked up again. Thus, if you loop over those calls, result1 and result2 will always remain the same value they were when first set.
This is the same behavior as expected from pure python modules on which you do “from pymodule import *”: you get a local copy (locally bound reference) of all global variables. If you want the result1 and result2 to be updated after resetting them at the module level, you’d have delete them at the end of the loop (in the scope of the looping code), use “import ROOT” and re-access them on module ROOT every time, or use C++ pointers.
Thanks Wim,
this helped me a lot!
I changed my code and now I’m using pointers. For people interested, here’s the new minimal example that works as expected: