# Applying a cut when filling a TTree

**URL:** <https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865>\
**Category:** ROOT\
**Created:** [March 30, 2013, 6:07pm UTC](https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865 "2013-03-30T18:07:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arekfu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arekfu/32/27537_2.png) [@arekfu](https://root-forum.cern.ch/u/arekfu)\
**Post date:** [March 30, 2013, 6:07pm UTC](https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865/1 "2013-03-30T18:07:41Z")

</div>

Hello ROOTers,

I wrote a program that fills a ROOT TTree with the events from a Monte Carlo simulation. Assume the tree contains the following variables:`Short_t n
Float_t x[n]
Float_t y[n]
`I’m not interested in the whole tree, but only in a small subset of events. The events are identified by some condition on the tree variables, e.g.`sqrt(x)<=1./y`which however is user-defined, i.e. known only at run time. I’d like to be able to fill the tree only with the events, or parts of events, that fulfil the user-defined condition.

Ideally I’d like to do something like`
// assign tree branches to variables n, x, y
n = 3;
x[0] = 1.0; x[1] = 1.0; x[2] = 1.0;
y[0] = 10.0; y[1] = 0.1; y[2] = 0.01;
TString condition;
std::cin >> condition;
tree->Fill(condition);`and that would fill the tree with the following event:`n=2
x[0]=1.0 y[0]=0.1
x[1]=1.0 y[1]=0.01`Or, if this is impossible, I could fill the tree only if any of the array items satisfies the relevant condition.

Is there any way to realize that? Perhaps I could fill a temporary tree and copy over only the relevant bits, but I’m not sure how to do that.

Cheers,  
Davide

---

<div class="post-metadata">

**Author:** ![arekfu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arekfu/32/27537_2.png) [@arekfu](https://root-forum.cern.ch/u/arekfu)\
**Post date:** [April 2, 2013, 3:46pm UTC](https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865/2 "2013-04-02T15:46:38Z")

</div>

I partially solved this by using a “buffer” tree.`// define tree and tree branches here
TTree *bufferTree = tree->CloneTree();`When I want to add a row, I add it to bufferTree:`bufferTree->Fill();`From time to time, I copy the relevant events over to `tree' and I reset`bufferTree’:`TTree *tempTree = bufferTree->CopyTree(condition);
tree->CopyEntries(tempTree);
delete tempTree;
bufferTree->Reset();`At the end of the simulation, I only write `tree’ to the output file.

This works, but it seems unnecessarily cumbersome. One could avoid creating a temporary tree if TTree::CopyEntries allowed to specify some selection, like TTree::CopyTree does.

---

<div class="post-metadata">

**Author:** ![rooter\_03](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rooter_03/32/19119_2.png) [@rooter\_03](https://root-forum.cern.ch/u/rooter_03)\
**Post date:** [June 28, 2016, 7:23pm UTC](https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865/3 "2016-06-28T19:23:21Z")

</div>

Hi,

I am interested on this too. I think it would have been very helpful to have the possibility of introducing a cut as an argument of the TTree::Fill() function, so that the tree gets filled only if the condition is satisfied. Another way of doing this could be adding:

[code] //\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
TTree c\_tree(“ctree”,“ctree”);  
c\_tree.Branch(“a”, &a);  
c\_tree.Branch(“b”, &b);

```
c_tree.Fill();

TTreeFormula form("tform", cut, &c_tree);

auto value = form.EvalInstance(i_entry);
// **************************************

 if (value)
   tree.Fill();
   [/code]

```

before filling the tree. However this means that one would have to make the branches all over again, normally tens of them, so probably the solution above is better.

I tried to obtain c\_tree as a clone of tree, but it seems that the branches of the clone are not linked to the variables a and b but some version of them inside tree, so that everytime I do c\_tree.Fill() I fill with the wrong number.

Cheers.

---

<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:** [May 29, 2017, 7:45pm UTC](https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865/4 "2017-05-29T19:45:09Z")

</div>

The upcoming TDataFrame interface might be a good direction for this kind of applications (See its snaphot feature in particular)

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 18, 2017, 9:53pm UTC](https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865/5 "2017-06-18T21:53:08Z")

</div>

Hi, assuming you have your tree in a file on disk, since v6.10 you can solve your problem with [TDataFrame](https://root.cern.ch/doc/master/classROOT_1_1Experimental_1_1TDataFrame.html), as pcanal suggested. The following code is not tested but should work about fine for branches containing a single float:

```auto
ROOT::Experimental::TDataFrame d("myTree", "file.root"); // instantiate TDF
d.Filter("sqrt(x) <= 1./y") // only consider events that fulfill this condition
 .Snapshot("skimmedTree", "output.root", {"n", "x", "y"}); // write to disk

```

For more complicated filter expressions you can use c++11 lambdas (or normal c++ functions). Here is a filter applied to branches `x` and `y` containing arrays of floats:

```auto
using arr_view_f = std::array_view<Float_t>;
auto myCut = [](const arr_view_f& x, const arr_view_f& y, int n) {
   /* your logic goes here, e.g. */
  auto xysum = 0;
  for(auto i = 0; i < n; ++i) xysum += x[i] + y[i];
  return xysum > 0;
}; 
d.Filter(myCut, {"x", "y", "n"}).Snapshot(/*as before*/);

```

---

<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:** [June 18, 2017, 10:10pm UTC](https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865/6 "2017-06-18T22:10:44Z")

</div>

Try:  
rooteventselector --help
