# Is there a way to save dataframe to an already existing root file?

**URL:** <https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974>\
**Category:** Newbie\
**Tags:** rdataframe\
**Created:** [July 22, 2021, 2:18pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974 "2021-07-22T14:18:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 22, 2021, 2:18pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/1 "2021-07-22T14:18:37Z")

</div>

Hi,

I’m trying to store filtered data to an already existing root file. I am using:

```auto
    c.Snapshot("tree", "rootFile");	

```

But what it does actually is overwriting the “rootFile” with a new file that only has “tree” because they have the same name. While what I want is to save new leaf in a “tree” of already existing “rootFile”. Would that be possible?

Thanks!

---

<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:** [July 22, 2021, 2:31pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/2 "2021-07-22T14:31:23Z")

</div>

Hi @Karl007 ,  
yes, you can pass options to `Snapshot`, and one of the options changes the file opening mode.

```auto
ROOT::RDF::RSnapshotOptions opts;
opts.fMode = "update";
c.Snapshot("t", "f.root", "", opts);

```

The third argument, `""` is just an empty list of columns to `Snapshot`, which by default means “all of them”.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 22, 2021, 2:55pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/3 "2021-07-22T14:55:15Z")

</div>

Hi @eguiraud  
I tried it using the tree and root file names I want to update, but it says that the tree name already exists. But this I already know and my intention is to update that tree with a new leaf.

I also tried a different tree name (to save it as a new tree) but I get segmentation violation.

---

<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:** [July 22, 2021, 3:07pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/4 "2021-07-22T15:07:49Z")

</div>

We must be doing something different. This works for me:

```cpp
#include <ROOT/RDataFrame.hxx>

int main() {
   auto df = ROOT::RDataFrame(10).Define("x", [] { return 42; });

   // produce initial file
   df.Snapshot<int>("t1", "f.root", {"x"});

   // write another tree to that file
   ROOT::RDF::RSnapshotOptions opts1;
   opts1.fMode = "update";
   df.Snapshot<int>("t2", "f.root", {"x"}, opts1);

   // overwrite the t1
   ROOT::RDF::RSnapshotOptions opts2;
   opts2.fMode = "update";
   opts2.fOverwriteIfExists = true;
   df.Range(5).Snapshot<int>("t1", "f.root", {"x"}, opts2);

   return 0;
}

```

At the end of the program `f.root` contains `t1` with 5 entries and `t2` with 10. What am I doing differently?

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 22, 2021, 3:27pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/5 "2021-07-22T15:27:44Z")

</div>

The tree was already saved, this is why I received segmentation violation error.

Thanks Enrico!

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 23, 2021, 8:37am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/6 "2021-07-23T08:37:18Z")

</div>

Hi @eguiraud

Sorry I have another question: Is it possible to save it inside the same tree? or maybe copy the produced leaf into a specific tree?

---

<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:** [July 23, 2021, 8:50am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/7 "2021-07-23T08:50:21Z")

</div>

No, that’s not possible with RDataFrame, but you can save the new leaf/branch in a separate TTree and then read this second tree together with the first as a single broader tree using `TTree::AddFriend`, see [ROOT: ROOT::RDataFrame Class Reference](https://root.cern/doc/master/classROOT_1_1RDataFrame.html#friends)

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 23, 2021, 9:34am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/8 "2021-07-23T09:34:05Z")

</div>

I am using:

```auto
	TFile *f = TFile::Open("rootfile");
	TTree *t = f.Get<TTree>("tree");
	TTree *ft = f.Get<TTree>("friendtree"); //friend tree
	 
	t.AddFriend(&ft, "myFriend");
	
	RDataFrame d(t);
	auto c = d.Define("h1", "friendtreeleaf[treeleaf == 3]");
	auto h = c.Histo1D("h1");
	h->DrawCopy();

```

But it cannot recognize AddFriend().

EDIT: tried this

```auto
	ROOT::RDataFrame t("tree", "rootfile");
	ROOT::RDataFrame ft("friendtree", "rootfile");
	t.AddFriend(&ft, "myFriend");
	
	RDataFrame d(t);
	auto c = d.Define("h1", "friendtreeleaf[treeleaf == 3]");
	auto h = c.Histo1D("h1");
	h->DrawCopy();

```

And it seems to still not recognize AddFriend(). Maybe my ROOT version?

---

<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:** [July 23, 2021, 10:58am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-save-dataframe-to-an-already-existing-root-file/45974/9 "2021-07-23T10:58:23Z")

</div>

Hi,  
C++ shenanigans, I guess. The compilation errors should point to what the problem is.  
In the first version:

```auto
t.AddFriend(&ft, "myFriend");

```

should be

```auto
t.AddFriend(ft, "myFriend");

```

because `ft` is already a pointer and `AddFriend` requires a pointer, and `RDataFrame d(t)` should be `RDataFrame d(*t)` because `RDataFrame` takes a reference and `t` is a pointer.

Cheers,  
Enrico
