# Matrix in CSV to root

**URL:** <https://root-forum.cern.ch/t/matrix-in-csv-to-root/40393>\
**Category:** Newbie\
**Created:** [July 12, 2020, 6:06pm UTC](https://root-forum.cern.ch/t/matrix-in-csv-to-root/40393 "2020-07-12T18:06:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![phy.vijayraj](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phy.vijayraj/32/18580_2.png) [@phy.vijayraj](https://root-forum.cern.ch/u/phy.vijayraj)\
**Post date:** [July 12, 2020, 6:06pm UTC](https://root-forum.cern.ch/t/matrix-in-csv-to-root/40393/1 "2020-07-12T18:06:22Z")

</div>

Dear Sir/Dr./Prof.,  
I have a data in 10\*10 matrix x,y. I want to plot it in root. I am unable to understand, how?. I tried it by typing matrix by matrix and a tracker but it takes a long time. Is there anyway through which I can call my csv file and see two D plot in between x and y for tracking secondary electrons.  
Data file in CSV attached. X(1st) Y(2nd) Z(3rd) are location and 4th column is tracking of a particle. Please have a look in attached file. I am eagerly looking for a help. Tomorrow I have to present it.  
Please help.  
Much Regards  
[2Dtrial.c](https://root-forum.cern.ch/uploads/short-url/h4PVn5MG4TFWcY4IHHeujFfMUYO.c) (1.8 KB) [Distribution\_Of\_Secondary\_Electrons1.csv.tar.gz](https://root-forum.cern.ch/uploads/short-url/hXaN0ZxZSjdVXfwOX0DPyiY0TU3.gz) (506 Bytes)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 12, 2020, 6:54pm UTC](https://root-forum.cern.ch/t/matrix-in-csv-to-root/40393/2 "2020-07-12T18:54:36Z")

</div>

See the documentation of [TTree](https://root.cern.ch/doc/master/classTTree.html), [TTree::ReadFile()](https://root.cern.ch/doc/master/classTTree.html#a9c8da1fbc68221b31c21e55bddf72ce7), and [TTree::Draw()](https://root.cern.ch/doc/master/classTTree.html#a73450649dc6e54b5b94516c468523e45)

```auto
  TTree *tree = new TTree("tree", "tree from Distribution_Of_Secondary_Electrons1.csv");
  tree->ReadFile("Distribution_Of_Secondary_Electrons1.csv", "X/F:Y:Z:T");
  tree->Draw("X:Y:T");

```

Note that if you search the forum, you can probably find several other solutions…

---

<div class="post-metadata">

**Author:** ![phy.vijayraj](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phy.vijayraj/32/18580_2.png) [@phy.vijayraj](https://root-forum.cern.ch/u/phy.vijayraj)\
**Post date:** [July 12, 2020, 7:46pm UTC](https://root-forum.cern.ch/t/matrix-in-csv-to-root/40393/3 "2020-07-12T19:46:13Z")

</div>

I am extremely happy to have your mail so promptly. I have plotted the graph, but the structure of the graph is quite different what I am expected. When I use your commands, it works and plot in three D. I want something like attached one. Can you please please give me an idea. I will be extremely happy for it.  
Much much regards

 ![TwoDPlot-VRS](https://root-forum.cern.ch/uploads/default/original/3X/f/4/f4b71c2a61ea94edbbdd864c82006fd67bb42043.png)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 12, 2020, 8:05pm UTC](https://root-forum.cern.ch/t/matrix-in-csv-to-root/40393/4 "2020-07-12T20:05:44Z")

</div>

How did you draw this histogram?

---

<div class="post-metadata">

**Author:** ![phy.vijayraj](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phy.vijayraj/32/18580_2.png) [@phy.vijayraj](https://root-forum.cern.ch/u/phy.vijayraj)\
**Post date:** [July 12, 2020, 8:06pm UTC](https://root-forum.cern.ch/t/matrix-in-csv-to-root/40393/5 "2020-07-12T20:06:13Z")

</div>

Dear Sir,  
I have gone through the tree draw option and changed 3D to 2D, but i can not see my scattered tracks. Its a matrix of 10 by 10 but unable to see anything. I used command to draw tree as  
tree-\>Draw(“X:Y:T”,"",“col”);

And it gives result like this.

 ![test-1](https://root-forum.cern.ch/uploads/default/original/3X/3/3/336e6aca3da4dd115a035eac5be09327788f5959.png)

---

<div class="post-metadata">

**Author:** ![phy.vijayraj](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phy.vijayraj/32/18580_2.png) [@phy.vijayraj](https://root-forum.cern.ch/u/phy.vijayraj)\
**Post date:** [July 12, 2020, 8:09pm UTC](https://root-forum.cern.ch/t/matrix-in-csv-to-root/40393/6 "2020-07-12T20:09:17Z")

</div>

I have written all matrix in a c file and run in root. I placed the T values as gives in my csv file. Please have a look to the code i used to create the figure. But writing in that way will be hard for me change the matrix size.  
Much respect to you.[2Dtrial.c](https://root-forum.cern.ch/uploads/short-url/h4PVn5MG4TFWcY4IHHeujFfMUYO.c) (1.8 KB)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 12, 2020, 8:36pm UTC](https://root-forum.cern.ch/t/matrix-in-csv-to-root/40393/7 "2020-07-12T20:36:00Z")

</div>

```auto
void ReadDrawMatrix()
{
   ifstream in;
   std::string dummy;
   TMatrixT<double> matrix(10,10);
   int x,y,z,t;
   char c;
   in.open("Distribution_Of_Secondary_Electrons1.csv", std::ios::binary);
   if (!in.is_open()) {
      std::cout << "failed to open Distribution_Of_Secondary_Electrons1.csv" << '\n';
      return;
   }
   for (auto i=0;i<9;++i) {
      std::getline(in, dummy);
   }
   while (in.is_open() && !in.eof()) {
      in >> x >> c >> y >> c >> z >> c >> t;
      matrix(x,y)=(float)t;
   }
   TCanvas *canvas = new TCanvas("canvas", "canvas");
   gStyle->SetOptStat(0);
   matrix.Draw("colz");
   canvas->Print("haha.pdf");
   matrix.Print();
}

```

---

<div class="post-metadata">

**Author:** ![phy.vijayraj](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phy.vijayraj/32/18580_2.png) [@phy.vijayraj](https://root-forum.cern.ch/u/phy.vijayraj)\
**Post date:** [July 12, 2020, 8:39pm UTC](https://root-forum.cern.ch/t/matrix-in-csv-to-root/40393/8 "2020-07-12T20:39:15Z")

</div>

waowww, it worked. Thankyou thankyou and thankyou. Much reagrds  
Sincerely
