Jpp  18.5.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMath.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 
6 #include "TRandom3.h"
7 
8 #include "JMath/JMath.hh"
9 
10 #include "Jeep/JPrint.hh"
11 #include "Jeep/JParser.hh"
12 #include "Jeep/JMessage.hh"
13 
14 
15 namespace {
16 
17  using namespace JPP;
18 
19  /**
20  * A random object with some arithmetic capabilities.
21  */
22  struct JObject :
23  public JMath<JObject>
24  {
25  public:
26  /**
27  * Default constructor.
28  */
29  JObject() :
30  x(0.0),
31  y(0.0),
32  z(0.0)
33  {}
34 
35 
36  /**
37  * Constructor.
38  *
39  * \param __x x
40  * \param __y y
41  * \param __z z
42  */
43  JObject(const double __x,
44  const double __y,
45  const double __z) :
46  x(__x),
47  y(__y),
48  z(__z)
49  {}
50 
51 
52  /**
53  * Add object.
54  *
55  * \param object object
56  * \return this object
57  */
58  JObject& add(const JObject& object)
59  {
60  x += object.x;
61  y += object.y;
62  z += object.z;
63 
64  return *this;
65  }
66 
67 
68  /**
69  * Scale object.
70  *
71  * \param factor multiplication factor
72  * \return this object
73  */
74  JObject& mul(const double factor)
75  {
76  x *= factor;
77  y *= factor;
78  z *= factor;
79 
80  return *this;
81  }
82 
83 
84  /**
85  * Write object to output.
86  *
87  * \param out output stream
88  * \param object object
89  * \return output stream
90  */
91  friend inline std::ostream& operator<<(std::ostream& out, const JObject& object)
92  {
93  using namespace std;
94 
95  out << showpos << FIXED(5,2) << object.x << ' '
96  << showpos << FIXED(5,2) << object.y << ' '
97  << showpos << FIXED(5,2) << object.z;
98 
99  return out;
100  }
101 
102 
103  double x;
104  double y;
105  double z;
106  };
107 }
108 
109 
110 /**
111  * \file
112  *
113  * Example program to test user class with arithmetic capabilities (based on class JMATH::JMath).
114  * \author mdejong
115  */
116 int main(int argc, char**argv)
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 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1514
int main(int argc, char *argv[])
Definition: Main.cc:15
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:109
JObject()
Default constructor.
Definition: JObject.hh:53
then usage $script< input file >[option[primary[working directory]]] nWhere option can be E
Definition: JMuonPostfit.sh:40
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
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
static const double C
Physics constants.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
Auxiliary base class for inline creation of a static value or clone from a temporary object...
Definition: JObject.hh:18
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
const double xmin
Definition: JQuadrature.cc:23
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
Utility class to parse command line options.
Base class for data structures with artithmetic capabilities.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
source $JPP_DIR setenv csh $JPP_DIR &dev null eval JShellParser o a A
do echo Generating $dir eval D
Definition: JDrawLED.sh:53
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62