Trouble with TGraph::SetName

Hi, I’m trying to do something pretty simple but I am having difficulty with TGraph. I am trying to add a macro to a browser that draws a graph to the canvas. Here is a simplified version of what I’m trying to do:

[code]// macrotest.C
#include “TApplication.h”
#include “TBrowser.h”
#include “TMacro.h”
#include “TH1D.h”
#include “TGraph.h”

class MyBrowser : public TBrowser {
public:
MyBrowser();
TH1D* h1;
TGraph* g1;
};

MyBrowser::MyBrowser() : TBrowser() {
// Make Hist
h1 = new TH1D(“h1”,“h1”,10,0,10);
h1->Fill(5);

// Make Graph
double x[3] = {1,2,3};
double y[3] = {.1,.4,.9};
g1 = new TGraph(3,x,y);
g1->SetName(“g1”);

// Make Hist Macro
TMacro* m;
m = new TMacro;
m->AddLine("{g1->Draw();}");
m->SetName(“Draw Graph”);
Add(m);

// Make Graph Macro
m = new TMacro;
m->AddLine("{h1->Draw();}");
m->SetName(“Draw Hist”);
Add(m);
}

int main() {
TApplication app(“app”,0,0);
MyBrowser* mb = new MyBrowser();
app.Run();
}[/code]

I complie using
g++ macrotest.C -o macrotest root-config --glibs --cflags

The macro that draws the hist works fine, but the macro that draws the graph gives an error:
Error: Symbol g1 is not defined in current scope /tmp/Draw Graph.CXZejFX:1:
Error: Failed to evaluate g1->Draw()

So it looks like TGraph::SetName is not working right. Any ideas on how I should proceed? Note that I do not want to simply add the TGraph directly to the browser, as I want the macro to do more interesting things, like check the status of a line_color variable before drawing, for instance.

I am finding TMacros difficult to use in compiled code in general. If there is a completely different direction I should take, please let me know.

gROOT->Append(g1);

Thanks for the reply. It works, but it gives the following warning:

Warning in TROOT::Append: Replacing existing TH1: g1 (Potential memory leak).

I’m afraid this is a long standing problem in ROOT.
You can name any objects which inherit from “TNamed” but ROOT will then choke on them as soon as you add them to its directories and try to treat them in the same way as another objects, like for example TH[123], which were designed for it.
Instead of adding your graph to “ROOT Memory”:
gROOT->Append(g1);
try to add it to its “Specials”:
gROOT->GetListOfSpecials()->Add(g1);
This should deceive ROOT sufficiently well.

Thank you!