Operations with TBranch


I would like to operate with TBranches without looping over all the events. The exercise I would like to do is something like:


Is this possible? If so, can I do more complex operations like sqrt,…? My trees have a lot of events and this way I save the time of looping over all of them and summing the values event by event.

Thank you very much


no: think of the branch as a pointer to the data it holds. As such, there’s always only one event that they point to. (And even if it were possible, the loop would still be there, even as it were implicit.)


I will loop over the events then.
Thank you very much for the help.


Hi Wile and Wim,

Thanks for the help. I think that I didn’t explain myself properly in the first post. What I want to do is not operate with branches, is operate with the variables of the branches and save the result in a new variable of a new TBranch in the same Tree.

I managed to use SetAlias and now I am able to operate with the variables but now I don’t know how to save results. For example:


I would like to store “NewVar” as a new variable in the Tree.
I also try:


But I didn’t succeed. Any ideas?


See “ROOT User’s Guide - Chapter 12. Trees - Example 3: Adding Friends to Trees” and search for “Adding a Branch to an Existing Tree” in http://root.cern.ch/root/html/TTree.html.

This isn’t exactly what you asked for, but I’ve gotten away with a lot of TTree->Draw calculations, which keep the loop over all elements in the ROOT side of things, making it much faster. The problem is storing the result. I don’t think you can store the result of a TTree->Draw operation in a branch, but you can direct it to a histogram for example.