Using ROOT.Numba: confusing imports are confusing

Hello,

I was trying to use Numba to jit python function (relying on numpy), to call from an RDataFrame graph. It works (amazing!), but I’m a bit confused about how to import pyROOT to use Numba as ROOT.Numba.

I thought that from cppyy import gbl as ROOT and import ROOT should be equivalent in practice, but that does not seem to be the case:

In [1]: from cppyy import gbl

In [2]: gbl.Numba
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-6f9dc22788fc> in <module>
----> 1 gbl.Numba

AttributeError: <namespace cppyy.gbl at 0x516b4d0> has no attribute 'Numba'. Full details:
  type object '' has no attribute 'Numba'
  'Numba' is not a known C++ class
  'Numba' is not a known C++ template
  'Numba' is not a known C++ enum

In [3]: gbl.ROOT.Numba
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-0909b72b901b> in <module>
----> 1 gbl.ROOT.Numba

AttributeError: <namespace cppyy.gbl.ROOT at 0x6d0f060> has no attribute 'Numba'. Full details:                                                                                             
  type object 'ROOT' has no attribute 'Numba'
  'ROOT::Numba' is not a known C++ class
  'Numba' is not a known C++ template
  'Numba' is not a known C++ enum

In [4]: import ROOT

In [5]: ROOT.Numba
Out[5]: <namespace cppyy.gbl.Numba at 0x72ce970>

Even stranger, once import ROOT has been called, this now works:

In [6]: gbl.Numba
Out[6]: <namespace cppyy.gbl.Numba at 0x72ce970>

Could someone help to clarify in which way the different imports are not equivalent, and why using Numba seems to require to import ROOT?

Cheers,
Sébastien


ROOT Version: 6.24/06
Platform: CC7
Compiler: GCC11


1 Like

Hello,

If you want to use ROOT.Numba you can only do it via the ROOT module, it’s a feature that is not provided by cppyy alone (ROOT does the injection of that feature).

I see, thanks. In general, is it therefore recommended to always use import ROOT? Is there any advantange of doing from cppyy import gbl as ROOT in some situations?

cppyy.gbl and ROOT are actually not exactly the same. cppyy.gbl is the global namespace. ROOT is a facade module that redirects lookups to cppyy.gbl, but also injects some extra functionality as it is the case of ROOT.Numba, and registers a number of pythonizations (extra behaviour in Python) for ROOT classes. Therefore if you are working with ROOT I would recommend to use import ROOT.

Perfect, thanks for clarifying!

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