Reading second or following files from TChain returns broken data

I have a set of root files with a tree and branches inside. Every branch is a class derived from TObject which contains a TClonesArray object where I store my final data.

If I use ROOT CLI to test it, eg:

TChain c("M").
c.Add("file1.root); c.Add("file2.root); c.Add("file3.root);
c.Draw("MGeantTrack.data.start.E")

all works fine and I see proper energy spectrum.

But when I try to use the same files from the application, where I read the chain using TChain::GetBranch() and TChain::SetAddress() and followed by getEntry(i), only data from the first file are OK. In my class I have function which can print all the members of the class, and when printing last two events of the first file the data are correct:

pos sta=(-63.860748,45.852509,-26.533522) sto=(-27.821383,43.458836,30.946054)
dir sta=(0.530840,-0.035504,0.846728) sto=(0.531200,-0.034308,0.846552)
start energy = 5382.276855, stop energy = 5082.808105
scat=0 process=
PID=1000110230 stop in det=1
num of sec=0

pos sta=(-66.399200,50.302349,16.030731) sto=(600.000000,-177.096909,-74.180840)
dir sta=(0.938943,-0.320097,-0.126192) sto=(0.938686,-0.320444,-0.127215)
start energy = 27827.259766, stop energy = 24863.156250
scat=0 process=
PID=1000150310 stop in det=0
num of sec=0

But every data from the second or later file gives always fixed data, always the same, like below:

pos sta=(-29.940292,91.304390,28.868572) sto=(462.300171,-327.365082,-600.000000)
dir sta=(0.545874,-0.464139,-0.697565) sto=(0.545903,-0.464326,-0.697417)
start energy = 84140.382812, stop energy = 83883.398438
scat=0 process=
PID=1000050110 stop in det=0
num of sec=0

Any idea where the difference in behaviour may come from?

Can you provide a small example of the problem you are experiencing?

It’s hard to provide one single file since the whole code is within a bigger framework I am working now. But here I extracted an essential part of the framework and analysis program and the input files. Please download this file:
http://rlalik.com/media/upload/mapt.tar.bz2
and extract in, then do following:

cd mapt
mkdir build
cd build
cmake … #(dot dot, markup language translates two dots into three…, the same line below)
make
./analysis/mapt_analysis …/files/CR_*_digi.root -e 20000

You will observe that the first 10k events will be processed slower than the following 10k.

After that, you can edit line 150 of analysis/analysis.cc and uncomment it. It will print out the information about each track,you will find the problem. I recommend to run the analysis program from line above with | less on the end.

Please ignore the exception error on the end. It’s irrelevant for this case.

Mostly likely you are not refreshing the TBranch pointer value when the file changed over. Use

Int_t TChain::SetBranchAddress 	( 	const char *  	bname,
		void *  	add,
		TBranch **  	ptr = 0 
	) 		
	virtual

Set branch address.

Parameters
    [in]	bname	is the name of a branch.
    [in]	add	is the address of the branch.
    [in]	ptr	Note: See the comments in TBranchElement::SetAddress() for a more detailed discussion of the meaning of the add parameter.

IMPORTANT REMARK:

In case TChain::SetBranchStatus is called, it must be called BEFORE calling this function.

See TTree::CheckBranchAddressType for the semantic of the return value.

Reimplemented from TTree.

Definition at line 2374 of file TChain.cxx.

i.e

TBranch *branchPtr = nullptr;

mychain->SetBranchAddress(branchname, pointerToDataPointer, branchPtr);

This was my thought as well. Didn’t have time to dig through your code though.

That was it. Before I had:

TBranch * br = tree->BetBranch(…);
br->SetAddress(…);

Now it works fine.

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