TTreeFormula in Loop()

Hi,

I use a TTreeFormula to evaluate a cut in Loop() in the code

from tree->MakeClass() like so:

[code]void tree::Loop()
{ if (fChain == 0) return;

TTreeFormula *cutf= new TTreeFormula(“cutf”,“x>0”,fChain);

Int_t nentries = Int_t(fChain->GetEntriesFast());
Int_t nbytes = 0, nb = 0;
for (Int_t jentry=0; jentry<nentries;jentry++) {
Int_t ientry = LoadTree(jentry);
if (ientry < 0) break;

  if ( cutf->EvalInstance() <= 0 ) continue;
  nb = fChain->GetEntry(jentry);   nbytes += nb;
}

}
[/code]

And then I run this piece of macro:

  TChain *chain= new TChain("tree");
  chain->Add("rtree1.root");
  chain->Add("rtree2.root");
  gROOT->LoadMacro("tree.C");
  tree t(chain);
  t.Loop();

It got segmentation violation when there is more than one file
in the chain. How do I solve this problem? I use 3.10/02.

Chih-hsiang

Hi,

The TTreeFormula object needs to be refreshed anytime the file change.
Hence after around LoadTree you have to do something along the line of

Int_t previous = fChain->GetTreeNumger(); Int_t ientry = LoadTree(jentry); if (ientry < 0) break; Int_t current = fChain->GetTreeNumger(); if (previous != current) cutf->UpdateFormulaLeaves();

Cheers,
Philippe.