Convert binary files to one ROOT file

Hi everyone,

I have 180 “.dat” files (binary files) and I want to convert it into a .root file. The main reason is because I am dealing with large amount of data.

The data type of the binary file is:

dtype=np.dtype([(“time”,np.float64),(“x1”,np.float64),(“y1”,np.float64),(“z1”,np.float64),(“ch1”,np.int8),(“x2”,np.float64),(“y2”,np.float64),(“z2”,np.float64),(“ch2”,np.int8),(“x3”,np.float64), (“y3”,np.float64),(“z3”,np.float64),(“ch3”,np.int8),(“x4”,np.float64),(“y4”,np.float64),(“z4”,np.float64),(“ch4”,np.int8),(“ll”,np.float64),(“pep”,np.float64),(“wep”,np.float64)])

Can anyone help me with this?

Thanks in advance.
Marg

The most suitable way to store such data in a ROOT file is to use a TTree. Youwill find a basic example showing how to create and fill a TTree in the TTree class documentation (the link I’ve given before), just before the class documentation.

Hi,

this is an interesting problem.
I’d ask you to provide a few lines of the binary file to see clearer but meanwhile I propose an option to achieve what you want too.

If you can convert these binaries in csv files, you can read them with a CSV data source into a dataframe and then snapshot it. In ROOT 6.12:

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

This will have the cost of an intermediate csv conversion which will happen once. Direct conversion of the file in binary format might be hairy but possible.

Cheers,
D

1 Like

Hello,

Thank you for your info. Actually my raw data are in .bin files and I
converted them into .dat files with Python. So, now I want to put 180 .dat
files into a single .root file to use it as an input in a C++ code. I have
attached one of my .dat files to have a general idea. I haven’t used ROOT
before so any idea would be really helpful.
Thank you.​
llv180.dat
https://drive.google.com/file/d/1UzJ1CWnryG9A75TxFN7Kyh2Awl0u7h95/view?usp=drive_web

Visit Topic

Hi,

let’s go through my comment: the ROOT piece of code you need is pasted in my previous post provided that you can transform in python these dat files into csv files. Can you?
My first question still holds: how are these bin files formatted? Do you have reader for those?

D

Hi,

I could try with cvs files but I think there will be a way to convert the .dat files directly. I found a relative code on Binary file to ROOT file but I am getting an error reading all requested bytes.
Obviously I am doing something wrong with the type of variables and the bit values. Anyway I will try what you said.

M

Hi,

it’s certainly faster to read the binary if you know its layout.
If you are able to write a reader, I can then provide the code to plug it inside TDataFrame (RDataFrame) in order to snapshot their content into root format easily.

Cheers,
D

Hi,

Well, the initial binary files have the format mentioned above. Then I created the .dat files as two arrays like:
arri=np.array([x1,y1,z1,u1,v1,w1,dbin],dtype=np.float32)
arro=np.array([x2,y2,z2,u2,v2,w2,ll],dtype=np.float32). I could send you an example of the .dat files but this format is not supported here.

M

Hi,

so can you read with a simple C++ piece of code that file if the format is known?

D

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