# How to plot two graph in one canvas from two separate root file, the key of the graph is canvas

**URL:** <https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013>\
**Category:** Newbie\
**Created:** [May 18, 2022, 10:38am UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013 "2022-05-18T10:38:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pjana](https://root-forum.cern.ch/u/pjana)\
**Post date:** [May 18, 2022, 10:38am UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/1 "2022-05-18T10:38:38Z")

</div>

Hello,  
I want to plot two graph from two separate root files into one canvas. I am able to get one plot but I could not able to plot two in one canvas. The key of the plot in the root file is canvas.  
I have attached my code here.  
I would be very grateful if anyone helps me.  
Thanks!  
[first.root](https://root-forum.cern.ch/uploads/short-url/9UPpizrhVjG2qch4LV1hZxIOAV4.root) (11.7 KB)  
[second.root](https://root-forum.cern.ch/uploads/short-url/f5I6afM6mGLUNLDFXSeGIRjRpJz.root) (11.7 KB)

```auto
void readplot(){
TFile* f1 = new TFile("first.root","READ");
TFile* f2 = new TFile("second.root","READ");
TCanvas* h1;
TCanvas* h2;
f1->GetObject("c",h1);
f2->GetObject("c",h2);
h1->Draw();
//h1->SetLineColor(kRed); 
//h2->SetLineColor(kBlue);
h2->Draw("same"); 
} 

```

---

<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:** [May 18, 2022, 11:46am UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/2 "2022-05-18T11:46:15Z")

</div>

You access TCanvas. You cannot superimpose TCanavs like histograms.  
Moreover your canvases do not contain histograms they contains `TGraphAsymmErrors `  
You can try to get them from the canvas with:

```auto
TGraphAsymmErrors *g1 = (TGraphAsymmErrors*)c1->GetListOfPrimitives()->FindObject(.......);

```

---

<div class="post-metadata">

**Author:** ![pjana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pjana](https://root-forum.cern.ch/u/pjana)\
**Post date:** [May 18, 2022, 12:30pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/3 "2022-05-18T12:30:12Z")

</div>

Hello,  
Many thanks!  
Now I updated my code:

```auto
void Test()
{

TFile *f1 = TFile::Open("first.root");
TCanvas *c1 = (TCanvas*)f1->Get("c");
TGraphErrors *g1 = (TGraphErrors*)c1->GetPrimitive("Graph");
g1->GetYaxis()->SetRangeUser(-3,3);
g1->GetXaxis()->SetLimits(-0.5,0.5);
c1->Draw();
c1->SetGridy(0);
c1->SetGridx(0);

TFile *f2 = TFile::Open("second.root");
TCanvas *c2 = (TCanvas*)f2->Get("c");
TGraphErrors *g2 = (TGraphErrors*)c2->GetPrimitive("Graph");
g2->GetYaxis()->SetRangeUser(-3,3);
g2->GetXaxis()->SetLimits(-0.5,0.5);
c2->Draw();
c2->SetGridy(0);
c2->SetGridx(0);
}

```

Now I am getting error like

```auto
warning: null passed to a callee that requires a non-null argument [-Wnonnull]
g1->GetYaxis()->SetRangeUser(-3,3);

```

I am not sure what should I fill the rang. Could you please help me to fix this?

---

<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:** [May 18, 2022, 1:05pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/4 "2022-05-18T13:05:49Z")

</div>

You should not stored the canvases. You should store the TGraphs. Storing the canvases is not practical at all !  
Any way with these two files you can do some tricks… but that’s really not straight forward:

```auto
void readplot(){
   TFile *f1 = TFile::Open("first.root");
   TCanvas *c1 = (TCanvas*)f1->Get("c");
   TPad *p1 = (TPad*)c1->GetPrimitive("p2");
   TGraphAsymmErrors *g1 = (TGraphAsymmErrors*)p1->GetPrimitive("2_Y_0190");

   TFile *f2 = TFile::Open("second.root");
   TCanvas *c2 = (TCanvas*)f2->Get("c");
   TPad *p2 = (TPad*)c2->GetPrimitive("p2");
   TGraphAsymmErrors *g2 = (TGraphAsymmErrors*)p2->GetPrimitive("2_Y_0190");

   auto C = new TCanvas();
   g1->Draw();
   g2->Draw("*");
}

```

 ![Screenshot 2022-05-18 at 15.03.49](https://root-forum.cern.ch/uploads/default/original/3X/0/4/049364e99dc98d53fd67afc6341f68e149e65792.png)

---

<div class="post-metadata">

**Author:** ![pjana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pjana](https://root-forum.cern.ch/u/pjana)\
**Post date:** [May 28, 2022, 7:10am UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/5 "2022-05-28T07:10:32Z")

</div>

Hello,  
Using this script, I am not able to show the X axis range. Could you please give some suggestions that how can I show the X axis range also?

[Lumi.C](https://root-forum.cern.ch/uploads/short-url/d9FAW0G9MRkDDjxPr4pidsceGzt.C) (1.4 KB)

---

<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:** [May 30, 2022, 11:36am UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/6 "2022-05-30T11:36:20Z")

</div>

I cannot run your macro.

```auto
Processing Lumi.C...
Error in <TFile::TFile>: file /Users/couet/Downloads/2_Y_0190_PLT.root does not exist
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
In file included from input_line_9:1:
/Users/couet/Downloads/Lumi.C:4:28: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
   TCanvas *c1 = (TCanvas*)f1->Get("c");
                           ^~

```

---

<div class="post-metadata">

**Author:** ![pjana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pjana](https://root-forum.cern.ch/u/pjana)\
**Post date:** [May 30, 2022, 11:42am UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/7 "2022-05-30T11:42:19Z")

</div>

I ran using the below command.

> root -l Lumi.C  
> It is producing the plots. I have attached  
> [first.root](https://root-forum.cern.ch/uploads/short-url/9UPpizrhVjG2qch4LV1hZxIOAV4.root) (11.7 KB)  
> [second.root](https://root-forum.cern.ch/uploads/short-url/f5I6afM6mGLUNLDFXSeGIRjRpJz.root) (11.7 KB)  
> here the root file also.  
> [2\_Y\_0190\_BCM1FPCVD.root](https://root-forum.cern.ch/uploads/short-url/nYHqbXNRTJIXZ8UZzlHdcFdqQ5Y.root) (11.7 KB)

---

<div class="post-metadata">

**Author:** ![pjana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pjana](https://root-forum.cern.ch/u/pjana)\
**Post date:** [May 30, 2022, 11:44am UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/8 "2022-05-30T11:44:04Z")

</div>

Here is the updated script.  
[Lumi.C](https://root-forum.cern.ch/uploads/short-url/xRwiNr4zPHFafOTDyNmChI5BVMD.C) (1.3 KB)

---

<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:** [May 30, 2022, 11:48am UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/9 "2022-05-30T11:48:00Z")

</div>

Still does not work:

```auto
root [0] 
Processing Lumi.C...
Error in <TFile::TFile>: file /Users/couet/Downloads/2_Y_0190_PLT.root does not exist
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
In file included from input_line_9:1:
/Users/couet/Downloads/Lumi.C:4:28: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
   TCanvas *c1 = (TCanvas*)f1->Get("c");
                           ^~

```

---

<div class="post-metadata">

**Author:** ![pjana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pjana](https://root-forum.cern.ch/u/pjana)\
**Post date:** [May 30, 2022, 11:57am UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/10 "2022-05-30T11:57:58Z")

</div>

Here is the updated script and root files  
[second.root](https://root-forum.cern.ch/uploads/short-url/f5I6afM6mGLUNLDFXSeGIRjRpJz.root) (11.7 KB)  
[2\_Y\_0190\_BCM1FPCVD.root](https://root-forum.cern.ch/uploads/short-url/nYHqbXNRTJIXZ8UZzlHdcFdqQ5Y.root) (11.7 KB)  
[Lumi.C](https://root-forum.cern.ch/uploads/short-url/4cHgeyWR1dN4AD9TfjawyGvC52b.C) (1.3 KB)

[first.root](https://root-forum.cern.ch/uploads/short-url/9UPpizrhVjG2qch4LV1hZxIOAV4.root) (11.7 KB)

It should work now.

Many 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:** [May 30, 2022, 12:10pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/11 "2022-05-30T12:10:49Z")

</div>

```auto
void Lumi(){

   TFile *f1 = TFile::Open("first.root");
   TCanvas *c1 = (TCanvas*)f1->Get("c");
   TPad *p1 = (TPad*)c1->GetPrimitive("p2");
   TGraphAsymmErrors *g1 = (TGraphAsymmErrors*)p1->GetPrimitive("2_Y_0190");
   g1->SetMarkerStyle(kCircle);
   g1->SetTitle("Y Plane scan, BCID 190//2017 XeXe,5.44 TeV");
   g1->SetMarkerColor(kRed);

   TFile *f2 = TFile::Open("second.root");
   TCanvas *c2 = (TCanvas*)f2->Get("c");
   TPad *p2 = (TPad*)c2->GetPrimitive("p2");
   TGraphAsymmErrors *g2 = (TGraphAsymmErrors*)p2->GetPrimitive("2_Y_0190");
   g2->SetMarkerStyle(kOpenSquare);
   g2->SetTitle("CMS Preliminary");
   g2->SetMarkerColor(kBlue);

   TFile *f3 = TFile::Open("2_Y_0190_BCM1FPCVD.root");
   TCanvas *c3 = (TCanvas*)f3->Get("c");
   TPad *p3 = (TPad*)c3->GetPrimitive("p2");
   TGraphAsymmErrors *g3 = (TGraphAsymmErrors*)p3->GetPrimitive("2_Y_0190");
   g3->SetLineColor(2);
   g3->SetTitle("VdM Scan:Fill 6295");
   g3->SetMarkerStyle(kFullSquare);
   g3->SetMarkerColor(kGreen);

   auto C = new TCanvas();
   auto mg = new TMultiGraph();
   mg->Add(g1);
   mg->Add(g2);
   mg->Add(g3);
   mg->Draw("AL*");
}

```

 ![Screenshot 2022-05-30 at 14.10.36](https://root-forum.cern.ch/uploads/default/original/3X/0/b/0bc83a89c939e834df5f6eb9a5a0e608af94c24a.png)

---

<div class="post-metadata">

**Author:** ![pjana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pjana](https://root-forum.cern.ch/u/pjana)\
**Post date:** [May 30, 2022, 12:17pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/12 "2022-05-30T12:17:00Z")

</div>

Thanks so much!!!

---

<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:** [May 30, 2022, 12:24pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/13 "2022-05-30T12:24:33Z")

</div>

You’re welcome. But it would be much better to store directly the TGraphs instead of the TCanvas and the TPad, The way you proceed is really not practical.

---

<div class="post-metadata">

**Author:** ![pjana](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pjana](https://root-forum.cern.ch/u/pjana)\
**Post date:** [May 30, 2022, 12:31pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-graph-in-one-canvas-from-two-separate-root-file-the-key-of-the-graph-is-canvas/50013/14 "2022-05-30T12:31:08Z")

</div>

Sorry! I will keep in mind next time.  
Thank you!
