How to evaluate a formula for a single tree event?

Hallo,
using the CloneTree method I want to read in a file and write out a file with a subset of the entries.
This is nicely explained in the tutorial copytree3.C. It is basically something like[code] TTree *newtree = oldtree->CloneTree(0);

for (Int_t i=0;i<nentries; i++) {
oldtree->GetEntry(i);
if (event->GetNtrack() > 605) newtree->Fill();
event->Clear();
}[/code]But when I want another test in the if statement, I always have to edit the macro. Is it possible to have a string parameter char* formula passed to the macro and then test for each event something likeif (oldtree->EvaluateFormula(formula)) newtree->Fill();This would be similar to the formula interpretation in TTree::Draw. Is this possible?

Hi,

You have two choice, you can make the selection in 2 passes. One where you use TTree::Draw directly to create a TEntryList and then iterate of this list rather than each and every entry. (This solution may end up being faster as it is likely to result in less data being read in total). The 2nd option is to use TTreeFormula (the engine behind TTree::Draw directly).

TTree *newtree = oldtree->CloneTree(0); TTreeFormula *formula = new TTreeFormula("form01",formula_text,oldtree); oldtree->SetNotify(formula); // This is needed only for TChain. for (Int_t i=0;i<nentries; i++) { oldtree->LoadEntry(i); // Does not read any data yet (TTreeFormula will read what it needs). Bool_t selected = kFALSE; for(Int_t j = 0; j < formula->GetNdata(); ++j) { if ( formula->EvalInstance(j) ) { selected = kTRUE; break; } } if (selected) { oldtree->GetEntry(i); if (event->GetNtrack() > 605) newtree->Fill(); } event->Clear(); }Note that this code support for the formula to be either a single number of many numbers for each entry and that if there many numbers (i.e. the formula is about the Track of the event), it ‘assumes’ that you want any event where at least one of the (Track/Object) matches.

Cheers,
Philippe.

Dear Philippe[quote]Note that this code support for the formula to be either a single number of many numbers[/quote]You mean a branch with an array or TClonesArray or the like. So, the TTreeFormula mechanism can support also this, but the TEntryList cannot, if I understood you correctly.
This is even better than I’ve hoped. Now I can do really fancy things. :wink:

Very much helped!
Werner

Hi,

[quote]You mean a branch with an array or TClonesArray or the like. So, the TTreeFormula mechanism can support also this, but the TEntryList cannot, if I understood you correctly[/quote]Per se you can do exactly the same things with both solutions since the goal in your case in to filter entries, but indeed the 2nd solution gives you a bit more control over the looping.

Cheers,
Philippe.