# How to write two different trees into one file?

**URL:** <https://root-forum.cern.ch/t/how-to-write-two-different-trees-into-one-file/24496>\
**Category:** ROOT\
**Tags:** tree\
**Created:** [April 21, 2017, 4:17am UTC](https://root-forum.cern.ch/t/how-to-write-two-different-trees-into-one-file/24496 "2017-04-21T04:17:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![p73](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/p73/32/14141_2.png) [@p73](https://root-forum.cern.ch/u/p73)\
**Post date:** [April 21, 2017, 4:17am UTC](https://root-forum.cern.ch/t/how-to-write-two-different-trees-into-one-file/24496/1 "2017-04-21T04:17:52Z")

</div>

Suppose I have file `file1` from which I get tree `tree1`.

```auto
TFile *f1 = new TFile("file1.root");
TTree *t1 = (TTree*)f1->Get("tree1");

```

I use this tree to create another tree `tree2`. Now question is how to write either `tree2` to the `file1` or write this trees in one file . If I try the first option

```auto
f1->cd();
tree2->Write();

```

the error is something like:

```auto
file1.root is not writeable. 

```

If I try the second option

```auto
TFile *f2 = new TFile("file2.root", "RECREATE");
...
f2->cd();
tree1->Write();
tree2->Write();

```

the following error occurs when I try to read `tree1` entries:

```auto
...
Error in <TFile::ReadBuffer> error reading all requested bytes from file `file1`, got 0 of 1112
Error in <TFile::ReadBuffer> error reading all requested bytes from file `file1`, got 0 of 1113
...

```

---

<div class="post-metadata">

**Author:** ![behrenhoff](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/behrenhoff/32/12285_2.png) [@behrenhoff](https://root-forum.cern.ch/u/behrenhoff)\
**Post date:** [April 21, 2017, 6:49am UTC](https://root-forum.cern.ch/t/how-to-write-two-different-trees-into-one-file/24496/2 "2017-04-21T06:49:22Z")

</div>

See the examples in tutorials/tree/copytree\*.C

First open the new file f2, then create your second tree tree2. To copy your tree t1 into the new file, use `t1->CloneTree`, see [https://root.cern.ch/doc/master/classTTree.html#a76927d17a94d091d4ef1ed0ba7ef3383](https://root.cern.ch/doc/master/classTTree.html#a76927d17a94d091d4ef1ed0ba7ef3383).

Think of large trees which don’t fit in your memory. ROOT still works because it already writes to disk when filling the tree. That is why the output file needs to be open already in the beginning.

Your first option desn’t work because you have opened f1 for reading only. You could pass the option “UPDATE” when opening f1. When you are sure you are doing “the right thing”, you can do that. Otherwise I suggest to use your second option and use a new file. That way you don’t change your input file and you can rerun your program and still get the same result.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 21, 2017, 7:27am UTC](https://root-forum.cern.ch/t/how-to-write-two-different-trees-into-one-file/24496/3 "2017-04-21T07:27:39Z")

</div>

Also see [How to Quickly Inspect the Content of a File?](https://root-forum.cern.ch/t/how-to-quickly-inspect-the-content-of-a-file/38086)

---

<div class="post-metadata">

**Author:** ![p73](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/p73/32/14141_2.png) [@p73](https://root-forum.cern.ch/u/p73)\
**Post date:** [April 21, 2017, 8:14am UTC](https://root-forum.cern.ch/t/how-to-write-two-different-trees-into-one-file/24496/4 "2017-04-21T08:14:55Z")

</div>

Very convenient option. Thank you.

---

<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:** [April 21, 2017, 5:04pm UTC](https://root-forum.cern.ch/t/how-to-write-two-different-trees-into-one-file/24496/5 "2017-04-21T17:04:27Z")

</div>

> [@p73](#):
>
> Now question is how to write either tree2 to the file1

For that just open the file for update:

```auto
TFile *f1 = new TFile("file1.root","UPDATE");

```

Cheers,  
Philippe.

---

<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:** [May 5, 2017, 5:04pm UTC](https://root-forum.cern.ch/t/how-to-write-two-different-trees-into-one-file/24496/6 "2017-05-05T17:04:31Z")

</div>

Closed on May 5, 2017, 5:04pm UTC

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