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

标签 python arrays numpy root-framework

所以我和我的研究伙伴正在尝试将一个相当大的 (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

关于Python:将 numpy 数组保存到 ROOT 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35858532/

相关文章:

python - y 轴的最小值未应用于 matplotlib vlines 图中

python - 将 Django 开发数据库从默认的 SQLite 更改为 PostgreSQL

python - 在Python中的线程之间分割任务

javascript - Promise 在真/假值数组中失败

javascript - 检查数组的更好方法

python - 如何在不导入模块的情况下按值类型对字典进行排序

javascript - 在特定索引上启动 for 循环并循环数组长度

Python、Numpy : How to select numpy array with given mask

numpy 运算符比较两个向量

python - ValueError:无法将字符串转换为 float : '62,6'