Fill a TTree branch with TVector3

Hi Rooters,

I am trying to fill a branch with an array of TVector3, however it appears that the branch containing TVector3 has entries either 0 or some absurd values.

To reproduce the problem I have attached a simple macro.
Can anyone suggest how to do this ?

Thanks in advance,

tree_with_vector.C (484 Bytes)

The first problem is that if you define a:
t.Branch(“vector”, &vPosition, “vposition[5]”);
then ROOT thinks that this branch is a:
*Br 1 :vector : Float_t vposition[5] *
Note the “Float_t” instead of the desired “TVector3”. See the “==> Case A” in the TTree class description.

root [0] TFile *f = TFile::Open(“test.root”, “read”)
root [1] tree->Print()

I guess you would need a single branch with a std::vector ("==> Case E") or a TClonesArray(“TVector3”) ("==> Case D"), or you would need to create 5 different branches, one branch per each “vposition[i]” (5 * “==> Case C”). You should probably also make sure that “splitlevel = 0”.

Another problem is that you create your tree on the stack “TTree t(…);”, but you’d better create it on the heap “TTree *t = new TTree(…);” (note: do NOT delete your “t” tree manually, it will be automatically deleted when your ROOT file “f” is deleted).

Well, maybe Philippe has any better ideas.

Hi Coyete,
Thanks !!
According to your suggestion I changed the file. I want to have a TVcetor3 (e.g., position) for each trajectory and I was thinking to create only one branch (“vector”) for each track.
I know if I create a branch with “double position[trajno][3]”, I can access those positions for each track. Is not it possible to have one TVector3 corresponding to one track …same like the “number” branch is doing ? So that if I do

tree->Scan(“vector[2]”); it will give information for trajectory number 2.

Another thing, evenif the sub-branches created under TVector3, say fX, I can not access the values for the tracks except 1st one. Now fX[2] is same as fX[0] ??
tree_with_vector.C (657 Bytes)

The line:
means that you will store a SINGLE object pointed by “vPosition” (i.e. ONLY the “vPosition[0]”, see the “==> Case C” in the TTree class description).

Hi Coyete,
A related question to your second reply, when should I mention the “splitlevel=0” in the branch specifically to avoid memory problem ?

This is not clear to me.

Thanks in advance,

What “memory problem” do you mean?

In my code there are several TTree branches with “std::vector<vector>” and 2D arrays for each track and while creating the branches “splitlevel=0” is not mentioned.

And there is memory leak and it seems to me that the source is not pointer.

Those branches can be a reason for memory leak ??

In general, the “splitlevel” should not create memory leaks (it’s related to the way the objects are stored in the ROOT file).
Setting “splitlevel=0” prevents sometimes problems related to “subbranches with identical names” (another way is to add a “.” character at the ends of the names of their master branches).
See ROOT User’s Guide - Chapter 12. Trees - Adding a TBranch to Hold an Object and TTree::Branch.