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

**URL:** <https://root-forum.cern.ch/t/how-to-export-the-data-inside-a-branch-in-a-ttree-to-a-txt-file/51399>\
**Category:** Newbie\
**Tags:** tree, hist, root\
**Created:** [August 30, 2022, 1:09pm UTC](https://root-forum.cern.ch/t/how-to-export-the-data-inside-a-branch-in-a-ttree-to-a-txt-file/51399 "2022-08-30T13:09:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [August 30, 2022, 1:09pm UTC](https://root-forum.cern.ch/t/how-to-export-the-data-inside-a-branch-in-a-ttree-to-a-txt-file/51399/1 "2022-08-30T13:09:13Z")

</div>

Hi,

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 !

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 30, 2022, 1:51pm UTC](https://root-forum.cern.ch/t/how-to-export-the-data-inside-a-branch-in-a-ttree-to-a-txt-file/51399/2 "2022-08-30T13:51:30Z")

</div>

Hi @gini ,

with [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) it’s pretty easy (I haven’t tested the code but it should give you an idea):

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

```

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [August 30, 2022, 2:05pm UTC](https://root-forum.cern.ch/t/how-to-export-the-data-inside-a-branch-in-a-ttree-to-a-txt-file/51399/3 "2022-08-30T14:05:15Z")

</div>

Hi Enrico,

Thanks for the reply!

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

 ![error](https://root-forum.cern.ch/uploads/default/original/3X/f/0/f0f2261c9ef08ec90fb6c30d1551113a00727f92.png)

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”.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 30, 2022, 2:09pm UTC](https://root-forum.cern.ch/t/how-to-export-the-data-inside-a-branch-in-a-ttree-to-a-txt-file/51399/4 "2022-08-30T14:09:36Z")

</div>

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](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) and the corresponding [tutorials](https://root.cern/doc/master/group __tutorial__ dataframe.html).

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 30, 2022, 2:24pm UTC](https://root-forum.cern.ch/t/how-to-export-the-data-inside-a-branch-in-a-ttree-to-a-txt-file/51399/5 "2022-08-30T14:24:49Z")

</div>

Also, [this post](https://root-forum.cern.ch/t/dump-ttree-in-a-txt-file/29069) explains how to do it with [TTree::Scan](https://root.cern/doc/master/classTTree.html#af8a886acab51b16d8ddbf65667c035e4).
