# .dat to .root

**URL:** <https://root-forum.cern.ch/t/dat-to-root/60045>\
**Category:** Newbie\
**Created:** [June 28, 2024, 10:06am UTC](https://root-forum.cern.ch/t/dat-to-root/60045 "2024-06-28T10:06:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [June 28, 2024, 10:06am UTC](https://root-forum.cern.ch/t/dat-to-root/60045/1 "2024-06-28T10:06:03Z")

</div>

* * *

Hi.

**This topic is to covert .dat file to .root file**  
I found the _basic2.C_ program in the tutorial and I use it for the conversion.

The few of the entries in my ‘_txtdata.dat_’ file are given in the file below:  
[txtdata.txt](https://root-forum.cern.ch/uploads/short-url/MJlLXNZ5JPOSo42PknmVaL9Jjb.txt) (314 Bytes)

Following is the _txt2root.C_ program that I use:

```auto
#include "Riostream.h"
void txt2root() {
   TString dir = gROOT->GetTutorialDir();
   dir.Append("/tree/");
   dir.ReplaceAll("/./","/");

   TFile *f = new TFile("output.root","RECREATE");
   /*TH1F *h1 = new TH1F("h1","x distribution",100,-4,4);*/
   TTree *T = new TTree("ntuple","data from ascii file");
   ULong64_t nlines = T->ReadFile(Form("%stxtdata.dat",dir.Data()),"x:y:z");
   printf(" found %llu points\n",nlines);
   /*T->Draw("x","z>2");*/
   T->Write();
}

```

I run this program in terminal and get the following:

```auto
$ root -l txt2root.C
root [0]
Processing txt2root.C...
x=20014208721000, y=1, z=645
x=20014209165640, y=1, z=6263
x=20014209480080, y=1, z=848
x=20014209657920, y=2, z=503
x=20014209790760, y=1, z=4993
 found 18966287 points

root [1] ntuple->Scan("x:y:z","","colsize=30")
***************************************************************************************************************
* Row * x * y * z *
***************************************************************************************************************
* 0 * 20014207860736 * 1 * 645 *
* 1 * 20014209957888 * 1 * 6263 *
* 2 * 20014209957888 * 1 * 848 *
* 3 * 20014209957888 * 2 * 503 *
* 4 * 20014209957888 * 1 * 4993 *
* 5 * 20014209957888 * 2 * 5016 *
* 6 * 20014209957888 * 2 * 5019 *
* 7 * 20014212055040 * 2 * 803 *
* 8 * 20014212055040 * 2 * 558 *
* 9 * 20014212055040 * 1 * 259 *
* 10 * 20014212055040 * 2 * 4015 *
* 11 * 20014212055040 * 1 * 3142 *
* 12 * 20014212055040 * 1 * 2224 *
* 13 * 20014214152192 * 2 * 5006 *

```

_Problem:_  
**Why only the ‘x’ values not same as that of my ‘txtdata.dat’ file?  
The ‘y’ and ‘z’ values are correctly stored in output.root file.**

_Note:_

1. The original ‘_txtdata.dat_’ is a big file of size ~980MB.
2. I also used the file of size ~314Bytes (which I have attached above), but got the same result.  
Any comments or an alternate to solve this?

ROOT Version: 6.26/10  
Platform: Ubuntu 22.04.2 LTS

Thanks,  
Newbieeee

* * *

---

<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 28, 2024, 11:30am UTC](https://root-forum.cern.ch/t/dat-to-root/60045/2 "2024-06-28T11:30:02Z")

</div>

Hello,

Thanks for the reproducer.  
If I take your input file and convert it like this (your code above):

```auto
   TFile *f = new TFile("output.root","RECREATE");
   TTree *T = new TTree("ntuple","data from ascii file");
   ULong64_t nlines = T->ReadFile("txtdata.txt","x:y:z");
   printf(" found %llu points\n",nlines);
   T->Write();

```

I see exactly the content of the txt file in the TTree:

```auto
cat txtdata.txt 
20014208721000 1 645
20014209165640 1 6263
20014209480080 1 848
20014209657920 2 503
20014209790760 1 4993
20014210162560 2 5016
20014210921720 2 5019
20014211039040 2 803
20014211166040 2 558
20014211197040 1 259
20014212153080 2 4015
20014212455920 1 3142
20014212712840 1 2224
20014213219800 2 5006

```

and

```auto
ntuple->Scan("x:y:z","","colsize=14")
***************************************************************
* Row * x * y * z *
***************************************************************
* 0 * 20014207860736 * 1 * 645 *
* 1 * 20014209957888 * 1 * 6263 *
* 2 * 20014209957888 * 1 * 848 *
* 3 * 20014209957888 * 2 * 503 *
* 4 * 20014209957888 * 1 * 4993 *
* 5 * 20014209957888 * 2 * 5016 *
* 6 * 20014209957888 * 2 * 5019 *
* 7 * 20014212055040 * 2 * 803 *
* 8 * 20014212055040 * 2 * 558 *
* 9 * 20014212055040 * 1 * 259 *
* 10 * 20014212055040 * 2 * 4015 *
* 11 * 20014212055040 * 1 * 3142 *
* 12 * 20014212055040 * 1 * 2224 *
* 13 * 20014214152192 * 2 * 5006 *
***************************************************************

```

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [June 28, 2024, 11:32am UTC](https://root-forum.cern.ch/t/dat-to-root/60045/3 "2024-06-28T11:32:49Z")

</div>

> [@Danilo](#):
>
> I see exactly the content of the txt file in the TTree:

Sorry, but your output of TTree isn’t same to the 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 28, 2024, 11:34am UTC](https://root-forum.cern.ch/t/dat-to-root/60045/4 "2024-06-28T11:34:01Z")

</div>

You are fully right! Let me see…

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 28, 2024, 11:39am UTC](https://root-forum.cern.ch/t/dat-to-root/60045/5 "2024-06-28T11:39:02Z")

</div>

Hi,

check this out: [ROOT: TTree Class Reference](https://root.cern.ch/doc/v630/classTTree.html#a9c8da1fbc68221b31c21e55bddf72ce7)

In particular,

> - If the type of the first variable is not specified, it is assumed to be “/F”
> - If the type of any other variable is not specified, the type of the previous variable is assumed. eg
> - 
> - `x:y:z` (all variables are assumed of type “F”)

And 20014208721000 is too much for a float:

```auto
root [0] float x1 = 20014208721000;
ROOT_prompt_0:1:12: warning: implicit conversion from 'long' to 'float' changes value from 20014208721000 to 20014207860736 [-Wimplicit-const-int-float-conversion]
float x1 = 20014208721000;
      ~~^~~~~~~~~~~~~~

```

---

<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 28, 2024, 11:43am UTC](https://root-forum.cern.ch/t/dat-to-root/60045/6 "2024-06-28T11:43:51Z")

</div>

Hi,

Please refer to this code and apologies for the mistake above!

```auto
   auto fileName = "txtdata.txt";
   auto df = ROOT::RDF::FromCSV(fileName, false, ' ');
   auto d = df.Display();
   df.Snapshot("ntuple", "rdfoutput.root");
   
   TFile f("rdfoutput.root");
   auto t = f.Get<TTree>("ntuple");
   t->Scan("Col0:Col1:Col2","","colsize=14");

```

This gives

```auto
***************************************************************
* Row * Col0 * Col1 * Col2 *
***************************************************************
* 0 * 20014208721000 * 1 * 645 *
* 1 * 20014209165640 * 1 * 6263 *
* 2 * 20014209480080 * 1 * 848 *
* 3 * 20014209657920 * 2 * 503 *
* 4 * 20014209790760 * 1 * 4993 *
* 5 * 20014210162560 * 2 * 5016 *
* 6 * 20014210921720 * 2 * 5019 *
* 7 * 20014211039040 * 2 * 803 *
* 8 * 20014211166040 * 2 * 558 *
* 9 * 20014211197040 * 1 * 259 *
* 10 * 20014212153080 * 2 * 4015 *
* 11 * 20014212455920 * 1 * 3142 *
* 12 * 20014212712840 * 1 * 2224 *
* 13 * 20014213219800 * 2 * 5006 *
***************************************************************

```

Which should be correct

Cheers,  
D

---

<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 28, 2024, 11:47am UTC](https://root-forum.cern.ch/t/dat-to-root/60045/7 "2024-06-28T11:47:30Z")

</div>

Just to complement the answer… Using RDataFrame has many advantages, including the possibility of manipulating your data before dumping as a TTree, for example applying selections or adding columns.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [June 28, 2024, 12:12pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/8 "2024-06-28T12:12:35Z")

</div>

> [@Danilo](#):
>
> Please refer to this code and apologies for the mistake above!

Although I don’t understand the RDataFrame…The code you provided worked.  
Thank you very much for the solution!

Also, if I can add this question here, how can one decrease the file size of the .root file.  
The ‘_rdfoutput.root_’ created is ~324M when I use my original ~980MB ‘_txtdata.dat_’ file.  
Can we modify the code that you sent above for the purpose of decreasing the file size?

Thanks again.

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [June 28, 2024, 12:16pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/9 "2024-06-28T12:16:44Z")

</div>

Thanks for the reply. I did look into something like that.

> [@yus](#):
>
> And 20014208721000 is too much for a float:

Is there a data type variable for this large no. ‘x’ which I can use in the line below:

```auto
 ULong64_t nlines = T->ReadFile("txtdata.txt","x/?:y/F:z/F");

```

Thanks.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 28, 2024, 12:23pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/10 "2024-06-28T12:23:05Z")

</div>

Based on your `txtdata.txt` I would assume

```auto
ULong64_t nlines = T->ReadFile("txtdata.txt","x/l:y/s:z/i"); // unsigned long64, unsigned short16, and unsigned int32

```

see [ROOT: TTree Class Reference](https://root.cern.ch/doc/v630/classTTree.html#addcolumnoffundamentaltypes) for details

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [June 28, 2024, 12:35pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/11 "2024-06-28T12:35:47Z")

</div>

Thanks.  
This also solved the problem.

Below is the correct code.

```auto
#include "Riostream.h"
void txt2root() {
   TString dir = gROOT->GetTutorialDir();
   dir.Append("/tree/");
   dir.ReplaceAll("/./","/");

   TFile *f = new TFile("output.root","RECREATE");
   /*TH1F *h1 = new TH1F("h1","x distribution",100,-4,4);*/
   TTree *T = new TTree("ntuple","data from ascii file");
   ULong64_t nlines = T->ReadFile(Form("%stxtdata.dat",dir.Data()),"x/l:y/F:z/F");
   printf(" found %llu points\n",nlines);
   /*T->Draw("x","z>2");*/
   T->Write();
}

```

To add a note:  
This produces the root file of size ~314M with my original txt file of ~980M.

---

<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 28, 2024, 12:38pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/12 "2024-06-28T12:38:02Z")

</div>

this is great!

Maybe one last thing. It’s probably not a requirement for the current situation, but using RDataFrame makes the code future proof. ROOT is working on the successor of TTree, RNTuple, and RDataFrame will be able to write out datasets in that format _without changes in user code_.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 28, 2024, 12:39pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/13 "2024-06-28T12:39:18Z")

</div>

> [@Mukul](#):
>
> This produces the root file of size ~314M with my original txt file of ~980M.

Try using

```auto
ULong64_t nlines = T->ReadFile(Form("%stxtdata.dat",dir.Data()),"x/l:y/s:z/i");

```

instead of

```auto
ULong64_t nlines = T->ReadFile(Form("%stxtdata.dat",dir.Data()),"x/l:y/F:z/F");

```

It will probably decrease the file size.

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [June 28, 2024, 12:43pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/14 "2024-06-28T12:43:51Z")

</div>

> [@yus](#):
>
> Try using
> 
> ```auto
> ULong64_t nlines = T->ReadFile(Form("%stxtdata.dat",dir.Data()),"x/l:y/s:z/i")
> 
> ```

Yes. It reduced to 303M.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 28, 2024, 12:51pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/15 "2024-06-28T12:51:01Z")

</div>

`"x/l:y/b:z/s"`  
Note: make sure you always have in your original data file: “`x >= 0`”, “`0 <= y <= 255`”, and “`0 <= z <= 65535`”

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [June 28, 2024, 1:03pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/16 "2024-06-28T13:03:52Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> “x/l:y/b:z/s”

Thanks. It reduced to 283M.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 28, 2024, 1:11pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/17 "2024-06-28T13:11:14Z")

</div>

Assuming “`x`” is at most 15 decimal digits long (in your original data file), try also (maybe you’ll get better compression): `"x/D:y/b:z/s"`

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [June 28, 2024, 1:21pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/18 "2024-06-28T13:21:40Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> maybe you’ll get better compression

Thanks for the reply, but it increased to 288M.

---

<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 28, 2024, 3:50pm UTC](https://root-forum.cern.ch/t/dat-to-root/60045/19 "2024-06-28T15:50:56Z")

</div>

Of course.

Coming back to our original example:

```auto
   auto fileName = "txtdata.txt";
   auto df = ROOT::RDF::FromCSV(fileName, false, ' ');
   auto d = df.Display();
   ROOT::RDF::RSnapshotOptions opts("RECREATE", ROOT::RCompressionSetting::EAlgorithm::kLZMA, 9, 0, 99, false);
   df.Snapshot("ntuple", "rdfoutput.root", ".*", opts);
   
   TFile f("rdfoutput.root");
   auto t = f.Get<TTree>("ntuple");
   t->Scan("Col0:Col1:Col2","","colsize=14");

```

That will compress your data with LZMA (see [ROOT: ROOT::RDF::RSnapshotOptions Struct Reference](https://root.cern/doc/master/structROOT_1_1RDF_1_1RSnapshotOptions.html))

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [June 29, 2024, 9:40am UTC](https://root-forum.cern.ch/t/dat-to-root/60045/20 "2024-06-29T09:40:49Z")

</div>

Thank you for the reply.

> [@Danilo](#):
>
> `ROOT::RDF::RSnapshotOptions opts("RECREATE", ROOT::RCompressionSetting::EAlgorithm::kLZMA, 9, 0, 99, false);`

This gave me an error (few lines are pasted below). Seems there is a problem with 2nd argument.  
I saw the documentation, but sorry I couldn’t understand it.

```auto
 error: no matching constructor for initialization of 'ROOT::RDF::RSnapshotOptions'
   ROOT::RDF::RSnapshotOptions opts("RECREATE", ROOT::RCompressionSetting::EAlgorithm::kLZMA, 9, 0, 99, false);
                               ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/root/include/ROOT/RSnapshotOptions.hxx:27:4: note: candidate constructor not viable: no known conversion from 'ROOT::RCompressionSetting::EAlgorithm::EValues' to 'ROOT::RDF::RSnapshotOptions::ECAlgo' (aka 'ROOT::ECompressionAlgorithm') for 2nd argument
   RSnapshotOptions(std::string_view mode, ECAlgo comprAlgo, int comprLevel, int autoFlush, int splitLevel, bool lazy,

/root/include/ROOT/RSnapshotOptions.hxx:25:4: note: candidate constructor not viable: requires 1 argument, but 6 were provided
   RSnapshotOptions(const RSnapshotOptions &) = default;

```

[Next page](https://root-forum.cern.ch/t/dat-to-root/60045.md?page=2)
