# From CSV file to graph

**URL:** <https://root-forum.cern.ch/t/from-csv-file-to-graph/33828>\
**Category:** Newbie\
**Created:** [April 30, 2019, 7:50am UTC](https://root-forum.cern.ch/t/from-csv-file-to-graph/33828 "2019-04-30T07:50:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![FabioFracas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabiofracas/32/18374_2.png) [@FabioFracas](https://root-forum.cern.ch/u/FabioFracas)\
**Post date:** [April 30, 2019, 7:50am UTC](https://root-forum.cern.ch/t/from-csv-file-to-graph/33828/1 "2019-04-30T07:50:50Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [April 30, 2019, 8:13am UTC](https://root-forum.cern.ch/t/from-csv-file-to-graph/33828/2 "2019-04-30T08:13:24Z")

</div>

Hi Fabio, have a look [here](https://root.cern.ch/doc/master/classTGraph.html#ab902beb9e0463b225f0f384b6826a434).

---

<div class="post-metadata">

**Author:** ![FabioFracas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabiofracas/32/18374_2.png) [@FabioFracas](https://root-forum.cern.ch/u/FabioFracas)\
**Post date:** [April 30, 2019, 8:33am UTC](https://root-forum.cern.ch/t/from-csv-file-to-graph/33828/3 "2019-04-30T08:33:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [April 30, 2019, 8:47am UTC](https://root-forum.cern.ch/t/from-csv-file-to-graph/33828/4 "2019-04-30T08:47:28Z")

</div>

Hmmm, actually because you want to filter your data, I think you may have a look on [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html#a0813d17002d46c962513acb96ddc8d57) (search for CSV). I did not try but this should work

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

```

---

<div class="post-metadata">

**Author:** ![FabioFracas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabiofracas/32/18374_2.png) [@FabioFracas](https://root-forum.cern.ch/u/FabioFracas)\
**Post date:** [April 30, 2019, 8:55am UTC](https://root-forum.cern.ch/t/from-csv-file-to-graph/33828/5 "2019-04-30T08:55:06Z")

</div>

> [@pamputt](#):
>
> auto tdf = ROOT::RDF::MakeCsvDataFrame(“file.csv”); auto filteredEvents = tdf.Filter(“N\_ID\>=a && N\_ID\<=b”); auto g = filteredEvents.Graph(“column1”,“column2”); g-\>Draw();

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

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [April 30, 2019, 10:51am UTC](https://root-forum.cern.ch/t/from-csv-file-to-graph/33828/6 "2019-04-30T10:51:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FabioFracas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabiofracas/32/18374_2.png) [@FabioFracas](https://root-forum.cern.ch/u/FabioFracas)\
**Post date:** [April 30, 2019, 1:20pm UTC](https://root-forum.cern.ch/t/from-csv-file-to-graph/33828/7 "2019-04-30T13:20:26Z")

</div>

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”);  
g-\>Draw();  
}

[Hit.c](https://root-forum.cern.ch/uploads/default/original/3X/2/3/233b27479d9fd006345f246ca443ab43889edc67.c) (6.5 KB)

Thanks!

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [April 30, 2019, 2:32pm UTC](https://root-forum.cern.ch/t/from-csv-file-to-graph/33828/8 "2019-04-30T14:32:28Z")

</div>

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 example[Hit.c](https://root-forum.cern.ch/uploads/default/original/3X/6/7/67a303d8d8f69de68c93efa0b81171cb90ec4f56.c) (5.9 KB)

Then, it seems that the [“Graph” method](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a804b466ebdbddef5c7e3400cc6b89301) has been added recently and so I cannot test on my ROOT version (6.16/00). @Danilo 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
{
  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");
  h->Draw("col");
}

```

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](https://root.cern.ch/root/html/tutorials/graphs/graph.C.html) (you have to adapt the part where x and y arrays are filled).

---

<div class="post-metadata">

**Author:** ![FabioFracas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabiofracas/32/18374_2.png) [@FabioFracas](https://root-forum.cern.ch/u/FabioFracas)\
**Post date:** [April 30, 2019, 2:50pm UTC](https://root-forum.cern.ch/t/from-csv-file-to-graph/33828/9 "2019-04-30T14:50:35Z")

</div>

> [@pamputt](#):
>
> you want using TGra

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.
