Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JTable2D.cc
Go to the documentation of this file.
1
2#include <iostream>
3#include <iomanip>
4#include <cmath>
5
6#include "TRandom3.h"
7
8#include "JTools/JTable2D.hh"
9#include "JTools/JGrid.hh"
10#include "JTools/JElement.hh"
11#include "JTools/JPolint.hh"
13#include "JTools/JMapList.hh"
17#include "JTools/JStats.hh"
18
19#include "Jeep/JPrint.hh"
20#include "Jeep/JTimer.hh"
21#include "Jeep/JParser.hh"
22#include "Jeep/JMessage.hh"
23
24
25namespace {
26
27 /**
28 * 3D function.
29 *
30 * \param x x value
31 * \param y y value
32 * \param z z value
33 * \return function value
34 */
35 inline double f3(const double x,
36 const double y,
37 const double z)
38 {
39 const double u = 1.0e2 / (x*x);
40
41 return exp(-0.5*(y*y)/(u*u) - 0.5*(z*z)/(u*u));
42 }
43}
44
45
46/**
47 * \file
48 *
49 * Example program to test interpolation between 2D tables.
50 * \author mdejong
51 */
52int main(int argc, char **argv)
53{
54 using namespace std;
55 using namespace JPP;
56
57 int numberOfEvents;
58 int debug;
59
60 try {
61
62 JParser<> zap("Example program to test interpolation between 2D tables.");
63
64 zap['n'] = make_field(numberOfEvents);
65 zap['d'] = make_field(debug) = 2;
66
67 zap(argc, argv);
68 }
69 catch(const exception &error) {
70 FATAL(error.what() << endl);
71 }
72
73
74 const int NX = 21;
75 const int NY = 11;
76 const int NZ = 11;
77
78 const JGrid<double> X(NX, -10.0, +10.0);
79 const JGrid<double> Y(NY, -5.0, +5.0);
80 const JGrid<double> Z(NZ, -5.0, +5.0);
81
82
83 typedef JTable2D<NY, NZ> JTable2D_t;
86
87
88 JFunction1D_t buffer;
89
90 for (int i = 0; i != X.getSize(); ++i) {
91
92 DEBUG("table[" << i << "]" << endl);
93
94 const double x = X.getX(i);
95
96 JTable2D_t& table = buffer[x];
97
98 for (int __i = 0; __i != NY; ++__i) {
99 for (int __j = 0; __j != NZ; ++__j) {
100
101 const double y = Y.getX(__i);
102 const double z = Z.getX(__j);
103
104 table[__i][__j] = f3(x,y,z);
105
106 DEBUG(' ' << FIXED(5,3) << table[__i][__j]);
107 }
108 DEBUG(endl);
109 }
110 }
111
112
113 // interpolator between table cells
114
115 typedef JMapList<JPolint1FunctionalGridMap> JMaplist_t;
117
118 JMultiFunction_t g2;
119
120 g2.configure(Y);
121
122 for (JMultiFunction_t::iterator i = g2.begin(); i != g2.end(); ++i) {
123 i->getY().configure(Z);
124 }
125
126
127 JTimer t1("table");
128 JTimer t2("polint");
129
130 JStats f1("table");
131 JStats f2("polint");
132
133
134 for (int i = 0; i != numberOfEvents; ++i) {
135
136 const double x = gRandom->Uniform(X.getXmin(), X.getXmax());
137 const double y = gRandom->Uniform(Y.getXmin(), Y.getXmax());
138 const double z = gRandom->Uniform(Z.getXmin(), Z.getXmax());
139
140 const double v = f3(x,y,z);
141
142
143 // simple lookup table
144
145 t1.start();
146
147 const int __i = (int) (Y.getSize() * (y - Y.getXmin()) / (Y.getXmax() - Y.getXmin()));
148 const int __j = (int) (Z.getSize() * (z - Z.getXmin()) / (Z.getXmax() - Z.getXmin()));
149
150 const double w1 = buffer(x)[__i][__j];
151
152 t1.stop();
153
154
155 // polynomial interpolation
156
157 t2.start();
158
159 const JTable2D_t& table = buffer(x);
160
161 for (int __i = 0; __i != Y.getSize(); ++__i) {
162 for (int __j = 0; __j != Z.getSize(); ++__j) {
163 g2.getY(__i).getY(__j) = table[__i][__j];
164 }
165 }
166
167 g2.compile();
168
169 const double w2 = g2(y,z);
170
171 t2.stop();
172
173 f1.put(v - w1);
174 f2.put(v - w2);
175 }
176
177
178 for (JStats buffer[] = { f1, f2, JStats() }, *i = buffer; i->getCount() != 0; ++i) {
179
180 cout << i->getTitle() << endl;
181 cout << "mean " << SCIENTIFIC(10,2) << i->getMean() << endl;
182 cout << "RMS " << SCIENTIFIC(10,2) << i->getSTDev() << endl;
183 }
184
185 for (JTimer buffer[] = { t1, t2, JTimer() }, *i = buffer; i->getTitle() != ""; ++i) {
186 i->print(cout, true, micro_t);
187 }
188}
The elements in a collection are sorted according to their abscissa values and a given distance opera...
Various implementations of functional maps.
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
double f3(const double x, const double y, const double z)
3D function.
I/O formatting auxiliaries.
int main(int argc, char **argv)
Definition JTable2D.cc:52
Auxiliary class for CPU timing and usage.
Definition JTimer.hh:33
void stop()
Stop timer.
Definition JTimer.hh:127
void start()
Start timer.
Definition JTimer.hh:106
const std::string & getTitle() const
Get title.
Definition JTitle.hh:55
Utility class to parse command line options.
Definition JParser.hh:1698
Multidimensional interpolation method.
Template class for polynomial interpolation in 1D.
Definition JPolint.hh:1095
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
2D Element.
Definition JPolint.hh:1131
Simple data structure for an abstract collection of equidistant abscissa values.
Definition JGrid.hh:40
virtual int getSize() const override
Get number of elements.
Definition JGrid.hh:75
virtual abscissa_type getXmin() const override
Get minimal abscissa value.
Definition JGrid.hh:98
virtual abscissa_type getXmax() const override
Get maximal abscissa value.
Definition JGrid.hh:109
virtual abscissa_type getX(int index) const override
Get abscissa value.
Definition JGrid.hh:87
Map list.
Definition JMapList.hh:25
Auxiliary data structure for running average and standard deviation.
Definition JStats.hh:44
long long int getCount() const
Get total count.
Definition JStats.hh:168
void put(const double x, const double w=1.0)
Put value.
Definition JStats.hh:119
2D table with arithmetic capabilities.
Definition JTable2D.hh:29
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488