# Generate root file from a data file

**URL:** <https://root-forum.cern.ch/t/generate-root-file-from-a-data-file/29295>\
**Category:** ROOT\
**Tags:** tree, hist\
**Created:** [June 8, 2018, 6:43am UTC](https://root-forum.cern.ch/t/generate-root-file-from-a-data-file/29295 "2018-06-08T06:43:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mustak21](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mustak21/32/12013_2.png) [@mustak21](https://root-forum.cern.ch/u/mustak21)\
**Post date:** [June 8, 2018, 6:43am UTC](https://root-forum.cern.ch/t/generate-root-file-from-a-data-file/29295/1 "2018-06-08T06:43:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 8, 2018, 6:54am UTC](https://root-forum.cern.ch/t/generate-root-file-from-a-data-file/29295/2 "2018-06-08T06:54:01Z")

</div>

Hi,

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:

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

```

while https://root.cern/doc/v612/classROOT\_1\_1Experimental\_1\_1TDF\_1\_1TCsvDS.html more documentation can be found to adapt the reading to separators which are not commas.

Let us know if this info is enough.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![mustak21](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mustak21/32/12013_2.png) [@mustak21](https://root-forum.cern.ch/u/mustak21)\
**Post date:** [June 8, 2018, 7:01am UTC](https://root-forum.cern.ch/t/generate-root-file-from-a-data-file/29295/3 "2018-06-08T07:01:00Z")

</div>

Hi,  
I have data in .txt file.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 8, 2018, 7:12am UTC](https://root-forum.cern.ch/t/generate-root-file-from-a-data-file/29295/4 "2018-06-08T07:12:08Z")

</div>

Ok.

How is it formatted?

D

---

<div class="post-metadata">

**Author:** ![mustak21](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mustak21/32/12013_2.png) [@mustak21](https://root-forum.cern.ch/u/mustak21)\
**Post date:** [June 8, 2018, 8:30am UTC](https://root-forum.cern.ch/t/generate-root-file-from-a-data-file/29295/5 "2018-06-08T08:30:56Z")

</div>

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](https://root-forum.cern.ch/uploads/default/original/3X/a/0/a0274dfa64c27c5147271e4cae805e26d332faea.C) (1.2 KB)

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 8, 2018, 9:13am UTC](https://root-forum.cern.ch/t/generate-root-file-from-a-data-file/29295/6 "2018-06-08T09:13:04Z")

</div>

Hi,

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
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");

```

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [June 22, 2018, 9:13am UTC](https://root-forum.cern.ch/t/generate-root-file-from-a-data-file/29295/7 "2018-06-22T09:13:05Z")

</div>

Closed on June 22, 2018, 9:13am UTC

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