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 {
TH1D* h1;
TGraph* g1;

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

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

// Make Hist Macro
TMacro* m;
m = new TMacro;
m->SetName(“Draw Graph”);

// Make Graph Macro
m = new TMacro;
m->SetName(“Draw Hist”);

int main() {
TApplication app(“app”,0,0);
MyBrowser* mb = new MyBrowser();

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.


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”:
try to add it to its “Specials”:
This should deceive ROOT sufficiently well.

Thank you!