Python: 将 numpy 数组保存到 ROOT 文件

Python: saving numpy array to ROOT file

所以我和我的研究伙伴正在尝试将一个相当大的 (47104,5) 数组保存到 ROOT 文件中的 TTree 中。 Python 一侧的数组工作正常。我们可以访问所有内容和 运行 普通命令,但是当我们 运行 root_numpy.array2root() 命令时,我们会得到一个奇怪的错误。

Object of type 'NoneType' has no len()

我们为这部分 运行 编写的代码如下:

import root_numpy as rnp
import numpy as np
import scipy
import logging

def save_array(outputArray, outputName):
    outputString =str(outputName)
    logging.info("Creating .Root file")
    rnp.array2root(outputArray,outputString,treename="Training_Variables",mode="recreate")

我们放置 outputString 变量是为了确保我们将文件名作为字符串放入。 (在我们的 python 终端中,我们在 outputName 的末尾添加 .root 以将其保存为 .root 文件。)。

这是航站楼的图片。 Showing exact error location in root_numpy

我们很困惑为什么 array2root() 正在调用我们认为不应该有 len 的对象的 len()?它应该只有一个形状。任何见解将不胜感激。

从 NumPy 数组到 ROOT 数据类型的转换例程适用于结构化数组。请参阅以下两个链接。 (未测试,但这很可能是问题所在,因为例程使用 arr.dtypes.namesarr.dtypes.fields 属性)。

http://rootpy.github.io/root_numpy/reference/generated/root_numpy.array2tree.html#root_numpy.array2tree

http://rootpy.github.io/root_numpy/reference/generated/root_numpy.array2root.html#root_numpy.array2root