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();
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.