Multiple TGraphs

ROOT Version: 6
Platform: Windows
Compiler: Ubuntu


I want to write an x-y graph and an x-z graph with x, y, z defined in the following source code. How can I create two TGraphs? As shown in this source code, I defined TGraph twice and tried it, but it didn’t work.

{
int n = 197;
gRandom->SetSeed();
TGraph *g=new TGraph(n);
TGraph *g1=new TGraph(n);
for(int cnt =0.0;cnt<n;){
double x=gRandom->Rndm()*40.0-20.0;
double y=gRandom->Rndm()*40.0-20.0;
double z=gRandom->Rndm()*40.0-20.0;

  if((x*x+y*y+z*z)<400.0){
  cnt+=1;
  g->SetPoint(cnt,x,y);
  g1->SetPoint(cnt,x,z);
}
}

g->SetTitle(";x:;y");
g->SetMarkerStyle(20);
g->Draw(“AP”);

g1->SetTitle(";x:;z");
g1->SetMarkerStyle(20);
g1->Draw(“AP”);
}

It does work, but you are drawing them on the same pad and canvas, so only the second graph is shown at the end. You can create a separate canvas for each, or use one canvas divided in 2 pads, e.g.

TCanvas *c = new TCanvas();
c->Divide(2,1);
c->cd(1);
g->SetTitle("g;x:;y");
g->SetMarkerStyle(20);
g->Draw("AP");
c->cd(2);
g1->SetTitle("g1;x:;z");
g1->SetMarkerStyle(20);
g1->Draw("AP");

it’s great!
I was able to do what I wanted to do!
thank you very much! !!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.