Reading csv file using PyROOT

I am having trouble reading csv file and making a histogram using pyroot.
Here’s my code:

  1 import ROOT
  2 import csv
  3 import array
  4 import numpy
  5 #canvas = ROOT.TCanvas("canvas", "canvas title", 800, 600)
  7 infilename = 'prac_data.csv'
  9 hist = ROOT.TH1F("variable", "Title; xaxis; yaxis", 4, 105, 125)
 11 with open(infilename, 'r') as csvfile:
 12     reader = csv.reader(csvfile)
 13     for i in reader:
 14         hist.Fill(i)
 16 hist.Draw()

This is the error message I am getting.

Also, when I print entries from the file, the output is in this format.


Wikipedia → Comma-separated values (CSV)

Hi @TheScientist ,

I think what @Wile_E_Coyote is hinting at is that your file is not in a standard CSV format, so you are probably reading broken strings in your i variable and then hist.Fill(i) does not know how to Fill hist with i. Printing the value of i should show what’s going on.



For that it’s enough to go through a dictionary of numpy arrays and use MakeNumpyDataFrame, see e.g. Saving pandas dataframe as TTree with RDataframe - #2 by swunsch .

It works as long as data is flat (one value per cell):

arr_dict = {c: np.array(pandas_df[c]) for c in pandas_df}
root_df = ROOT.RDF.MakeNumpyDataFrame(arr_dict)