# Segmentation violation during Snapshot() of an existing tree using RDataFrame

**URL:** <https://root-forum.cern.ch/t/segmentation-violation-during-snapshot-of-an-existing-tree-using-rdataframe/54928>\
**Category:** ROOT\
**Tags:** tree, rdataframe, python, root\
**Created:** [May 18, 2023, 2:33pm UTC](https://root-forum.cern.ch/t/segmentation-violation-during-snapshot-of-an-existing-tree-using-rdataframe/54928 "2023-05-18T14:33:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![apetukho](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apetukho](https://root-forum.cern.ch/u/apetukho)\
**Post date:** [May 18, 2023, 2:33pm UTC](https://root-forum.cern.ch/t/segmentation-violation-during-snapshot-of-an-existing-tree-using-rdataframe/54928/1 "2023-05-18T14:33:45Z")

</div>

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](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/4) to `python` and it works perfectly.

```python
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.

```auto
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

* * *

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [May 19, 2023, 7:54am UTC](https://root-forum.cern.ch/t/segmentation-violation-during-snapshot-of-an-existing-tree-using-rdataframe/54928/2 "2023-05-19T07:54:49Z")

</div>

Dear @apetukho ,

In the same post you mention it is also discussed that it is not possible to update an already existing tree within a file [Is there a way to save dataframe to an already existing root file? - #6 by Karl007](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/6) .This hasn’t changed meanwhile. But as Enrico suggests in the post, you could create a new tree with the branch you were trying to attach and just use it as a friend tree via `TTree::AddFriend` later on.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![apetukho](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apetukho](https://root-forum.cern.ch/u/apetukho)\
**Post date:** [May 19, 2023, 12:05pm UTC](https://root-forum.cern.ch/t/segmentation-violation-during-snapshot-of-an-existing-tree-using-rdataframe/54928/3 "2023-05-19T12:05:48Z")

</div>

Dear Vincenzo,

Thank you for your response.  
Then I’m confused about the meaning of the `fOverwriteIfExists` [snapshot option](https://root.cern/doc/master/structROOT_1_1RDF_1_1RSnapshotOptions.html). The description says “If fMode is “UPDATE”, overwrite object in output file if it already exists.” Isn’t it the same as “updating an already existing tree within a file”? And why does it work for the first example, updating the `t1` tree?

I’m afraid using a friend tree is not an option as it is not supported by the fitting frameworks that are used afterwards. Right now, I’m trying to calculate the TMVA BDT classifier response and write it as a branch in the same tree as the input variables using `pyROOT`. Right now I’ve got a working but poorly performing implementations using `for event in tree` python loops. I was hoping to use the [`RReader`](https://root.cern.ch/doc/master/tmva003__RReader_8C.html) interface within `RDataFrame` to speed up this process. But this approach is now hindered by inability to write the updated tree directly to the existing file.  
So I guess the easiest workaround right now is to write the trees in a new file using `Snapshot()`, join the files and just remove the old ones? Or is there any other approach that would speed up the TMVA response computation in `pyROOT` and would allow to update an already existing tree within a file?

Best reagrds,  
Aleksandr

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [May 30, 2023, 1:57pm UTC](https://root-forum.cern.ch/t/segmentation-violation-during-snapshot-of-an-existing-tree-using-rdataframe/54928/4 "2023-05-30T13:57:08Z")

</div>

Hi,

sorry for the late reply. Do I understand correctly that you want to overwrite the contents of a tree _while you are reading that same tree_? I don’t think that’s supported (by TTree/TFile, that is).

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [May 30, 2023, 7:22pm UTC](https://root-forum.cern.ch/t/segmentation-violation-during-snapshot-of-an-existing-tree-using-rdataframe/54928/5 "2023-05-30T19:22:01Z")

</div>

> [@apetukho](#):
>
> I’m afraid using a friend tree is not an option as it is not supported by the fitting frameworks that are used afterwards.

If you make the ‘friendship’ permanent (by storing an updated `TTree` object (which does not duplicate the data) after setting the friendship, some tools should not notice the difference between the single tree and friend tree case.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [June 13, 2023, 7:22pm UTC](https://root-forum.cern.ch/t/segmentation-violation-during-snapshot-of-an-existing-tree-using-rdataframe/54928/6 "2023-06-13T19:22:31Z")

</div>

Closed on June 13, 2023, 7:22pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
