# How to get graph from a TCanvas

**URL:** <https://root-forum.cern.ch/t/how-to-get-graph-from-a-tcanvas/43243>\
**Category:** ROOT\
**Tags:** graphics\
**Created:** [January 25, 2021, 4:12am UTC](https://root-forum.cern.ch/t/how-to-get-graph-from-a-tcanvas/43243 "2021-01-25T04:12:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![renqipan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/renqipan/32/22738_2.png) [@renqipan](https://root-forum.cern.ch/u/renqipan)\
**Post date:** [January 25, 2021, 4:12am UTC](https://root-forum.cern.ch/t/how-to-get-graph-from-a-tcanvas/43243/1 "2021-01-25T04:12:30Z")

</div>

Hi experts,  
I have a canvas stored in a .root file.  
when I open the root file, I can see a key "KEY: TCanvas can1;1 ".  
If I draw the canvas directly using “can1-\>Draw();”, I would get three graphs. How can I get each graphs from the canvas. I don’t know their primitive names. I show the content of the canvas as following.

```auto
root [18] can1->ls()
Canvas Name=can1 Title=CANVAS-SCAN1D Option=
 TCanvas fXlowNDC=0 fYlowNDC=0 fWNDC=1 fHNDC=1 Name= can1 Title= CANVAS-SCAN1D Option=
  OBJ: TList	TList	Doubly linked list : 0
   TFrame X1= -1.004789 Y1=0.000000 X2=1.004789 Y2=18.000000
   OBJ: TGraph	Exp1D : 1 at: 0x563aaec253c0
   OBJ: TGraph	Exp1D : 1 at: 0x563aaec4aeb0
   OBJ: TGraph	Exp1D : 1 at: 0x563aaec59c50
   TLine X1=-1.000000 Y1=1.000000 X2=1.000000 Y2=1.000000
   TLine X1=0.000000 Y1=0.000000 X2=0.000000 Y2=0.000000
   TLine X1=-1.000000 Y1=3.840000 X2=1.000000 Y2=3.840000
   TLine X1=0.000000 Y1=0.000000 X2=0.000000 Y2=0.000000
   OBJ: TGraph	Exp1D : 0 at: 0x563aaecce1c0
   OBJ: TGraph	Exp1D : 1 at: 0x563aaec253c0
   OBJ: TGraph	Exp1D : 1 at: 0x563aaec253c0

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 25, 2021, 9:01am UTC](https://root-forum.cern.ch/t/how-to-get-graph-from-a-tcanvas/43243/2 "2021-01-25T09:01:13Z")

</div>

```auto
TGraph *g = (TGraph*)can1->GetListOfPrimitives()->FindObject("Exp1D");

```

---

<div class="post-metadata">

**Author:** ![renqipan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/renqipan/32/22738_2.png) [@renqipan](https://root-forum.cern.ch/u/renqipan)\
**Post date:** [January 25, 2021, 9:23am UTC](https://root-forum.cern.ch/t/how-to-get-graph-from-a-tcanvas/43243/3 "2021-01-25T09:23:22Z")

</div>

Hi,  
Thanks for your suggestion.  
There are three graphs. I tried this method, but I can only obtain the first graph.  
How can I obtain all of them.  
thanks.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 25, 2021, 9:28am UTC](https://root-forum.cern.ch/t/how-to-get-graph-from-a-tcanvas/43243/4 "2021-01-25T09:28:23Z")

</div>

Yes they all have the same name … too bad.  
May be something similar to what is done in this HowTo can help:

> [@How to draw an object with special characters in its name?](https://root-forum.cern.ch/t/how-to-draw-an-object-with-special-characters-in-its-name/28248):
>
> In general it is not a good idea to have object’s name containing special character like “”, “/”, “#” etc … or even have object name starting with a number because they might be not accessible from the ROOT command line. For instance “/” is the separator for directory level in a file therefore an object having a “/” in its name cannot be access from the command line. Nevertheless it may happen that the some object can be named that way and stored in a ROOT file. The following macro show how to…

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [February 8, 2021, 9:28am UTC](https://root-forum.cern.ch/t/how-to-get-graph-from-a-tcanvas/43243/5 "2021-02-08T09:28:28Z")

</div>

Closed on February 8, 2021, 9:28am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
