Logo ROOT   6.08/07
Reference Guide
stress3D.cxx
Go to the documentation of this file.
1 #include "Math/Vector3D.h"
2 #include "Math/Point3D.h"
3 #include "TVector3.h"
4 
5 #include "Math/Transform3D.h"
6 #include "Math/Rotation3D.h"
7 #include "Math/Translation3D.h"
8 #include "Math/RotationZYX.h"
9 #include "TRotation.h"
10 
11 #include "TStopwatch.h"
12 #include "TRandom3.h"
13 
14 #include <vector>
15 
16 using namespace ROOT::Math;
17 
18 class VectorTest {
19 
20 private:
21 
22  size_t n2Loop ;
23  size_t nGen;
24 
25 // global data variables
26  std::vector<double> dataX;
27  std::vector<double> dataY;
28  std::vector<double> dataZ;
29 
30 
31 
32 public:
33 
34  VectorTest(int n1, int n2) :
35  n2Loop(n1),
36  nGen(n2),
37  dataX(std::vector<double>(n2)),
38  dataY(std::vector<double>(n2)),
39  dataZ(std::vector<double>(n2))
40  {}
41 
42 
43 
44 
45  void print(TStopwatch & time, std::string s) {
46  int pr = std::cout.precision(8);
47  std::cout << s << "\t" << " time = " << time.RealTime() << "\t(sec)\t"
48  // << time.CpuTime()
49  << std::endl;
50  std::cout.precision(pr);
51  }
52 
53 
54  int check(std::string name, double s1, double s2, double s3, double scale=1) {
55  double eps = 10*scale*std::numeric_limits<double>::epsilon();
56  if ( std::fabs(s1-s2) < eps*std::fabs(s1) && std::fabs(s1-s3) < eps*std::fabs(s1) ) return 0;
57  std::cout.precision(16);
58  std::cout << s1 << "\t" << s2 <<"\t" << s3 << "\n";
59  std::cout << "Test " << name << " failed !!\n\n";
60  return -1;
61  }
62 
63 
64  void genData() {
65  int n = nGen;
66 
67  // generate n -4 momentum quantities
68  TRandom3 r;
69  for (int i = 0; i < n ; ++ i) {
70 
71  double phi = r.Rndm()*3.1415926535897931;
72  double eta = r.Uniform(-5.,5.);
73  double pt = r.Exp(10.);
74 
75  // fill vectors
76 
77  ROOT::Math::RhoEtaPhiVector q( pt, eta, phi);
78  dataX[i] = q.x();
79  dataY[i] = q.y();
80  dataZ[i] = q.z();
81 
82  }
83  }
84 
85 
86 
87  template <class V>
88  void testCreate( std::vector<V *> & dataV, TStopwatch & tim, double& t, std::string s) {
89 
90  int n = dataX.size();
91  dataV.resize(n);
92  tim.Start();
93  for (int i = 0; i < n; ++i) {
94  dataV[i] = new V( dataX[i], dataY[i], dataZ[i] );
95  }
96  tim.Stop();
97  t += tim.RealTime();
98  print(tim,s);
99  }
100 
101 
102 template <class V>
103 double testVectorAddition( const std::vector<V *> & dataV, TStopwatch & tim, double& t, std::string s) {
104  unsigned int n = std::min(n2Loop, dataV.size() );
105  tim.Start();
106  V vSum = *(dataV[0]);
107  for (unsigned int i = 1; i < n; ++i) {
108  vSum += *(dataV[i]);
109  }
110  tim.Stop();
111  print(tim,s);
112  t += tim.RealTime();
113  return vSum.Mag2();
114 }
115 
116 template <class P>
117 double testPointAddition( const std::vector<P *> & dataP, TStopwatch & tim, double& t, std::string s) {
118  unsigned int n = std::min(n2Loop, dataP.size() );
119  tim.Start();
120  P pSum = *(dataP[0]);
121  for (unsigned int i = 1; i < n; ++i) {
122  P & p2 = *(dataP[i]);
123 #ifndef HEAP_CREATION
124  pSum += ROOT::Math::XYZVector(p2);
125 #else
127  pSum += *v2;
128 #endif
129  }
130  tim.Stop();
131  print(tim,s);
132  t += tim.RealTime();
133  return pSum.Mag2();
134 }
135 
136 template<class V>
137 double getSum(const V & v) {
138  return v.x() + v.y() + v.z();
139 }
140 
141 
142 // direct translation
143 template <class V>
144  double testTranslation( std::vector<V *> & dataV, const Translation3D & tr, TStopwatch & tim, double& t, std::string s) {
145 
146  unsigned int n = dataV.size();
147  tim.Start();
148  double sum = 0;
149  double dx,dy,dz;
150  tr.GetComponents(dx,dy,dz);
151  V vtrans(dx,dy,dz);
152  for (unsigned int i = 0; i < n; ++i) {
153  V & v1 = *(dataV[i]);
154  V v2 = v1 + vtrans;
155  sum += getSum(v2);
156  }
157  tim.Stop();
158  print(tim,s);
159  t += tim.RealTime();
160  return sum;
161  }
162 
163 // transformation
164 template <class V, class T>
165  double testTransform( std::vector<V *> & dataV, const T & trans, TStopwatch & tim, double& t, std::string s) {
166 
167  unsigned int n = dataV.size();
168  tim.Start();
169  double sum = 0;
170  for (unsigned int i = 0; i < n; ++i) {
171  V & v1 = *(dataV[i]);
172  V v2 = trans * v1;
173  sum += getSum(v2);
174  }
175  tim.Stop();
176  print(tim,s);
177  t += tim.RealTime();
178  return sum;
179  }
180 
181 // transformation product
182 template <class V, class T1, class T2>
183  double testTransformProd( std::vector<V *> & dataV, const T1 & trans, const T2 &, TStopwatch & tim, double& t, std::string s) {
184 
185  unsigned int n = dataV.size();
186  tim.Start();
187  double sum = 0;
188  for (unsigned int i = 0; i < n; ++i) {
189  V & v1 = *(dataV[i]);
190  V v2 = T2(XYZVector(v1)) * trans * v1;
191  sum += getSum(v2);
192  }
193  tim.Stop();
194  print(tim,s);
195  t += tim.RealTime();
196  return sum;
197  }
198 
199 // transformation product
200 template <class V, class T1, class T2>
201 double testTransformProd2( std::vector<V *> & dataV, const T1 & trans, const T2 &, TStopwatch & tim, double& t, std::string s) {
202 
203  unsigned int n = dataV.size();
204  tim.Start();
205  double sum = 0;
206  for (unsigned int i = 0; i < n; ++i) {
207  V & v1 = *(dataV[i]);
208  V v2 = trans * T2(XYZVector(v1)) * v1;
209  sum += getSum(v2);
210  }
211  tim.Stop();
212  print(tim,s);
213  t += tim.RealTime();
214  return sum;
215  }
216 
217 // transformation product
218 template <class V, class T1, class T2>
219 double testTransformProd3( std::vector<V *> & dataV, const T1 & trans1, const T2 & trans2, TStopwatch & tim, double& t, std::string s) {
220 
221  unsigned int n = dataV.size();
222  tim.Start();
223  double sum = 0;
224  for (unsigned int i = 0; i < n; ++i) {
225  V & v1 = *(dataV[i]);
226  V v2 = trans2 * trans1 * v1;
227  sum += getSum(v2);
228  }
229  tim.Stop();
230  print(tim,s);
231  t += tim.RealTime();
232  return sum;
233  }
234 
235 }; // end class VectorTest
236 
237 
238 int main(int argc,const char *argv[]) {
239 
240  int ngen = 1000000;
241  if (argc > 1) ngen = atoi(argv[1]);
242  int nloop2 = ngen;
243  if (argc > 2) nloop2 = atoi(argv[1]);
244 
245  std::cout << "Test with Ngen = " << ngen << " n2loop = " << nloop2 << std::endl;
246 
247  TStopwatch t;
248 
249  VectorTest a(ngen,nloop2);
250 
251  a.genData();
252 
253 
254  double t1 = 0;
255  double t2 = 0;
256  double t3 = 0;
257 
258  std::vector<TVector3 *> v1;
259  std::vector<ROOT::Math::XYZVector *> v2;
260  std::vector<ROOT::Math::XYZPoint *> v3;
261 
262  double s1,s2,s3;
263 
264  a.genData();
265  a.testCreate (v2, t, t2, "creation XYZVector " );
266 
267  a.genData();
268  a.testCreate (v3, t, t3, "creation XYZPoint " );
269 
270  a.genData();
271  a.testCreate (v1, t, t1, "creation TVector3 " );
272 
273  std::cout << "\n";
274 
275 
276  s1=a.testVectorAddition (v1, t, t1, "Addition TVector3 " );
277  s2=a.testVectorAddition (v2, t, t2, "Addition XYZVector " );
278  s3=a.testPointAddition (v3, t, t3, "Addition XYZPoint " );
279 
280  a.check("Addition",s1,s2,s3);
281 
282 #ifdef MORETEST
283 
284  s2=a.testVectorAddition (v2, t, t2, "Addition XYZVector " );
285  s1=a.testVectorAddition (v1, t, t1, "Addition TVector3 " );
286  s3=a.testPointAddition (v3, t, t3, "Addition XYZPoint " );
287 
288  s2=a.testVectorAddition (v2, t, t2, "Addition XYZVector " );
289  s3=a.testPointAddition (v3, t, t3, "Addition XYZPoint " );
290  s1=a.testVectorAddition (v1, t, t1, "Addition TVector3 " );
291 
292  s1=a.testVectorAddition (v1, t, t1, "Addition TVector3 " );
293  s3=a.testPointAddition (v3, t, t3, "Addition XYZPoint " );
294  s2=a.testVectorAddition (v2, t, t2, "Addition XYZVector " );
295 
296  s3=a.testPointAddition (v3, t, t3, "Addition XYZPoint " );
297  s2=a.testVectorAddition (v2, t, t2, "Addition XYZVector " );
298  s1=a.testVectorAddition (v1, t, t1, "Addition TVector3 " );
299 
300  s3=a.testPointAddition (v3, t, t3, "Addition XYZPoint " );
301  s1=a.testVectorAddition (v1, t, t1, "Addition TVector3 " );
302  s2=a.testVectorAddition (v2, t, t2, "Addition XYZVector " );
303 
304 #endif
305  std::cout << "\n";
306 
307  // test the rotation and transformations
308  TRotation r1; r1.RotateZ(3.); r1.RotateY(2.); r1.RotateX(1);
309  Rotation3D r2 (RotationZYX(3., 2., 1.) );
310 
311  s1=a.testTransform (v1, r1, t, t1, "TRotation TVector3 " );
312  s2=a.testTransform (v2, r2, t, t2, "Rotation3D XYZVector " );
313  s3=a.testTransform (v3, r2, t, t3, "Rotation3D XYZPoint " );
314 
315  a.check("Rotation3D",s1,s2,s3);
316  double s2a = s2;
317  std::cout << "\n";
318 
319  double s4;
320  double t0;
321  Translation3D tr(1.,2.,3.);
322  s1=a.testTranslation (v1, tr, t, t1, "Shift TVector3 " );
323  s2=a.testTranslation (v2, tr, t, t2, "Shift XYZVector " );
324  s3=a.testTransform (v3, tr, t, t3, "Translation3D XYZPoint " );
325  s4=a.testTransform (v2, tr, t, t0, "Translation3D XYZVector " );
326 
327  a.check("Translation3D",s1,s2,s3);
328 
329  std::cout << "\n";
330 
331  Transform3D tf(r2,tr);
332  //s1=a.testTransform (v1, tf, t, t1, "Transform3D TVector3 " );
333  s2=a.testTransform (v2, tf, t, t0, "Transform3D XYZVector " );
334  s3=a.testTransform (v3, tf, t, t0, "Transform3D XYZPoint " );
335  double s2b = s2;
336 
337 
338  std::cout << "\n";
339 
340  // test product of one vs the other
341 
342  double s5;
343  // rotation x translation
344  //s1=a.testTransformProd (v1, r2, tf, t, t1, "Delta * Rot TVector3 " );
345  s2=a.testTransformProd (v2, r2, tr, t, t0, "Delta * Rot XYZVector " );
346  s3=a.testTransformProd (v3, r2, tr, t, t0, "Delta * Rot XYZPoint " );
347 
348  Transform3D tfr(r2);
349  s4=a.testTransformProd (v2, tfr, tf, t, t0, "Delta * Rot(T) XYZVector " );
350  s5=a.testTransformProd (v3, tfr, tf, t, t0, "Delta * Rot(T) XYZPoint " );
351  a.check("Delta * Rot",s3,s5,s5);
352  // only rot on vectors
353  a.check("Trans Vec",s2a,s2b,s2);
354  a.check("Trans Vec",s2a,s2,s4);
355 
356 
357  std::cout << "\n";
358 
359  // translation x rotation
360  //s1=a.testTransformProd2 (v1, r2, tf, t, t1, "Rot * Delta TVector3 " );
361  s2=a.testTransformProd2 (v2, r2, tr, t, t0, "Rot * Delta XYZVector " );
362  s3=a.testTransformProd2 (v3, r2, tr, t, t0, "Rot * Delta XYZPoint " );
363 
364  s4=a.testTransformProd2 (v2, tfr, tf, t, t0, "Rot * Delta(T) XYZVector " );
365  s5=a.testTransformProd2 (v3, tfr, tf, t, t0, "Rot * Delta(T) XYZPoint " );
366 
367  a.check("Rot * Delta",s3,s5,s5);
368  // only rot per vec
369  a.check("Trans Vec",s2a,s2,s4);
370 
371 
372  std::cout << "\n";
373  s2=a.testTransformProd (v2, tf, Translation3D(), t, t0, "Delta * Trans XYZVector " );
374  s3=a.testTransformProd (v3, tf, Translation3D(), t, t0, "Delta * Trans XYZPoint " );
375  s4=a.testTransformProd (v2, tf, Transform3D(), t, t0, "TDelta * Trans XYZVector " );
376  s5=a.testTransformProd (v3, tf, Transform3D(), t, t0, "TDelta * Trans XYZPoint " );
377  a.check("Delta * Trans",s3,s5,s5);
378  a.check("Delta * Trans Vec",s2a,s2,s4);
379 
380  std::cout << "\n";
381  s2=a.testTransformProd2 (v2, tf, Translation3D(), t, t0, "Trans * Delta XYZVector " );
382  s3=a.testTransformProd2 (v3, tf, Translation3D(), t, t0, "Trans * Delta XYZPoint " );
383  s4=a.testTransformProd2 (v2, tf, Transform3D(), t, t0, "Trans * TDelta XYZVector " );
384  s5=a.testTransformProd2 (v3, tf, Transform3D(), t, t0, "Trans * TDelta XYZPoint " );
385  a.check("Delta * Trans",s3,s5,s5);
386  a.check("Delta * Trans Vec",s2a,s2,s4);
387 
388  std::cout << "\n";
389  s2=a.testTransformProd (v2, tf, Translation3D(), t, t0, "Delta * Trans XYZVector " );
390  s3=a.testTransformProd (v3, tf, Translation3D(), t, t0, "Delta * Trans XYZPoint " );
391  s4=a.testTransformProd (v2, tf, Transform3D(), t, t0, "TDelta * Trans XYZVector " );
392  s5=a.testTransformProd (v3, tf, Transform3D(), t, t0, "TDelta * Trans XYZPoint " );
393  a.check("Delta * Trans",s3,s5,s5);
394  a.check("Delta * Trans Vec",s2a,s2,s4);
395 
396  std::cout << "\n";
397  s2=a.testTransformProd2 (v2, tf, Translation3D(), t, t0, "Trans * Delta XYZVector " );
398  s3=a.testTransformProd2 (v3, tf, Translation3D(), t, t0, "Trans * Delta XYZPoint " );
399  s4=a.testTransformProd2 (v2, tf, Transform3D(), t, t0, "Trans * TDelta XYZVector " );
400  s5=a.testTransformProd2 (v3, tf, Transform3D(), t, t0, "Trans * TDelta XYZPoint " );
401  a.check("Delta * Trans",s3,s5,s5);
402  a.check("Delta * Trans Vec",s2a,s2,s4);
403 
404  std::cout << "\n";
405  s1=a.testTransformProd3 (v1, r2, r2, t, t0, "Rot * Rot TVector3 " );
406  s2=a.testTransformProd3 (v2, r2, r2, t, t0, "Rot * Rot XYZVector " );
407  s3=a.testTransformProd3 (v3, r2, r2, t, t0, "Rot * Rot XYZPoint " );
408  a.check("Rot * Rot",s1,s2,s3);
409  s2a = s2;
410 
411  std::cout << "\n";
412 
413  s2=a.testTransformProd3 (v2, tf, r2, t, t0, "Rot * Trans XYZVector " );
414  s3=a.testTransformProd3 (v3, tf, r2, t, t0, "Rot * Trans XYZPoint " );
415  s4=a.testTransformProd3 (v2, tf, Transform3D(r2), t, t0, "TRot * Trans XYZVector " );
416  s5=a.testTransformProd3 (v3, tf, Transform3D(r2), t, t0, "TRot * Trans XYZPoint " );
417  a.check("Rot * Trans Pnt",s3,s5,s5);
418  a.check("Rot * Trans Vec",s2a,s2,s4);
419 
420  std::cout << "\n";
421 
422  s2=a.testTransformProd3 (v2, r2, tf, t, t0, "Trans * Rot XYZVector " );
423  s3=a.testTransformProd3 (v3, r2, tf, t, t0, "Trans * Rot XYZPoint " );
424  s4=a.testTransformProd3 (v2, Transform3D(r2), tf, t, t0, "Trans * TRot XYZVector " );
425  s5=a.testTransformProd3 (v3, Transform3D(r2), tf, t, t0, "Trans * TRot XYZPoint " );
426 
427  a.check("Rot * Trans Pnt",s3,s5,s5);
428  a.check("Rot * Trans Vec",s2a,s2,s4);
429 
430  std::cout << "\n";
431 
432  std::cout << "Total Time for TVector3 = " << t1 << "\t(sec)" << std::endl;
433  std::cout << "Total Time for XYZVector = " << t2 << "\t(sec)" << std::endl;
434  std::cout << "Total Time for XYZPoint = " << t3 << "\t(sec)" << std::endl;
435 
436 }
static long int sum(long int i)
Definition: Factory.cxx:1786
Random number generator class based on M.
Definition: TRandom3.h:29
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:110
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom3.cxx:94
TRotation & RotateZ(Double_t)
Rotate around z.
Definition: TRotation.cxx:378
double T(double x)
Definition: ChebyshevPol.h:34
void GetComponents(IT begin, IT end) const
Get the 3 components into data specified by an iterator begin and another to the end of the desired d...
TRotation & RotateX(Double_t)
Rotate around x.
Definition: TRotation.cxx:346
Rotation class with the (3D) rotation represented by angles describing first a rotation of an angle p...
Definition: RotationZYX.h:71
TArc * a
Definition: textangle.C:12
STL namespace.
TLatex * t1
Definition: textangle.C:20
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
int main(int argc, const char *argv[])
Definition: stress3D.cxx:238
static double p2(double t, double a, double b, double c)
int getSum(const int *x, int n)
Definition: GoFTest.cxx:532
The TRotation class describes a rotation of objects of the TVector3 class.
Definition: TRotation.h:22
Class describing a generic displacement vector in 3 dimensions.
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
TPaveText * pt
TRandom2 r(17)
SVector< double, 2 > v
Definition: Dict.h:5
unsigned int r1[N_CITIES]
Definition: simanTSP.cxx:321
Rotation class with the (3D) rotation represented by a 3x3 orthogonal matrix.
Definition: Rotation3D.h:65
static double P[]
Basic 3D Transformation class describing a rotation and then a translation The internal data are a 3D...
Definition: Transform3D.h:85
REAL epsilon
Definition: triangle.c:617
Class describing a 3 dimensional translation.
Definition: Translation3D.h:57
#define T2
Definition: md5.inl:146
TRotation & RotateY(Double_t)
Rotate around y.
Definition: TRotation.cxx:362
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
DisplacementVector3D< Cartesian3D< double >, DefaultCoordinateSystemTag > XYZVector
3D Vector based on the cartesian coordinates x,y,z in double precision
Definition: Vector3Dfwd.h:34
#define T1
Definition: md5.inl:145
float * q
Definition: THbookFile.cxx:87
const Int_t n
Definition: legend1.C:16
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322
char name[80]
Definition: TGX11.cxx:109
virtual Double_t Exp(Double_t tau)
Returns an exponential deviate.
Definition: TRandom.cxx:212
Stopwatch class.
Definition: TStopwatch.h:30