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){
		gHadron_2D_1->GetPoint(k,x,y,z);
		gHadron_2D_2->SetPoint(k,x,y,z);
    }

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->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();

I tried it with:

  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.

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");
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 (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");
   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");
}
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++){
      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);
   }

It worked, thanks to everyone!