TCut in event loop

Hello,

I’d like to use a TCut inside an event loop but it looks there is no easy/simple way. Few info below:

  • I can’t use a fChain->Draw(“>>eventlist”,aCut) because I need all events and then do different things if an event pass or not the cut
  • I found a suggestion in a very old thread (more than 20 years ago) here [0] and it’s ok but the execution time increase a lot and I wonder if it depends on the complexity of the cut or it’s just not an efficient solution.
  • I know RDataFrame could be an option but it looks to me that because I need to calculate many quantities and, depending on the success or not of the cut, do so many things, is not really a good option.

So I wonder if there is a different approach because I don’t want to implement 20 or more if in my loop and it would be great if there is a more simple and above all efficient way to just check if an event pass a cut or not.

Thanks for helping

Attilio

[0] TCut objects as arguments

Just to give you some number… if I use the solution given in [0] above, for 200K loops it takes 180s while if I copy & paste the TCut expression taken from the GetTitle method (it’s a string longer than 400 characters), it takes less than 1 sec… so the solution given in [0] is definitely not useful. So back to the original question: is there any other method?

Attilio

Hi,

do you mind elaborating a bit on what exactly you are trying to achieve? I take it you have a TTree (or a TChain) and you want to select its entries that satisfy a TCut expression? And what do you mean exactly by

I can’t use a fChain->Draw(">>eventlist",aCut) because I need all events

? What “all events”?

Hi

I have a TChain and as you correctly understood, I’d like to do something like this:

TCut aCut("some expression");
for (Long64_t i = 0 ; i < fChain->GetEntries() ; ++i) {
    fChain->GetEntry(i);
    if(aCut) { do something... }
    else { do something else... }
}

When there is no else condition I could simply use fChain->Draw(">>eventlist",aCut) and get a list of events which pass the aCut cut and loop on the list. That’s easy, but if I have the else condition what can I do?

I hope now is more clear.

Ciao

Attilio

Try to play with the “TTreeFormula::EvalInstance”, e.g.:

{
  TFile *f = TFile::Open("hsimple.root");
  TTree *t = f->Get<TTree>("ntuple");
  TTreeFormula tf1("tf1", "i%100", t);
  TTreeFormula tf2("tf2", "px*px + py*py + pz*pz > 0.9", t);
  TTreeFormula tf3("tf3", "random < 0.1", t);
  Long64_t n = t->GetEntries();
  for (Long64_t j = 0; j < n; j++) {
    t->GetEntry(j);
    std::cout << j << " : "
              << tf1.EvalInstance() << " : "
              << tf2.EvalInstance() << " : "
              << tf3.EvalInstance() << std::endl;
  }
}

Perfect! Thanks a lot. This is exactly what I need (with this solution 200K loops take less than 1 sec).

Ciao

Attilio