TCut in a TTreereader loop

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


is there any way to do something like this :

x , y : tree branches variables

TCut cut(x*y<1);

TTreeReader loop:

if (cut.IsPass()) do something


Any idea is welcome,
Thank you!

Hi @panagiotis_bellos,
with TTreeReader you don’t really need to use a TCut, you can directly write (not tested but should give you the idea):

TFile f("f.root");
TTreeReader r("tree", &f);
TTreeReaderValue<float> x_reader(r, "x");
TTreeReaderValue<float> y_reader(r, "y");
while(r.Next()) {
  float x = *x_reader;
  float y = *y_reader;
  if (x * y < 1)

If you have ROOT v6.14 (latest release), RDataFrame offers a nicer syntax to accomplish pretty much the same thing:

ROOT::RDataFrame df("tree", "f.root");
df.Filter("x*y < 1").Foreach(do_something);

where do_something is the name of some function that you want to execute for each event that passes the filter. More RDF tutorials here.

If you really have to use a TCut object in conjuction with a TTreeReader: I don’t think there is a direct way to do that, but you should be able to produce a TEntryList from a TCut using TTree::Draw and then you can use the TEntryList with TTreeReader.


Hi Enrico,

thanks for your reply. Actually I would like to create an array of cuts and apply them through an iteration. It is much more flexible than changing them inside the reader loop each time. I tried to find out how the TCut works in the TTree->Draw() function but is seems quite complex.

The RDataFrame solution seems to be very convenient.


note that the strings passed to RDataFrame must be valid C++ code, they do not follow any other special syntax other than the fact that you can use branch names as variables. But yes, in general RDataFrame is pretty flexible.

Using TTree::Draw to fill a TEntryList is actually quite simple: tree.Draw(">>yplus","y>0") fills a TEntryList called yplus with the entries for which y > 0 is true. You can read more at the TTree::Draw docs (search for " Saving the result of Draw to a TEventList, a TEntryList or a TEntryListArray").


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