1
2
3
4
5
6
7
8
9 package org.lcsim.math.moments;
10
11 import java.util.Random;
12 import junit.framework.TestCase;
13 import org.lcsim.spacegeom.CartesianPoint;
14 import org.lcsim.spacegeom.SpacePoint;
15
16 import static java.lang.Math.PI;
17 import static java.lang.Math.sin;
18 import static java.lang.Math.cos;
19
20
21
22
23
24 public class CentralMomentsCalculatorTest extends TestCase
25 {
26
27
28 public void testCentralMomentsCalculator()
29 {
30 double[] x = {0., 1., 2.};
31 double[] y = {0., 0., 0.};
32 double[] z = {0., 0., 0.};
33 double[] w = {1., 1., 1.};
34
35 CentralMomentsCalculator mc = new CentralMomentsCalculator();
36
37 mc.calculateMoments(x, y, z, w);
38
39 double[] c = mc.centroid();
40 assertEquals(c[0],1.);
41 assertEquals(c[1],0.);
42 assertEquals(c[2],0.);
43
44 w[2] = 2.;
45 mc.calculateMoments(x, y, z, w);
46 SpacePoint p = new CartesianPoint(mc.centroid());
47
48
49 int nPoints = 1000;
50 x = new double[nPoints];
51 y = new double[nPoints];
52 z = new double[nPoints];
53 w = new double[nPoints];
54
55
56
57
58
59
60 generateEvents(10., 10., 10., nPoints, x, y, z, w);
61 mc.calculateMoments(x, y, z, w);
62 p = new CartesianPoint(mc.centroid());
63
64 double[] inv = mc.invariants();
65
66
67
68
69
70
71
72
73
74
75 generateEvents(5., 17., 32., nPoints, x, y, z, w);
76 mc.calculateMoments(x, y, z, w);
77 p = new CartesianPoint(mc.centroid());
78
79 inv = mc.invariants();
80
81
82
83
84
85
86
87
88
89 generateEvents(5., 5., 32., nPoints, x, y, z, w);
90 mc.calculateMoments(x, y, z, w);
91 p = new CartesianPoint(mc.centroid());
92
93 inv = mc.invariants();
94
95
96
97
98
99
100
101
102
103 generateEvents(5., 20., 20., nPoints, x, y, z, w);
104 mc.calculateMoments(x, y, z, w);
105 p = new CartesianPoint(mc.centroid());
106
107 inv = mc.invariants();
108
109
110 }
111
112
113
114
115
116
117 void generateEvents(double a, double b, double c, int nPoints, double[] x, double[] y, double[] z, double[] w)
118 {
119 Random r = new Random();
120 for(int i=0; i<nPoints; ++i)
121 {
122 double t = PI*r.nextDouble();
123 double p = 2.*PI*r.nextDouble();
124 x[i] = a*cos(p)*sin(t);
125 y[i] = b*sin(p)*sin(t);
126 z[i] = c*cos(t);
127 w[i] = 1.;
128 }
129 }
130 }