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
(int)0
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
Hi,
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.
If you don’t like keeping raw pointers around (and in general you shouldn’t ) 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.