Problem with TChain reset and TEventList

Hello,
I have 2 files with the same TTree and I’m trying to read them in sequenze applying some cut to the events. The problem is that when I try to read the second file the event loop gives the last entry of the first file. I guess using Reset() on the chain is not enough… I wrote a simple macro to show what I get. Could you check what’s wrong with the following code?
Thanks

Attilio

void test() {

  UInt_t event;

  // write first file
  TFile f1("f1.root","recreate");
  f1.cd();
  TTree *fTree1 = new TTree ("test", "test");
  fTree1->Branch("event", &event, "event/i");
  for(int n=0;n<10;n++) {
    event=n;
    fTree1->Fill();
  }
  f1.Write();
  f1.Close();

  // write second file
  TFile f2("f2.root","recreate");
  f2.cd();
  TTree *fTree2 = new TTree ("test", "test");
  fTree2->Branch("event", &event, "event/i");
  for(int n=10;n<20;n++) {
    event=n;
    fTree2->Fill();
  }
  f2.Write();
  f2.Close();

  // define TChain and cut
  TChain * fChain = new TChain("test","");
  TCut odd("event%2!=0");
  TBranch        *b_event;   //!
  fChain->SetBranchAddress("event", &event, &b_event);

  // read first file OK
  fChain->Add("f1.root");
  fChain->Draw(">>eventlist",odd);
  TEventList *elist1 = (TEventList*)gDirectory->Get("eventlist");
  for (Long64_t i = 0 ; i < elist1->GetN() ; ++i) {
    fChain->GetEntry(elist1->GetEntry(i));
    cout << event << endl;
  }
  fChain->Reset();

  // read second file NOPE
  fChain->Add("f2.root");
  fChain->Draw(">>eventlist",odd);
  TEventList *elist2 = (TEventList*)gDirectory->Get("eventlist");
  for (Long64_t i = 0 ; i < elist2->GetN() ; ++i) {
    fChain->GetEntry(elist2->GetEntry(i));
    cout << event << endl;
  }
  fChain->Reset();

}

Macro output:

   ------------------------------------------------------------------
  | Welcome to ROOT 6.26/06                        https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosxarm64 on Sep 24 2022, 11:23:00                   |
  | From tag , 28 July 2022                                          |
  | With Apple clang version 14.0.0 (clang-1400.0.29.102)            |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

root [0]
Processing test.C...
1
3
5
7
9
9
9
9
9
9

I guess @pcanal can help you.

Before I get to the technical answer, why are you using TChain::Reset rather than simply adding both file to the same chain or directly use the TTree from the file?

There is 2 issues with the script mentioned. One is that we have the pattern:

 TChain * fChain = new TChain("test","");
...
  fChain->SetBranchAddress("event", &event, &b_event);
...
  fChain->Reset()
...
     fChain->GetEntry(...);

TChain::Reset (of course) also reset the SetBranchAddress and thus the chain is no longer contect to the value.
Use:

  fChain->Add("f2.root");
  fChain->SetBranchAddress("event", &event, &b_event);

The second issue is that when attempting to retrieve the second TEventList the first one is still list and thus the statement:

  TEventList *elist2 = (TEventList*)gDirectory->Get("eventlist");

actually returns the first list rather than the second one (and thus we have elist1 == elist2.
Instead use

  fChain->Draw(">>eventlist2",odd);
  TEventList *elist2 = (TEventList*)gDirectory->Get("eventlist2");

(and/or delete the first TEventList)

Hi pcanal,
thanks for solving the problem. The solution is actually adding

  fChain->SetBranchAddress("event", &event, &b_event);

after the reset of the first TChain. The problem of the elist1==elist2 was a minor one (actually in my original macro the 2 TEventList were different). Finally, I can’t add the 2 files in a single chain because the 2 files contains the same events with different value for just few branches (its a systematic error study) so I need to run independently on the 2 chains to fill 2 histos with the same variable to compare the difference.

Thanks again for helping

Attilio

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.