Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JTable1D.cc File Reference

Example program to test interpolation between 1D tables. More...

#include <iostream>
#include <iomanip>
#include <cmath>
#include "TRandom3.h"
#include "JTools/JTable1D.hh"
#include "JTools/JGrid.hh"
#include "JTools/JElement.hh"
#include "JTools/JPolint.hh"
#include "JTools/JGridCollection.hh"
#include "JTools/JMapList.hh"
#include "JTools/JMultiFunction.hh"
#include "JTools/JFunctionalMap_t.hh"
#include "JTools/JFunction1D_t.hh"
#include "JTools/JStats.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JTimer.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Example program to test interpolation between 1D tables.

Author
mdejong

Definition in file JTable1D.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 50 of file JTable1D.cc.

51{
52 using namespace std;
53 using namespace JPP;
54
55 int numberOfEvents;
56 int debug;
57
58 try {
59
60 JParser<> zap("Example program to test JTab1D & interpolation between 1D tables.");
61
62 zap['n'] = make_field(numberOfEvents);
63 zap['d'] = make_field(debug) = 2;
64
65 zap(argc, argv);
66 }
67 catch(const exception &error) {
68 FATAL(error.what() << endl);
69 }
70
71
72 const int NX = 21;
73 const int NY = 11;
74
75 const JGrid<double> X(NX, -10.0, +10.0);
76 const JGrid<double> Y(NY, -5.0, +5.0);
77
78 typedef JTable1D<NY> JTable1D_t;
81
82
83 JFunction1D_t buffer;
84
85 for (int i = 0; i != X.getSize(); ++i) {
86
87 DEBUG("table[" << i << "]" << endl);
88
89 const double x = X.getX(i);
90
91 JTable1D_t& table = buffer[x];
92
93 for (int __i = 0; __i != NY; ++__i) {
94
95 const double y = Y.getX(__i);
96
97 table[__i] = f2(x,y);
98
99 DEBUG(' ' << FIXED(5,3) << table[__i] << endl);
100
101 }
102 }
103
104 // Test 1: basic operation on table
105 const int NT = 3;
106 JTable1D<NT> tab1;
107 tab1[0] = 1; tab1[1] = 2; tab1[2] = 3;
108 JTable1D<NT> tab2;
109 tab2[0] = 4; tab2[1] = 5; tab2[2] = 6;
110
111 ASSERT(tab1.getNX() == NT, "getNX");
112 ASSERT(tab2[1] == 5, "GetEntry");
113 ASSERT(tab2.negate()[2] == -6, "Negate");
114 ASSERT((tab2.add(tab1))[0] == -3, "Add");
115 ASSERT((tab2.sub(tab1))[0] == -4, "Subtract");
116 ASSERT(tab1.mul(5)[1] == 10, "Multiply");
117 ASSERT(tab1.div(2)[1] == 5, "Divide");
118
119 // Test 2: performance of interpolator between table cells
121
122 g2.configure(Y);
123
124 JTimer t1("table");
125 JTimer t2("polint");
126
127 JStats fa("table");
128 JStats fb("polint");
129
130
131 for (int i = 0; i != numberOfEvents; ++i) {
132
133 const double x = gRandom->Uniform(X.getXmin(), X.getXmax());
134 const double y = gRandom->Uniform(Y.getXmin(), Y.getXmax());
135
136 const double v = f2(x,y);
137
138
139 // simple lookup table
140
141 t1.start();
142
143 const int __i = (int) (Y.getSize() * (y - Y.getXmin()) / (Y.getXmax() - Y.getXmin()));
144
145 const double w1 = buffer(x)[__i];
146
147 t1.stop();
148
149
150 // polynomial interpolation
151
152 t2.start();
153
154 const JTable1D_t& table = buffer(x);
155
156 for (int __i = 0; __i != Y.getSize(); ++__i) {
157 g2.getY(__i) = table[__i];
158
159 }
160
161 g2.compile();
162
163 const double w2 = g2(y);
164 t2.stop();
165
166 fa.put(v - w1);
167 fb.put(v - w2);
168 }
169
170
171 for (JStats buffer[] = { fa, fb, JStats() }, *i = buffer; i->getCount() != 0; ++i) {
172
173 cout << i->getTitle() << endl;
174 cout << "mean " << SCIENTIFIC(10,2) << i->getMean() << endl;
175 cout << "RMS " << SCIENTIFIC(10,2) << i->getSTDev() << endl;
176 }
177
178 for (JTimer buffer[] = { t1, t2, JTimer() }, *i = buffer; i->getTitle() != ""; ++i) {
179 i->print(cout, true, micro_t);
180 }
181}
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
Auxiliary class for CPU timing and usage.
Definition JTimer.hh:33
const std::string & getTitle() const
Get title.
Definition JTitle.hh:55
Utility class to parse command line options.
Definition JParser.hh:1698
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
Type definition of a 1st degree polynomial interpolation based on a JGridCollection with result type ...
Simple data structure for an abstract collection of equidistant abscissa values.
Definition JGrid.hh:40
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
1D table with arithmetic capabilities.
Definition JTable1D.hh:29
JTable1D & div(const double factor)
Scale table.
Definition JTable1D.hh:146
JTable1D & add(const JTable1D &table)
Add table.
Definition JTable1D.hh:99
static int getNX()
Get number of entries.
Definition JTable1D.hh:48
JTable1D & sub(const JTable1D &table)
Subtract table.
Definition JTable1D.hh:115
JTable1D & negate()
Negate table.
Definition JTable1D.hh:83
JTable1D & mul(const double factor)
Scale table.
Definition JTable1D.hh:130
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488