Retrieving TVector3 from a Tree

Hi @unmovingcastle,

it’s designed like the because ROOT needs to support general types in TTrees and avoid unnecessary copies.

Assume you have a TVector myVec when reading, instead of a point. Then, when getting the TTree entries, ROOT would have to copy-assign to this myVec, from the TVector inside the TTree, right? That would cause some overhead, and only support types that are copy-assignable.

So instead ROOT expects you do do this:

  TVector3*inVec_ptr=nullptr;

  inTree->SetBranchAddress("my_branch",&inVec_ptr);
  inTree->GetEntry(0);

So what’s going on? You create some pointer to a TVector3, and then you pass it to ROOT by pointer (e.g. ROOT gets a pointer to a pointer to a TVector). This allows ROOT to modify the pointer when you call GetEntry() to not be a nullptr anymore, but to point to the actual object in the TTree for the corresponding entry.

For “primitive” types like floating point, integer and bool types it’s different yes. You have to pass a pointer to the variable directly and then ROOT will update the variable when loading the entries. That’s because the situation is different there: there types are so small that the values can be quickly copied, while using also this updating of pointers would have the larger overhead.

I hope this taught you something about C++ and ROOT :slight_smile:

Cheers,
Jonas

1 Like