I draw a canvas with no picture

when I try to draw picture, I do things like that:
First: write a C document
the follow is my document named macro.c

void macro()
TGraph graph{"P.txt"};

Second: I use ROOT to run the program, my order is below

[juno@localhost 0707.1998V2]$ root -l macro.c

Finally what I get is a canvas named c1 with no picture on it.
and ROOT shows:

root [0] 
Processing macro.c...
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

After that happened, I try to directly use ROOT to draw the picture.
what I did is

[juno@localhost 0707.1998V2]$ root -l 
root [0] TGraph graph("P.txt");
root [1] graph.Draw()
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

In this turn I get what I want. I want to know why these things happened.
Thanks a lot~

ROOT Version: 5.34.11
Platform: Centos 7
Compiler: Not Provided

With older versions of ROOT you need to specify a drawing option containing A .


classic ROOT lifetime problems!
The TGraph goes out of scope (“dies”) at the end of the macro, so it disappears from the canvas (not a very friendly behavior, admittedly).
Using a TGraph* graph = new TGraph("P.txt"); and letting it live after macro has finished executing should fix the problem. ROOT will take care of properly deleting the TGraph at exit.


I just try what you said, sadly, it doesn’t work.

In addition to add the option, you should also make your macro as follow (as Enrico said):

void macro()
   TGraph* graph = new TGraph{"P.txt"};

Thanks a lot, what you said really help me.

If you don’t like keeping raw pointers around (and in general you shouldn’t :slight_smile:) you can also try with a DrawClone instead of a Draw: it creates a ROOT-owned copy of your TGraph and draws that, so you can go back to using a stack-allocated TGraph graph.

Yes, it works too. thanks for your help~. :sunny:

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