# How to extract the XY-values from .root file

**URL:** <https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790>\
**Category:** Newbie\
**Created:** [November 12, 2019, 7:48pm UTC](https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790 "2019-11-12T19:48:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xavier\_Brock](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xavier_brock/32/20225_2.png) [@Xavier\_Brock](https://root-forum.cern.ch/u/Xavier_Brock)\
**Post date:** [November 12, 2019, 7:48pm UTC](https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790/1 "2019-11-12T19:48:53Z")

</div>

Hello all,

I am in the home stretch of my thesis and am trying to construct a multigraph.  
I need my data, from a .root file, overlayed with the data from a .csv file that I generated to replicate someone’s previous work. I have gotten the following to graph two different .csv files,

// Manage several graphs as a single entity.  
void multigraph(){  
TCanvas \*c1 = new TCanvas(“c1”,“multigraph”,700,500);  
c1-\>SetGrid();

auto mg = new TMultiGraph();

// create first graph  
auto gr1 = new TGraph(“Carrier\_Be\_5MeV\_recreation.csv”);  
gr1-\>SetMarkerColor(kBlue);  
gr1-\>SetMarkerStyle(20);  
mg-\>Add(gr1);

// create second graph

auto gr2 = new TGraph(“Carrier\_Be\_1MeV\_recreation.csv”);  
gr2-\>SetMarkerColor(kRed);  
gr2-\>SetMarkerStyle(20);  
mg-\>Add(gr2);

mg-\>Draw(“apl”);

gPad-\>Update();  
gPad-\>Modified();  
}

But, the data from my runs are in a .root file, and I can only get the above code to work with two .csv files, and not a combo of .root and .csv.  
This leaves me with one of two options:  
1.) Figure out how to get the above code to graph my .root and corresponding .csv files.  
2.) Figure out how to get XY-Values from my .root file so that I can generate a .csv file and use the above code.  
If number #1 isn’t possible, which it hasn’t yet been for me, then I need #2 and I really don’t wanna painstakingly take all 100 point values by hand, for 20+ graphs. It seems to me that there should be some way to extract the XY-values from my .root file, but I can’t figure this out.

I am also having issues getting a clean Gaussian fit on my .csv file. I am having to resort to using a high order polynomial to get a close fit. It seems to me that there should be a way to use the gaussian fit as my data is a gaussian curve. I use the g-\>Fit("pol6) instead of g-\>Fit(“gaus”). See the picture attached for an understanding of what is going on with the gaussian fit. I can make do with the poly6 for this data if I end the graph at 1 to eliminate the rest of the fit line, but I don’t know if this workaround will continue to work for other data files, and I would like to know how to do it properly. It seems to me that this should be a simple graph to get a good clean fit for.

 ![bestfitgaus](https://root-forum.cern.ch/uploads/default/original/3X/3/0/30449a96ee7bd151a8fc1a69d96064f1cf90566c.png)

Thanks, Xavier

---

<div class="post-metadata">

**Author:** ![ilam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ilam/32/19180_2.png) [@ilam](https://root-forum.cern.ch/u/ilam)\
**Post date:** [November 12, 2019, 9:36pm UTC](https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790/2 "2019-11-12T21:36:36Z")

</div>

Hi,

You could read the XY values in your .root file and put them into two arrays, and feed those arrays as arguments to a TGraph. You can use graph-\>Draw(“same”) to overlay it with the other two you have.

For your second question, I am assuming the black points are your data points. They don’t look Gaussian which probably why a pure Gaussian fit doesn’t work. Maybe a composite function could work better i.e. a Gaussian with a linear function.

---

<div class="post-metadata">

**Author:** ![Xavier\_Brock](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xavier_brock/32/20225_2.png) [@Xavier\_Brock](https://root-forum.cern.ch/u/Xavier_Brock)\
**Post date:** [November 12, 2019, 9:41pm UTC](https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790/3 "2019-11-12T21:41:43Z")

</div>

Thank you for your timely reply.

I didnt start using ROOT till a few days ago. I dont know how to read the XY values form the .root or anything in rest of solution. Could you provide the comand/code to do so or pooint me to the sources where I may figure out how to follow your solution?

---

<div class="post-metadata">

**Author:** ![ilam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ilam/32/19180_2.png) [@ilam](https://root-forum.cern.ch/u/ilam)\
**Post date:** [November 12, 2019, 9:58pm UTC](https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790/4 "2019-11-12T21:58:34Z")

</div>

Hi,

For reading the .root file, which I am assuming contains a TTree, this should get you started:  
[https://root.cern.ch/root/htmldoc/guides/users-guide/Trees.html](https://root.cern.ch/root/htmldoc/guides/users-guide/Trees.html)  
Specifically, look at section 14.14.

For the composite functions, this should help: [https://root.cern.ch/doc/master/classTF1.html](https://root.cern.ch/doc/master/classTF1.html) . Specifically, look at section 4.

Hopefully this can help you get started.

---

<div class="post-metadata">

**Author:** ![Xavier\_Brock](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xavier_brock/32/20225_2.png) [@Xavier\_Brock](https://root-forum.cern.ch/u/Xavier_Brock)\
**Post date:** [November 12, 2019, 10:27pm UTC](https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790/5 "2019-11-12T22:27:33Z")

</div>

I don’t know if it dose have a TTree,  
when I do root -l file.root  
i get  
Attaching file FCGa.root as \_file0…  
(TFile _) 0x26554b0  
root [1] .ls  
TFile_\* FCGa.root  
TFile\* FCGa.root  
KEY: TH1D 8;1 Edep (MeV.cm2/g) along x/r0

I dont see TTre listed here as I do in other examples.

Also, I have looked at the link you provided for the reading of the root. The solution is not jumping out at me but I can continue to look at it. I am really new to this so the extent of my knowledge is using the rootbrose and having cut and pasted the orginal file together to get two graphs .

I think this is what I want

```auto
   Int_t nentries = (Int_t)t1->GetEntries();
   for (Int_t i=0; i<nentries; i++) {
      t1->GetEntry(i);
      hpx->Fill(px);
      hpxpy->Fill(px,py);

```

where I would need to modify it for my situation where I already have the .root and don’t need to generate it. Most of my attempts to do anything with this file seems to fail because it’s named 8, which is an int.

I had assumed there would be a terminal command or way in the browser to get the points. It the only way do do this to write it into the C macro?

---

<div class="post-metadata">

**Author:** ![ilam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ilam/32/19180_2.png) [@ilam](https://root-forum.cern.ch/u/ilam)\
**Post date:** [November 12, 2019, 10:43pm UTC](https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790/6 "2019-11-12T22:43:27Z")

</div>

Hi,

Oh. Your .root file contains a TH1D object, which is a 1D histogram.So I think you can directly plot it. You can do something like this:

```auto
TFile *f1 = new TFile("yourfile.root","open"); //loads in the .root file
TH1D * h1 = (TH1D *)f1->Get("8"); //gets the TH1D object from the .root file
h1->Draw("same"); // draw on the same canvas as the rest.

```

See if this works.

---

<div class="post-metadata">

**Author:** ![Xavier\_Brock](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xavier_brock/32/20225_2.png) [@Xavier\_Brock](https://root-forum.cern.ch/u/Xavier_Brock)\
**Post date:** [November 13, 2019, 5:27pm UTC](https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790/7 "2019-11-13T17:27:28Z")

</div>

Thank you, I belive this will lead to it working, however I am geting a error:  
// Manage several graphs as a single entity.  
void multigraph(){  
TCanvas \*c1 = new TCanvas(“c1”,“multigraph”,700,500);  
c1-\>SetGrid();

auto mg = new TMultiGraph();

// create first graph  
auto gr1 = new TGraph(“Carrier\_Be\_5MeV\_recreation.csv”);  
gr1-\>SetMarkerColor(kBlue);  
gr1-\>SetMarkerStyle(20);  
mg-\>Add(gr1);

// create second graph

auto gr2 = new TGraph(“Carrier\_Be\_1MeV\_recreation.csv”);  
gr2-\>SetMarkerColor(kRed);  
gr2-\>SetMarkerStyle(20);  
mg-\>Add(gr2);

mg-\>Draw(“apl”);

TFile \*f1 = new TFile(“DDT5root”,“open”); //loads in the .root file  
TH1D \* h1 = (TH1D \*)f1-\>Get(“8”); //gets the TH1D object from the .root file  
h1-\>Draw(“same”); // draw on the same canvas as the rest.

gPad-\>Update();  
gPad-\>Modified();  
}  
In file included from input\_line\_9:1:  
/home/Badger/Documents/multigraph.C:25:4: warning: null passed to a callee that requires a non-null argument [-Wnonnull]  
h1-\>Draw(“same”); // draw on the same canvas as the rest.

I don’t know hwy this is but will continue to play with it and see if I can figure it out.

---

<div class="post-metadata">

**Author:** ![Xavier\_Brock](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xavier_brock/32/20225_2.png) [@Xavier\_Brock](https://root-forum.cern.ch/u/Xavier_Brock)\
**Post date:** [November 13, 2019, 5:33pm UTC](https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790/8 "2019-11-13T17:33:05Z")

</div>

lol, I had DDT5root not DDT5.root

This allowed it to graph  
I do have a scaling issue but that is managable.  
Thank you so much  
the final code fopr reference  
// Manage several graphs as a single entity.  
void multigraph(){  
TCanvas \*c1 = new TCanvas(“c1”,“multigraph”,700,500);  
c1-\>SetGrid();

auto mg = new TMultiGraph();

// create first graph  
auto gr1 = new TGraph(“Carrier\_Be\_5MeV\_recreation.csv”);  
gr1-\>SetMarkerColor(kBlue);  
gr1-\>SetMarkerStyle(20);  
mg-\>Add(gr1);

// create second graph

auto gr2 = new TGraph(“Carrier\_Be\_1MeV\_recreation.csv”);  
gr2-\>SetMarkerColor(kRed);  
gr2-\>SetMarkerStyle(20);  
mg-\>Add(gr2);

mg-\>Draw(“apl”);

TFile \*f1 = new TFile(“DDT5.root”,“open”); //loads in the .root file  
TH1D \* h1 = (TH1D \*)f1-\>Get(“8”); //gets the TH1D object from the .root file  
h1-\>Draw(“same”); // draw on the same canvas as the rest.

gPad-\>Update();  
gPad-\>Modified();  
}

p.s chii is the best cat , love your icon picture. And thanks again for your help.

---

<div class="post-metadata">

**Author:** ![ilam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ilam/32/19180_2.png) [@ilam](https://root-forum.cern.ch/u/ilam)\
**Post date:** [November 13, 2019, 6:26pm UTC](https://root-forum.cern.ch/t/how-to-extract-the-xy-values-from-root-file/36790/9 "2019-11-13T18:26:59Z")

</div>

Hi,

Great! Glad that worked out. Yea, Chi is adorable!
