However, I am not sure why inVec_ptr above has to be a pointer though (sorry I am pretty new to C++ and ROOT). As I understand it, to retrieve a double, say, one would write
Thanks for the interesting question. The modern and preferred way to treat columnar datasets, i.e. TTrees, is through RDataFrame. It allows you to treat datasets, without bothering with the details, in a high level but yet very performant way.
Below, you find an example code that uses RDataFrame starting from the rootfile you can create with your write function above:
Just for completeness: with RDataFrame, it’s also possible to create datasets. Below I try to propose a RDataFrame based version of your write function:
void write()
{
const auto nEvts = 1;
auto myIdx = 0;
auto df = ROOT::RDataFrame(nEvts);
df.Define("my_branch",
[&myIdx](){myIdx++;return TVector3(myIdx, myIdx + 1, myIdx + 2);})
.Snapshot("my_tree", "out.root");
}
Of course, the data is not very meaningful: hopefully the example is useful.
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 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