Need help to get data from Root file

Hello,I have a file with a lot of trees and need to get data from it as a text file.Who have experience in such kind of work?I can connect you by email or smth and send file.

Hi @elizaveta,
see e.g. my reply here.

Hope this helps!
Enrico

Thanks for your answer.I looked at tutorials,but didnt understand anything :pensive:

At a very basic level, in C++ you can use the following:

ROOT::RDataFrame df("treename", "filename.root");
df.Foreach(someProcessingFunction, {"column1", "column2", "column3"});

where someProcessingFunction is any C++ function that takes three arguments of the type of column1, column2 and column3 in your dataset.

That’s probably the simplest C++ recipe for processing ROOT data, and someProcessingFunction could e.g. print the contents of the columns to standard output so that you can put them in a text file.

@swunsch might be able to suggest something as simple for Python.

Hope this helps!
Enrico

Hi there,

In case you are familiar with the scipy software stack, here namely numpy and pandas, the most convenient way is probably the following snippet (it’s fully runnable, feel free to try!):

# Load the ROOT data as numpy arrays
import ROOT
df = ROOT.RDataFrame('TreeS', 'http://root.cern/files/tmva_class_example.root')
npy = df.AsNumpy(['var1', 'var2', 'var3'])

# Print the numpy arrays
print(npy)

# Make a pandas dataframe
import pandas
pdf = pandas.DataFrame(npy)

# Print the dataframe
print(pdf)

# Save them as a csv file
pdf.to_csv('data.csv')

Best
Stefan

1 Like

And a word of warning: exporting to text files is usually not what you want. You should rather learn the tools at hand; Excel is rarely the answer :wink:

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