Using TGraph in c++

Hallo everybody,

my problem is, that I tried to use TGraphs in c++, but it does not work. Till now I only used histograms and that in the following way:

in my c++ program it looks like:

#include <TFile.h>
#include <TH1D.h>

using namespace std;

int main(int argc, char** argv) {
   TH1D* histo1 = new TH1D("histo1", "", 20, 10, 100);
   //here the data is read in the histogram
   outFile.Write();
}

after compiling and executing the program I start root and use there the two commands:

TFile *_file0=TFile::Open("out.root")
histo1->Draw()

Till here all works. Now I tried the same with TGraphs, so it looks like:

#include <TFile.h>
#include <TH1D.h>

using namespace std;

int main(int argc, char** argv) {
   //values of field1 and field2 are set
   TGraph *gr = new TGraph(n, field1, field2);
   outFile.Write();
}

and in root:

TFile *_file0=TFile::Open("out.root")
gr->Draw()

compiling and executing works, but if I try to show the Tgraph in root, there come the error

Error: Symbol gr is not defined in current scope  (tmpfile):1:
Error: Failed to evaluate gr->Draw()
*** Interpreter error recovered ***

which is my mistake?

cheers
Prometheus

Hi,

The TGraph object are not attached to a TFile by default and thus : TGraph *gr = new TGraph(n, field1, field2); outFile.Write();does not store the TGraph object. Also the ‘name’ of the graph defaults in ‘TGraph’.

Try: TGraph *gr = new TGraph(n, field1, field2); outFile.WriteObject(gr,"gr");

Cheers,
Philippe.