Hi,
I have a strange problem trying to access an axis in a graph. The sample code is:
void GraphTest()
{
const int npoints=9;
const int ngraphs=2;
double averW[npoints]={0.98,1.18,1.38,1.58,1.78,1.98,2.18,2.38,2.58};
double tag_counts_sim[npoints]={9.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
TGraphErrors **gr1= new TGraphErrors*[ngraphs];
// Defining graphs
for(int ctr=0;ctr<ngraphs;ctr++){
gr1[ctr] = new TGraphErrors(npoints);
gr1[ctr]->SetMarkerStyle(21);
gr1[ctr]->SetMarkerColor(kBlue);
// gr1[ctr]->GetXaxis()->SetTitle("W");
}
////////////////////////////
for(int ctr=0;ctr<npoints;ctr++){
gr1[1]->SetPoint(ctr,averW[ctr],tag_counts_sim[ctr]);
}
gr1[1]->Draw("ACP");
}
This works fine. If I uncomment the line gr1[ctr]->GetXaxis()->SetTitle(“W”);
it produces an empty graph with axes going from 0 to approximately 1 (on the positive side, the x axis gets the required label). Could you, please, tell me what is going wrong.
I’m running root 5.14, gcc 4.1.3
Initially I tried running the script on Ubuntu linux (the results are in the first message of the thread).
After the reply, I tried it on Fedora 8 with root 5.18 (CINT/ROOT C/C++ Interpreter version 5.16.29). It drew the graph, but ignored the SetTitle command altogether (the 5.14 on ubuntu drew the title, but not the graph).
Could you, please, also tell me what is wrong with the old (my) way. I haven’t
worked with Graphs much, but I would always define histograms and all their (and their axes) attributes at the beginning, then filled them , then drew them. None of them ever complained.
You should change the title after having drawn the TGraph. When the graph is drawn a temporary histogram is created and when you change the title that’s the histogram title with is actually changed … that’s why the graph should be drawn 1st.