Histogram not displaying after program finishes running

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define pi 3.14159
using namespace std;
void bai1()
 {
    double x1, y1, x2, y2, z2, r1, r2, r3, theta, phi, r;
    double z1 = 0;    

    TH3F* h1 = new TH3F("h1", "1a", 50, -5, 5, 50, -5, 5, 50, -5, 5 );
    TH3F* h2 = new TH3F("h2", "1b", 100, -5, 5, 100, -5, 5, 100, -5, 5 );

    srand(time(NULL)); //Hàm khởi tạo số ngẫu nhiên
    
    int a = -1;
    int b = 1;
    
   for(int i=1;i<10000;i++)
    { 

      r1 = double(rand())/RAND_MAX;
      r2 = double(rand())/RAND_MAX;
      r3 = double(rand())/RAND_MAX;
      
      // a,
      x1 = r1*(b-a) + a;
      y1 = r2*(b-a) + a;
      if(x1*x1 + y1*y1< 1)
        {
          h1->Fill(x1, y1, z1);
        }

      // b,
      r = r1;
      theta = r2*pi;
      phi = r3*2*pi;
      
      x2 = r * sin(theta) * cos(phi);
      y2 = r * sin(theta) * sin(phi);
      z2 = r * cos(theta);

      h2->Fill(x2,y2,z2);

    }
      
    TCanvas *c1 = new TCanvas("c1", "bai1 a", 800, 700);
    h1->Draw();
    TCanvas *c2 = new TCanvas("c2", "bai1 b", 800, 700);  
    h2->Draw(); 
 }

Thats my code, when I run root bai1.C, it just says it’s done and doesn’t show any histograms. (I’m running through WSL)**

Dear Hoang,

Thanks for the post and welcome to the ROOT community!

I ran the macro with this command root -l ba1.C as well as copied-pasted the code in the root prompt and I see a nice 3D plot. How do you execute the code ?

Cheers,
D

1 Like

Dear Danilo,

I apologize if my English is not very good. I know that my code runs well and has no errors. However, I would like to ask if there is something wrong with the software, because it cannot run “geometry” such as TCanvas or histograms.

I have tried uninstalling and reinstalling Ubuntu, ROOT, and WSL to fix this problem, but still have not been successful. Could you please help me solve this problem?

Dear Hoang,

No worries for your English: it’s very clear!!
I missed this is WSL. I add @bellenot in the loop given his consolidated expertise with the Windows OS.

Cheers,
D

1 Like

Are you running an Xserver? Try this:

(the X server has to be running before you open a WSL terminal).
More here:

2 Likes

Thank you, I tried installing vcXsrv and it really works.

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