# 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:** 1\
**Showing post:** 5

<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*/);

```

---

_[View the full topic](https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865)._
