PyROOT convert ctypes.c_int when passing it as argument


ROOT Version: 6.18/PY3
Platform: CC7
Compiler: GCC6.2


Hi all,

I have a piece of code that use PyROOT and it looks like this:

    trackid    = ROOT.Long()
    energy_dep = ROOT.Double()
    mcLink = {}
    for i in range( aClus.Size() ):
        for n in range( mccell.TrackEnergySize()):
            mccell.GetTrackEnergySlow(n, trackid, energy_dep) #<- problem here

It raise a runtime error:

TypeError: void ecalCellMC::GetTrackEnergySlow(int n, int& trackid, double& energy_dep) =>
    could not convert argument 2 (use ctypes.c_int for pass-by-ref of ints)

If I read the error code correctly the issue is the trackid parameter.

I found a similar issue without an actual solution: "int pass-by-ref not implemented in p3"

Are there any workaround? I didn’t have the same problem with Py2.

Cheers,
Simone

I think @etejedor can help you.

Hi Simone,

Does it work if you declare trackid as a ctypes.c_int (or c_long) instead of a ROOT.Long()?

AFAIK the Long and Double types are going to be deprecated in Cppyy and the use of the equivalent ctypes types should be encouraged.

Yes, no such thing in cppyy. Note that ROOT.Double and ROOT.Long predate ctypes and p3 by a good margin, and they’ve simply become obsolete.

What ROOT.Long did/does is take advantage of the fact that the payload of a Python ‘int’ is a C long, and sizeof(int) == sizeof(long) on many systems. So, it creates a fresh Python int and accesses the underlying by reference. On p3, int objects are more complex, so can not passed by-reference this way.

The ctypes.c_int approach will work on both p2 and p3, with the old PyROOT and with cppyy. And although it’s from after the topic that you reference, that solution has been around for several years now, so should be safe to use unless you still have to deal with ROOT5.

1 Like

Many thanks!

Using ctypes.c_int() instead of ROOT.Long() solves the problem.

ctypes must be imported.

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