# 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:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rene](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/7cd45c/32.png) [@rene](https://root-forum.cern.ch/u/rene)\
**Post date:** [May 22, 2017, 2:28pm UTC](https://root-forum.cern.ch/t/analogous-of-ttreereader-for-writing/24852/1 "2017-05-22T14:28:03Z")

</div>

Hi there,

now that the nice TTreeReader is available, is there an easy way to write only a subset of an input TTree (meaning only a few entries are retained)?

Thanks.

---

<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).

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 14, 2017, 12:28pm UTC](https://root-forum.cern.ch/t/analogous-of-ttreereader-for-writing/24852/4 "2017-06-14T12:28:10Z")

</div>

Hi,

ROOT 6.10 is out: [https://root.cern.ch/content/release-61000](https://root.cern.ch/content/release-61000)  
As it was correctly stressed in the previous post, TDataFrame easily allows to:

1. Select events (Filter transformation)
2. Define new columns in the dataset (Define transformation)
3. Save all or a few of the columns (branches) in a new tree (SnapShot action)

In a nutshell, supposed to have a tree with 2 branches, b1 and b2. Suppose you want to select some events according to some cut and you want to create an additional column. Suppose you want to save the result as a new dataset.

```auto
int MyMacro()
{
   // We prepare an input tree to run on
   auto fileName = "myinputfile.root";
   auto outFileName = "myoutputfile.root";
   auto treeName = "myTree";

   // We read the tree from the file and create a TDataFrame.
   ROOT::Experimental::TDataFrame d(treeName, fileName);

   // ## Select entries
   // We now select some entries in the dataset and save the intermediate TDataFrame
   auto d_cut = d.Filter("b1 % 2 == 0");
   // ## Enrich the dataset
   // Build some temporary columns: we'll write them out
   // One column is defined with a lambda, one with a jitted C++ string
   auto d2 = d_cut.Define("b1_square", "b1 * b1")
                          .Define("b2_vector",
                              [](float b2) {
                                 std::vector<float> v;
                                 for (int i = 0; i < 3; i++) v.push_back(b2*i);
                                    return v;
                              },
                            {"b2"});
   // ## Write it to disk in ROOT format
   // We now write to disk a new dataset with one of the variables originally
   // present in the tree and the new variables.
   // The user can explicitly specify the types of the columns as template
   // arguments of the Snapshot method, otherwise they will be automatically
   // inferred.
   d2.Snapshot(treeName, outFileName, {"b1", "b1_square", "b2_vector"});

```

All the tutorials can be found here: [https://root.cern/doc/master/group\_\_tutorial\_\_tdataframe.html](https://root.cern/doc/master/group __tutorial__ tdataframe.html)

Cheers,  
D
