TTree: Sum/average across entries


imagine you have a TTree, each entry is one event, and you would like to know the average value of a certain branch across all entries (say average MET), without writing a loop.

I suspect there is some smart thing to pass to TTree::Draw(), but I couldn’t figure it out. (I only found ways to get the sum or average of several quantities within the same entry.)

Is this possible without writing a loop, and if so, what’s the best way to do this?


TTree::Draw is also doing it by iterating over the whole TTree in a loop…it’s just pre-compiled to be able to use TTreeFormulas so it’s faster in CINT and PyROOT. If you write a macro that deactivates all branches except the one that you want, then “manually” loops over the tree to take the average, it should be as fast as TTree::Draw.

Using TTree::Draw, you can draw a histogram of the branch you want, then the histogram’s mean will be sort of close to the actual mean of the data, if your binning is appropriate and you don’t have overflows.

Note: I am not a real ROOT expert, so there might be an unbinned way using TTee::Draw, but I don’t think so.


Hi Jean-François,

Right, TTree::Draw has to make use of a loop as well. The motivation of my question was that I would like to write compact code and not write stuff in my application that is unnecessary. That’s advantageous both for the cleanliness of the code as well as the speed of writing.

What you wrote is what I suspected. Does anybody else have ideas?