Dear ROOT experts,
I’ve got a file that contains 3 trees. I want to add a branch to one of these trees and write it to the same file with the same name. I’ve converted the code from this post to python
and it works perfectly.
import ROOT
df = ROOT.ROOT.RDataFrame(10).Define("x", "gRandom->Rndm()")
# produce initial file
df.Snapshot("t1", "f.root", "")
# write another tree to that file
opts1 = ROOT.ROOT.RDF.RSnapshotOptions()
opts1.fMode = "update"
df.Snapshot("t2", "f.root", "", opts1)
# overwrite the t1
opts2 = ROOT.ROOT.RDF.RSnapshotOptions()
opts2.fMode = "update"
opts2.fOverwriteIfExists = True
df.Range(5).Snapshot("t1", "f.root", "", opts2)
However, in a more realistic situation, when I try to open an already existing file and write to it, the program fails with a segmentation violation and non descriptive python and C++ errors.
import ROOT
def create_file(file_name, tree_name_list):
df = ROOT.ROOT.RDataFrame(10).Define("x", "gRandom->Rndm()")
df.Snapshot(tree_name_list[0], file_name, "")
for tree_name in tree_name_list[1:]:
opts = ROOT.ROOT.RDF.RSnapshotOptions()
opts.fMode = "update"
df.Snapshot(tree_name, "f.root", "", opts)
def change_tree(file_name, tree_name):
df = ROOT.ROOT.RDataFrame(tree_name, file_name)
opts = ROOT.ROOT.RDF.RSnapshotOptions()
opts.fMode = "update"
opts.fOverwriteIfExists = True
df.Range(5).Snapshot(tree_name, file_name, "", opts)
def change_tree_add_branch(file_name, tree_name, branch_name):
df = ROOT.ROOT.RDataFrame(tree_name, file_name)
df = df.Define(branch_name, "x")
opts = ROOT.ROOT.RDF.RSnapshotOptions()
opts.fMode = "update"
opts.fOverwriteIfExists = True
df.Snapshot(tree_name, file_name, "", opts)
def main():
file_name = "f.root"
tree_name_list = ["t1", "t2", "t3"]
create_file(file_name, tree_name_list)
change_tree(file_name, tree_name_list[0])
change_tree_add_branch(file_name, tree_name_list[0], "y")
if __name__ == "__main__":
main()
create_file()
function works perfectly fine but both functions change_tree()
(that truncates the tree as in the example thread) and change_tree_add_branch()
(that adds branch to the tree) fail.
What is the correct way to add a new branch to the existing tree using RDataFrame?
Thanks in advance,
Aleksandr
ROOT Version: 6.28.04
Platform: Ubuntu 20.04
Compiler: Precompiled