How to export the data inside a branch in a ttree to a txt file?


I have a ttree. I want to export a particular branch (need to have the X and Y data points) inside the TTree say “Mass” into a txt file.

Can someone tell me how to do this ?

Thanks in advance !

Hi @gini ,

with RDataFrame it’s pretty easy (I haven’t tested the code but it should give you an idea):

ROOT::RDataFrame("Mass", "inputfile.root")
  .Foreach([](float x, float y) { std::cout << x << "," << y << '\n'; },
           {"x", "y"});


Hi Enrico,

Thanks for the reply!

When I try to implement your suggestion I get an error :

What I have is a root file " RAW158OS_treeFF01.root" and inside it I have 3 different TTree’s. From that I want to export the data from “Mass” branch which is inside one of the ttree called “Analysis_fis”.

That code was meant to be written all in one statement, it’s not valid C++ if you split it in two.
See also the RDataFrame user guide and the corresponding tutorials.


Also, this post explains how to do it with TTree::Scan.