# Making cuts on ntuple data

**URL:** <https://root-forum.cern.ch/t/making-cuts-on-ntuple-data/37121>\
**Category:** Newbie\
**Created:** [December 9, 2019, 11:45am UTC](https://root-forum.cern.ch/t/making-cuts-on-ntuple-data/37121 "2019-12-09T11:45:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luiz\_iniciante](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/b9e5f3/32.png) [@luiz\_iniciante](https://root-forum.cern.ch/u/luiz_iniciante)\
**Post date:** [December 9, 2019, 11:45am UTC](https://root-forum.cern.ch/t/making-cuts-on-ntuple-data/37121/1 "2019-12-09T11:45:39Z")

</div>

Hello Fellow Rooters,  
When plotting histograms, it’s easy to make cuts on ntuple data. However, extracting a subset of ntuple data is surprisingly more complex and one basically has to loop over all the data.  
The workman-like code attached extracts a 2-dimensional plot from a 3-D plot along the plane (X==3 && Y==3). Two questions occurred to me while working on it:

1. Is there a way to improve the code and achieve the same goal more efficiently/elegantly?

2. If I wish to extract the 'i’th X-value from my nTuple “test\_bench”, the code goes something like:

This seems somewhat odd to me. I first point to the ‘leaf’ where the data is stored, then I point to the ‘branch’, then I point to the ‘entry’. I would have expected that the proper order would have been: ‘Branch’-\>‘Leaf’-\>‘Entry’. Can someone kindly explain?

Regards,  
Ricardo[test\_code.zip](https://root-forum.cern.ch/uploads/short-url/8MW8zOrCcmyKwf9FgRBNhVrcOnX.zip) (166.1 KB)

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [December 9, 2019, 1:48pm UTC](https://root-forum.cern.ch/t/making-cuts-on-ntuple-data/37121/2 "2019-12-09T13:48:18Z")

</div>

Hi,

There are several ways to extract data from an ntuple. The new recommended way is to use the RData frame, which is the most efficient since it provides code optimised for your machine, using all your available cores and it provides for you the event loop.  
See [https://root.cern/doc/master/classROOT\_1\_1RDataFrame.html](https://root.cern/doc/master/classROOT_1_1RDataFrame.html)

Concerning your questions about TLeaf and TBranch, maybe @pcanal can answer for you

Best Regards

Lorenzo

---

<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:** [December 9, 2019, 5:51pm UTC](https://root-forum.cern.ch/t/making-cuts-on-ntuple-data/37121/3 "2019-12-09T17:51:12Z")

</div>

In you case, you can simplify the code with either RDataFrame or

```auto
Z_D->Draw("D:Z","X==3 && Y==3");

```

or

```auto
TBranch *X_branch = nullptr;  
TBranch *Y_branch = nullptr;  
TBranch *D_branch = nullptr;  
TBranch *Z_branch = nullptr;  

test_bench->SetBranchAddress("X", &X_value, &X_branch);
test_bench->SetBranchAddress("Y", &Y_value, &Y_branch);
test_bench->SetBranchAddress("Z", &Z_value, &Z_branch);
test_bench->SetBranchAddress("D", &D_value, &D_branch);

```

and since you are reading all the branches

```auto
test_bench->GetEntry(i); // it is also fine to call each branch's GetEntry

```

> I would have expected that the proper order would have been: ‘Branch’-\>‘Leaf’-\>‘Entry’

You can also do the search that way. Using the other way around is practical when you are not sure of the branchname.

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![luiz\_iniciante](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/b9e5f3/32.png) [@luiz\_iniciante](https://root-forum.cern.ch/u/luiz_iniciante)\
**Post date:** [December 10, 2019, 1:53am UTC](https://root-forum.cern.ch/t/making-cuts-on-ntuple-data/37121/4 "2019-12-10T01:53:25Z")

</div>

Thanks @pcanal and @moneta. Your informative replies demonstrate why this subsection of the forum is so useful for us newbies.

Regards,  
RL
