Convert root file to csv

Hi
I want to convert .root file to .csv file.
at first I used "rootls -t ./cT.root " in the terminal and it worked.
Then I use “root2csv -o out.csv -t tree -f ./cT.root” but it did not and send a error “root2csv: command not found”. How can I fix it?
My file is attached.
cT.root (20.0 KB)

I don’t think ROOT/C++ has a root2csv command.

but groot does:

but it seems to me the ROOT file you’ve linked above doesn’t contain any TTree (just a TCanvas that itself contains a TGraph)
root2csv won’t work with a TCanvas.

$> root-ls -t ~/Downloads/cT.root
=== [/home/binet/Downloads/cT.root] ===
version: 62606
TCanvas cT      Multiplication (cycle=1)

Thanks.
ok. How can I convert it to .csv file?

with ROOT/C++, you would open the ROOT file, get the TCanvas, retrieve the “embedded” TGraph and loop over all the points of that TGraph.
then, for each point of that TGraph:

auto cnv = (TCanvas*)_file0->Get("cT");
auto g = (TGraph*)cnv->FindObject("Graph0");
const int N = g->GetN(); // number of points in graph
for (int i = 0; i < N; i++) {
    double x, y;
    g->GetPoint(i, &x, &y);
    std::cout << x << ";" << y << "\n";
}

hth,
-s

FYI, the next version of Go-HEP (v0.35) should have support for converting TGraph{,Errors,AsymErrors} automatically:

$> root2csv -f ./cT.root -t cT/Graph0 -o output.csv
$> head ./output.csv
## Automatically generated from "./cT.root"
x;y
66.666666632;0
67.18375580845168;0
67.7048557031515;0
68.22999742458441;0
68.75921232252328;0
69.29253198990041;0
69.82998826469353;0
70.37161323182649;0
[...]
1 Like

can you explain that how can I open ROOT file?
Can you explain by details?

Type root in terminal then type

TBrowser a

to create a TBrowser object. It’s a GUI(Graphical User Interface) which will help you to access everything easily.

thank for replying.
I open “TBrowser a” but I can not save the plot data in csv file.
I have a root file and I can plot it. I can not save the x and y of plot.
Can you help me?

Thanks for your replying.
I do it.
I save the data by .c in TBrowser a and get the data.

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