TCanvas close doubt

Hello all,

I searched on TCanvas class but I couldnt find anything like this. I create a TCanvas on my code somewhere using:

TCanvas * whatever = new TCanvas(“bla”, “bla”)

Than on destructor I would do:

if (whatever) delete whatever;

But I noticed it was crashing because of that and I discovered that there is this Close member on TCanvas. So now I do:

if (whatever) {
whatever->Close();
delete whatever;
}

But if I close it with the mouse and quit the program it will crash also. I would like to know if there is something like isClosed() or anything that I can use to know if the canvas is opened and I need to close it.

Thank you,

Werner Freund,

And is it possible to change the axis so that it’s scales are on pi (like pi/4 pi/2 and that stuff)?

Thank you.

Concerning your canvas deletion problem, could you post the shortest possible running script featuring this problem?
Concerning the axis with pi labelling, we will include this option in a coming release.
Meanwhile, you can proceed as shown in the script below.

Rene

{ Double_t pi = TMath::Pi(); TH1F h("h","test",100,-pi,pi); h.FillRandom("gaus",5000); h.GetXaxis()->SetLabelOffset(99); h.GetXaxis()->SetNdivisions(-4); h.Draw(); TLatex l; l.SetTextSize(0.04); l.SetTextAlign(23); double yl = -2; l.DrawLatex(-pi,yl,"-#pi"); l.DrawLatex(-pi/2,yl,"-#frac{#pi}{2}"); l.DrawLatex(0,yl,"0"); l.DrawLatex(pi/2,yl,"#frac{#pi}{2}"); l.DrawLatex(pi,yl,"#pi"); }

Hello Rene,

I will show some code parts so that you can see if I am doing something stupid:

3 RelEfficCanvas::RelEfficCanvas(RelEfficBase *userRelEfficData){
4 relCanvas = 0;
5 relEfficData = userRelEfficData;
6 relEfficElc = 0;
7 relEfficJet = 0;
8 }

18 int RelEfficCanvas::Draw(){
19
20 relCanvas = new TCanvas(“Relative Efficiency”, “Relative Efficiency”);
…}

30 ~RelEfficCanvas::RelEfficCanvas(){
31 if (relCanvas){
32 relCanvas->Close();
33 delete relCanvas;
34 }
35 }

So, if I take out the ->Close() it would do segmentation faul when I quit root without closing it myself, and if I put the Close and close it by myself it would do segmentation faul when I quit root. If there was a function like relCanvas->isClosed() I could avoid this problem, but maybe I am doing something wrong.

Thank you for the Pi label x),

Werner.

Hi Werner,

You can use the Canvas::Closed() signal. See for example:
root.cern.ch/phpBB2/viewtopic.php?t=7518

Cheers, Bertrand.

Thank you!

I used:

 30     ~RelEfficCanvas(){
 31         if (relCanvas){
 32             if (gROOT->GetListOfCanvases()->FindObject("Relative Efficiency")){
 33                 relCanvas->Closed();
 34                 delete relCanvas;
 35             }
 36         }
 37     }

and not its not crashing anymore.