Hello,
I have a need to process a tchain by individually processing each of the trees myself (I want to do it this way because I will be producing an output file for each individual tree in the chain). But when I try to process the trees individually, the indexed friend tree I have attached to my tree does not work as expected. I’ve prepared a minimal code example that I believe demonstrates my problem.
Create a simple proxy (called it friendProxy.C):
double friendProxy() {
Int_t fVar = friendTree.friendVar;
Int_t mVar = mainVar;
Info("friendProxy","mVar=%d, fVar=%d",mVar,fVar);
return fVar;
}
Now run this:
{
TFile f1("mainTree.root","RECREATE");
TTree *t = new TTree("mainTree","mainTree");
Int_t mVar(0);
t->Branch("mainVar",&mVar);
for(int i=0;i<10;i++) {
mVar = i/2;
t->Fill();
}
t->Write();
f1.Close();
TFile f2("friendTree.root","RECREATE");
TTree *ft = new TTree("friendTree","friendTree");
Int_t fVar(0);
ft->Branch("mainVar",&mVar);
ft->Branch("friendVar",&fVar);
for(int i=0;i<5;i++) {
mVar = i;fVar = mVar*2;
ft->Fill();
}
ft->BuildIndex("mainVar");
ft->Write();
f2.Close();
TChain *c = new TChain("mainTree");
c->Add("mainTree.root");c->Add("mainTree.root"); //added twice to demonstrate
TChain *fc = new TChain("friendTree");
fc->Add("friendTree.root");
fc->BuildIndex("mainVar"); //why do we need to rebuild the index!?
c->AddFriend(fc);
c->MakeProxy("genProxy","friendProxy.C");
TSelector* selector = TSelector::GetSelector("genProxy.h++");
cout << "Process whole chain" << endl;
c->Process(selector);
cout << "Process first tree" << endl;
c->LoadTree(0);
c->GetTree()->Process(selector);
cout << "Process second tree" << endl;
c->LoadTree(10);
c->GetTree()->Process(selector);
}
The processing from the whole chain gives the expected results…
Process whole chain
Info in <genProxy::friendProxy>: mVar=0, fVar=0
Info in <genProxy::friendProxy>: mVar=0, fVar=0
Info in <genProxy::friendProxy>: mVar=1, fVar=2
Info in <genProxy::friendProxy>: mVar=1, fVar=2
Info in <genProxy::friendProxy>: mVar=2, fVar=4
.....
Info in <genProxy::friendProxy>: mVar=3, fVar=6
Info in <genProxy::friendProxy>: mVar=3, fVar=6
Info in <genProxy::friendProxy>: mVar=4, fVar=8
Info in <genProxy::friendProxy>: mVar=4, fVar=8
But the processing of the first tree alone gives incorrect results:
Info in <genProxy::friendProxy>: mVar=0, fVar=0
Info in <genProxy::friendProxy>: mVar=0, fVar=0
Info in <genProxy::friendProxy>: mVar=1, fVar=0
Info in <genProxy::friendProxy>: mVar=1, fVar=0
Info in <genProxy::friendProxy>: mVar=2, fVar=0
Info in <genProxy::friendProxy>: mVar=2, fVar=0
Info in <genProxy::friendProxy>: mVar=3, fVar=0
Info in <genProxy::friendProxy>: mVar=3, fVar=0
Info in <genProxy::friendProxy>: mVar=4, fVar=0
Info in <genProxy::friendProxy>: mVar=4, fVar=0
And then the processing of the second tree actually causes a crash for me!!??
Can someone please explain how to correctly load up each individual tree for processing?
Thanks,
Will