Crashed while writing

Hi,

In order to change the range of x-axis in TGraph, I found a post which suggests to use SetHistogram(). I used this and it works but it I can’t write to file after this. It crashes while writing to file because of SetHistogram(). Here is a sample code.

TFile *file = new TFile("test.root", "recreate");
TCanvas *c = new TCanvas("c", "c", 800, 800);
TH1D *h = new TH1D("h", "h", 200, 0, 8);

TRandom *rand = new TRandom();
TGraph *gr = new TGraph(400);
for(int i=0; i<400; i++) gr->SetPoint( i, rand->Uniform(6), rand->Uniform(6) );

gr->SetHistogram(h);
gr->Draw("AP");
c->Write();
file->Close();

Is there any other way around?
Thanks.

[quote=“manoj”]Hi,

Is there any other way around?
Thanks.[/quote]

Yes, for example:

void wa()
{
   TRandom rand;
   const int nPts = 400;
   TGraph *gr = new TGraph(nPts);
   for(int i = 0; i < nPts; ++i)
      gr->SetPoint(i, rand.Uniform(6), rand.Uniform(6));
   if(gr->GetXaxis())
      gr->GetXaxis()->Set(200, 0., 8.);

   TFile file("test.root", "recreate");
   TCanvas *c = new TCanvas("c", "c", 800, 800);
   gr->Draw("AP");
   c->Write();
}

P.S.

Still I did not investigate, why original code crashes on c->Write() call.

Hi,

TH1D *h = new TH1D("h", "h", 200, 0, 8); .... gr->SetHistogram(h); SetHistogram must be given the address of a TH1F pointer. The code was updated in revision 38549 to make this a compile time error.

Cheers,
Philippe.