导入直方图时 Pyroot AttributeError

Pyroot AttributeError while importing histograms

我对 pyroot 有疑问。当我尝试导入 ROOT 直方图时,我总是得到相同的 AttributeError。

>>> from ROOT import TH1F
AttributeError: type object 'TArray' has no attribute '__getitem__'

During handling of the above exception, another exception occurred:

SystemError: <built-in method mro of ROOT.PyRootType object at 0x328fb18> returned a result with an error set
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'TH1F'

我也试过 rootpy,但是不行。可能相关?

我安装了 Python 3.5,并使用 gcc 5.2.0 进行了 ROOT 的全新安装。当我 运行 root-config --features.

时列出 Python 模块

有什么想法吗?或者解决方案?

您遇到的问题与 Python 中最近的更改有关,该更改解决了错误的异常处理。 Pythonize.cxx 包装器中的调用试图将 __getitem__ 属性重命名为 class TArray,但它不存在。这导致在 Python 中被忽略的 AttributeError,直到新的 python3.5 版本。

要恢复旧行为,您需要修改 $ROOTSYS/bindings/pyroot/src/ 目录中的文件 Utility.cxx。搜索方法

Bool_t PyROOT::Utility::AddToClass( PyObject* pyclass, const char* label, const char* func )

应该在第230行左右。在这个方法中是一个if条件:

if ( ! pyfunc )
    return kFALSE;

这里需要将上面的代码替换成下面几行:

if ( ! pyfunc ) {
   PyErr_Clear();
   return kFALSE;
}

PyErr_Clear() 的调用将解决这个问题。保存文件并重新编译您的 ROOT 安装。这应该可以解决问题。

编辑:此问题已有错误报告:https://sft.its.cern.ch/jira/browse/ROOT-7640