View Javadoc

1   /*
2    * CentralMomentsCalculatorTest.java
3    *
4    * Created on April 5, 2006, 1:41 PM
5    *
6    * $Id: CentralMomentsCalculatorTest.java,v 1.1 2010/12/01 01:25:26 jeremy Exp $
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   * @author Norman Graf
23   */
24  public class CentralMomentsCalculatorTest extends TestCase
25  {
26      
27      /** Creates a new instance of CentralMomentsCalculatorTest */
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  //        System.out.println(p);
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          // try a sphere
56          // a = b = c = 10.
57  //        System.out.println("");
58  //        System.out.println("");
59  //        System.out.println("Sphere");
60          generateEvents(10., 10., 10., nPoints, x, y, z, w);
61          mc.calculateMoments(x, y, z, w);
62          p = new CartesianPoint(mc.centroid());
63  //        System.out.println(p);
64          double[] inv = mc.invariants();
65  //        System.out.println("inv: "+inv[0]+" "+inv[1]+" "+inv[2]);
66          
67          
68          // try an ellipsoid
69          // a = 5
70          // b = 17
71          // c = 32
72  //        System.out.println("");
73  //        System.out.println("");
74  //        System.out.println("Ellipsoid");
75          generateEvents(5., 17., 32., nPoints, x, y, z, w);
76          mc.calculateMoments(x, y, z, w);
77          p = new CartesianPoint(mc.centroid());
78  //        System.out.println(p);
79          inv = mc.invariants();
80  //        System.out.println("inv: "+inv[0]+" "+inv[1]+" "+inv[2]);
81          
82          // try a cigar
83          // a = 5
84          // b = 5
85          // c = 32
86  //        System.out.println("");
87  //        System.out.println("");
88  //        System.out.println("Cigar");
89          generateEvents(5., 5., 32., nPoints, x, y, z, w);
90          mc.calculateMoments(x, y, z, w);
91          p = new CartesianPoint(mc.centroid());
92  //        System.out.println(p);
93          inv = mc.invariants();
94  //        System.out.println("inv: "+inv[0]+" "+inv[1]+" "+inv[2]);        
95          
96          // try a plate
97          // a = 5
98          // b = 20
99          // c = 20
100 //        System.out.println("");
101 //        System.out.println("");
102 //        System.out.println("Plate");
103         generateEvents(5., 20., 20., nPoints, x, y, z, w);
104         mc.calculateMoments(x, y, z, w);
105         p = new CartesianPoint(mc.centroid());
106 //        System.out.println(p);
107         inv = mc.invariants();
108 //        System.out.println("inv: "+inv[0]+" "+inv[1]+" "+inv[2]);        
109         
110     }
111     
112     // generate nPoints events according to the ellipsoid equation:
113     // x = a cos(phi) sin(theta)
114     // y = b sin(phi) sin(theta)
115     // z = c cos(theta)
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 }