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