How to convert .root to .txt file?


ROOT Version: 6.10
Platform: Ubuntu 16.04
Compiler: Not Provided


I got some ‘dump.root’ file and want to change the information to the text file.
The program I used, which is ‘GATE’, recommend me to use the commands in the followings:

///////////////////////////////////////////////////////////////////////////////////

//
// Name this file "dump.cxx" and use as:
//
// root [0] .x dump.cxx(); > dump.txt
//
// Produces "dump.txt" and "dump.xml" files.
//

void dump(const char *fname = "dump.root",
          const char *nname = "ntuple")
{
  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("dump.xml");
  
  delete f; // no longer needed (automatically deletes "t")
}

//////////////////////////////////////////////////////////////////////////////////////

It DIDN’T work and got some errors.

And I checked the similar question asked by someone before. But I couldn’t solve.

the similar question link:

the error I got

What is the problem??

I sincerely appreciate your attention.

Do you mean to convert root graphs data into .dat/.txt file?

If yes then I do the following.

I use the following codes (attached tar file) to convert my histograms in .dat format

Steps:

  1. $ tar xvzf browser.tar.gz
  2. $ cd browser
  3. $ make

This will make an executable named general. Copy the executable in the directory where you have the root file
4) $ ./general

Another window will open. in that window do the following,
5) Use the browser button to locate, open and draw the histogram.
6) Use ASCII button to create the ascii dat (graph.dat)

browser_ubuntu.tar.gz (6.1 KB)

alternatively, you could use go-hep.org/x/hep/rootio/cmd/root-dump, from Go-HEP.

examples:

$> root-ls graphs.root 
=== [graphs.root] ===
version: 60806
TGraph            tg      graph without errors         (cycle=1)
TGraphErrors      tge     graph with errors            (cycle=1)
TGraphAsymmErrors tgae    graph with asymmetric errors (cycle=1)

$> root-dump graphs.root
>>> file[graphs.root]
key[000]: tg;1 "graph without errors" (TGraph)
BEGIN YODA_SCATTER2D /tg
Path=/tg
Title=graph without errors
Type=Scatter2D
# xval	 xerr-	 xerr+	 yval	 yerr-	 yerr+
1.000000e+00	0.000000e+00	0.000000e+00	2.000000e+00	0.000000e+00	0.000000e+00
2.000000e+00	0.000000e+00	0.000000e+00	4.000000e+00	0.000000e+00	0.000000e+00
3.000000e+00	0.000000e+00	0.000000e+00	6.000000e+00	0.000000e+00	0.000000e+00
4.000000e+00	0.000000e+00	0.000000e+00	8.000000e+00	0.000000e+00	0.000000e+00
END YODA_SCATTER2D

key[001]: tge;1 "graph with errors" (TGraphErrors)
BEGIN YODA_SCATTER2D /tge
[...]

$> root-ls gauss-h1.root 
=== [gauss-h1.root] ===
version: 60806
TH1D    h1d     h1d     (cycle=1)
TH1F    h1f     h1f     (cycle=1)
TH1F    h1d-var h1d-var (cycle=1)
TH1F    h1f-var h1f-var (cycle=1)

$> root-dump gauss-h1.root
>>> file[gauss-h1.root]
key[000]: h1d;1 "h1d" (TH1D)
BEGIN YODA_HISTO1D /h1d
Path=/h1d
Title=h1d
Type=Histo1D
# Mean: 2.812016e-02
# Area: 1.100600e+04
# ID	 ID	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
Total   	Total   	1.100600e+04	1.211000e+04	3.094905e+02	7.128989e+04	10004
Underflow	Underflow	2.000000e+00	2.000000e+00	0.000000e+00	0.000000e+00	2
Overflow	Overflow	4.000000e+00	8.000000e+00	0.000000e+00	0.000000e+00	2
# xlow	 xhigh	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
-4.000000e+00	-3.200000e+00	6.600000e+00	7.260000e+00	0.000000e+00	0.000000e+00	6
-3.200000e+00	-2.400000e+00	7.260000e+01	7.986000e+01	0.000000e+00	0.000000e+00	66
-2.400000e+00	-1.600000e+00	5.434000e+02	5.977400e+02	0.000000e+00	0.000000e+00	494
-1.600000e+00	-8.000000e-01	1.708300e+03	1.879130e+03	0.000000e+00	0.000000e+00	1553
-8.000000e-01	2.220446e-16	3.130600e+03	3.443660e+03	0.000000e+00	0.000000e+00	2846
0.000000e+00	8.000000e-01	3.136100e+03	3.449710e+03	0.000000e+00	0.000000e+00	2851
8.000000e-01	1.600000e+00	1.753400e+03	1.928740e+03	0.000000e+00	0.000000e+00	1594
1.600000e+00	2.400000e+00	5.401000e+02	5.941100e+02	0.000000e+00	0.000000e+00	491
2.400000e+00	3.200000e+00	1.012000e+02	1.113200e+02	0.000000e+00	0.000000e+00	92
3.200000e+00	4.000000e+00	7.700000e+00	8.470000e+00	0.000000e+00	0.000000e+00	7
END YODA_HISTO1D

key[001]: h1f;1 "h1f" (TH1F)
[...]

it should work with most of TTrees as well.

you can install it (once you have Go) like so:

$> go get go-hep.org/x/hep/rootio/cmd/root-dump

otherwise, I have compiled it for a couple of OS/Arch here:

hth,
-s

hummm … in ROOT 6, the usage of this script should be:

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

The .x dump.cxx(); > dump.txt part is a retired interface supported only in ROOT 5 and older.

Alternative use the following script:

// Name this file "dump.cxx" and use as:
//
// root [0] .x dump.cxx("dump.txt")
//
// Produces "dump.txt" and "dump.xml" files.
//

void dump(const char *outputname = "dump.txt",
          const char *fname = "dump.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")
}

Cheers,
Philippe.

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