使用 root_numpy (Python) 向 ROOT 文件中的 TTree 添加一个新分支
Add a new branch to a TTree in a ROOT file with root_numpy (Python)
我想知道如何使用 Python.
向 ROOT 文件中的一个 TTree 添加新分支
with root_open('MC_output.root', mode='a') as myfile:
new_column = numpy.array(gb_weights , dtype=[('weight', 'f8')])
root_numpy.array2tree(new_column , tree=myfile.TrainTree)
new_column_test = numpy.array(gb_weights_test , dtype=[('weight', 'f8')])
root_numpy.array2tree(new_column_test, tree=myfile.TestTree)
myfile.write()
myfile.close()
在这个例子中,TrainTree和TestTree已经存在于ROOT文件中。我只想给他们添加一个新分支 'weight'。我这里的问题是它会复制树,所以在我的文件中我有 2 个 TrainTree 和 2 个 TestTree。
我必须使用临时文件来解决这个问题吗?或者有更好、更简单的方法吗?
感谢您的帮助!
其实我自己也能找到解决办法:
我没有使用正确的 numpy 方法,我应该使用 array2root 而不是 array2tree。
new_column = numpy.array(gb_weights , dtype=[('weight', 'f8')])
root_numpy.array2root(new_column, 'MC_output.root' , 'TrainTree')
new_column_test = numpy.array(gb_weights_test , dtype=[('weight', 'f8')])
root_numpy.array2root(new_column_test, 'MC_output.root', 'TestTree')
才是正确的做法。请注意,在这种情况下,文件未打开,因此不会临时加载其包含。
我想知道如何使用 Python.
向 ROOT 文件中的一个 TTree 添加新分支with root_open('MC_output.root', mode='a') as myfile:
new_column = numpy.array(gb_weights , dtype=[('weight', 'f8')])
root_numpy.array2tree(new_column , tree=myfile.TrainTree)
new_column_test = numpy.array(gb_weights_test , dtype=[('weight', 'f8')])
root_numpy.array2tree(new_column_test, tree=myfile.TestTree)
myfile.write()
myfile.close()
在这个例子中,TrainTree和TestTree已经存在于ROOT文件中。我只想给他们添加一个新分支 'weight'。我这里的问题是它会复制树,所以在我的文件中我有 2 个 TrainTree 和 2 个 TestTree。
我必须使用临时文件来解决这个问题吗?或者有更好、更简单的方法吗?
感谢您的帮助!
其实我自己也能找到解决办法:
我没有使用正确的 numpy 方法,我应该使用 array2root 而不是 array2tree。
new_column = numpy.array(gb_weights , dtype=[('weight', 'f8')])
root_numpy.array2root(new_column, 'MC_output.root' , 'TrainTree')
new_column_test = numpy.array(gb_weights_test , dtype=[('weight', 'f8')])
root_numpy.array2root(new_column_test, 'MC_output.root', 'TestTree')
才是正确的做法。请注意,在这种情况下,文件未打开,因此不会临时加载其包含。