Convert the text data file to the root data file


I would like to convert from the text data file to the root data file (binary file).
The text data file includes numbers,
for instance,

x y errorx errory
0.5 2.5 0.01 0.01
1.5 3.0 0.01 0.02
2.5 3.1 0.02 0.01

and root data file means “filename.root” (binary code).
How can I convert from the text data file to the root data file?
How about from the root data file to the text data file??

Thank you!


They are quite a few examples:

With ReadFile, you will need to either remove the first line or change it to: x/F:y/F:errorx/F:errory/F and then do:f = new TFile("filename.root"); t = new TTree("thedata",""); t->ReadFile("file.dat"); … iew=markup

For string output of a TTree, try TTree::Scan. Its documentation can be found here



It works and thank you very much. But I want to make 2 dimensional graph rather than TTree or histograms. The data includes just x, y, and x error, y error. How do you create these data from ascii files to root files?

I want to create these root files (such as filename.root) for root fitting macros. And the original data is written by text files. That’s why I want to convert ascii files to root files.

Thank you!

See the constructors from TGraph and TGraphErrors building a graph directly from an ascii file



Note that you can fit the data in a TTree directly (see TTree::UnbinnedFit and TTree::Fit). You can also generate a TGraph out of TTree (see TTree::Draw).

There is also a way to create TGraph from a ascii file see the TGraph constructor (