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

Example program to test user class with arithmetic capabilities (based on class JMATH::JMath). More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include "TRandom3.h"
#include "JMath/JMath.hh"
#include "Jeep/JPrint.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 user class with arithmetic capabilities (based on class JMATH::JMath).

Author
mdejong

Definition in file JMath.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 116 of file JMath.cc.

117{
118 using namespace std;
119 using namespace JPP;
120
121 double alpha;
122 double precision;
123 int debug;
124
125 try {
126
127 JParser<> zap("Example program to test user class with arithmetic capabilities.");
128
129 zap['a'] = make_field(alpha);
130 zap['e'] = make_field(precision) = 1.0e-10;
131 zap['d'] = make_field(debug) = 3;
132
133 zap(argc, argv);
134 }
135 catch(const exception &error) {
136 FATAL(error.what() << endl);
137 }
138
139
140 gRandom->SetSeed(0);
141
142 const Double_t xmin = -1.0;
143 const Double_t xmax = +1.0;
144
145
146 const JObject A(gRandom->Uniform(xmin, xmax),
147 gRandom->Uniform(xmin, xmax),
148 gRandom->Uniform(xmin, xmax));
149
150 const JObject B(gRandom->Uniform(xmin, xmax),
151 gRandom->Uniform(xmin, xmax),
152 gRandom->Uniform(xmin, xmax));
153
154 const JObject C = A + B;
155 const JObject D = A * alpha;
156 const JObject E = interpolate(A, B, alpha);
157
158 DEBUG("A " << A << endl);
159 DEBUG("B " << B << endl);
160 DEBUG("A + B = " << C << endl);
161 DEBUG("A * alpha = " << D << endl);
162 DEBUG("interpolate " << E << endl);
163
164 ASSERT(fabs(C.x - (A.x + B.x)) <= precision, "test of addition");
165 ASSERT(fabs(C.y - (A.y + B.y)) <= precision, "test of addition");
166 ASSERT(fabs(C.z - (A.z + B.z)) <= precision, "test of addition");
167
168 ASSERT(fabs(D.x - (A.x * alpha)) <= precision, "test of multiplication");
169 ASSERT(fabs(D.y - (A.y * alpha)) <= precision, "test of multiplication");
170 ASSERT(fabs(D.z - (A.z * alpha)) <= precision, "test of multiplication");
171
172 ASSERT(fabs(E.x - (A.x * (1.0 - alpha) + B.x * alpha)) <= precision, "test of interpolation");
173 ASSERT(fabs(E.y - (A.y * (1.0 - alpha) + B.y * alpha)) <= precision, "test of interpolation");
174 ASSERT(fabs(E.z - (A.z * (1.0 - alpha) + B.z * alpha)) <= precision, "test of interpolation");
175
176 {
177 vector<JObject> buffer;
178
179 double x = 0.0;
180 double y = 0.0;
181 double z = 0.0;
182
183 const int N = 10;
184
185 for (int i = 0; i != N; ++i) {
186
187 buffer.push_back(JObject(gRandom->Uniform(xmin, xmax),
188 gRandom->Uniform(xmin, xmax),
189 gRandom->Uniform(xmin, xmax)));
190
191 x += buffer.rbegin()->x;
192 y += buffer.rbegin()->y;
193 z += buffer.rbegin()->z;
194 }
195
196 x /= N;
197 y /= N;
198 z /= N;
199
200 JObject U = getAverage(buffer.begin(), buffer.end());
201
202 DEBUG("U " << U << endl);
203 DEBUG("U' " << JObject(x,y,z) << endl);
204
205 ASSERT(fabs(U.x - x) <= precision, "test of averaging");
206 ASSERT(fabs(U.y - y) <= precision, "test of averaging");
207 ASSERT(fabs(U.z - z) <= precision, "test of averaging");
208 }
209
210 {
211 double buffer[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
212
213 ASSERT(fabs(getAverage(buffer) - 5.0) <= precision, "test of averaging");
214 }
215
216 return 0;
217}
#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
Utility class to parse command line options.
Definition JParser.hh:1698
const double xmax
const double xmin
T interpolate(const T &first, const T &second, const double alpha)
Interpolation between objects.
Definition JMath.hh:397
std::iterator_traits< T >::value_type getAverage(T __begin, T __end)
Get average.
Definition JMath.hh:494
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary base class for inline creation of a static value or clone from a temporary object.
Definition JObject.hh:18