#include #include using namespace std; void writeReadDataGraph(const int num=1000) { // write data to file //ofstream ofile("data",ios::binary); ofstream ofile("data"); for (int i=0; iRndm(); float err = gRandom->Rndm()*10.; //cout << "rnd " << rnd << endl; float y = 10.*i+5.+rnd*10.; //cout << "rnd " << rnd << " i " << i << " y " << y << endl; ofile << i*1. << " " << y << " " << err << endl; } ofile.close(); // now read data from file //ifstream ifile("data",ios::binary); ifstream ifile("data"); int nlines=0; float xx[num]; float exx[num]; float yy[num]; float eyy[num]; while (ifile.good()) { float x,y,ey; ifile >> x >> y >> ey; //cout << "Entry:" << nlines << ": X " << x << " Y " << y << endl; if (ifile.eof()) break; xx[nlines]=x; yy[nlines]=y; exx[nlines]=0.1; eyy[nlines]=ey; nlines++; }; ifile.close(); gStyle->SetOptFit(1111); gr1 = new TGraphErrors(nlines,xx,yy,exx,eyy); gr1->SetMarkerColor(kBlue); gr1->SetMarkerStyle(21); gr1->Fit("pol1"); TF1* f=dynamic_cast(gr1->GetFunction("pol1")); cout << "F: " << f->GetChisquare() << " NDoF " << f->GetNumberFreeParameters()<< " " << f->GetNumberFitPoints() << endl; gr1->Draw("ALP"); }