# Can I make TGraph from two histograms

**URL:** <https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056>\
**Category:** Newbie\
**Tags:** root\
**Created:** [May 26, 2023, 1:48pm UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056 "2023-05-26T13:48:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gkoe](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@gkoe](https://root-forum.cern.ch/u/gkoe)\
**Post date:** [May 26, 2023, 1:48pm UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/1 "2023-05-26T13:48:08Z")

</div>

Hi,  
I have a root file with few histograms in it. I have to make a TGraph - on xAaxis to have data form one histogram, and yAxis - from other histogram. Is it possible? And can you give me some hints how to do it?  
Thanks!

---

<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:** [May 26, 2023, 2:15pm UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/2 "2023-05-26T14:15:20Z")

</div>

Can it be that you want a so-called [QQ-plot](https://root.cern/doc/master/classTH1.html#adb27b38cd934622c8702c402bb840b23)?

---

<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 26, 2023, 2:38pm UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/3 "2023-05-26T14:38:46Z")

</div>

> [@gkoe](#):
>
> I have to make a TGraph - on xAaxis to have data form one histogram, and yAxis - from other histogram. Is it possible?

may be something like that:

```auto
{

   // Open the Root file containing your histograms

   auto file = new TFile("YourRootFile.root");

   // Assuming you have two histograms named "histogramX" and "histogramY"
   
   TH1F *histogramX = (TH1F*)file->Get("histogramX"); // Retrieve the first histogram
   TH1F *histogramY = (TH1F*)file->Get("histogramY"); // Retrieve the second histogram

   int numBinsX = histogramX->GetNbinsX();

   TGraph *graph = new TGraph();

   for (int i = 1; i <= numBinsX; ++i)
   {
      double x = histogramX->GetBinCenter(i); // Use bin center as x-value
      double y = histogramY->GetBinContent(i); // Use bin content as y-value

      graph->AddPoint(x, y); // Add the point in the TGraph
   }

   // Draw the TGraph
   graph->Draw("A*L"); // APL option is used to draw points with lines
}

```

---

<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:** [May 30, 2023, 1:47pm UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/7 "2023-05-30T13:47:43Z")

</div>

@couet Well, the code is correct now, but I think it is worse than the “original” version, especially for a “new user”. You now start with an empty graph, but then the “AddPoint” will often need to reallocate the arrays, which always takes additional time. In the “original” version, you nicely allocated the correct arrays just once at the beginning and only then filled them (so that “inexperienced users” could also learn how to avoid the “costly” memory reallocations).

---

<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, 2023, 1:54pm UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/8 "2023-05-30T13:54:13Z")

</div>

I was more thinking about code simplicity. But yes the old one has the advantages you describe. here it is:

```auto
{

   // Open the Root file containing your histograms

   auto file = new TFile("YourRootFile.root");

   // Assuming you have two histograms named "histogramX" and "histogramY"
   
   TH1F *histogramX = (TH1F*)file->Get("histogramX"); // Retrieve the first histogram
   TH1F *histogramY = (TH1F*)file->Get("histogramY"); // Retrieve the second histogram

   int numBinsX = histogramX->GetNbinsX();

   TGraph *graph = new TGraph(numBinsX);

   for (int i = 1; i <= numBinsX; ++i)
   {
      double x = histogramX->GetBinCenter(i); // Use bin center as x-value
      double y = histogramY->GetBinContent(i); // Use bin content as y-value

      graph->SetPoint(i-1,x, y); // Set the point in the TGraph
   }

   // Draw the TGraph
   graph->Draw("A*L"); // APL option is used to draw points with lines
}

```

---

<div class="post-metadata">

**Author:** ![gkoe](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@gkoe](https://root-forum.cern.ch/u/gkoe)\
**Post date:** [May 31, 2023, 9:00am UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/9 "2023-05-31T09:00:36Z")

</div>

Thank you very much. I check the code, but in this case I get data from two histos in Xaxis. I want to have data from one histos on X, other on Y. If I have two histograms : Mass and Temperature for example, I want to make graph Mass vs Temperature.

---

<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 31, 2023, 9:04am UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/11 "2023-05-31T09:04:46Z")

</div>

> [@gkoe](#):
>
> I want to have data from one histos on X, other on Y.

That’s what is done:

```auto
   double x = histogramX->GetBinCenter(i); // Use bin center as x-value
   double y = histogramY->GetBinContent(i); // Use bin content as y-value

```

---

<div class="post-metadata">

**Author:** ![gkoe](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@gkoe](https://root-forum.cern.ch/u/gkoe)\
**Post date:** [May 31, 2023, 9:05am UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/12 "2023-05-31T09:05:49Z")

</div>

It looks like working. 🙂 Thank you!

---

<div class="post-metadata">

**Author:** ![gkoe](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@gkoe](https://root-forum.cern.ch/u/gkoe)\
**Post date:** [May 31, 2023, 9:11am UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/13 "2023-05-31T09:11:28Z")

</div>

![Screenshot from 2023-05-31 12-09-27](https://root-forum.cern.ch/uploads/default/original/3X/5/6/56f38cb425dcbad3a02eb5888e1a17a271e62556.png)

here is what I get. By X - From -400 to 0 is histogramY, from 0 to 400 is the histogramX.

---

<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 31, 2023, 9:28am UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/14 "2023-05-31T09:28:58Z")

</div>

> [@gkoe](#):
>
> here is what I get. By X - From -400 to 0 is histogramY, from 0 to 400 is the histogramX.

And that’s what you expected?

---

<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:** [May 31, 2023, 5:13pm UTC](https://root-forum.cern.ch/t/can-i-make-tgraph-from-two-histograms/55056/15 "2023-05-31T17:13:27Z")

</div>

> [@gkoe](#):
>
> If I have two histograms : Mass and Temperature for example, I want to make graph Mass vs Temperature.

It seems to me that “Mass vs Temperature” means that you want to have a [TH2](https://root.cern/doc/master/classTH2.html) histogram.  
You cannot get it by combining two [TH1](https://root.cern/doc/master/classTH1.html) histograms (the opposite way is possible, though; search for “profile” and “projection” in the [TH2](https://root.cern/doc/master/classTH2.html) class reference).
