Logo ROOT   6.08/07
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 = 51;
33  Int_t i = 0;
34  Float_t xcoord[n], ycoord[n];
35  for ( double xi = 0; xi < 10; xi += 0.2) {
36  xcoord[i] = xi;
37  ycoord[i] = itp.Eval(xi);
38  double dyi = itp.Deriv(xi);
39  double dy2i = itp.Deriv2(xi);
40  double igyi = itp.Integ(0, xi);
41  std::cout << xcoord[i] << " " << ycoord[i] << " " << dyi << " " << dy2i << " " << igyi << std::endl;
42  i++;
43  }
44 
45  if (showGraphics) {
46  TGraph *gr = new TGraph(n,xcoord,ycoord);
47  gr->SetMarkerColor(kBlue);
48  if (drawSame) gr->SetMarkerColor(kGreen);
49  gr->SetMarkerStyle(7);
50  gr->Draw("CP");
51  if (!drawSame) {
52  TLegend * l = new TLegend(0.1,0.7,0.4,0.9);
53  l->SetName("legend");
54  l->AddEntry(grorig,"original data");
55  l->AddEntry(gr,type.c_str());
56  l->Draw();
57  }
58  else if (gPad) {
59  TLegend * l = (TLegend*) gPad->GetListOfPrimitives()->FindObject("legend");
60  if (l) {
61  l->AddEntry(gr, type.c_str() );
62  l->Draw();
63  }
64  }
65  }
66  if (gPad) gPad->Update();
67 
68  return;
69 
70 }
71 
72 
74 
75  // Create data
76  int n = 10;
77  Float_t xorig[11], yorig[11];
78  std::vector<double> x(n+1);
79  std::vector<double> y(n+1);
80  for (int i = 0; i < n+1; ++i) {
81  x[i] = i + 0.5 * std::sin(i+0.0);
82  y[i] = i + std::cos(i * i+0.0);
83  xorig[i] = x[i];
84  yorig[i] = y[i];
85  }
86 
87  TCanvas *c1 = 0;
88  if (showGraphics) {
89  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);
90  c1->Divide(2,3);
91  c1->cd(1);
92  }
93 
94  grorig = new TGraph(n+1,xorig,yorig);
95  grorig->SetMarkerColor(kRed);
96  grorig->SetMarkerStyle(20);
97  grorig->GetYaxis()->SetRange(0,40);
98  grorig->Draw("AP");
99 
100  //ROOT::Math::Interpolator itp1(x, y, ROOT::Math::Interpolation::kLINEAR);
102  itp1.SetData(x,y);
103  interpolate(itp1);
104 
105 
106  if (showGraphics) {
107  c1->cd(2);
108  grorig->Draw("AP");
109  }
110 
112  interpolate(itp2);
113 
114 
115  if (showGraphics) {
116  c1->cd(3);
117  grorig->Draw("AP");
118  }
119 
120  //std::cout << "Cubic Spline Interpolation: " << std::endl;
122  itp3.SetData(x.size(), &x[0], &y[0]);
123  interpolate(itp3);
124 
125  if (showGraphics) {
126  c1->cd(4);
127  grorig->Draw("AP");
128  }
129 
130  //std::cout << "Akima Interpolation: " << std::endl;
132  interpolate(itp4);
133 
134  if (showGraphics) {
135  c1->cd(5);
136  grorig->Draw("AP");
137  }
138 
139  //std::cout << "Cubic Spline Periodic Interpolation: " << std::endl;
141  interpolate(itp5);
142 
143  if (showGraphics) {
144  c1->cd(6);
145  grorig->Draw("AP");
146  }
147 
148  //std::cout << "Akima Periodic Interpolation: " << std::endl;
150  interpolate(itp6);
151 
152  std::cout << "\n***********************************" << std::endl;
153  std::cout << "Using default Interpolation type: " << std::endl;
154 
156  itp7.SetData(x,y);
157  interpolate(itp7,true);
158 
159 
160 }
161 
162 #ifndef __CINT__
163 int main(int argc, char **argv)
164 {
165  using std::cerr;
166  using std::cout;
167  using std::endl;
168  showGraphics = false;
169 
170  // Parse command line arguments
171  for (Int_t i=1 ; i<argc ; i++) {
172  std::string arg = argv[i] ;
173  if (arg == "-g") {
174  showGraphics = true;
175  }
176  if (arg == "-v") {
177  showGraphics = true;
178  //verbose = true;
179  }
180  if (arg == "-h") {
181  cerr << "Usage: " << argv[0] << " [-g] [-v]\n";
182  cerr << " where:\n";
183  cerr << " -g : graphics mode\n";
184  cerr << " -v : verbose mode";
185  cerr << endl;
186  return -1;
187  }
188  }
189 
190  TApplication* theApp = 0;
191 
192  if ( showGraphics )
193  theApp = new TApplication("App",&argc,argv);
194 
196 
197  if ( showGraphics )
198  {
199  theApp->Run();
200  delete theApp;
201  theApp = 0;
202  }
203 
204  return 0;
205 }
206 #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:76
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:27
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:61
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
bool showGraphics
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
TAxis * GetYaxis() const
Get y axis of the graph.
Definition: TGraph.cxx:1596
Class for performing function interpolation of points.
Definition: Interpolator.h:66
int Int_t
Definition: RtypesCore.h:41
Definition: Rtypes.h:61
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:747
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:43
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:887
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:45
TGraphErrors * gr
Definition: legend1.C:25
The Canvas class.
Definition: TCanvas.h:41
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:280
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:1089
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:53
#define gPad
Definition: TVirtualPad.h:289
double Eval(double x) const
Return the interpolated value at point x.
Definition: Rtypes.h:61
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:45
const Int_t n
Definition: legend1.C:16