TGraph and SetTitle

Hello,
I have a problem with the TGraph. I need to display the graph title, X and Y label and so on, but with the following code I’m not able to display these objects:

void test2() {
TCanvas* c1 = new TCanvas();
TGraph graph;
Double_t x[10], y[10];
for(int i=0; i< 10; i++) x[i]=y[i]=i;
gPad->Clear();
graph.DrawGraph(10, x, y, “AL”);
graph.SetTitle(“test 2”); //don’t work
graph.GetXaxis()->SetTitle(“energy”); //don’t work
graph.GetYaxis()->SetTitle(“count”); //don’t work
graph.Draw(“AL”);
c1->Modified();
c1->Update();
}

I need to create the TGraph object before the array of value. The script is in attachment.

Thank you very much
Andrea Bulgarelli
test2.C (435 Bytes)

Clearly your code cannot work because the TGraph object is automatically deleted when
leaving the scope of your function test2.
Use TGraph *graph = new TGraph(10) ;

Rene

Thank you Rene, ok, this is a problem, but I think that this isn’t the only problem of the code. In my original code I haven’t initialized the TGraph object and the Draw method don’t work. Thank you.

In addition, I’m not able to understand how works the following functions:

  • Draw(): it seems that draw the axis, label, title and so on, but the scale of the axis is 1
  • DrawGraph(): draw the data with the correct units on the axis, but with a generic title (“Graph”).
    If I use both the method (with the A parameter) I obtain an overlapping of two axis with different units. I have tried various combination of the parameters, but the problem remains.
    I’m not able to use only one method because the display is not complet.
    Thank you very much
    Andrea Bulgarelli

Try to follow the various examplse with graphs in the tutorials. I have simplified your script to be:
void test2() {
TCanvas* c1 = new TCanvas();
Double_t x[10], y[10];
for(int i=0; i< 10; i++) x[i]=y[i]=i;
TGraph *graph = new TGraph(10,x,y);
graph->SetTitle(“test 2”);
graph->GetXaxis()->SetTitle(“energy”);
graph->GetYaxis()->SetTitle(“count”);
graph->Draw(“AL”);
}

Rene