# Merging trees in root

**URL:** <https://root-forum.cern.ch/t/merging-trees-in-root/54347>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [April 3, 2023, 2:37pm UTC](https://root-forum.cern.ch/t/merging-trees-in-root/54347 "2023-04-03T14:37:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Baltazar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonathan_baltazar/32/32348_2.png) [@Jonathan\_Baltazar](https://root-forum.cern.ch/u/Jonathan_Baltazar)\
**Post date:** [April 3, 2023, 2:37pm UTC](https://root-forum.cern.ch/t/merging-trees-in-root/54347/1 "2023-04-03T14:37:55Z")

</div>

Hi, I am trying to Merge 2 or 3 LHEF trees using root. My final TTree has the correct structure, but the values are not saved. The relavant code is the following. The Init function just sets the branch adresses of the initial trees:

```cpp
TFile* fFile = (TFile*)TFile::Open(filename.Data());
  TTree* tree1 = (TTree*)fFile->Get("LHEF");
  Init(tree1);

  TFile* fFile2 = TFile::Open(filename2.Data());
  TTree* tree2 = (TTree*)fFile2->Get("LHEF");
  Init(tree2);

  TFile* fFile3 = TFile::Open(filename3.Data());
  TTree* tree3 = (TTree*)fFile3->Get("LHEF");
  Init(tree3);

  TFile *outfile = new TFile(outfilename, "RECREATE");
  TTree *mergedTree = new TTree("mergedTree", "Merged Tree");

  mergedTree->Branch("Event", &Event_, "fUniqueID/I:fBits/I:Number/I:Nparticles/I:ProcessID/I:Weight/F:ScalePDF/F:CouplingQED/F:CouplingQCD/F");
  mergedTree->Branch("Rwgt", &Rwgt_, "fUniqueID/I:fBits/I:Weight/F");
  mergedTree->Branch("Particle", &Particle_, "fUniqueID/I:Particle.fBits/I:Particle.PID/I:Particle.Status/I:Particle.Mother1/I:Particle.Mother2/I:Particle.ColorLine1/I:Particle.ColorLine2/I:Particle.Px/F:Particle.Py/F:Particle.Pz/F:Particle.E/F:Particle.M/F:Particle.PT/F:Particle.Eta/F:Particle.Phi/F:Particle.Rapidity/F:Particle.LifeTime/F:Particle.Spin/F");

  
  int nentries1 = tree1->GetEntries();
  for (int i = 0; i < nentries1; i++) {
    tree1->GetEntry(i);
    mergedTree->Fill();
  }

  int nentries2 = tree2->GetEntries();
  cout << "TEST" << Particle_PT[5] << endl;
  for (int i = 0; i < nentries2; i++) {
    tree2->GetEntry(i);

    mergedTree->Fill();
  }

  int nentries3 = tree3->GetEntries();
  for (int i = 0; i < nentries3; i++) {
    tree3->GetEntry(i);
    mergedTree->Fill();
  }

  // Save the merged tree to a file
  outfile->Write();
  //outfile->Close();

  cout << "<I> Merged tree saved to file: " << outfilename << endl;

```

I would appreciate any help, if someone knows why the values of the initial trees aren’t stored in the merged one. Thank you.

- Jonathan

* * *

---

<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 3, 2023, 3:07pm UTC](https://root-forum.cern.ch/t/merging-trees-in-root/54347/2 "2023-04-03T15:07:22Z")

</div>

`hadd -?`

---

<div class="post-metadata">

**Author:** ![Jonathan\_Baltazar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonathan_baltazar/32/32348_2.png) [@Jonathan\_Baltazar](https://root-forum.cern.ch/u/Jonathan_Baltazar)\
**Post date:** [April 4, 2023, 11:27am UTC](https://root-forum.cern.ch/t/merging-trees-in-root/54347/3 "2023-04-04T11:27:50Z")

</div>

Hi, when using hadd, the trees merge end to end. I.e. the total number of events goes to 30000, with 10000 events from each tree. I need the branches from the three trees to be merges to a single branch on the final tree, i.e. all pt values from all trees on the same final branch, such that the final number of events is the same as in the input trees…

---

<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 4, 2023, 11:35am UTC](https://root-forum.cern.ch/t/merging-trees-in-root/54347/4 "2023-04-04T11:35:13Z")

</div>

[ROOT](https://root.cern.ch/) offers the concept of friends for [TTree](https://root.cern/doc/master/classTTree.html) / [TChain](https://root.cern/doc/master/classTChain.html) (see [TTree::AddFriend](https://root.cern/doc/master/classTTree.html#a011d362261b694ee7dd780bad21f030b) / [TChain::AddFriend](https://root.cern/doc/master/classTChain.html#a967c9e4e14e314b46e4c11bc6a1c2353)).  
See also, e.g.: [`${ROOTSYS}/tutorials/tree/treefriend.C`](https://root.cern/doc/master/treefriend_8C.html)

If you attach your files, we could try to create a “monolithic” tree.
