Creating and using TVector3's in a TTree

Hi all, new to ROOT and my first post so please excuse anything I ask which may seem trivial.

I am having some issues in trying to put TVector3’s in a TTree and then accessing later. After searching around here, (I think) I determined from this thread ([url]Filling a TVector3 into a TTree that I want to fill the Tree as such:

TVector3* r1 = new TVector3; TFile *f = new TFile("VDB.root","RECREATE"); TTree *tree = new TTree("VDB","VDB Tube Positions"); tree->Branch("r1","TVector3",&r1);
where my object r1 is a pointer.

However I was also a little confused when comparing this to the tutorial hvector.C )http://root.cern.ch/root/html/tutorials/tree/hvector.C.html) where a a different type of vector, std::vector, is filled in a tree. There the object there is created by:

std::vector<float> vpx; TTree *t = new TTree("tvec","Tree with vectors"); t->Branch("vpx",&vpx);
where vpx is not a pointer. Are these two types of vectors completely different such that you need to use a pointer for one but not the other? Some clarification would be nice.

My other problem comes when I try to read this tree in a different MACRO. What I want to be able to do is read the TVector3 out of the TTree and then use normal operators (+,-,=, etc.) with other TVector3’s initiated in the new MACRO. I am unsure of the proper way to do this, and any way I have tried so far has resulted in a segmentation violation. Here is what I’ve tried which I thought had the best potential

TVector3* r1; TChain *chain = new TChain("VDB"); chain->Add("VDB.root"); chain->SetBranchAddress("r1",&r1);
This gives a segmentation violation and presents a problem when I want to add this to a TVector3 from that MACRO since now r1 is a pointer. I also tried doing this where r1 is not a pointer, again sementation violation.

Thanks for the help.

[quote]Are these two types of vectors completely different such that you need to use a pointer for one but not the other? Some clarification would be nice.[/quote]This code fragments are example of 2 of the possible syntax and are both useable for all types. [quote]TVector3* r1;
TChain *chain = new TChain(“VDB”);
chain->Add(“VDB.root”);
chain->SetBranchAddress(“r1”,&r1);[/quote]You must initialize the pointer:TVector3* r1 = 0; // or = new TVector3();otherwise the result in unpredictable.

[quote]I also tried doing this where r1 is not a pointer, again sementation violation.[/quote]SetBranchAddress does not yet support the not pointer form.

Cheers,
Philippe.

Thanks for the clarification on the two types of syntax.

Got the code to work from initializing as suggested.

Thanks again.