Generate root file from a data file

I have a data file with say 5 columns and 10 rows of data. I want to create a root file where the columns in the data file will be leaves of my root file. How to do that? Any help will be much appreciated.


this very much depends on the format of your data, e.g. binary or ascii. If it is ASCII, it’s highly probable that it can be handled smoothly by the ROOT’s data frame.
Here you can see an example of conversion to ROOT file from a CSV file:

fileName = "yourfile.csv"
MakeCsvDataFrame = ROOT.ROOT.Experimental.TDF.MakeCsvDataFrame
tdf = MakeCsvDataFrame(fileName)
tdf.Snapshot("yourtree", "yourfile.root")

while more documentation can be found to adapt the reading to separators which are not commas.

Let us know if this info is enough.


I have data in .txt file.


How is it formatted?


Hi, I managed to write the macro which can now convert the data file into root file. I am attaching it.
alpha_data_gs.C (1.2 KB)

1 Like


from your code I could deduce (hopefully correctly) the format of your input file. This is, for future readers of the Forum, another possible approach, which is significantly shorter than the nice solution you proposed:

auto fileName = "alpha_data_gs.dat";
auto delimiter = ' '; // I *think* the delimiter is a space, if it's a tab, just use '\t'
auto tdf = ROOT::Experimental::TDF::MakeCsvDataFrame(fileName, false, delimiter);
tdf.Snapshot("alpha_data_gs", "alpha_data_gs.root");


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