# Adding points from TGraph into another TGraph

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?

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

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:

``````        TGraph2D *gHadron_2D_1 = new TGraph2D(5);

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->SetPoint(0, 1.5, 11.3, 3.1);
gHadron_2D_2->SetPoint(1, 4.0, 6.1, 5.2);

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
{
}

``````

I tried it with:

``````  Double_t 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.

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)…

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

``````TString filethermal = "/Users/thermal2D_0_10_highmass_12_10_2021.root";
TFile *file_withchiral = TFile::Open(filethermal,"READ");

TString filethermal2 = "/Users/thermal2D_0_10_withchiral.root";
TFile *file_withchiral2 = TFile::Open(filethermal2,"READ");
``````

thermal2D_0_10_highmass_12_10_2021.root (33.3 KB)
thermal2D_0_10_withchiral.root (279.6 KB)

@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).

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

TFile *file_withchiral = TFile::Open("thermal2D_0_10_highmass_12_10_2021.root","READ");
c->cd(1);

TFile *file_withchiral2 = TFile::Open("thermal2D_0_10_withchiral.root","READ");
c->cd(2);

auto g = new TGraph2D ();
double x,y,z;
int k;
for(k=0; k < gHadron_2D_1->GetN(); k++){
}
for(k=0; k < gHadron_2D_l2->GetN(); k++){
}
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");
}
``````
``````Number of points in gHadron_2D_1  = 1280
Number of points in gHadron_2D_l2 = 7120
Number of points in g             = 8400
``````

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

1 Like

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?

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

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

``````   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++){