void approx() { //********************************************** // Macro to test interpolation function Approx // Author: Christian Stratowa, Vienna, Austria. // Created: 26 Aug 2001 //********************************************** // test data (square) Int_t n = 11; Double_t x[] = {1,2,3,4,5,6,6,6,8,9,10}; Double_t y[] = {1,4,9,16,25,25,36,49,64,81,100}; grxy = new TGraph(n,x,y); // x values, for which y values should be interpolated Int_t nout = 14; Double_t xout[] = {1.2,1.7,2.5,3.2,4.4,5.2,5.7,6.5,7.6,8.3,9.7,10.4,11.3,13}; // create Canvas vC1 = new TCanvas("vC1","square",200,10,700,700); vC1->Divide(2,2); // initialize graph with data grin = new TGraph(n,x,y); // interpolate at equidistant points (use mean for tied x-values) TGraphSmooth *gs = new TGraphSmooth("normal"); grout = gs->Approx(grin,"linear"); DrawSmooth(1,"Approx: ties = mean","X-axis","Y-axis"); // re-initialize graph with data // (since graph points were set to unique vales) grin = new TGraph(n,x,y); // interpolate at given points xout grout = gs->Approx(grin,"linear", 14, xout, 0, 130); DrawSmooth(2,"Approx: ties = mean","",""); // print output variables for given values xout Int_t vNout = grout->GetN(); Double_t vXout, vYout; for (Int_t k=0;k<vNout;k++) { grout->GetPoint(k, vXout, vYout); cout << "k= " << k << " vXout[k]= " << vXout << " vYout[k]= " << vYout << endl; } // re-initialize graph with data grin = new TGraph(n,x,y); // interpolate at equidistant points (use min for tied x-values) // grout = gs->Approx(grin,"linear", 50, 0, 0, 0, 1, 0, "min"); grout = gs->Approx(grin,"constant", 50, 0, 0, 0, 1, 0.5, "min"); DrawSmooth(3,"Approx: ties = min","",""); // re-initialize graph with data grin = new TGraph(n,x,y); // interpolate at equidistant points (use max for tied x-values) grout = gs->Approx(grin,"linear", 14, xout, 0, 0, 2, 0, "max"); DrawSmooth(4,"Approx: ties = max","",""); // cleanup delete gs; }
void motorcycle() { // data taken from R library MASS: mcycle.txt TString dir = gROOT->GetTutorialDir(); dir.Append("/graphs/"); dir.ReplaceAll("/./","/"); // read file and add to fit object Double_t *x = new Double_t[133]; Double_t *y = new Double_t[133]; Double_t vX, vY; Int_t vNData = 0; ifstream vInput; vInput.open(Form("%smotorcycle.dat",dir.Data())); while (1) { vInput >> vX >> vY; if (!vInput.good()) break; x[vNData] = vX; y[vNData] = vY; vNData++; }//while vInput.close(); grin = new TGraph(vNData,x,y); // draw graph vC1 = new TCanvas("vC1","Smooth Regression",200,10,900,700); vC1->Divide(2,3); // Kernel Smoother // create new kernel smoother and smooth data with bandwidth = 2.0 TGraphSmooth *gs = new TGraphSmooth("normal"); grout = gs->SmoothKern(grin,"normal",2.0); DrawSmooth(1,"Kernel Smoother: bandwidth = 2.0","times","accel"); // redraw ksmooth with bandwidth = 5.0 grout = gs->SmoothKern(grin,"normal",5.0); DrawSmooth(2,"Kernel Smoother: bandwidth = 5.0","",""); // Lowess Smoother // create new lowess smoother and smooth data with fraction f = 2/3 grout = gs->SmoothLowess(grin,"",0.67); DrawSmooth(3,"Lowess: f = 2/3","",""); // redraw lowess with fraction f = 0.2 grout = gs->SmoothLowess(grin,"",0.2); DrawSmooth(4,"Lowess: f = 0.2","",""); // Super Smoother // create new super smoother and smooth data with default bass = 0 and span = 0 grout = gs->SmoothSuper(grin,"",0,0); DrawSmooth(5,"Super Smoother: bass = 0","",""); // redraw supsmu with bass = 3 (smoother curve) grout = gs->SmoothSuper(grin,"",3); DrawSmooth(6,"Super Smoother: bass = 3","",""); // cleanup delete [] x; delete [] y; delete gs; }