Re: Code for Postscript and SetXTitle tutorial

Rene Brun (Rene.Brun@cern.ch)
Wed, 19 Feb 1997 13:13:10 +0100


Matthias Vitt wrote:
>
> Hi Rene,
>
> Well, Surely I put "TPostScript" as class identifier in this script, since
> without I must have gotten an error. The file fff.ps is printed but not in
> 8 cm x8 cm. It is still the default size. The Fit is done without any problems,
> but I still get the "illelgal pointer to class GetHistogram()" error when I
> try to set the x-axis title.
>
> So here's the code. It is bascically the tutorial graph.C. Maybe you find a gross error. (better hopefully you find
> one , I really want to use root). Off course there is the possibility to set
> an axis title via a PaveText-object. But the other way would be more satisfying.
>
> Thank you very much for your help.
>
> Mats
>
> {
> gROOT->Reset();
>
> c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);
>
> c1->SetFillColor(42);
> c1->SetGridx();
> c1->SetGridy();
> c1->GetFrame()->SetFillColor(21);
> c1->GetFrame()->SetBorderSize(12);
>
> Int_t n = 8;
> Float_t x[n], y[n];
>
> for (Int_t i=0;i<n;i++) {
> x[i] = i*0.5;
> y[i] = sin(x[i]+1);
> printf(" i %i %f %f \n",i,x[i],y[i]);
> }
>
> gr = new TGraph(n,x,y);
> gr->GetHistogram()->SetXTitle("x axis");
> gr->Fit("pol1");
> gr->SetFillColor(19);
> gr->SetLineColor(2);
> gr->SetLineWidth(4);
> gr->SetMarkerColor(4);
> gr->SetMarkerStyle(4);
>
> TPostScript mps("fff.ps");
> mps.Range(8,8);
> gr->Draw("AP");
> c1->Draw();
> mps.Close();
>
> c1->Modified();
> }

I have modified your macro (see below) to make it to work.
The point is that the graph histogram object is created when
the graph is being painted. gr->GetHistogram() returns a NULL
pointer where you call it.
See
http://root.cern.ch/root/HowtoDraw.html
You must force drawing in a macro by calling c1->Update().

Rene Brun

//===============Your modified macro=====================
{
gROOT->Reset();

c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);

c1->SetFillColor(42);
c1->SetGridx();
c1->SetGridy();
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(12);

Int_t n = 8;
Float_t x[n], y[n];

for (Int_t i=0;i<n;i++) {
x[i] = i*0.5;
y[i] = sin(x[i]+1);
printf(" i %i %f %f \n",i,x[i],y[i]);
}

gr = new TGraph(n,x,y);
gr->Fit("pol1");
gr->SetFillColor(19);
gr->SetLineColor(2);
gr->SetLineWidth(4);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(4);
gr->Draw("AP");
c1->Update();
gr->GetHistogram()->SetXTitle("x axis");

TPostScript mps("fff.ps");
mps.Range(8,8);
c1->Draw();
mps.Close();

}