# TH3, TF3 and GL crashes

Dear ROOTers,

I am trying to draw a function which is a “conical gaussian” in 3D. There are two problems:

1. TF3 draw result is empty
2. I can fill a histogram with TF3 and successfully draw it, unles I try gl options. Then any refresh in the canvas causes a crash or a hang. OpenGL viewer draws everything OK, but its drawing options are quite limited…

I would apprieciate help.

___3dcone.py (809 Bytes)

ROOT Version: 6.18
Platform: Fedora 31
Compiler: Not Provided

You may have something wrong with your equation.
I tried the following simpler one and it works:

``````void cone3d()
{
gStyle->SetCanvasPreferGL(1);
TCanvas *cnv = new TCanvas("3dcone", "3dcome", 200, 10, 600, 600);
double xs = -10, ys=-10, zs = 1;
double xe = 10 , ye=10, ze = 10;
auto f5 = new TF3("f5", "x*x + y*y + (z-5)*(z-5)-9", xs, xe, ys, ye, zs, ze);
f5->Draw("gl");
}
``````

May be the equation is not correct ? may be the limits are out of the range of the equation ?

Hi,

Seems to be, when TF3::Draw() is called, it does not draw function itself, but solution of TF3(x,y,z) = 0.

Here is method used for painting:

https://root.cern/doc/master/classTPainter3dAlgorithms.html#a21eab91f04ce6c7d036e395d0462ee22

Therefore if you want to draw TF3 values, you should create and draw histogram

Regards,
Sergey

Exactly. In the example I posted a sphere of radius 3 center on (0,0,5) is drawn.
The equation is:

xˆ2 + yˆ2 + (z-5)ˆ2 = 9

and you should write in TF3:

xˆ2 + yˆ2 + (z-5)ˆ2 - 9 = 0

So, for instance: `x*x + y*y + (z-5)*(z-5)-9`

Thanks, I understand, this makes sense (although some automatic conversion from TF3 to TH3 would make things much simpler).

However, can you comment on the crashes of the histogram?

``````void cone3d()
{
gStyle->SetCanvasPreferGL(1);
TCanvas *cnv = new TCanvas("3dcone", "3dcome", 200, 10, 600, 600);
double xs = -10, ys=-10, zs = 1;
double xe = 10 , ye=10, ze = 10;
auto f5 = new TF3("f5", "x*x + y*y + (z-5)*(z-5)-9", xs, xe, ys, ye, zs, ze);
int n = 20;
auto h5 = new TH3F("h5","h5",n,xs,xe,n,ys,ye,n,zs,ze);

double x,y,z;
TAxis *ax = h5->GetXaxis();
TAxis *ay = h5->GetYaxis();
TAxis *az = h5->GetZaxis();
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
for (int k=1; k<=n; k++) {
x = ax->GetBinCenter(i);
y = ay->GetBinCenter(j);
z = az->GetBinCenter(k);
h5->SetBinContent(i,j,k,f5->Eval(x, y, z));
}
}
}

h5->Draw("BOX2");
}
``````

This is not the function that I have. I understand that something may be wrong with the function, but I guess it should not end with a crash. And nothing is obviously wrong if I can draw the histogram, but trying to rotate it, etc., causes a crash…

This one works:

``````void cone3d()
{
gStyle->SetCanvasPreferGL(1);
TCanvas *cnv = new TCanvas("3dcone", "3dcome", 200, 10, 600, 600);
double xs = -10, ys=-10, zs = 1;
double xe = 10 , ye=10, ze = 10;
auto f5 = new TF3("f5", "*exp(-(sqrt(x*x+y*y)-*z)^2/(2*^2))", xs, xe, ys, ye, zs, ze);
f5->SetParameters(1,1,1);

int n = 20;
auto h5 = new TH3F("h5","h5",n,xs,xe,n,ys,ye,n,zs,ze);

double x,y,z;
TAxis *ax = h5->GetXaxis();
TAxis *ay = h5->GetYaxis();
TAxis *az = h5->GetZaxis();
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
for (int k=1; k<=n; k++) {
x = ax->GetBinCenter(i);
y = ay->GetBinCenter(j);
z = az->GetBinCenter(k);
h5->SetBinContent(i,j,k,f5->Eval(x, y, z));
}
}
}

h5->Draw("box2");
}
``````

I do not get any crash and I get the following picture:

Indeed, your code does not crash. Thanks! Due to that, I found out what was the issue. In python, one needs to execute

otherwise keeping script alive, so that canvas does not close, causes crashes.

So that was more a python issue.

Well, pyROOT. And it is interesting, that it crashes with OpenGL, but not without it.

May be @etejedor has something to say about it.

Hi,

If the inputhook mechanism for the graphics in your system works, I would use it.

On the other hand, if you run your script with `python -i my_script.sh` to keep the canvas alive, it crashes? What does the error message say?

When I run python2 -i 3dcone.py after commenting the keep-alive loop, the canvas with contents is displayed, but I can’t rotate the histogram inside, resizing the canvas does make the interior all black, etc. Still, it doesn’t crash. Surprisingly, if I run it through ipython2, everything works fine.

If I add the inputhook, then it works fine both in python2 and ipython2.

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