# Compare TGraphs from several ROOT files

**URL:** <https://root-forum.cern.ch/t/compare-tgraphs-from-several-root-files/52055>\
**Category:** Newbie\
**Created:** [October 22, 2022, 7:22am UTC](https://root-forum.cern.ch/t/compare-tgraphs-from-several-root-files/52055 "2022-10-22T07:22:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex14](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ab097/32.png) [@alex14](https://root-forum.cern.ch/u/alex14)\
**Post date:** [October 22, 2022, 7:22am UTC](https://root-forum.cern.ch/t/compare-tgraphs-from-several-root-files/52055/1 "2022-10-22T07:22:41Z")

</div>

Hello,

I’m a root beginner, and I would like to ask for help with the following. I want to open two or several root files where I have stored TGraphs and compare them by drawing them on the same canvas. Could you please give me some advice or a basic code on how to do this? I appreciate the help.

Cheers,  
Alex

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [October 22, 2022, 12:45pm UTC](https://root-forum.cern.ch/t/compare-tgraphs-from-several-root-files/52055/2 "2022-10-22T12:45:56Z")

</div>

To plot them together it’s better to use a [TMultiGraph](https://root.cern/doc/master/classTMultiGraph.html); something like:

```auto
  // do similarly for all files/graphs:
  TFile *f1 = new TFile("file1.root");
  TGraph *g1 = (TGraph*)f1->Get("graph1"); // supposing file1.root contains graph1
  // ... (f2, g2, etc.)

  // then add all graphs to a multigraph and draw it:
  TMultiGraph *mg = new TMultiGraph();
  mg->Add(g1);
  mg->Add(g2);
  // ...
  mg->Draw("Al*");

```

Drawing options here:  
[https://root.cern/doc/master/classTGraphPainter.html](https://root.cern/doc/master/classTGraphPainter.html)

---

<div class="post-metadata">

**Author:** ![alex14](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ab097/32.png) [@alex14](https://root-forum.cern.ch/u/alex14)\
**Post date:** [October 22, 2022, 5:24pm UTC](https://root-forum.cern.ch/t/compare-tgraphs-from-several-root-files/52055/3 "2022-10-22T17:24:57Z")

</div>

Hi, thank you so much for your answer! I just have one more question. Unfortunately, when I try to run a macro I’m getting this error:

**error:**  **no matching member function for call to ‘Add’**

mg-\>Add(“gr1”);

** ~~~~^~~ **

**/usr/local/Cellar/root/6.26.06\_1/include/root/TMultiGraph.h:53:22: note:** candidate function not viable: no known conversion from ‘const char [4]’ to

‘TGraph \*’ for 1st argument

virtual void Add(TGraph \*graph, Option\_t \*chopt=“”);

I tried several solutions including the root suggestion, but none of them seems to work. Can you tell me if you see something? Thanks.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [October 22, 2022, 6:06pm UTC](https://root-forum.cern.ch/t/compare-tgraphs-from-several-root-files/52055/4 "2022-10-22T18:06:13Z")

</div>

Sorry, there were extra quotes in Add (should be `Add(g1)` etc.); now it’s corrected.

---

<div class="post-metadata">

**Author:** ![alex14](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ab097/32.png) [@alex14](https://root-forum.cern.ch/u/alex14)\
**Post date:** [October 22, 2022, 6:12pm UTC](https://root-forum.cern.ch/t/compare-tgraphs-from-several-root-files/52055/5 "2022-10-22T18:12:33Z")

</div>

Thanks again for your answer. I already tried that, but I’m getting:

\*\*\* Break \*\*\* segmentation violation

every time. I don’t know why.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 23, 2022, 2:21pm UTC](https://root-forum.cern.ch/t/compare-tgraphs-from-several-root-files/52055/6 "2022-10-23T14:21:19Z")

</div>

It looks like your graph was not found in your file.  
You can try: `if (g...) mg->Add(g...);`  
Try also: `rootls -l yourfile.root`
