# Adding points from TGraph into another TGraph

**URL:** <https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316>\
**Category:** Newbie\
**Created:** [June 8, 2022, 2:31pm UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316 "2022-06-08T14:31:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [June 8, 2022, 2:31pm UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/1 "2022-06-08T14:31:15Z")

</div>

Hi,

I have 2 TGraph2D (gHadron\_2D\_1 and gHadron\_2D\_2) , one has a range of 0-1.5 in x-axis and the other has a range of 1.5-4 in x-axis. I want to connect these 2 Graphes. I tried to do this following code but the output is wrong. It doesn’t take the full range of x-axis. Can somebody help me?

```auto
Double_t x,y,z;
    for(Int_t k=0; k < gHadron_2D_1->GetN(); ++k){
		gHadron_2D_1->GetPoint(k,x,y,z);
		gHadron_2D_2->SetPoint(k,x,y,z);
    }

```

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 8, 2022, 4:36pm UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/2 "2022-06-08T16:36:30Z")

</div>

Hi,

can you show what is not working as expected exactly? I ran the following and I think the final `TGraph2D` is okay, see below:

```auto
        TGraph2D *gHadron_2D_1 = new TGraph2D(5);
        gHadron_2D_1->SetName("originalGraph");

        gHadron_2D_1->SetPoint(0, 0.0, 10.4, 2.1); // some arbitrary numbers
        gHadron_2D_1->SetPoint(1, 0.8, 8.1, 6.2);
        gHadron_2D_1->SetPoint(2, 0.3, 15.6, 1.7);
        gHadron_2D_1->SetPoint(3, 1.4, 9.7, 1.5);
        gHadron_2D_1->SetPoint(4, 0.7, 5.3, 7.3);

        TGraph2D *gHadron_2D_2 = new TGraph2D(5);
        gHadron_2D_2->SetName("secondGraph");
        gHadron_2D_2->SetPoint(0, 1.5, 11.3, 3.1);
        gHadron_2D_2->SetPoint(1, 4.0, 6.1, 5.2);

        const Int_t nPredefinedPointsOngHadron_2D_2 = gHadron_2D_2->GetN();
        Double_t x,y,z;
        for( Int_t k=nPredefinedPointsOngHadron_2D_2; k < gHadron_2D_1->GetN() + nPredefinedPointsOngHadron_2D_2; ++k) // k from 2 (because we already had two points predefined in gHadron_2D_2) through 2+5=7
        {
                gHadron_2D_1->GetPoint(k-nPredefinedPointsOngHadron_2D_2,x,y,z);
                gHadron_2D_2->SetPoint(k,x,y,z);
        }

        gHadron_2D_2->Draw();

```

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/4/9/4980da937e8a59cf10425111de9c5247b92a8385.png)

---

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [June 8, 2022, 8:25pm UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/3 "2022-06-08T20:25:59Z")

</div>

I tried it with:

```auto
  Double_t x,y,z;
  Int_t nPredefinedPointsHadron = gHadron_2D_2->GetN();
  for(Int_t k= nPredefinedPointsHadron; k < gHadron_2D_1->GetN() + nPredefinedPointsHadron; ++k){
    
    gHadron_2D_1->GetPoint(k-nPredefinedPointsHadron,x,y,z);
    gHadron_2D_2->SetPoint(k,x,y,z);
    cout << "Point: " << k << " m: " << x << " pt: " << y << " yield: " << z << endl;
		
  }

```

The problem is that gHadron\_2D\_1 range of points is gonna be from 7000 to 8000.  
In my case the gHadron\_2D\_1 has 7000 points and gHadron\_2D\_2 1000 points.  
I did a cout in the for loop and it shows me only the values from 7000 to 8000.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 8, 2022, 8:40pm UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/4 "2022-06-08T20:40:33Z")

</div>

Can you maybe provide a minimal reproducer along with its textual output? It’s a bit difficult to understand what is it exactly you are doing (and observing)…

---

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [June 8, 2022, 9:28pm UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/5 "2022-06-08T21:28:49Z")

</div>

I have attached you 2 root files where you get the TGraph2D. Just focus on the TGraph2D “Hadron” from these files. You can test it

```auto
TString filethermal = "/Users/thermal2D_0_10_highmass_12_10_2021.root";
TFile *file_withchiral = TFile::Open(filethermal,"READ");
TGraph2D *gHadron_2D_1 = (TGraph2D *) file_withchiral->Get("Hadron");

TString filethermal2 = "/Users/thermal2D_0_10_withchiral.root";
TFile *file_withchiral2 = TFile::Open(filethermal2,"READ");
TGraph2D *gHadron_2D_l2 = (TGraph2D *) file_withchiral2->Get("Hadron");

```

[thermal2D\_0\_10\_highmass\_12\_10\_2021.root](https://root-forum.cern.ch/uploads/short-url/zzdKhUA9suKQBwXH2oygwUJaRyt.root) (33.3 KB)  
[thermal2D\_0\_10\_withchiral.root](https://root-forum.cern.ch/uploads/short-url/wGdo06WOOudS2E8uIbkYVJRkhA0.root) (279.6 KB)

---

<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:** [June 9, 2022, 6:51am UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/7 "2022-06-09T06:51:23Z")

</div>

@zmomtaz The loop in the source code provided by @yus is adding points from the “`gHadron_2D_1`” to the “`gHadron_2D_2`” (so your “`cout`” shows only the “newly added” values).

---

<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:** [June 9, 2022, 7:04am UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/9 "2022-06-09T07:04:10Z")

</div>

```auto
{
   auto c = new TCanvas();
   c->Divide(3,1);

   TFile *file_withchiral = TFile::Open("thermal2D_0_10_highmass_12_10_2021.root","READ");
   TGraph2D *gHadron_2D_1 = (TGraph2D *) file_withchiral->Get("Hadron");
   c->cd(1);
   gHadron_2D_1->Draw("P0");

   TFile *file_withchiral2 = TFile::Open("thermal2D_0_10_withchiral.root","READ");
   TGraph2D *gHadron_2D_l2 = (TGraph2D *) file_withchiral2->Get("Hadron");
   c->cd(2);
   gHadron_2D_l2->Draw("P0");

   auto g = new TGraph2D ();
   double x,y,z;
   int k;
   for(k=0; k < gHadron_2D_1->GetN(); k++){
      gHadron_2D_1->GetPoint(k,x,y,z);
      g->AddPoint(x,y,z);
   }
   for(k=0; k < gHadron_2D_l2->GetN(); k++){
      gHadron_2D_l2->GetPoint(k,x,y,z);
      g->AddPoint(x,y,z);
   }
   printf("Number of points in gHadron_2D_1 = %d\n",gHadron_2D_1->GetN());
   printf("Number of points in gHadron_2D_l2 = %d\n",gHadron_2D_l2->GetN());
   printf("Number of points in g = %d\n",g->GetN());
   c->cd(3);
   g->Draw("P0");
}

```

```auto
Number of points in gHadron_2D_1 = 1280
Number of points in gHadron_2D_l2 = 7120
Number of points in g = 8400

```

 ![Screenshot 2022-06-09 at 09.03.57](https://root-forum.cern.ch/uploads/default/original/3X/0/e/0ee397214e9928117591a5450e575573310f34cd.png)

---

<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:** [June 9, 2022, 7:09am UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/10 "2022-06-09T07:09:34Z")

</div>

@couet Better:  
`auto g = new TGraph2D(gHadron_2D_1->GetN() + gHadron_2D_l2->GetN());`

---

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [June 9, 2022, 7:13am UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/11 "2022-06-09T07:13:44Z")

</div>

Oh I am using an older Root version, so the function addpoint is not available in this version. Is it also possible to do it with Setpoint?

---

<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:** [June 9, 2022, 7:14am UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/12 "2022-06-09T07:14:53Z")

</div>

`g->SetPoint(g->GetN(), x, y, z);`

---

<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:** [June 9, 2022, 7:17am UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/13 "2022-06-09T07:17:05Z")

</div>

Yes of course it is possible, you need to do:

```auto
   int n1 = gHadron_2D_1->GetN();
   int n2 = gHadron_2D_l2->GetN();
   int n = n1+n2;
   auto g = new TGraph2D (n);
   double x,y,z;
   int k;
   for(k=0; k < n1; k++){
      gHadron_2D_1->GetPoint(k,x,y,z);
      g->SetPoint(k,x,y,z);
or
      g->SetPoint(g->GetN(), x, y, z);
   }
   for(k=0; k < n2; k++){
      gHadron_2D_l2->GetPoint(k,x,y,z);
      g->SetPoint(k+n1,x,y,z); 
or
      g->SetPoint(g->GetN(), x, y, z);
   }

```

---

<div class="post-metadata">

**Author:** ![zmomtaz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bbe5ce/32.png) [@zmomtaz](https://root-forum.cern.ch/u/zmomtaz)\
**Post date:** [June 10, 2022, 9:01am UTC](https://root-forum.cern.ch/t/adding-points-from-tgraph-into-another-tgraph/50316/14 "2022-06-10T09:01:54Z")

</div>

It worked, thanks to everyone!
