testTabulatedFunction.cc
Go to the documentation of this file.
1 
9 #include <utl/TabulatedFunction.h>
10 #include <utl/TabulatedFunctionErrors.h>
11 #include <utl/MultiTabulatedFunction.h>
12 #include <utl/MultiTabulatedFunctionErrors.h>
13 #include <utl/AugerException.h>
14 #include <iostream>
15 #include <iomanip>
16 #include <cstdlib>
17 #include <fstream>
18 
19 using namespace std;
20 
21 using namespace utl;
22 
23 
24 int
26 {
27  const double aX[] = { 1.1, 2.3, 4.2, 4.5, 6.7, 8.333, 12.12, 13.45 };
28  const double aY[] = { 5.7, 3.3, 6.3, 11.1, 1.1, -18.4, -20.7, 44.3 };
29  const int aN = 8;
30 
31  const double a2X[] = { 1, 2, 4, 3, 5, 6, 7, 8, 9, 10 };
32  const double a2Y[] = { 1, 2, 4, 3, 5, 6, 7, 8, 9, 10 };
33  const int a2N = 10;
34 
35  const std::vector<double> vecX(aX, aX + aN);
36  const std::vector<double> vecY(aY, aY + aN);
37  const std::vector<double> vec2X(a2X, a2X + a2N);
38  const std::vector<double> vec2Y(a2Y, a2Y + a2N);
39 
40  TabulatedFunction TF(vecX, vecY);
41  TabulatedFunction TF2(vec2X, vec2Y);
42 
43  const double tx[] = { 2.11, 5.6, 2.7, 8.333, 9 };
44  const int tN = 5;
45  cout << "Test points TF:\n";
46  for (int i = 0; i < tN; ++i)
47  cout << "x= " << setw(5) << tx[i] << " y= " << setw(5) << TF.Y(tx[i]) << '\n';
48  cout << endl;
49 
50  const double t2x[] = { 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5 };
51  const int t2N = 7;
52  cout << "Test points TF2:\n";
53  for (int i = 0; i < t2N; ++i)
54  cout << "x= " << setw(3) << t2x[i] << " y= " << setw(5) << TF2.Y(t2x[i]) << '\n';
55  cout << endl;
56 
57  double a3X[] = { 1, 3, 5 };
58  double a3Y[] = { 1, 9, 25 };
59  const int a3N = 3;
60  const std::vector<double> vec3X(a3X, a3X + a3N);
61  const std::vector<double> vec3Y(a3Y, a3Y + a3N);
62  const TabulatedFunction TF3(vec3X, vec3Y);
63 
64  cout << "Test points TF3:\n";
65  for (double xVal = 1.0; xVal <= 5.0; xVal += 0.1)
66  cout << "x= " << setw(3) << xVal << " y= " << setw(5) << TF3.Y(xVal) << '\n';
67  cout << endl;
68 
69  cout << "Test points TF3 - interpolation order 1:\n";
70  for (double xVal = 1.0; xVal <= 5.0 ; xVal += 0.1)
71  cout << "x= " << setw(3) << xVal << " y= " << setw(5) << TF3.InterpolateY(xVal, 1) << '\n';
72  cout << endl;
73 
74  cout << "Test points TF3 - interpolation order 2:\n";
75  for (double xVal = 1.0; xVal <= 5.0 ; xVal += 0.1)
76  cout << "x= " << setw(3) << xVal << " y= " << setw(5) << TF3.InterpolateY(xVal, 2) << '\n';
77 
78  cout << endl << "Testing operator[]..." << endl
79  << TF[0].X() << " " << TF[0].Y() << endl
80  << endl << "Testing iterator" << endl;
81 
83 
85  it != TF.End(); ++it)
86  cout << it->X() << " " << it->Y() << endl;
87 
88  cout << endl << "Testing operator[]..." << endl;
89  for (unsigned int i = 0; i < TF.GetNPoints(); ++i)
90  cout << TF[i].X() << " " << TF[i].Y() << endl;
91 
92  cout << endl
93  << endl << "Testing PushBack..." << endl;
94 
96  f.PushBack(1, 2);
97  f.PushBack(3, 4);
98  f.PushBack(5, 6);
99  f.PushBack(7, 8);
100  f.PushBack(9, 10);
101 
103  it != f.End(); ++it)
104  cout << it->X() << " " << it->Y() << endl;
105 
106  cout << "Bounds " << f.GetX(0) << " " << f.GetX(f.GetNPoints()-1) << endl;
107  cout << endl << "Testing const iterator" << endl;
108 
109  const TabulatedFunction cf(vecX, vecY);
110 
112  it != cf.End(); ++it)
113  cout << it->X() << " " << it->Y() << endl;
114  cout << endl;
115 
117  {
118  const double a = 1;
119  double b = 2;
120  double c = 3;
121  const double d = 4;
122 
123  tab.PushBack(a, b);
124  tab.PushBack(c, d);
125  }
126 
127  for (unsigned int i = 0; i < tab.GetNPoints(); ++i)
128  cout << tab[i].X() << " " << tab[i].Y() << endl;
129 
130  cout << "Testing TabulatedFunctionError" << endl;
131 
133 
134  tberr.PushBack(11., 0.5, 1., 0.1);
135  tberr.PushBack(12., 0.6, 2., 0.2);
136  tberr.PushBack(13., 0.7, 3., 0.3);
137  tberr.PushBack(14., 0.8, 4., 0.4);
138  tberr.PushBack(15., 0.9, 5., 0.5);
139  tberr.PushBack(16., 1.0, 6., 0.6);
140  tberr.PushBack(17., 1.1, 7., 0.7);
141 
142  cout << " iterator " << endl;
143 
145  it != tberr.End(); ++it)
146  cout << "x " << it->X() << " +- " << it->XErr()
147  << ", y " << it->Y() << " +- " << it->YErr() << endl;
148  cout << endl;
149 
150  cout << "Y(16.5)= " << tberr.Y(16.5) << endl;
151  cout << "Y(11.5)= " << tberr.Y(11.5) << endl;
152 
153  for (int i = 0; i < 3; ++i)
154  cout << "x " << tberr[i].X() << " +- " << tberr[i].XErr()
155  << ", y " << tberr[i].Y() << " +- " << tberr[i].YErr() << endl;
156 
157  cout << "Test MultiTabulatedFunction..." << endl;
158 
161 
162  mtabf.AddTabulatedFunction(f, 1);
163  mtabferr.AddTabulatedFunctionErrors(tberr, 2);
164 
166  cout << "x " << fp[0].X() <<endl;
167 
169  cout << "x " << fp2[0].X() << " +- " << fp2[0].XErr() << endl;
170 
171  cout << "Test copy ctor" << endl;
172  MultiTabulatedFunction copy = mtabf;
173  TabulatedFunction& fpcopy1 = copy.GetTabulatedFunction(1);
174  cout << "x " << fpcopy1[0].X() << endl;
175 
176  MultiTabulatedFunctionErrors copye = mtabferr;
178  cout << "x " << fpcopy2[0].X() << " +- " << fpcopy2[0].XErr() << endl;
179 
180  // see what happens if you initialize a tabluated function with just one pair
181  const std::vector<double> sX(1, 1.0);
182  const std::vector<double> sY(1, 10.0);
183  TabulatedFunction STF(sX, sY);
184  cout << "tabulated func initialzed with one pair" << endl;
185 
186  try {
187  cout << " STF(1.0) " << STF.Y(1.0) << endl;
188  } catch (AugerException& ex) {
189  cout << "message " << ex.GetMessage() << endl;
190  }
191 
192  try {
193  cout << " STF(0.5) " << STF.Y(0.5) << endl;
194  } catch (AugerException& ex) {
195  cout << "caught expected exception " << ex.GetExceptionName() << endl;
196  }
197 
198  cout << "checking FindX and FindY methods" << endl;
199  for(TabulatedFunctionErrIterator it = tberr.Begin(); it != tberr.End(); it++)
200  {
201  cout << it->X() << " == " << tberr.FindY(it->Y())->X() << "?\t"
202  << it->Y() << " == " << tberr.FindX(it->X())->Y() << "?" << endl;
203  }
204 
205  // try to push back two identical points
206  //
207  cout << "pushing the same point twice " << endl;
208  TabulatedFunction crap;
209  crap.PushBack(5., 15.);
210  crap.PushBack(10., 20.);
211  crap.PushBack(10., 20.);
212 
213  cout << "pushed 3 points, got " << crap.GetNPoints() << '\n'
214  << "crap(7) = " << crap.InterpolateY(7., 3) << endl;
215 
216  return EXIT_SUCCESS;
217 }
218 
219 
220 // Configure (x)emacs for this file ...
221 // Local Variables:
222 // mode: c++
223 // compile-command: "make -C .. testTabulatedFunction -k"
224 // End:
unsigned int GetNPoints() const
Base class for all exceptions used in the auger offline code.
Class to hold collection (x,y) points and provide interpolation between them.
void AddTabulatedFunction(const int label)
void PushBack(const double x, const double y)
double X() const
Definition: Pair.h:31
int main(int argc, char *argv[])
Definition: DBSync.cc:58
TabulatedFunction & GetTabulatedFunction(const int label=0)
Returns the TabulatedFunction for /par source, throws an exception if na.
const int tab
Definition: SdInspector.cc:35
A collection of TabulatedFunctionErrors, which provides methods to access different sources...
void PushBack(const double x, const double xErr, const double y, const double yErr)
TabulatedFunctionErrors & GetTabulatedFunctionErrors(const int label=0)
Returns the TabulatedFunctionErrors for /par source.
double InterpolateY(const double x, const unsigned int polyDegree) const
Interpolate the Y value with a polyDegree polynomial.
const double & GetX(const unsigned int idx) const
A collection of TabulatedFunction.
ConstIteratorErr FindX(const double x) const
double Y() const
Definition: Pair.h:32
double Y(const double x) const
Get or interpolate the Y value that corresponds to parameter x.
ConstIteratorErr FindY(const double y) const
const std::string & GetMessage() const
Retrieve the message from the exception.

, generated on Tue Sep 26 2023.