Trouble ttrees and tvector3s

This code, when typed line-by-line into the interpreter, works fine:

	TFile* tf = new TFile("/apps/HallB/workspace/data/eppi0eta.root", "READ");
	TTree* tt = tf->Get("tt");
	
	TVector3 Rtgt;	
	TBranch* b_Rtgt = tt->GetBranch("Rtgt");
	b_Rtgt->SetAddress(&Rtgt);
	b_Rtgt->GetEntry(0);

But when wrapped into a macro and executed, it segfaults. I can get the macro to work if I use this code instead:

	TFile* tf = new TFile("/apps/HallB/workspace/data/eppi0eta.root", "READ");
	TTree* tt = tf->Get("tt");
	
	TVector3* Rtgt = new TVector3();	
	TBranch* b_Rtgt = tt->GetBranch("Rtgt");
	b_Rtgt->SetAddress(&Rtgt);
	b_Rtgt->GetEntry(0);

This problem does not occur for me when I use TLorentzVectors. I have yet to see any hard and fast rules for what should be passed to TBranch::SetAddress() – I can cite examples of addresses of stack-allocated variables, pointers, and references to pointers all being passed to TBranch::SetAddress(). How should I be using it, and why does it behave differently for different objects?

[quote]I have yet to see any hard and fast rules for what should be passed to TBranch::SetAddress() [/quote]That’s because it strongly depends on the content of the branch. For top level branches containing objects, the parameter has to be the address of a pointer to the object.
Hence your second code is correct while the first one is not.
I recommend using TTree::SetBranchAddress instead of TBranch::SetAddress since the TTree method (in the latest version of ROOT) contains extra code to verify that the address being passed is of an acceptable type.

Cheers,
Philippe.

PS. The fact that you first code work line by line is most likely cheer luck.