Drawing TGraph versus TH1F

I have a file called example.C which looks like

void example()
{
TH1F * hist = new TH1F(“hist”,“hist”,100,0,100);
TGraph gr = new TGraph(55);

}

I do root example.C+ at the command line. The program completes. Then I type

hist->Draw()

into root and the histogram is draw. Then I type

gr->Draw()

into root and I get the following error:

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

Can someone explain why this works for the TH1F but not for the TGraph?

Thanks.

Andrew

void example() { // ... TH1F *hist = new TH1F("hist", "hist", 100, 0, 100); // ... TGraph *gr = new TGraph(5*5); gr->SetNameTitle("gr", "gr"); gDirectory->Add(gr); // ... }

thanks

if I do this:

void example() {

TH1F *hist = new TH1F(“hist”, “hist”, 100, 0, 100);

TGraph gr = new TGraph(55);
gr->SetNameTitle(“gr”, “gr”);
gDirectory->Add(gr);

gr->Draw()
}

Then after the program ends I get this warning:

Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
Warning in TROOT::Append: Replacing existing TH1: gr (Potential memory leak).

and then if I try to type gr->Draw() into the command line I still get this error

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

Why does drawing the graph in the example prevent you from drawing it afterwards?

void example() {
   TH1F *hist = new TH1F("hist", "hist", 100, 0, 100);
   TGraph *gr = new TGraph(5*5);
   gr->SetNameTitle("gr", "gr");
   gr->Draw();
}

gives:

root [0] .x example.C
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [1] gr->Draw();
root [2]

[code]#include <TGraph.h>
#include <TROOT.h>
#include

void example() {
TGraph *gr = new TGraph;
gr->SetNameTitle(“gr”, “gr”);
gr->SetPoint(0,5,4);
gDirectory->Add(gr);
gr->Draw();
}[/code]

Use the code above. Type “root example.C+” into your command line. The graph is drawn. Close the graph. Then type gr->Draw(). That gives the following error:

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

I don’t understand why you can’t draw the graph at the command line when you draw it in the example.

see the macro I sent you …
it does not have gDirectory->Add(gr);

By the way … to draw a graph you need the option A if you want to see the axis and define the users coordinates. You also need a drawing option like L P C … see the help:
root.cern.ch/root/html/TGraphPainter.html

The same thing happens when I use

[code]
#include <TGraph.h>
#include <TROOT.h>
#include

void example() {
TGraph *gr = new TGraph;
gr->SetNameTitle(“gr”, “gr”);
gr->SetPoint(0,5,4);
gr->Draw("*A");
}[/code]

not in my case:

imaccouet:roottest couet$ root example.C+
  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version   5.33/03      6 March 2012   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

ROOT 5.33/03 (trunk@43282, Mar 08 2012, 11:05:41 on macosx64)

CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] 
Processing example.C+...
Info in <TUnixSystem::ACLiC>: creating shared library /Users/couet/roottest/./example_C.so
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [1]    gr->Draw();
root [2] 

Could the version of root that we are using be causing this difference?


  •                                     *
    
  •    W E L C O M E  to  R O O T       *
    
  •                                     *
    
  • Version 5.27/06b 5 November 2010 *
  •                                     *
    
  • You are welcome to visit our Web site *
  •      [root.cern.ch](http://root.cern.ch)            *
    
  •                                     *
    

ROOT 5.27/06b (branches/v5-27-06-patches@36515, Nov 05 2010, 15:46:56 on linuxx8664gcc)

Yes, very likely that’s the reason.