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.