# Selecting events in TTree

**URL:** <https://root-forum.cern.ch/t/selecting-events-in-ttree/52228>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [November 5, 2022, 4:28pm UTC](https://root-forum.cern.ch/t/selecting-events-in-ttree/52228 "2022-11-05T16:28:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [November 5, 2022, 4:28pm UTC](https://root-forum.cern.ch/t/selecting-events-in-ttree/52228/1 "2022-11-05T16:28:56Z")

</div>

Hello,

I am curious if there is an easier/faster way to get selection from a TTree.  
What I currently do:

I have a function makes average of waveforms for a given standard class that I work with. However, I like to make some selection in the waveforms before adding it to the average. Besides, I’ve different cuts for different data sets. To be able to use the same function aways, I work in the following way:

```auto
class MyDataClass{

  public:
  static const Int_t npts = 1000;
  Double_t wvf[npts];

};

void makeAverage(string selection = ""){
   TFile *f = new TFile("mydata.root","READ");
   TTree *t1 = (TTree*)f->Get("mytree");

   MyDataClass data;
   TBranch *b = t1->GetBranch("data");
   b->SetAddress(&data);

   for(Int_t i = 0; i < t1->GetEntries(); i++){
     
     if (selection != ""){
       Int_t nselec = t1->Draw("1",Form("Entry$==%d && %s",i,selection.c_str()),"goff");
       if(nselec == 0) continue;
     }
     b->GetEvent(i); 
   }

   // from here, actually make average... etc
}

```

Doing like that allows me to call any selection I want. Using `Entry$ == ` makes things much faster.

Is there any faster or better way to make this sort of selection?

Cheers,  
Henrique

* * *

---

<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:** [November 9, 2022, 9:05am UTC](https://root-forum.cern.ch/t/selecting-events-in-ttree/52228/2 "2022-11-09T09:05:51Z")

</div>

Hi Henrique,

sorry for the high latency.

If I understand correctly, for each event in the TTree you are looping over the whole TTree again, filling a histogram with just one entry and checking it has more than zero entries. Indeed this is extremely wasteful.

If the cut must be a valid `TTree::Draw` selection as a string, something you could with TTree do is apply the cut once (before the explicit loop over entries) with `t1->Draw(">>entrylist", cut)`. This creates a list of entries `entrylist` that satisfy the cut in a single loop over the TTree. Then you can loop again over just the entries in `entrylist` to do the rest of the processing.

In general I would recommend looking at [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) where you would express your logic like this:

```cpp
ROOT::RDataFrame df("mytree", "mydata.root");
df.Filter("x > 0").Foreach(processSomeColumns, {"x", "y", "z"});

```

P.S.

I guess it should be `t1->GetEntries()` (as a function call) in `for(Int_t i = 0; i < t1->GetEntries; i++)`?

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [November 10, 2022, 1:21am UTC](https://root-forum.cern.ch/t/selecting-events-in-ttree/52228/3 "2022-11-10T01:21:50Z")

</div>

Dear Enrico,

Thank you for the reply!

That is the solution I was looking for! Thanks.  
(I do not understand properly RDataFrame as I never used it, I will look better in the future)

The solution with “Entry$==” is still fast when the selection is not strong, because the time of calling “Entry$==1” or “Entry$==20000” is the same. However, there are the annoying warning messages when there is no event in the graph, which can make things much slower.

I’ve corrected the typo, thanks.

Cheers,  
Henrique

---

<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:** [November 10, 2022, 8:52am UTC](https://root-forum.cern.ch/t/selecting-events-in-ttree/52228/4 "2022-11-10T08:52:06Z")

</div>

I think you get the same time with “`t1->Draw(...)`” for any “`Entry$`” because it internally always runs through all tree entries (so inside of your own “`for`” loop over events, for each of them, it internally runs another one).

You could easily improve it, I think:

```cpp
Int_t nselec = t1->Draw("1", selection.c_str(), "goff", 1, i); // just the entry "i"
if (nselec < 1) continue; // -1 in case of error, 0 in case selection not fulfilled

```

Maybe @couet can give a better way (unfortunately, there is no “Cut” method in [TTree](https://root.cern/doc/master/classTTree.html)).

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 10, 2022, 9:16am UTC](https://root-forum.cern.ch/t/selecting-events-in-ttree/52228/5 "2022-11-10T09:16:06Z")

</div>

I have no better solution.

---

<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:** [November 10, 2022, 9:24am UTC](https://root-forum.cern.ch/t/selecting-events-in-ttree/52228/6 "2022-11-10T09:24:27Z")

</div>

> [@hvsouza](#):
>
> The solution with “Entry$==” is still fast

I would strongly suggest to refactor things so that you don’t have to execute a `TTree::Draw` call for each entry in the tree. As @Wile_E_Coyote says, this runs another full event loop for each entry in the TTree, so it’s way, way slower than it could be.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [November 10, 2022, 3:20pm UTC](https://root-forum.cern.ch/t/selecting-events-in-ttree/52228/7 "2022-11-10T15:20:21Z")

</div>

Thank you very much! Indeed this is a much better approach.

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [November 10, 2022, 3:31pm UTC](https://root-forum.cern.ch/t/selecting-events-in-ttree/52228/8 "2022-11-10T15:31:55Z")

</div>

Hi Enrico,

I’ve change for the TEventList 🙂

I tested the method with an empty selection:  
`t1->Draw(">>entrylist", "")`  
and the time was basically the same as just calling `b->GetEvent(i)`

Thanks again.
