# Split Root File in two separate Files

**URL:** <https://root-forum.cern.ch/t/split-root-file-in-two-separate-files/41333>\
**Category:** Newbie\
**Created:** [September 8, 2020, 7:56am UTC](https://root-forum.cern.ch/t/split-root-file-in-two-separate-files/41333 "2020-09-08T07:56:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![C\_S](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/c_s/32/23279_2.png) [@C\_S](https://root-forum.cern.ch/u/C_S)\
**Post date:** [September 8, 2020, 7:56am UTC](https://root-forum.cern.ch/t/split-root-file-in-two-separate-files/41333/1 "2020-09-08T07:56:39Z")

</div>

Hey all,

I am using a root file to read values from it, the file is called main.root. The root file consists of a tree “myvalues” and several branches like “Energy”, “Momentum”, “Event”.  
Now I would like to separate my main.root file in two different root files main1.root and main2.root depending on the Event Variable that can either be 0 or 1. In the case of 0, I would like to save all the values from this line in the table (Energy, Momentum, Event) in the file main1.root. When the Event Variable is 1, I would like to save all values (Energy, Momentum, Event) in the main2.root file.

I searched that this can be possible with the TTreeReaderValue.

TFile file(“main.root”);  
TTree\* tree = (TTree\*) file.Get(“myvalues”);  
TTreeReaderValue var\_event(reader, “Event”);  
TTreeReaderValue var\_energy(reader, “Energy”);

while (reader.Next()) {  
std::cout\<\< "Eventvalue: "\<\<\*var\_event\<\< std::endl;  
}

Unfortunately I dont know how to save these values. Is this done with a chain?

\_ROOT Version: 6.20.04  
\_Platform: Ubuntu 18.04.5 LTS (Bionic Beaver)  
\_Compiler: gcc -v 7.5.0

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [September 8, 2020, 9:32am UTC](https://root-forum.cern.ch/t/split-root-file-in-two-separate-files/41333/2 "2020-09-08T09:32:08Z")

</div>

If it’s a simple selection and you want to copy all branches, you don’t need a TTreeReader, since a [CopyTree](https://root.cern/doc/master/classTTree.html#a2b57854ba133da5fc523931aa0427e21) will do it. In short: open the original file, get the tree, create the 2 new files, do the 2 tree copies, save each copy to a file:

```auto
  TFile *f = new TFile("main.root");
  TTree *T = (TTree*)f->Get("myvalues");

  TFile *f1 = new TFile("main1.root","RECREATE");
  TTree *T1 = T->CopyTree("Event==0");
  f1->cd(); // make sure T1 goes to f1
  T1->Write("myvalues1");
  f1->Close();
  // ... and similar for the second tree/file

```

---

<div class="post-metadata">

**Author:** ![C\_S](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/c_s/32/23279_2.png) [@C\_S](https://root-forum.cern.ch/u/C_S)\
**Post date:** [September 8, 2020, 12:15pm UTC](https://root-forum.cern.ch/t/split-root-file-in-two-separate-files/41333/3 "2020-09-08T12:15:11Z")

</div>

Many thanks! That worked. It helped me a lot.
