C++ references in PyROOT

Hi all,

there are a number of ROOT functions (such as TH1::GetBinXYZ or TColor::RGB2HSV) that use references to return several values. Since PyROOT does not support C++ references (is that correct?), none of them can be used from the Python side, which is sometimes quite annoying.

Does anybody know an easy workaround to use these functions from Python?

Best regards,



Nope :wink:

In [1]: h = ROOT.TH2F( "h", "h", 100, 0, 100, 100, 0, 100 ) In [2]: x, y, z = ROOT.Long(), ROOT.Long(), ROOT.Long() In [3]: h.GetBinXYZ( 105, x, y, z ) or (x, y, z) Out[5]: (3, 1, 0)

For Doubles, use ROOT.Double. You can give them an initial value like so: x = ROOT.Long(5); print x == 5 # Gives ‘True’.

Hope that helps.

  • Peter
1 Like

Well, that was what I was looking for… Thanks a lot!


I just noticed one problem: it does not work for float references (e.g. in TColor::RGB2HSV). Looking at /bindings/pyroot/src/Converters.cxx, that makes sense: converters are defined for int, long and double references, but not for float.

Yeah, I have no idea why they didn’t implement that.

Nasty, unportable hack ahead. (Requires ctypes installed, or python >=2.5, only tested on Linux, ROOT 5.20)

Only use it if you’re desperate, otherwise wait for them to implement floats :wink:

I’ve only briefly checked the results are right, so there could be a mistake.

[code]from ctypes import c_float, POINTER, CFUNCTYPE

c_float_p = POINTER( c_float )

TRGB2HSV = CFUNCTYPE( None, c_float, c_float, c_float, c_float_p, c_float_p, c_float_p )

RGB2HSV = TRGB2HSV( ROOT.gROOT.ProcessLine( “TColor::RGB2HSV” ) )

h, s, v = c_float(), c_float(), c_float()

RGB2HSV( 255, 100, 0, h, s, v ) or (h, s, v)[/code]

The reason this works is that internally, references are no different to pointers.

To access the values of the ctypes, use “h.value”.


  • Pete