# Convert a root file to a readable txt file

**URL:** <https://root-forum.cern.ch/t/convert-a-root-file-to-a-readable-txt-file/39957>\
**Category:** ROOT\
**Tags:** tree, root\
**Created:** [June 17, 2020, 9:24pm UTC](https://root-forum.cern.ch/t/convert-a-root-file-to-a-readable-txt-file/39957 "2020-06-17T21:24:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zxjessica](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zxjessica/32/19828_2.png) [@zxjessica](https://root-forum.cern.ch/u/zxjessica)\
**Post date:** [June 17, 2020, 9:24pm UTC](https://root-forum.cern.ch/t/convert-a-root-file-to-a-readable-txt-file/39957/1 "2020-06-17T21:24:26Z")

</div>

Hi Everyone,  
I want to convert my root file to a txt file. I followed the instruction [extract data to txt file](https://root-forum.cern.ch/t/how-to-extract-a-data-from-dna-root-file/16182).  
My cxx file is like this：

```auto
void root2txt(const char *fname = "4lyr1.root",
          const char *nname = "Coincidences")
{
  if (!fname || !(*fname) || !nname || !(*nname)) return; // just a precaution
  
  TFile *f = TFile::Open(fname, "READ");
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
  
  TTree *t; f->GetObject(nname, t);
  if (!t) { delete f; return; } // just a precaution
  

  t->SetScanField(0);
  t->Scan("*");

  t->SaveAs("modify.txt");
  // t->SaveAs(TString::Format("%s.xml", nname));
  
  delete f; // no longer needed (automatically deletes "t")
}

```

I generated the txt file successfully, but I couldn’t read it. When I opened the txt file in my laptop, it just shows:  
{  
//========= Macro generated from object: Coincidences/The root tree for coincidences  
//========= by ROOT version6.18/04  
//Primitive: Coincidences/The root tree for coincidences. You must implement TTree::SavePrimitive  
}  
These are not the data that I want. Does anyone can help? How can I generate a readable txt file?  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 17, 2020, 10:11pm UTC](https://root-forum.cern.ch/t/convert-a-root-file-to-a-readable-txt-file/39957/2 "2020-06-17T22:11:16Z")

</div>

Use:

```auto
((TTreePlayer*)(t->GetPlayer()))->SetScanRedirect(true);
((TTreePlayer*)(t->GetPlayer()))->SetScanFileName("modify.txt"); 

```

---

<div class="post-metadata">

**Author:** ![zxjessica](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zxjessica/32/19828_2.png) [@zxjessica](https://root-forum.cern.ch/u/zxjessica)\
**Post date:** [June 17, 2020, 11:06pm UTC](https://root-forum.cern.ch/t/convert-a-root-file-to-a-readable-txt-file/39957/3 "2020-06-17T23:06:21Z")

</div>

Thank you for reply! Do you means add this two lines in the code or do I need to delete something?  
I delete

```auto
  t->SetScanField(0);
  t->Scan("*");
  t->SaveAs("modify.txt");

```

In this way, there are nothing in the txt file. Sorry I am new to ROOT.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 17, 2020, 11:28pm UTC](https://root-forum.cern.ch/t/convert-a-root-file-to-a-readable-txt-file/39957/4 "2020-06-17T23:28:41Z")

</div>

You need to drop the

```auto
t->SaveAs("modify.txt");

```

which overwrite the file with something you don’t want.

---

<div class="post-metadata">

**Author:** ![zxjessica](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zxjessica/32/19828_2.png) [@zxjessica](https://root-forum.cern.ch/u/zxjessica)\
**Post date:** [June 18, 2020, 12:15am UTC](https://root-forum.cern.ch/t/convert-a-root-file-to-a-readable-txt-file/39957/5 "2020-06-18T00:15:34Z")

</div>

Thank you! This works!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 2, 2020, 12:15am UTC](https://root-forum.cern.ch/t/convert-a-root-file-to-a-readable-txt-file/39957/6 "2020-07-02T00:15:37Z")

</div>

Closed on July 2, 2020, 12:15am UTC

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