# Analogous of TTreeReader for writing

**URL:** <https://root-forum.cern.ch/t/analogous-of-ttreereader-for-writing/24852>\
**Category:** ROOT\
**Tags:** rdataframe\
**Created:** [May 22, 2017, 2:28pm UTC](https://root-forum.cern.ch/t/analogous-of-ttreereader-for-writing/24852 "2017-05-22T14:28:02Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Graipher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/graipher/32/11624_2.png) [@Graipher](https://root-forum.cern.ch/u/Graipher)\
**Post date:** [May 22, 2017, 3:37pm UTC](https://root-forum.cern.ch/t/analogous-of-ttreereader-for-writing/24852/2 "2017-05-22T15:37:12Z")

</div>

If you mean, you want to select certain events, then you can use [`TTree::CopyTree`](https://root.cern.ch/doc/master/classTTree.html#a2b57854ba133da5fc523931aa0427e21).  
If you mean, copy only certain branches, then you can disable branches you don’t want to see in the new file:

```auto
auto inFile = TFile::Open("in.root");
auto tree = (TTree*) inFile->Get("tree_name");
// Disable all branches
tree->SetBranchStatus("*", kFALSE);
// Re-enable desired branches
tree->SetBranchStatus("mass", kTRUE);
...

auto newFile = new TFile("out.root", "recreate");
// Copy only those parts of the tree for which the selection string is true
// Note that any branch needed in the selection needs to be enabled (and will therefore also be copied)
auto newTree = tree->CopyTree("mass > 100");
newTree->Write();
newFile->Write();
newFile->Close();

```

If you want to add branches whose value depends on some of the old branches, have a look at my post about a function that accomplishes that: [Add a new branch to a tree given only a formula](https://root-forum.cern.ch/t/add-a-new-branch-to-a-tree-given-only-a-formula/24728/8)

All of this will hopefully become way easier, as soon as ROOT 6.10 is published with its [`TDataFrame`](https://root.cern.ch/doc/master/classROOT_1_1Experimental_1_1TDF_1_1TDataFrame.html).

---

_[View the full topic](https://root-forum.cern.ch/t/analogous-of-ttreereader-for-writing/24852)._
