Fill then use a TEntryList several time

Dear experts,

I wrote a function with a TChain* as argument (among others), in which I fill a TEntryList* using a TCut, then I loop over the entries of the TEntryList* in the TChain* to compute a weight and fill a histogram (took examples from the documentation) :

tree->Draw(">>skim",cut,"entrylist");
TEntryList *skim = (TEntryList*)gDirectory->Get("skim");
int nEntries = skim->GetN();
tree->SetEntryList(skim);

int treenum, iEntry, chainEntry;
treenum = iEntry = chainEntry = -1;

for(Long64_t i=0 ; i<nEntries ; i++) {
  iEntry     = skim->GetEntryAndTree(i, treenum);
  chainEntry = iEntry + (tree->GetTreeOffset())[treenum];
  tree->GetEntry(chainEntry);
  [... compute a weight and fill a histogram ...]
}
  1. Is it the correct way to generate the TEntryList* and then to use it to loop over chain entries ?

  2. As I call this function many times to process many TChain* using many different TCuts, at the second call a segmentation violation occurs, when trying the gDirectory->Get.

I guess this is due to recording several times in a row a TEntryList* under the same name, so how should I do this ?

Best regards

HI,

I recommend you use:

tree->Draw(">>skim",cut,"entrylist");
TEntryList *skim = (TEntryList*)gDirectory->Get("skim");
int nEntries = skim->GetN();
tree->SetEntryList(skim);

int treenum, iEntry, chainEntry;
treenum = iEntry = chainEntry = -1;

for(Long64_t i=0 ; i<nEntries ; i++) {
   iEntry = skim->GetEntryAndTree(i, treenum);
   tree->LoadTree(treenum);
   chainEntry = iEntry + (tree->GetTreeOffset())[treenum];
   tree->GetEntry(chainEntry);
   [... compute a weight and fill a histogram ...]
}

tree->SetEntryList(0);
delete skim;

Cheers,
Philippe.

From the code of TChain::SetEntryList(0) , it seems it deletes the previously generated entrylist … Is there a way to avoid such frequent delete, and actually keep rewritting generated tentrylist without causing code crash on deletion of TChain ?

You can take ownership of the TEntryList by calling (directly or indirectly)

chain->GetEntryList()->ResetBit(kCanDelete);

Surprisingly, I checked the ownership and the chain is not the owner.
Yet when chain is deleted the code crashes. I am attaching the file
containing the relevant function as full code is very big.
minimal.C (11.0 KB)

Right before the “Draw”, add: ch->SetEntryList(0);