Convert root file to txt, csv file

Hi all, I would like to convert the root file to a more intuitive way of storing data, such as txt, csv file format, can anyone solve this problem, thank you very much (Linux, Ubuntu 16.04, ROOT 6.10/02)

Hi,

Welcome to the ROOT community!

I do not think ROOT provides something out of the box to achieve that, but it should be enough to read your binary ROOT files in Python or C++ to then create the ASCII file in the format you see fit.
I just feel compelled to write that, while you can easily achieve human readable files, you will unavoidably end up with an I/O system which is far less efficient than the one natively provided by ROOT.

Best,
Danilo

You can easily get a readable text version of some ROOT objects using dedicated methods like Print() (for histograms and graph) or Scan() for trees. This is useful to visually check the data but, as Danilo said, not recommended for storage (specially histograms and tress).

some (loosely related) ideas:

there’s a simple root2csv command in the groot package (a Go-based set of libraries and commands).

see:

See also

https://root.cern/doc/master/df026__AsNumpyArrays_8py.html

import ROOT

df = ROOT.RDataFrame("mytree", "myfile.root")
npy_arrays = df.AsNumpy()

You can then easily output any type of trivial data format from the numpy arrays.

Also,

(Linux, Ubuntu 16.04, ROOT 6.10/02)

This is an ancient system and ROOT version, I highly suggest you update.

Cheers,
Vincenzo

1 Like

Can anybody help me out how to form a TFrame ? such that to save the data in ascii ?
Till now I have .root file which contains a Photon spectrum graph .But I am unable to convert .root file to .ascii or .csv or .dat file .

“Because a canvas is a ROOT object, it can be saved into a ROOT file as any other ROOT object (or user class inheriting from the common base ROOT class). However, it is also possible to automatically generate and save the C++ code that would produce the same result.” This is from ROOT Manual . So how to generate c++ code automatically ?

On the canvas window, just click on the menu File → Save As… and give it a name with a .C extension (e.g. “myfile.C”); or with File → Save and choose the one with .C (in this case the name is taken from the name of the TCanvas); or, in code, c->SaveAs("myfile.C"); (where c is the pointer to the canvas).

Thanks @dastudillo , I saved the .C file successfully . So now can you suggest how to proceed further to get a .csv or .dat or .ascii file to get the data of the graph ?

https://root.cern/doc/master/classTGraph.html#a88e093c3929ce11977b9e3d085e23793
e.g.

gr->SaveAs("mygraphdata.csv");

Thanks @dastudillo .
Do i need to add this in same file .C ?

void TGraph::SaveAs ( const char * filename = graph,
Option_t * option = ) const

Though I tried and error was prompted in terminal.

error :
In file included from input_line_51:1:
/home/dipa/root/bin/Canvas_1.C:152:17: error: definition or redeclaration of ‘SaveAs’ not allowed inside a function
void TGraph::SaveAs ( const char * root = “graph”,
~~~~~~~~^
/home/dipa/root/bin/Canvas_1.C:153:26: error: expected ‘;’ at end of declaration
Option_t * bin = “,” )
^
;
How to resolve this ?

Thanks

I gave you above the only line you need to add.
Full example:

 {
  TCanvas *c1 = new TCanvas("c1","c1",500,400);

  const Int_t n = 20;
  Double_t x[n], y[n];
  for (Int_t i=0;i<n;i++) {
    x[i] = i*0.1;
    y[i] = 10*sin(x[i]+0.2);
    printf(" i %i %f %f \n",i,x[i],y[i]);
  }
  TGraph *gr1 = new TGraph(n,x,y);
  gr1->SetMarkerStyle(21);
  gr1->SetTitle("Graph");
  gr1->GetXaxis()->SetTitle("X title");
  gr1->GetYaxis()->SetTitle("Y title");

  gr1->Draw("ACP");
  gr1->SaveAs("graph_data.csv");
}

and compare the screen printout from this example to the file graph_data.csv

1 Like

Thank you @dastudillo for the help.
The above example helped to resolve my program errors .Thank you .