# How do I print the data to a text file from the output tree?

**URL:** <https://root-forum.cern.ch/t/how-do-i-print-the-data-to-a-text-file-from-the-output-tree/59462>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [May 25, 2024, 9:58am UTC](https://root-forum.cern.ch/t/how-do-i-print-the-data-to-a-text-file-from-the-output-tree/59462 "2024-05-25T09:58:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoongi\_Min](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yoongi_min/32/36630_2.png) [@Yoongi\_Min](https://root-forum.cern.ch/u/Yoongi_Min)\
**Post date:** [May 25, 2024, 9:58am UTC](https://root-forum.cern.ch/t/how-do-i-print-the-data-to-a-text-file-from-the-output-tree/59462/1 "2024-05-25T09:58:02Z")

</div>

Hello, I need help.  
I need to print out a txt file with the values X Y Z Plane Bar Position Time. The data is taken from the output tree. Each event’s time of flight will have a start and end point with coordinates (X Y Z Plane Bar Position Time) (I have a sample result similar to what I want).  
I’ve tried and tried to follow so many similar things like that, but couldn’t get any.  
Please help me to get it.

 ![f6f3065e-1a8d-4d59-b2a3-35b092d9abc1](https://root-forum.cern.ch/uploads/default/original/3X/4/b/4b5a336e181aec206548381a42665225787ba3b6.jpeg)  
 ![8bacb85d-cf79-4ff3-aeb8-727e9a9cdc73](https://root-forum.cern.ch/uploads/default/original/3X/3/e/3e0b0bdc4e58293c4d98be08a2d8f3dd31d90ceb.jpeg)

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [May 25, 2024, 12:51pm UTC](https://root-forum.cern.ch/t/how-do-i-print-the-data-to-a-text-file-from-the-output-tree/59462/2 "2024-05-25T12:51:29Z")

</div>

Hi,

Welcome to the ROOT Community.  
There are several ways in which you can achieve this.  
One could be via [RDataFrame::Foreach](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#ad2822a7ccb8a9afdf3e5b2ea321886ca). Suppose to have a file called `myfile.root` containing a TTree `tree` containing 3 columns `x`, `y`, and `z` of type `float`:

```cpp
ROOT::RDataFrame d("tree", "myfile.root");
d.Foreach([](float x, float y, float z){ cout << x << " " << y << " " << z << endl;},{"x", "y", "z"});

```

In PyROOT, that could be:

```py
import ROOT
myfile = TFile("myfile.root")
for evt in myfile.tree:
    print("%s %s %s" %(evt.x, evt.y, evt.z))

```

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Yoongi\_Min](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yoongi_min/32/36630_2.png) [@Yoongi\_Min](https://root-forum.cern.ch/u/Yoongi_Min)\
**Post date:** [May 25, 2024, 2:09pm UTC](https://root-forum.cern.ch/t/how-do-i-print-the-data-to-a-text-file-from-the-output-tree/59462/3 "2024-05-25T14:09:07Z")

</div>

Thank you so much

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [May 25, 2024, 2:26pm UTC](https://root-forum.cern.ch/t/how-do-i-print-the-data-to-a-text-file-from-the-output-tree/59462/4 "2024-05-25T14:26:24Z")

</div>

You can also achieve it using TTree::SetScanField(0); TTree::Scan()  
See [ROOT: TTreePlayer Class Reference](https://root.cern.ch/doc/master/classTTreePlayer.html#a52ff279e3339188632e5eb0fd9b5e371)
