Logo ROOT   6.10/09
Reference Guide
testInterpolation.cxx
Go to the documentation of this file.
1 //Example of the various methods of interpolation provided by the
2 // ROOT::Math::Interpolator class
3 //
4 //Example can also be run in ROOT by doing :
5 //
6 // root> .x testInterpolation.cxx
7 //
8 #include "TGraph.h"
9 #include "TAxis.h"
10 #include "TCanvas.h"
11 #include "TLegend.h"
12 #include "TApplication.h"
13 #include "TList.h"
14 #include "Math/Interpolator.h"
15 #include <iostream>
16 #include <string>
17 #include <cstdlib>
18 
19 #include <cmath>
20 
21 bool showGraphics = true;
22 
24 
25 void interpolate( const ROOT::Math::Interpolator & itp, bool drawSame = false ) {
26 
27  std::string type = itp.Type();
28  std::cout << "\n" << type << " interpolation:" << std::endl;
29 
30  std::cout << "x[i] y[i] deriv[i] deriv2[i] integral[i] \n" << std::endl;
31  // print result of interpolation
32  const Int_t n = 50; //JH replacing n = 51;
33  Int_t i = 0;
34  double xi = 0.;
35  Float_t xcoord[n], ycoord[n];
36 
37  for (i = 0; i < n; ++i) {
38  xi = 0.2 * i;
39  // JH replacing for ( double xi = 0; xi < 10.01; xi += 0.2) {
40  xcoord[i] = xi;
41  ycoord[i] = itp.Eval(xi);
42  double dyi = itp.Deriv(xi);
43  double dy2i = itp.Deriv2(xi);
44  double igyi = itp.Integ(0, xi);
45  std::cout << xcoord[i] << " " << ycoord[i] << " " << dyi << " " << dy2i << " " << igyi << std::endl;
46  }
47 
48  if (showGraphics) {
49  TGraph *gr = new TGraph(n,xcoord,ycoord);
50  gr->SetMarkerColor(kBlue);
51  if (drawSame) gr->SetMarkerColor(kGreen);
52  gr->SetMarkerStyle(7);
53  gr->Draw("CP");
54  if (!drawSame) {
55  TLegend * l = new TLegend(0.1,0.7,0.4,0.9);
56  l->SetName("legend");
57  l->AddEntry(grorig,"original data");
58  l->AddEntry(gr,type.c_str());
59  l->Draw();
60  }
61  else if (gPad) {
62  TLegend * l = (TLegend*) gPad->GetListOfPrimitives()->FindObject("legend");
63  if (l) {
64  l->AddEntry(gr, type.c_str() );
65  l->Draw();
66  }
67  }
68  }
69  if (gPad) gPad->Update();
70 
71  return;
72 
73 }
74 
75 
77 
78  // Create data
79  int n = 10;
80  Float_t xorig[11], yorig[11];
81  std::vector<double> x(n+1);
82  std::vector<double> y(n+1);
83  for (int i = 0; i < n+1; ++i) {
84  x[i] = i + 0.5 * std::sin(i+0.0);
85  y[i] = i + std::cos(i * i+0.0);
86  xorig[i] = x[i];
87  yorig[i] = y[i];
88  }
89 
90  TCanvas *c1 = 0;
91  if (showGraphics) {
92  c1 = new TCanvas("c1","Original (red), Linear (upper left), Polynomial (upper right), Spline , Spline periodic, Akima (lower left) and Akima Periodic (lower right) Interpolation",10,10,1000,800);
93  c1->Divide(2,3);
94  c1->cd(1);
95  }
96 
97  grorig = new TGraph(n+1,xorig,yorig);
98  grorig->SetMarkerColor(kRed);
99  grorig->SetMarkerStyle(20);
100  grorig->GetYaxis()->SetRange(0,40);
101  grorig->Draw("AP");
102 
103  //ROOT::Math::Interpolator itp1(x, y, ROOT::Math::Interpolation::kLINEAR);
105  itp1.SetData(x,y);
106  interpolate(itp1);
107 
108 
109  if (showGraphics) {
110  c1->cd(2);
111  grorig->Draw("AP");
112  }
113 
115  interpolate(itp2);
116 
117 
118  if (showGraphics) {
119  c1->cd(3);
120  grorig->Draw("AP");
121  }
122 
123  //std::cout << "Cubic Spline Interpolation: " << std::endl;
125  itp3.SetData(x.size(), &x[0], &y[0]);
126  interpolate(itp3);
127 
128  if (showGraphics) {
129  c1->cd(4);
130  grorig->Draw("AP");
131  }
132 
133  //std::cout << "Akima Interpolation: " << std::endl;
135  interpolate(itp4);
136 
137  if (showGraphics) {
138  c1->cd(5);
139  grorig->Draw("AP");
140  }
141 
142  //std::cout << "Cubic Spline Periodic Interpolation: " << std::endl;
144  interpolate(itp5);
145 
146  if (showGraphics) {
147  c1->cd(6);
148  grorig->Draw("AP");
149  }
150 
151  //std::cout << "Akima Periodic Interpolation: " << std::endl;
153  interpolate(itp6);
154 
155  std::cout << "\n***********************************" << std::endl;
156  std::cout << "Using default Interpolation type: " << std::endl;
157 
159  itp7.SetData(x,y);
160  interpolate(itp7,true);
161 
162 
163 }
164 
165 #ifndef __CINT__
166 int main(int argc, char **argv)
167 {
168  using std::cerr;
169  using std::cout;
170  using std::endl;
171  showGraphics = false;
172 
173  // Parse command line arguments
174  for (Int_t i=1 ; i<argc ; i++) {
175  std::string arg = argv[i] ;
176  if (arg == "-g") {
177  showGraphics = true;
178  }
179  if (arg == "-v") {
180  showGraphics = true;
181  //verbose = true;
182  }
183  if (arg == "-h") {
184  cerr << "Usage: " << argv[0] << " [-g] [-v]\n";
185  cerr << " where:\n";
186  cerr << " -g : graphics mode\n";
187  cerr << " -v : verbose mode";
188  cerr << endl;
189  return -1;
190  }
191  }
192 
193  TApplication* theApp = 0;
194 
195  if ( showGraphics )
196  theApp = new TApplication("App",&argc,argv);
197 
199 
200  if ( showGraphics )
201  {
202  theApp->Run();
203  delete theApp;
204  theApp = 0;
205  }
206 
207  return 0;
208 }
209 #endif
void testInterpolation()
double Integ(double a, double b) const
Return the Integral of the interpolated function over the range [a,b].
virtual void SetName(const char *name="")
Definition: TPave.h:72
bool SetData(const std::vector< double > &x, const std::vector< double > &y)
Set the data vector ( x[] and y[] ) To be efficient, the size of the data must be the same of the val...
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
double Deriv(double x) const
Return the derivative of the interpolated function at point x.
float Float_t
Definition: RtypesCore.h:53
return c1
Definition: legend1.C:41
Definition: Rtypes.h:56
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:452
bool showGraphics
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
TAxis * GetYaxis() const
Get y axis of the graph.
Definition: TGraph.cxx:1602
Class for performing function interpolation of points.
Definition: Interpolator.h:66
int Int_t
Definition: RtypesCore.h:41
Definition: Rtypes.h:56
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
double cos(double)
Double_t x[n]
Definition: legend1.C:17
virtual void Run(Bool_t retrn=kFALSE)
Main application eventloop. Calls system dependent eventloop via gSystem.
TGraph * grorig
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
double sin(double)
void interpolate(const ROOT::Math::Interpolator &itp, bool drawSame=false)
virtual void SetRange(Int_t first=0, Int_t last=0)
Set the viewing range for the axis from bin first to last.
Definition: TAxis.cxx:889
std::string Type() const
Return the type of interpolation method.
TLine * l
Definition: textangle.C:4
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
TGraphErrors * gr
Definition: legend1.C:25
The Canvas class.
Definition: TCanvas.h:31
int main(int argc, char **argv)
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:359
int type
Definition: TGX11.cxx:120
Double_t y[n]
Definition: legend1.C:17
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1135
double Deriv2(double x) const
Return the second derivative of the interpolated function at point x.
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
#define gPad
Definition: TVirtualPad.h:284
double Eval(double x) const
Return the interpolated value at point x.
Definition: Rtypes.h:56
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:39
const Int_t n
Definition: legend1.C:16