From CSV file to graph

Hi to all. I’m really a newbie and it’s my first approach to ROOT.

I’m trying to undestand the logics so what I wish to do is only to read a CSV file (in form N_ID, column1, column2, column3), show the content of the file within an arbitrary range (from N_ID=a to N_ID=b) and print a graph of the corresponding colums.

Can you give me the corresponding ROOT code?

Thanks in advance for your response!

Hi Fabio, have a look here.

Thank you, pamputt. I would still need an example code to better understand how ROOT works.

Hmmm, actually because you want to filter your data, I think you may have a look on RDataFrame (search for CSV). I did not try but this should work

auto tdf = ROOT::RDF::MakeCsvDataFrame("file.csv");
auto filteredEvents =
   tdf.Filter("N_ID>=a &&  N_ID<=b");
auto g = filteredEvents.Graph("column1","column2");

I tried but I received this error: “libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Error reading headers of CSV file”.

The name of the headers I used are correct.

Thank you, again

Could you share your csv file, or at least a part of it?

These are the CSV (Hit.c, please change the extension in .csv) file and the ROOT code I used:

auto tdf = ROOT::RDF::MakeCsvDataFrame(“Hit.csv”);
auto filteredEvents =
tdf.Filter(“hit_id>=1 && hit_id<=20”);
auto g = filteredEvents.Graph(“ch0”,“ch1”);

Hit.c (6.5 KB)


First, your csv file is not really clean. Remove the first lines and name properly your column in order in works “automatically”. Currently, RDataFrame cannot guess what is the name of your column. I attach an exampleHit.c (5.9 KB)

Then, it seems that the “Graph” method has been added recently and so I cannot test on my ROOT version (6.16/00). @dpiparo may confirm.

So if you do not use the ROOT master version, you may want to plot a TH2F. If so, this piece of code works (with the good CSV file)

  auto tdf = ROOT::RDF::MakeCsvDataFrame("Hit.csv");
  auto filteredEvents = tdf.Filter("hit_id>5 && hit_id<10");
  auto h = filteredEvents.Histo2D({"h", "h", 100,0,1000,100,0.,1.},"ch0","value");

Otherwise, you will need to parse yourself the CSV file, to store all the values in vectors and then to plot what you want using TGraph. TO know how to do, you can see this tutorial (you have to adapt the part where x and y arrays are filled).


Tank you very much! I will look to the tutorial and I will do some test with the CSV file.
I’ll let you know if everything works.