Use TTree::Query before the event is written to the tree

Hello

I have a selection criteria to fill a TTree which is passed as a string by the user, for instance:

“signal>0.1 && noise<0.001 && charge>12.3”

The selection can be any combination of the variables in the branches. I would like to know if it is possible, before writing the actual event in the tree to check if the event satisfies the selection. I know this can be done for instance with TTree::Query once the event has already been written to the tree, but I would like to test it before.

If this is not possible, any hints on how to parse this TString and Fill or not the data?

Thank you very much


Please read tips for efficient and successful posting and posting code

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


Hi,

Thanks for the interesting post.
The question you are asking is very generic, and is about transforming something known at runtime, the cut string, into something that deals with code frozen at compile time, the variables associated to your branches.
There are many ways to solve this, none really trivial. For example, you would need some sort of cut parser and some jitting.
This is a small snippet to get you started:

    std::string cutString = "signal>0.1 && noise<0.001 && charge>12.3";
    std::stringstream cppFuncss;
    cppFuncss << "bool cutFunc(double signal, double noise, double charge) {return "
              << cutString
              << ";}";

    // Just in time compile
    gInterpreter->Declare(cppFuncss.str().c_str());

    // Invoke
    auto signal = .3; 
    auto noise = .00001;
    auto charge = 14.;

    std::stringstream invocationss;
    invocationss << "cutFunc(" << signal << ", " << noise << ", " << charge << ");";
    auto keepEvent = gInterpreter->ProcessLine(invocationss.str().c_str());

    std::cout << keepEvent << std::endl;

Mind, the invocation of the function is less than optimal in terms of performance, but there are ways to improve in case this is the direction in which you’d like to go.

I hope this helps getting you started.

Cheers,
Danilo

Hello Danilo,

thank you very much for your answer! I see what you mean.

After the post I thought about a possible solution that I would like to try as well. Since I need a tree to be able to evaluate the selection, I could create a memory resident tree with a circular buffer, setting maxEntries=1. This tree is a Clone of my tree. I create it once outside the main loop, so I do not need to create it for each iteration. Then I fill this memory resident tree with a single event, I ask TTree::Query to tell me whether or not the current event satisfies the selection and Fill the output tree accordngly.

I never used trees with CircularBuffers, I hope this may work…