ROOT file -> txt format

Hi,

I want to convert root file into text file,
Does anyone know how to convert a ROOT file to text format?

Thanks a lot

A ROOT file can contain many types of objects, so you have to be more specific on what you want to export. In any case, there are many examples on this forum, just search for the specific thing you want to export, e.g. Search results for 'export histogram to text' - ROOT Forum

depending on what the ROOT file contains, root2csv, root2yoda or root-dump may be suitable:

root2csv is suitable for converting simple ROOT TTrees into CSV data.
root2yoda can convert TH{1,2} histograms into their YODA form.
root-dump can display almost every thing into a somewhat human format.

these commands are available from the Go-HEP project:

hth,
-s

Hi,
Thank you for your reply. I want to convert the TTree root file as Edep and interaction coordinates in the root file as ntuples and histograms to an .txt or .Ascii file.
Thanks alot for help

Spring

You’re generally better off using the ROOT file format rather than ASCII text which another tool then needs to read again… If you “insist” in doing this you can simply use tree->Scan() and redirect the output, or write a little script that prints the column values that you care about.

@eguiraud that mightbe a nice feature for RDataFrame, too: Print() printing all or the given column values, maybe for every n-th entry, or the first n entries. Especially with Define() etc this could be nice!

Done! That’s Display :slight_smile:

1 Like

Thanks for the reply, I need a script to convert all root data to a text file to convert to txt format

I ran the following script in ROOT 6,

.> dump.txt
.x dump.cxx
.>

But its txt output was empty, it only contained the following:

[0mroot [2] .x dump.cxx
[0m [0mroot [3]

I do not know why it creates the .txt file but it does not write the data of the root file, can anyone help?

++++++++++++++++++++++++

//
// Products “dump.txt” and “dump.xml” files.
//

void dump (const char * outputname = “dump.txt”,
const char * fname = “output.root”,
const char * nname = “ntuple”)
{
if (! outputname! (* outputname)! 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);
((TTreePlayer *) t-> GetPlayer ()) → SetScanRedirect (kTRUE);
((TTreePlayer ) t-> GetPlayer ()) → SetScanFileName (outputname);
t-> Scan ("
");
t-> SaveAs (“dump.xml”);

delete f; // no longer needed (automatically deletes “t”)
}

+++++++++++++++++++++++

First, list the contents of your file:
[...]$ rootls -l yourfile.root

rootls -l output.root

I typed in the terminal showed the following error:

Traceback (most recent call last):
File “/ usr / local / bin / rootls”, line 10, in
import cmdLineUtils
ImportError: No module named cmdLineUtils

Did not display list the contents of my file.

Then try:
[...]$ root -b -l -q yourfile.root -e 'gFile->ls();'

Thank you very much for your reply, it is now showing the content:

Attaching file output_t0.root as _file0 …
(TFile *) 0x55eb9649e640
TFile ** output_t0.root
TFile * output_t0.root
KEY: TDirectoryFile ntuple; 1 ntuple

Now how do I put this content in a txt file?

Try:
[...]$ root -b -l -q output_t0.root -e 'gFile->cd("ntuple"); gDirectory->ls();'

Attaching file output_t0.root as _file0…
(TFile ) 0x55f8915f8650
TDirectoryFile
ntuple ntuple
KEY: TTree ntuple_1;1 physical_stage
KEY: TTree ntuple_2;1 chem_stage

Try (creates “dump.txt” and “dump.xml”):
[...]$ root -b -l -q 'dump.cxx("dump.txt", "output_t0.root", "ntuple/ntuple_1")'

Processing dump.cxx(“dump.txt”, “output_t0.root”, “ntuple/ntuple_1”)…
File <dump.txt> created
Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Info in TTree::SaveAs: ROOT file dump.xml has been created

I have no idea where these “basket's WriteBuffer failed” errors come from (maybe @pcanal could guess).
But, at least, you now have some “dump.txt” and “dump.xml” to play with.

Thank you very much for your valuable guidance, the physical stage in ntuple1 contains 5 branches, do you think this error is not because ntuple 1 branches are not considered?

This (writing a TTree directly in xml format) is not supported.

@pcanal TObject::SaveAs should explicitly say “not supported” instead of issuing mysterious errors.