approx.C

Go to the documentation of this file.
00001 TCanvas *vC1;
00002 TGraph *grxy, *grin, *grout;
00003 
00004 void approx()
00005 {
00006 //**********************************************
00007 // Macro to test interpolation function Approx
00008 // Author: Christian Stratowa, Vienna, Austria.
00009 // Created: 26 Aug 2001                           
00010 //**********************************************
00011 
00012 
00013 // test data (square)
00014    Int_t n = 11;
00015    Double_t x[] = {1,2,3,4,5,6,6,6,8,9,10};
00016    Double_t y[] = {1,4,9,16,25,25,36,49,64,81,100};
00017    grxy = new TGraph(n,x,y);
00018 
00019 // x values, for which y values should be interpolated
00020    Int_t nout = 14;
00021    Double_t xout[] = 
00022       {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};
00023 
00024 // create Canvas
00025    vC1 = new TCanvas("vC1","square",200,10,700,700);
00026    vC1->Divide(2,2);
00027 
00028 // initialize graph with data
00029    grin = new TGraph(n,x,y);
00030 // interpolate at equidistant points (use mean for tied x-values)
00031    TGraphSmooth *gs = new TGraphSmooth("normal");
00032    grout = gs->Approx(grin,"linear");
00033    DrawSmooth(1,"Approx: ties = mean","X-axis","Y-axis");
00034 
00035 // re-initialize graph with data 
00036 // (since graph points were set to unique vales)
00037    grin = new TGraph(n,x,y);
00038 // interpolate at given points xout
00039    grout = gs->Approx(grin,"linear", 14, xout, 0, 130);
00040    DrawSmooth(2,"Approx: ties = mean","","");
00041 
00042 // print output variables for given values xout
00043    Int_t vNout = grout->GetN();
00044    Double_t vXout, vYout;
00045    for (Int_t k=0;k<vNout;k++) {
00046       grout->GetPoint(k, vXout, vYout);
00047       cout << "k= " << k << "  vXout[k]= " << vXout
00048            << "  vYout[k]= " << vYout << endl;
00049    }
00050 
00051 // re-initialize graph with data
00052    grin = new TGraph(n,x,y);
00053 // interpolate at equidistant points (use min for tied x-values)
00054 //   grout = gs->Approx(grin,"linear", 50, 0, 0, 0, 1, 0, "min");
00055    grout = gs->Approx(grin,"constant", 50, 0, 0, 0, 1, 0.5, "min");
00056    DrawSmooth(3,"Approx: ties = min","","");
00057 
00058 // re-initialize graph with data
00059    grin = new TGraph(n,x,y);
00060 // interpolate at equidistant points (use max for tied x-values)
00061    grout = gs->Approx(grin,"linear", 14, xout, 0, 0, 2, 0, "max");
00062    DrawSmooth(4,"Approx: ties = max","","");
00063 
00064 // cleanup
00065    delete gs;
00066 }
00067 
00068 void DrawSmooth(Int_t pad, const char *title, const char *xt, 
00069    const char *yt)
00070 {
00071    vC1->cd(pad);
00072    TH1F *vFrame = gPad->DrawFrame(0,0,15,150);
00073    vFrame->SetTitle(title);
00074    vFrame->SetTitleSize(0.2);
00075    vFrame->SetXTitle(xt);
00076    vFrame->SetYTitle(yt);
00077    grxy->SetMarkerColor(kBlue);
00078    grxy->SetMarkerStyle(21);
00079    grxy->SetMarkerSize(0.5);
00080    grxy->Draw("P");
00081    grin->SetMarkerColor(kRed);
00082    grin->SetMarkerStyle(5);
00083    grin->SetMarkerSize(0.7);
00084    grin->Draw("P");
00085    grout->DrawClone("LP");
00086 }
00087 

Generated on Tue Jul 5 15:44:22 2011 for ROOT_528-00b_version by  doxygen 1.5.1