4

I am trying to create a graph and save it as an image. I am required to use ROOT. I created the graph with

TGraph graph = TGraph(xvect, yvect);

but now I'm stuck on how to get that saved as a png (or other image format). I'm using a linux machine if that makes a difference. Also, if anyone knows link to the documentation that describes the method for writing the graph to an image file, I could figure it out myself from there, but I have been unsuccessful in finding that in the documentation thus far.

Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
mrswmmr
  • 2,081
  • 5
  • 21
  • 23
  • 2
    Watch out for micro black holes. – Emile Cormier Jun 10 '11 at 22:44
  • 2
    Relying on undefined behavior in ROOT causes micro black holes. – rubenvb Aug 10 '11 at 15:37
  • 1
    Ancient post, but I'll just point out that this line creates a temporary TGraph on the stack and then for no reason copies it when reassigning to the "graph" variable. Not a big overhead with one small TGraph object, but with big objects or thousands of them it's silly. Just do this: TGraph graph(xvect, yvect). Sorted :-) – andybuckley Apr 08 '13 at 13:59

3 Answers3

3
TCanvas*c1 = new TCanvas();
graph->Draw();
c1->Print("name.png");

Will certainly work in the cint shell. It may need some fine-tuning to work in compiled code.

You'll find all this basic stuff exhaustively covered in the on-line tutorials and HowTos. Also see the documentation in general.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • 3
    probably you need to use something like `graph->Draw("AP")` – Ruggero Turra Oct 10 '11 at 21:50
  • I don't understand the ROOTer obsession with new'ing everything: TCanvas c1; ... c1.Print(...); c1.SaveAs(...); etc. works just fine -- and as a bonus is both shorter and doesn't leak memory either ;-) – andybuckley Apr 08 '13 at 13:57
  • @andybuckley High change of cargo-cult nonsense in this, but... It's because the documentation does it that way, and *that* is because almost everything inherits from `TObject` which allocates persistent storage on a private heap for keeping track of the names of everything to get a certain amount of reflection, and some features of that reflection system seem to rely on heap allocation. Though I have never been very clear on the detains and could be mistaken. – dmckee --- ex-moderator kitten Apr 09 '13 at 01:40
  • @dmckee Cargo-cult or not, that's interesting/useful! I've not personally had problems, but getting tripped up by that non-local state is one of the main reasons that I prefer other analysis tools! PyROOT (and now rootpy built on that) are a fairly nice way to get access to the ROOT core functionality with fewer memory issues. – andybuckley Apr 10 '13 at 09:16
0

The complete macro will be:

TCanvas *c1 = new TCanvas();
const Int_t n = 10;
Double_t xvect[n];
Double_t yvect[n];
.... initialize xvect and y vect
TGraph graph = TGraph(n, xvect, yvect);
graph->Draw("al"); // draw the graph as a line (see the ROOT wen site for more option)
c1->SaveAs("c1.png"); // many other formats are available (PS, PDF, JPEG etc...)
Couet
  • 61
  • 3
0
TCanvas*cvs = new TCanvas();
graph->Draw();
cvs->SaveAs("name.png");

SaveAs has been my go to function for saving graphs in root. As a side note the online documentation is very useful as dmckee said. class list

ac1dicburn
  • 36
  • 3