# What is the Easiest Way to Merge a Large Number of Small ROOT files each containing multiple TTrees?

**URL:** <https://root-forum.cern.ch/t/what-is-the-easiest-way-to-merge-a-large-number-of-small-root-files-each-containing-multiple-ttrees/57410>\
**Category:** ROOT\
**Created:** [December 6, 2023, 5:21am UTC](https://root-forum.cern.ch/t/what-is-the-easiest-way-to-merge-a-large-number-of-small-root-files-each-containing-multiple-ttrees/57410 "2023-12-06T05:21:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jocl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jocl/32/35197_2.png) [@jocl](https://root-forum.cern.ch/u/jocl)\
**Post date:** [December 6, 2023, 5:21am UTC](https://root-forum.cern.ch/t/what-is-the-easiest-way-to-merge-a-large-number-of-small-root-files-each-containing-multiple-ttrees/57410/1 "2023-12-06T05:21:26Z")

</div>

* * *

_Please fill also the fields below. Note that **`root -b -q`** will tell you this info, and starting from 6.28/06 upwards, you can call **`.forum bug`** from the ROOT prompt to pre-populate a topic._

_ROOT Version:_ 6.26/06  
_Platform:_ Scientific Linux  
_Compiler:_ None (macro)

* * *

Hi ROOT forum,

I’ve read several threads on this topic, but none of those I found quite fits my use case - I need to merge a large number of ROOT files each containing multiple TTrees. The TTree names are consistent between the files. The resultant file will be \> 100 GB.

Currently, I do the following:

```auto
int merge_root(int nfiles, string tag, int datormc, int cor)
{
  TChain ch("ttree");
  string filename;
  string base = "output/evt/events_"+tag+(tag!=""?"_":"");
  string dmc = (!datormc?"data_":"mc_");
  string cuc = (cor?"cor_":"unc_");
  base += dmc;
  base += cuc;
  string ext = ".root";
  for(int i=0; i<nfiles; i++)
    {
      if(i%10==0) cout << i << endl;
      filename = base;
      filename += to_string(i);
      filename += ext;
      cout << filename << endl;
      const char *alsofile = filename.c_str();
      const char* othertree = (filename+"?#outt").c_str();
      try
        {
          ch.Add(alsofile);
          ch.Add(othertree);
	}
      catch(...)
	{
          continue;
	}
    }
  filename = "results/merged_" +tag+(tag!=""?"_":"") + dmc + cuc + to_string(nfiles) + ext;
  ch.Merge(filename.c_str());
  return 0;
}

```

Note that the first tree is named “ttree”, while the second is named “outt”. The result I get contains ONLY the merged “ttree”, with no “outt” in sight.

Curiously, the error messages I receive say that the second call to TChain::Add() does not find the file/tree it is looking for, and contain some “characters” which aren’t recognized by the system I am on - i.e., they show up as rhombi with question marks in them.

Any ideas?

Thanks!

---

<div class="post-metadata">

**Author:** ![mdessole](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mdessole/32/34021_2.png) [@mdessole](https://root-forum.cern.ch/u/mdessole)\
**Post date:** [December 6, 2023, 1:28pm UTC](https://root-forum.cern.ch/t/what-is-the-easiest-way-to-merge-a-large-number-of-small-root-files-each-containing-multiple-ttrees/57410/2 "2023-12-06T13:28:47Z")

</div>

Hi @jocl,  
the problem is that the object `filename+"?#outt"` is destroyed after the declaration of othertree, thus the memory where it pointing is corrupted. This is why you see those rhombi with question marks when you call the Merge method. In order to solve your issue you should just declare a string variable to assign to `filename+"?#outt"`, as done below.

```auto
int merge_root(int nfiles, string tag, int datormc, int cor)
{
  TChain ch("ttree");
  string filename, filenameoutt;
  string base = "output/evt/events_"+tag+(tag!=""?"_":"");
  string dmc = (!datormc?"data_":"mc_");
  string cuc = (cor?"cor_":"unc_");
  base += dmc;
  base += cuc;
  string ext = ".root";
  for(int i=0; i<nfiles; i++)
    {
      if(i%10==0) cout << i << endl;
      filename = base;
      filename += to_string(i);
      filename += ext;
      cout << filename << endl;
      const char * alsofile = filename.c_str();
      filenameoutt = filename+"?#outt";
      cout << filenameoutt << endl;
      const char * othertree = filenameoutt.c_str();
      try
        {
          ch.Add(alsofile);
          ch.Add(othertree);
        }
      catch(...)
        {
          continue;
        }
    }
  filename = "results/merged_" +tag+(tag!=""?"_":"") + dmc + cuc + to_string(nfiles) + ext;
  cout << filename << endl;
  ch.Merge(filename.c_str());
  return 0;
}

```

Cheers,  
Monica

---

<div class="post-metadata">

**Author:** ![jocl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jocl/32/35197_2.png) [@jocl](https://root-forum.cern.ch/u/jocl)\
**Post date:** [December 6, 2023, 4:28pm UTC](https://root-forum.cern.ch/t/what-is-the-easiest-way-to-merge-a-large-number-of-small-root-files-each-containing-multiple-ttrees/57410/3 "2023-12-06T16:28:10Z")

</div>

This does in fact resolve the error messages (thank you), but there is a new issue: the output file contains only one tree, which is always the first one added to the chain.

My code now looks like:

```auto
int merge_root(int nfiles, string tag, int datormc, int cor)
{
  TChain ch("tchain");
  string filename, filenameoutt, filenamettree;
  string base = "output/evt/events_"+tag+(tag!=""?"_":"");
  string dmc = (!datormc?"data_":"mc_");
  string cuc = (cor?"cor_":"unc_");
  base += dmc;
  base += cuc;
  string ext = ".root";
  for(int i=0; i<nfiles; i++)
    {
      if(i%10==0) cout << i << endl;
      filename = base;
      filename += to_string(i);
      filename += ext;
      filenamettree = filename+"?#ttree";
      cout << filenamettree << endl;
      const char *alsofile = filenamettree.c_str();
      filenameoutt = filename+"?#outt";
      cout << filenameoutt << endl;
      const char* othertree = filenameoutt.c_str();
      try
        {
          ch.Add(alsofile);
          ch.Add(othertree);
        }
      catch(...)
        {
          continue;
        }
    }
  filename = "results/merged_dEdeta_" +tag+(tag!=""?"_":"") + dmc + cuc + to_string(nfiles) + ext;
  ch.Merge(filename.c_str());
  return 0;
}

```

If I run it in this form, I see an output file with one tree named “tchain” containing the values from what is called “ttree” in the un-merged files. The tree/values associated to “outt” is/are nonexistent.

On the other hand, if I comment out the line “ch.Add(alsofile)”, I still see a tree named “tchain”, but it contains the values from “outt”.

Actually, for my purposes it might be more useful to not merge the files at all and put them in a TChain in the file in which I do my processing, but I would still like to know if what I’m trying to do is possible…

On the other hand,

---

<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:** [December 20, 2023, 4:28pm UTC](https://root-forum.cern.ch/t/what-is-the-easiest-way-to-merge-a-large-number-of-small-root-files-each-containing-multiple-ttrees/57410/4 "2023-12-20T16:28:25Z")

</div>

Closed on December 20, 2023, 4:28pm UTC

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

---

<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:** [December 21, 2023, 6:57pm UTC](https://root-forum.cern.ch/t/what-is-the-easiest-way-to-merge-a-large-number-of-small-root-files-each-containing-multiple-ttrees/57410/5 "2023-12-21T18:57:53Z")

</div>

Did you try the `hadd` command line with the work-around described in [Root 6.04.14 hadd 100Gb and rootlogon - #2 by pcanal](https://root-forum.cern.ch/t/root-6-04-14-hadd-100gb-and-rootlogon/24581/2) ?
