TTree Scan: Is it possible to store the scan value as a variable?

I have multiple files containing the same TTree and I am able to output the scan value for that tree as a text file. But I was wondering if it is possible to give the value of scan to a variable as my ultimate goal is to find the sum of all those values.

Dear spatel,

That’s not possible, as far as I know.
To achieve what you would like to achieve traditionally youwould create a small class with TTree::MakeClass and use the Loop function (see doc of TTree::MakeClass).
If you are using 6.14 or at least a version >= 6.10 you can use the new RDataFrame technology with a dedicated lambda function to sum up you variable; we can provide an example if you are willing to try.

G Ganis

I do not need to use this frequently in my current work but I would sure like to streamline the code further.

An example would be very helpful, thanks.

S Patel

Hi @spatel,
to get the sum of a branch into a variable with RDataFrame in ROOT v6.14 you can simply do the following:

#include <ROOT/RDataFrame.hxx>

int main() {
   ROOT::RDataFrame df("treename", "filename.root");
   auto sum = df.Sum("branchname"); // sum is a smart pointer to the result of the branch summation
   std::cout << "sum is " << *sum << std::endl;

   return 0;
}

You can find more resources on RDataFrame here. Tutorials are also available here.

Hope this helps!
Enrico

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.