TCanvas doesn't show up

Hello everyone,

I want to use TCanvas library for my C++ project to plot some data. For that I did created test.cc file which looks like this

int main(int argc, char* argv[])
{
  double var_x[3] = {1, 2, 3};
  double var_y[3] = {1, 2, 3};

  TCanvas *can1 = new TCanvas();
  TGraph *gr1 = new TGraph(3, var_x, var_y);
  gr1->Draw("ALP");

  std::cout << "code is working" << std::endl; 
  return 1;
}

Since, it is a C++ code, I also created this make file

test: test.o
	g++ test.o -o test `root-config --cflags --glibs`

test.o: test.cc
	g++ -c test.cc `root-config --cflags`

clean:
	rm test.o

However, when I run ./test in terminal, the canvas doesn’t show up.

Please note that if I create a macro

int test2()
{
  // same code as test.C
}

and run it with root -l test2.C, then only canvas shows up. But this is not what I want. I want to create an executable test. Any solution to this problem?

Thanks in advance.

Best,
Divyang.

If the same macro runs correctly in the interactive prompt, I guess the canvas is also being created and shown when compiled, but note that when the program ends (same as quitting ROOT in interactive mode) the canvas is destroyed; in short, it is shown and closed so fast that you don’t really see it. Try saving the canvas within the macro and then you can look for the output file (like a png, for instance) to check whether it ran correctly; e.g.

//...
  gr1->Draw("ALP");
  can1->SaveAs("myplot.png");
//...

Or make the macro ask for some user input after drawing, to create a “pause” to allow you to see the canvas.

if I do can1->SaveAs("can1.pdf"); then it does save the pdf file. However, this is not what I need.

I tried to add the pause part by asking the user for an input. Unfortunately, the canvas is still not appearing. Here is what I added

int dummy_inpt;
std::cout << "wait for canvas: ";
std::cin >> dummy_inpt;

Here is my ROOT version

   ------------------------------------------------------------------
  | Welcome to ROOT 6.24/08                        https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Sep 29 2022, 13:04:57                 |
  | From tags/v6-24-08@v6-24-08                                      |
  | With                                                             |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

My machine specifications

OS: Ubuntu 20.04.6 LTS x86_64 
Host: 82KB Lenovo V15 G2 ITL Ua 
Kernel: 5.15.0-130-generic 
Packages: 2453 (dpkg), 39 (snap) 
Shell: bash 5.0.17 
Resolution: 1920x1080, 1920x1080 
DE: Unity 
WM: Mutter 
CPU: 11th Gen Intel i5-1135G7 (8) @ 4.200GHz
GPU: Intel Device 9a49 
Memory: 3888MiB / 19795MiB 

Try with a TApplication:

#include "TCanvas.h"
#include "TGraph.h"
#include "TApplication.h"
#include <iostream>

// g++ test.cc -o test `root-config --cflags --glibs`

int main(int argc, char* argv[])
{
  TApplication a("a", 0, 0);

  double var_x[3] = {1, 2, 3};
  double var_y[3] = {1, 2, 3};

  TCanvas *can1 = new TCanvas();
  TGraph *gr1 = new TGraph(3, var_x, var_y);
  gr1->Draw("ALP");
  gPad->Modified();
  gPad->Update();
  //can1->SaveAs("b.png");

  std::cout << "Ctrl+c or choose File->Quit ROOT from TCanvas menu to end" << std::endl;

  a.Run(kTRUE);
  return 1;
}

Thank you very much @dastudillo. It worked!

I added --glibs into the makefile as you suggested. Also, I am just curious about why this worked with TApplication. Any comment or links for further reading will be a great help, as I will include this part in my main project.

Thanks again.

Probably others can give you better insight, but you can read a bit here:

and also search this Forum for posts related to TApplication, you’ll find some explanations here and there.

1 Like

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