How to save a scatter picture to a ROOT file

Hi experts,

I write a script to draw a scatter picture and write the picture to a root file, but the picture in the root file is not a scatter picture but a broken line graph.

Could you please give me some advice?

  0 #include "TCanvas.h"
  1 #include "TGraph.h"
  2 #include "TFile.h"
  4 int main(int argc, char* argv[]) {
  6     TFile *f2 = new TFile("scatter_plot.root","RECREATE");
  7     TCanvas *c1 = new TCanvas("c1", "Scatter Plot", 800, 600);
  9     Double_t x[] = {1.0, 2.0, 3.0, 4.0, 5.0};
 10     Double_t y[] = {2.0, 3.0, 1.0, 4.0, 5.0};
 12     TGraph *scatterPlot = new TGraph(5, x, y);
 13     scatterPlot->SetMarkerStyle(20);
 14     scatterPlot->SetMarkerColor(kBlue);
 16     scatterPlot->Draw("AP");
 17     c1->Draw();
 18     c1->SaveAs("123.pdf");
 19     scatterPlot->Write();
 20     f2->Close();
 23     return 0;
 24 }

The draw options are not saved to the root files, so if you simply double-click on the graph in a TBrowser, it is drawn with some defaults (the markers and line you see). But if you draw it from the interactive prompt (or another script) you control how it is drawn (e.g., use Graph->Draw("ap"); to see the same you used when creating it).


Actually, you can store scatter plot with draw options - if you save TCanvas object.
In your script you should call c1->Write().

If you saving the only canvas in the file - most easy way is to call c1->SaveAs("file.root");.
In such case you do not need to create and manage TFile instance yourself.

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