Evaluate expression during runtime on per event basis

Hi Rooters,

I am searching for a while now if it is possible to evaluate a expression given at initialisation for each event.

e.g. a pseudocode example:

  TTreeReader tr( "ntuple", file );
  TTreeReaderValue<bool> Expr(tr , "value < 300");
  while(tr.Next()){
     if(*Expr) do something
  }

Since it is possible with TTree->CloneTree for a complete tree I was thinking that it is also somehow possible on a per event basis.

Thanks!

Hi,
two straightforward ways I can think of in C++:

  • with TTreeReader
TTreeReader tr("ntuple", file);
TTreeReaderValue<int> v(tr, "value");
auto expr = [](int x) { return x < 300; };
while (tr.Next())
  if (expr(*v))
    doStuff();
using namespace ROOT::Experimental;
TDataFrame df("ntuple", "file.root");
df.Filter("value < 300").Foreach(doStuff);

TDataFrame uses TTreeReader under the hood and has one notable advantage which is easy parallel execution: you just need to call ROOT::EnableImplicitMT() before constructing the TDataFrame object.
Hope this helps, just ask if you have more questions.

Cheers,
Enrico

1 Like

Thanks Enrico!

I already came across TDataFrame. It should give what I am looking for but right now I am not able to update to 6.10 without some testing of my code.

However, the first solution should do the trick.
But i think I do not gain much compared to just using if-statements right?

My idea was to do such selections without to much overhead of defining variables and such (by simply defining a string as in TTree->CloneTree ) since I am usually evaluating multiple and more complex logical expressions.

Thanks again for the answer!

But i think I do not gain much compared to just using if-statements right?

Well I am just using if statements :smiley: That’s pretty much the fastest single-thread code you can write.
I agree it can get verbose, that was one of the main reasons to introduce TDataFrame.

Or if you don’t mind a bit of a performance hit you can switch to python and pyROOT – pyROOT syntax is more terse

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