Problem using TChain TEntryList & TBranch together

All -

I am having problems using the combination of TChain, TEntryList (and/or TEventList) & TBranch together in the same application. I have seen similar topics discussed in the past and most of time, it seems that the answer is referred to this help page:

http://root.cern.ch/root/html/TEntryList.html

But I tried to follow this example, and my code still does not seem to work as I think it should. The high level objective of the code is fairly generic: I wanted to use TChain to process more than one file (all contain the same tree structure). I wanted to build a entrylist (or eventlist) with some cuts and I wanted to use TBranch::GetBranch to view the results. After many trials, I have settled on the following code, which sort of works, but has some flaws (which I will explain later):

  TChain* ch = new TChain("bin");

  ch->Add("tree1.root");
  ch->Add("tree2.root");

  // ...

  ch->Draw(">>list1", cut);

  TEventList* list1 = (TEventList*)gDirectory->Get("list1");

  Long64_t N = list1->GetN();

  ch->SetEventList(list1);

  Float_t param[10];        //  1

  Long64_t j, j1;

  for (Long64_t i = 0; i < N; ++i) {
    j = list1->GetEntry(i);
    j1 = j + ch->GetTreeOffset()[0];
    ch->GetBranch("param")->SetAddress(param);
    ch->GetEntry(j1);
    printf("%lld\t%f\n", i, param[0]);
  }

This code works, but there’s a slight problem. It won’t fetch the branch data for the first event, ie, the event correspond to i=0. I need to do something like this to make it work:

  Float_t param[10];        //  1

  Long64_t j, j1;

  j = list1->GetEntry(0);
  j1 = j + ch->GetTreeOffset()[0];
  ch->GetBranch("param")->SetAddress(param);
  ch->GetEntry(j1);

  for (Long64_t i = 0; i < N; ++i) {
    j = list1->GetEntry(i);
    j1 = j + ch->GetTreeOffset()[0];
    ch->GetBranch("param")->SetAddress(param);
    ch->GetEntry(j1);
    printf("%lld\t%f\n", i, param[0]);
  }

Here’s something else that I tried, which I think follows the example in the above quoted tutorial (uses the TChain::LoadTree & TBranch::GetEntry combination), but it completely does not work (I need to change the above eventlist to entrylist):

  Float_t param[10];        //  1
  TBranch* param_b = ch->GetBranch("param");
  param_b->SetAddress(param);

  Long64_t j, j1;

  for (Long64_t i = 0; i < N; ++i) {
    j = list1->GetEntryAndTree(i, 0);
    j1 = j + ch->GetTreeOffset()[0];
    ch->LoadTree(j1);
    param_b->GetEntry(j);
    printf("%lld\t%f\n", i, param[0]);
  }

There are many other combinations that I initially tried. Mostly I find that if you move the TBranch::GetBranch outside the event loop, the code won’t work. The reason I first started with this is that my most previous codes do not use eventlist or entrylist, and in those cases, it doens’t matter the GetBranch is inside or outside the event loop. If there’s only one file in the TChain, then it seems that doesn’t matter what combination I tried, most seem to work fine.

So for some reasons, if the TChain has more than one file, and you also use TEntryList and TBranch to view events, I am having a very hard time to get things working. My files are very large and bumping against the 2GB limit, so I can’t use TChain::Merge to combine them. Another thing I tried is to use TChain::Scan to view the events in Eventlist/EntryList, that seem to work (don;t have to use TBranch is this case). So I don;t think there’s any issues with the tree files I have.

any help will be appreciated !

quan zhu

Hi,

WHen using a TChain avoiding dealing with TBranch directly. Use:

and loop with: for (entry=firstentry;entry<firstentry+nentries;entry++) { entryNumber = fTree->GetEntryNumber(entry); if (entryNumber < 0) break; Long64_t localEntry = fTree->LoadTree(entryNumber); if (localEntry < 0) break;

Cheers,
Philippe.