How to read a tree with a split-level branch?

I want to see if I am thinking about this correctly:

Let’s consider the following example:

We have a tree with a branch named Events that stores events of class MyEvent. In this class, there are member functions RunId(), EventId(), etc.

When the data is written to the branch Events, a split-level is performed so that mRunId, mEventId, etc, are all stored in sub-branches.

Now we have a tree MyTree with a branch named Events and within the branch Events, we have sub-branches . One sub-branch has all the mRunId values, another sub-branch has all the mEventId value, etc.

Would this be the right way to process a branch from MyTree ?

TChain* eventChain = new Chain("MyTree")

eventChain -> Add(eventFile.root) // Assuming that other files will be chained too.

MyEvent* event = 0;
eventChain->SetBranchAddress("Events", &event)

eventChain->GetEvent(1)

event->RunId() // Where RunId() is a member function of the class MyEvent and it returns the value `mRunId`
event->EventId() // Where EventId() is a member function of the class MyEvent and it returns the value `mEventId`

I am providing this example in an attempt to see if:

a) I need to assign a pointer to each sub-branch,

b) or if I can still have one pointer of class MyEvent and all the information from the sub-branches can be accessed using one pointer, in this case the pointer would be event, since one entry of the sub-branches corresponds to all the data members of class MyEvent and the pointer event is of type MyEvent .

Can you please let me know if I am thinking about reading the tree correctly, and if either my interpretation in a or b is correct? If not, what is the correct way to go about it?

Hi Bassam,

Your assumptions are correct (modulo typos in the code sample). You only need the MyEvent pointer, ROOT populates the class members from sub branches automatically. If your MyEvent class contains many more members and you are only interested in reading a few of them, you can use the SetBranchStatus() method in order to optimize the performance by reading only necessary branches. Like so

eventChain->SetBranchStatus("*", 0); // disable all sub branches
eventChain->SetBranchStatus("mRunId", 1);  // but enable mRunId
eventChain->SetBranchStatus("mEventId", 1); // as well as mEventId

Note that the indexing starts with 0, so in your code sample you’d read the second event.

Cheers,
Jakob

1 Like

Thank you very much! I really appreciate the detailed response!