Problems reading a branch from a chain

Hello,-
I have a problem which is probably caused by my lacking understanding of the
difference between a TTree and a TChain.
I have narrowed the problem down and can reproduce it using a slightly modified version of
the tree2.C example provided my the root installation.
I call the tree2w() method, which writes the tree t2 to the file tree2.root.
Then I make an identical copy of tree2.root to tree3.root.
Next I modify the tree2r() method so it works on a TChain in stead of a TTree.
This works without problems as long as I add only tree2.root.
But when I add two files to the chain (tree2.root and tree3.root) things don’t work.
Here is my modifed tree2r:

void tree2r()
{
TChain* t2 = new TChain(“t2”);
t2->Add(“tree2.root”);
t2->Add(“tree3.root”);

// TFile *f = new TFile(“tree2.root”);
// TTree t2 = (TTree)f->Get(“t2”);

static Float_t destep;
TBranch *b_destep = t2->GetBranch(“destep”);
b_destep->SetAddress(&destep);

TH1F *hdestep = new TH1F(“hdestep”,“destep in Mev”,150,0e-5,3e-5);

//read only the destep branch for all entries
Int_t nentries = (Int_t)t2->GetEntries();
printf(“nentries= %d\n”, nentries );

Int_t nbtot = 0;
for (Int_t i=0;i<nentries;i++) {
// Int_t nb = b_destep->GetEntry(i);
Int_t nb = t2->GetEntry(i);
if ( nb==0 ) {
printf(“nb=%d for i=%d\n”, nb, i );
break;
}
nbtot += nb;
hdestep->Fill(destep);
}

printf(“nBytes read in total: %d\n”, nbtot );

TCanvas *c1 = new TCanvas(“c1”,“c1”,600,800);
hdestep->Draw();
}

What happens when running the above, is that destep is alway 0.
I.e. destep is not correctly filled.
If I change to load only via b_destep->GetEntry(i) I get a segmentaiton violation.

What do I have to add to this example for it to work?

I am running v5.26 64 bit on a MacOsX 10.6.

Best wishes,
-Mogens Dam

Hello again,-
I have a followup on this, which actually gets closer to my real problem.
I exchange the lines in tree2r as follows:

// TBranch *b_destep = t2->GetBranch(“destep”);
// b_destep->SetAddress(&destep);
TBranch *b_destep;
t2->SetBranchAddress(“destep”,&destep,&b_destep);

Now it seems I can successfully read the complete chain when having:
Int_t nb = t2->GetEntry(i);

This returns
nentries= 20000
nBytes read in total: 1118440

But when I try to read only the destep branch, I only succeed to read the first file:
Int_t nb = b_destep->GetEntry(i);

nentries= 20000
nb=0 for i=10000
nBytes read in total: 40000

When attempting to read the first entry of the second file it returns 0
bytes and I bail out.

So, my problem is to read only a single branch from a TChain.
This is important for me, since in my real application I am accessing about 1 TB of data which
I read over the network. Reading only one branch would reduce the network traffic by more
than a factor 1000.

Best wishes,
-Mogens

Hi Mogens,

When using a chain and reading only a branch, use the following synopsis (the issue is that the value you need to pass to the Branch’s GetEntry is not the same as the one you need to pass to the TChain’s GetEntry because the TBranch related to the underlying TTree and not to the TChain):[code]void tree2r()
{
TChain* t2 = new TChain(“t2”);
t2->Add(“tree2.root”);
t2->Add(“tree3.root”);

// TFile *f = new TFile(“tree2.root”);
// TTree t2 = (TTree)f->Get(“t2”);

static Float_t destep;
TBranch *b_destep = 0;
t2->SetBranchAddress(“destep”,&destep,&b_destep);

TH1F *hdestep = new TH1F(“hdestep”,“destep in Mev”,150,0e-5,3e-5);

//read only the destep branch for all entries
Long64_t nentries = t2->GetEntries();
printf(“nentries= %d\n”, nentries );

Int_t nbtot = 0;
for (Long64_t i=0;i<nentries;i++) {
Long64_t localEntry = t2->LoadTree(i);
Int_t nb = b_destep->GetEntry(localEntry);
if ( nb==0 ) {
printf(“nb=%d for i=%d\n”, nb, i );
break;
}
nbtot += nb;
hdestep->Fill(destep);
}

printf(“nBytes read in total: %d\n”, nbtot );

TCanvas *c1 = new TCanvas(“c1”,“c1”,600,800);
hdestep->Draw();
}[/code]

Cheers,
Philippe.