Home | News | Documentation | Download

Sum of scalar branches in a TTreeFormula

Hi all,
I was wondering if there is a simple way to do the following operation from a TTreeFormula. Ideally I have a scalar branch, one entry per event, and I would like to do something like

// assumes other code to load variable_in_tree
float sum_var=0;
for (int i =0; i<N_total_events_in_tree; i++ ) sum_var+= variable_in_tree;

In other words, I would like to be able to have the equivalent of the pseudocode above in an interactive ROOT session in order to retried the sum of all values that the variable_in_tree takes in my tree.

Thanks in advance,
Nicola

Hi Nicola,
welcome to the ROOT forum!

Not sure with TTreeFormula, but with RDataFrame in an interactive session:

root[0] ROOT::RDataFrame df("treename", "filename.root");
root[1] *df.Sum("variable_in_tree");

Cheers,
Enrico

Hi Enrico,
thank you for the reply. That is pretty convenient !
Cheers,
Nicola