Get list of Entries in a TTree that matches a specific cut,

Hi everybody,

what I’m trying to do is take a list of entries in my TTree that passes trough a specific selection. Is there anyway to do this? C++ or Python code is fine.

Thanks,

Lucas

Search for “entrylist” and / or “eventlist” in the TTree::Draw method description.

And how can I put this list in a variable?

There are some examples in the “Using a TEventList, TEntryList or TEntryListArray as Input” paragraph therein.

I don’t want to use as an input for the tree itself, but to something else that doesn’t use the ttree or root file. I just want the list of the entries.

Hi lmendes,
if you have access to ROOT v6.10/02 (the latest release, or 6.10), TDataFrame might help you.
This is very crude, but it might be a good starting place to come up with something that suits your needs:

#include "ROOT/TDataFrame.hxx"
using namespace ROOT::Experimental;

int main() {
  TDataFrame d("treename", "file.root");
  std::vector<ULong64_t> valid_entries;
  Long64_t current_entry = -1l;
  d.Filter([&current_entry] { current_entry++; return /*your cut goes here instead of*/true; }) 
   .Foreach([&valid_entries, &current_entry] { valid_entries.push_back(current_entry); });

  return 0;
}

Here I create a TDataFrame (an object that offers a high-level interface to loop over your data and manipulate it), define a Filter (which updates the current_entry and then returns true if the entry passes your filter) and then Foreach entry that passes the Filter I push it back in the vector of valid_entries. The short user guide linked above might help clarify any doubts – I am defining Filter and Foreach using a c++11 lambda.

I would love to have a simpler answer to your simple question actually. I will think about it.

Notes:
What I do in the Foreach call is not thread-safe so do not try it to run this in multi-threading :slight_smile:
It also only works for single files, where the entry number simply goes up from 0 to N one by one.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.