TGraph2D and me not getting along

Hello everyone,

I’m having some trouble with TGraph2D. I’m just trying to plot a bunch of points in 3D, some of which could have the same x and y but differ in z.

Using the below bit of code for testing, it seems that it matters if I get the axis and label it or not. It also seems to matter if I draw one or two rings; nStepsZ set to 1 or 2. In most cases things look pretty messed up, miss the axes, segfault or whatever. I think I can only get one ring to work, and then only if z!=0.

I tried this using ROOT v4.04/02.

Am I just using this the wrong way or what’t going on? Any help would be appreciated.

Cheers,
Jeroen

{
  gROOT->Reset();

  size_t nPoints(9);
  size_t nStepsZ(1);
  TGraph2D geoGraph(nPoints*nStepsZ);

  float scaleX(2.);
  float scaleY(2.);
  float stepZ(3.);
  float stepRad = TMath::TwoPi()/nPoints;

  for (size_t j = 0; j != nStepsZ; ++j) {
    float z = j+1.;
    for (size_t i = 0; i != nPoints; ++i) {
      float x = scaleX*TMath::Cos(i*stepRad);
      float y = scaleY*TMath::Sin(i*stepRad);
      geoGraph.SetPoint(j+i*nStepsZ,x,y,z);
    } // for (i)
  } // for (j)

  TCanvas canvas("canvas");

  geoGraph.SetMarkerStyle(7);
  geoGraph.Draw("P");

//   geoGraph.GetXaxis()->SetTitle("x");
//   canvas.Update();
}

Try this:

[code]{
size_t nPoints = 9;
size_t nStepsZ = 2;
TGraph2D geoGraph(nPoints*nStepsZ);

float scaleX = 2.;
float scaleY = 2.;
float stepZ = 3.;
float stepRad = TMath::TwoPi()/nPoints;

Int_t N = 0;
for (size_t j = 0; j != nStepsZ; ++j) {
float z = j+1.;
for (size_t i = 0; i != nPoints; ++i) {
float x = scaleXTMath::Cos(istepRad);
float y = scaleYTMath::Sin(istepRad);
printf("%g %g %g\n",x,y,z);
geoGraph.SetPoint(N,x,y,z);
N++;
} // for (i)
} // for (j)

TCanvas canvas(“canvas”);

geoGraph.SetMarkerStyle(7);
geoGraph.Draw(“P”);
}

[/code]
Note that the only option you will be able to use to draw this graph will be the option “P”. The option TRI or any histogram options do not work on bunch of points having different z but same x,y.

Thanks for the help. Even though I don’t really understand the difference, this does draw something useful. But now try setting nStepsZ to 1. Then instead of drawing one circle, it draws a diagonal line (at least for me).

What’s happening in that case?

Cheers,
Jeroen

Also, just adding the line
’geoGraph.GetXaxis()->SetTitle(“X”);’

will produce lots of warnings (so many that everything pretty much hangs up) like
’Warning in TGraphDelaunay::Interpolate: Two of these three points are coincident 79 1532 132’

So I guess that for some reason, setting the axis title results in a change of drawing options to something that builds the Delaunay triangles and that does not work (due to the ‘same-z’ points).

Should this upgrade to a bug report?

Jeroen

after the loops add:

 geoGraph->GetHistogram()->SetMinimum(-20);
 geoGraph->GetHistogram()->SetMaximum( 20);