Perform TCut style cut on single event from TTree

Is there a way to call TTree::GetEntry(I) and then call something like bool TTree::ApplyCut(TCut) or bool TTree::ApplyCut(const char* selection) which would return true if the currently loaded event passes the cut and false if not?

You could then do an event loop like this:

for(Long64_t I=0; I<ptree->GetEntries(); ++I) {
  ptree->GetEntry(I);
  if(ptree->ApplyCut("A<0.&&TMath::Log(B) < 5.")) {
    //Do something here.
  }
}

Hi,

there is a more efficient way to achieve what you describe with TEventList.

mytree->Draw(">>myEventList",cut);
auto myEventList = (TEventList*) gDirectory->Get("myEventList");
auto nentries = myEventList->GetN();
for (Int_t ievt = 0;ievt<nentries;ievt++) {
   auto entry = myEventList->GetEntry(ievt);
   mytree->GetEntry(entry);
   // Do work
}

Cheers,
D

1 Like

No,

the point of my method is to

1.) Avoid the need to attach an explicit local variable to the TTree (i.e. with ptree->SetBranchAddress(blahh…)
2.) Perform multiple cuts for each event, perhaps forming multiple TEntryLists

Like this:

TEntryList list_a("list_a", "list_a");
TEntryList list_b("list_b", "list_b");

for(Int_t ievt = 0; ievt <ptree->GetEntries(); ++ievt) {
  ptree->GetEntry(ievt);
  if(ptree->ApplyCut(cuta) {
    list_a.Enter(ievt);
  }
  if(ptree->ApplyCut(cutb) {
    list_b.Enter(ievt);
  }
}

ptree->SetEntryList(list_a);
//Do stuff with pre-selected TTree

ptree->SetEntryList(list_b);
//Do other stuff with pre-selected TTree

The attractive part about this code, is that all your cuts can be written in plain text, and passed by a text file and hence, it can be easily modified. Of course if you load the branches into local variables it’ll probably be faster since root doesn’t have to compile the cut expression each time, but it’ll also be less flexible.

As far as I can tell, your method doesn’t escape the attaching local variables to branches problem.