# Merging ROOT files containing TTree

**URL:** <https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914>\
**Category:** ROOT\
**Created:** [April 11, 2013, 5:47pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914 "2013-04-11T17:47:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Mattione](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@Paul\_Mattione](https://root-forum.cern.ch/u/Paul_Mattione)\
**Post date:** [April 11, 2013, 5:47pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914/1 "2013-04-11T17:47:09Z")

</div>

When merging two (or more) ROOT files containing a TTree (with hadd) it creates two separate ROOT trees (or at least keys) in the output file: one containing all of the data, and another containing only the data from the first-listed root file (in the hadd argument). This can be very confusing when trying to browse a TTree’s contents in the TBrowser.

To demonstrate this I wrote a small script:

```auto
//TreeTest.C
void TreeTest(string locFileName, double locNumber)
{
	TFile* locFile1 = new TFile(locFileName.c_str(), "RECREATE");
	TTree* locTree1 = new TTree("testtree", "testtree");
	double locValue1 = new double(locNumber);
	locTree1->Branch("Value", locValue1);
	locTree1->Fill();
	locFile1->Write();
	locFile1->Close();
}
```

And executed it in ROOT by calling:

```auto
root [0] .L TreeTest.C 
root [1] TreeTest("test1.root", 1.0);
root [2] TreeTest("test2.root", 2.0);
root [3] .q
```

And merged the files with:

```auto
hadd test_merge.root test1.root test2.root
```

And you can load up test\_merge.root in the TBrowser and you’ll see two TTree keys. Like I said, this is very confusing, and a user could easily view the wrong one. Please fix this bug. Thank you!

---

<div class="post-metadata">

**Author:** ![Paul\_Mattione](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@Paul\_Mattione](https://root-forum.cern.ch/u/Paul_Mattione)\
**Post date:** [April 11, 2013, 5:48pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914/2 "2013-04-11T17:48:19Z")

</div>

I guess I should clarify that I’m using version 5.30/00 of ROOT.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 11, 2013, 6:11pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914/3 "2013-04-11T18:11:01Z")

</div>

[url][Aux;1 aux;2 aux3;](https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345/1)

---

<div class="post-metadata">

**Author:** ![Paul\_Mattione](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@Paul\_Mattione](https://root-forum.cern.ch/u/Paul_Mattione)\
**Post date:** [April 11, 2013, 6:17pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914/4 "2013-04-11T18:17:50Z")

</div>

Yes, it is the same. But it was never fixed. There shouldn’t be two keys in the new file in the first place, regardless of their cycle. There should only be one (unless there’s some really good reason for it that I’m missing) because it is misleading in it’s current format.

---

<div class="post-metadata">

**Author:** ![bozzochet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bozzochet/32/13718_2.png) [@bozzochet](https://root-forum.cern.ch/u/bozzochet)\
**Post date:** [April 12, 2013, 6:05pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914/5 "2013-04-12T18:05:38Z")

</div>

I can be wrong but they should be the ‘cycles’: in a TFile you can have several version of the very same object (an histogram or a tree) but taken at different times. Usually they are indicated like “h;1”, “h;2”, …

Are you sure that when you simply “click” on the TTree (via the TBrowser) you can really get the “wrong” one?  
Retrieving the TTree from a macro or a program with TFile::Get(\<name\_of\_the\_tree\>) will give you the last (the younger) tree and so, in your case, the correct one… I’m quite sure…

Ciao,  
Matteo

---

<div class="post-metadata">

**Author:** ![Paul\_Mattione](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@Paul\_Mattione](https://root-forum.cern.ch/u/Paul_Mattione)\
**Post date:** [April 12, 2013, 6:15pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914/6 "2013-04-12T18:15:34Z")

</div>

In the TBrowser there are two different objects: they have the same name but different cycles. Depending on which one you pick in the TBrowser you pick the contents are different: one contains all of the information, and one only contains the information from the first TTree. You can easily duplicate this with the above code.

Yes, calling TDirectoryFile::Get() in a macro you get the correct one, but in the TBrowser you can indeed choose the wrong one. It’s not a huge deal but it’s confusing to those who are unaware of the phenomenon, and I would like the extra,old object to be removed for clarity.

---

<div class="post-metadata">

**Author:** ![bozzochet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bozzochet/32/13718_2.png) [@bozzochet](https://root-forum.cern.ch/u/bozzochet)\
**Post date:** [April 12, 2013, 6:23pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914/7 "2013-04-12T18:23:33Z")

</div>

Is a little bit boring but you can delete the previous cycle

[root.cern.ch/root/html/TFile.html#TFile:Delete](http://root.cern.ch/root/html/TFile.html#TFile:Delete)

Or, otherwise you can “patch” the hadd (is very simple) and add the “kOverWrite” flag to the TFile::Write() method…

Ciao,  
Matteo

---

<div class="post-metadata">

**Author:** ![Paul\_Mattione](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@Paul\_Mattione](https://root-forum.cern.ch/u/Paul_Mattione)\
**Post date:** [April 12, 2013, 7:37pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914/8 "2013-04-12T19:37:34Z")

</div>

But what I am asking is that the ROOT developers “patch” hadd for a future release 🙂

---

<div class="post-metadata">

**Author:** ![bozzochet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bozzochet/32/13718_2.png) [@bozzochet](https://root-forum.cern.ch/u/bozzochet)\
**Post date:** [April 12, 2013, 7:40pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914/9 "2013-04-12T19:40:51Z")

</div>

Yes I understood. But if you need today… 😉

Ciao,  
Matteo

---

<div class="post-metadata">

**Author:** ![basil](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/59ef9b/32.png) [@basil](https://root-forum.cern.ch/u/basil)\
**Post date:** [April 20, 2013, 3:48pm UTC](https://root-forum.cern.ch/t/merging-root-files-containing-ttree/15914/10 "2013-04-20T15:48:21Z")

</div>

This should be already fixed, you’re just using an old root version.
