SetBranchAddress does not work with np.int16 and uint16 in PyROOT

Hello,

This bug exists in 6.34.04 and master (today) but not in 6.32.02 and 6.30.06.

It seems SetBranchAddress in PyROOT stopped accepting numpy’s short: uint16 and int16:

import ROOT
import numpy as np

a = np.zeros(3, np.uint16)
t = ROOT.TTree("t", "t")
t.Branch("b", a, "b/s")
t.Print()
t.SetBranchAddress("b", a)

In the working versions, there is no error, in the newer ROOTs the output is:

******************************************************************************
*Tree    :t         : t                                                      *
*Entries :        0 : Total =             754 bytes  File  Size =          0 *
*        :          : Tree compression factor =   1.00                       *
******************************************************************************
*Br    0 :b         : b/s                                                    *
*Entries :        0 : Total  Size=        465 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
Traceback (most recent call last):
  File "/tmp/./root_branch_test.py", line 10, in <module>
    t.SetBranchAddress("b", a)
  File "/home/lewhoo/software/root/root-6.34.04/lib/ROOT/_pythonization/_ttree.py", line 236, in _SetBranchAddress
    data_type = _determine_data_type(addr)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lewhoo/software/root/root-6.34.04/lib/ROOT/_pythonization/_ttree.py", line 204, in _determine_data_type
    return _get_cpp_type_from_numpy_type(addr.__array_interface__["typestr"][1:])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lewhoo/software/root/root-6.34.04/lib/ROOT/_pythonization/_rvec.py", line 90, in _get_cpp_type_from_numpy_type
    raise RuntimeError("Object not convertible: Python object has unknown data-type '" + dtype + "'.")
RuntimeError: Object not convertible: Python object has unknown data-type 'u2'.

Platform: Fedora 40


Hi @LeWhoo,

Thank you for your question. I think maybe @jonas can help.

Cheers,
Dev

I guess this might be related to issue PyROOT multidimenstional int16_t arrays stopped working in 6.34 · Issue #17841 · root-project/root · GitHub

Could be, but not in a very obvious (to me) manner. In my case it occurs also for 1D arrays, goes from numpy to C++, not from C++ to numpy, and results in an error message.