How can I convert .root file to .txt file?

:smiley: Help required! I need help to convert a root file into text file.

Hi,

this depends very much on the content of the rootfile. In general I would not encourage at all this step as you would loose several benefits such as runtime performance and data compression.

Danilo

[quote=“dpiparo”]Hi,

this depends very much on the content of the rootfile. In general I would not encourage at all this step as you would loose several benefits such as runtime performance and data compression.

Danilo[/quote]

Hi Danilo,

Thank you for your reply. I have for example this (attached, results.root) rootfile, what is the way to convert into text file? I hope you will help me.
results.root (53.9 KB)

Hi,

so you would like to transform a tree created from a txt file (“novaCosmogenic data from given txt file”) in a txt file.
I still don’t think it’s a good idea, but you can transverse the tree easily like this, in Python:

f = ROOT.TFile("results.root")
for e in f.T:
    print e.evtN, e.partID, ...., e.pz

Danilo

[quote=“dpiparo”]Hi,

so you would like to transform a tree created from a txt file (“novaCosmogenic data from given txt file”) in a txt file.
I still don’t think it’s a good idea, but you can transverse the tree easily like this, in Python:

f = ROOT.TFile("results.root")
for e in f.T:
    print e.evtN, e.partID, ...., e.pz

Danilo[/quote]

Hello,

Is it possible to convert this using ROOT? I am asking because I don’t know about Python.

Ashok

Hi,

you can have a look to the TTreeReader tutorial to see how one reads a tree in ROOT in C++: root.cern.ch/doc/master/hsimpleReader_8C.html

Danilo

Hello,

I know we can read data using ROOT but I am interested to convert rootfile into .txt file, so that we can handle that text file using others program too. I need to provide this data to those who don’t know the ROOT and C++.

Ashok

The tutorial shows what you asked, i.e. how you read the rootfile in C++.
You’ll just have to print out the variables on screen.

1 Like

Excellent, I got it. Thank you.

Ashok

Please, can you tell me how do I use this code to convert the root histogram into an .ascii file?
Thank you.

Related: THistSaveAs txt or csv · Issue #10474 · root-project/root · GitHub
root2csv command - go-hep.org/x/hep/cmd/root2csv - Go Packages