View Javadoc

1   /*
2    * CovarianceMatrixTransformerTest.java
3    *
4    * Created on March 30, 2006, 3:57 PM
5    *
6    * $Id: CovarianceMatrixTransformerTest.java,v 1.1 2010/12/01 01:25:26 jeremy Exp $
7    */
8   
9   package org.lcsim.math.coordinatetransform;
10  
11  import junit.framework.TestCase;
12  import static java.lang.Math.atan2;
13  import static java.lang.Math.sqrt;
14  import static java.lang.Math.PI;
15  import static java.lang.Math.cos;
16  import static java.lang.Math.sin;
17  /**
18   *
19   * @author Norman Graf
20   */
21  public class CovarianceMatrixTransformerTest extends TestCase
22  {
23      
24      /** Creates a new instance of CovarianceMatrixTransformerTest */
25      public  void testCovarianceMatrixTransformer()
26      {
27          double eps = 1E-8;
28          double x = 1.0;
29          double y = 0.0;
30          double sx = .1;
31          double sy = 0.;
32          double sxy = 0.;
33          
34          double[] cov = CovarianceMatrixTransformer.xy2rphi(x, y, sx*sx, sy*sy, sxy);
35  //        System.out.println("srr= "+cov[0]);
36  //        System.out.println("sphiphi= "+cov[1]);
37  //        System.out.println("srphi= "+cov[2]);
38          
39          assertEquals(cov[0], sx*sx, eps);
40          assertEquals(cov[1], sy*sy, eps);
41          assertEquals(cov[2], sxy, eps);
42          
43  //        System.out.println(" ");
44          sx = 0.;
45          sy = .1;
46          cov = CovarianceMatrixTransformer.xy2rphi(x, y, sx*sx, sy*sy, sxy);
47  //        System.out.println("srr= "+cov[0]);
48  //        System.out.println("sphiphi= "+cov[1]);
49  //        System.out.println("srphi= "+cov[2]);
50          
51          assertEquals(cov[0], sx*sx, eps);
52          assertEquals(cov[1], sy*sy, eps);
53          assertEquals(cov[2], sxy, eps);
54          
55  //        System.out.println(" ");
56          x = .5;
57          y = .5;
58          sx = .1;
59          sy = .1;
60          sxy = -.1;
61          cov = CovarianceMatrixTransformer.xy2rphi(x, y, sx*sx, sy*sy, sxy);
62  //        System.out.println("srr= "+cov[0]);
63  //        System.out.println("sphiphi= "+cov[1]);
64  //        System.out.println("srphi= "+cov[2]);
65          
66          // now let's round-trip...
67          double srr = cov[0];
68          double sphiphi = cov[1];
69          double srphi = cov[2];
70          double r = sqrt(x*x+y*y);
71          double phi = atan2(y,x);
72          
73          
74          cov = CovarianceMatrixTransformer.rphi2xy(r, phi, srr, sphiphi, srphi);
75  //        for(int i=0; i<3; ++i) System.out.println("cov["+i+"]= "+cov[i]);
76          
77          
78          assertEquals(sqrt(cov[0]), sx, eps);
79          assertEquals(sqrt(cov[1]), sy, eps);
80          assertEquals(cov[2], sxy, eps);
81          
82          
83  //        System.out.println(" ");
84          // start from rPhi...
85          r = 1.;
86          double dr = 0.;
87          phi= PI/4.;
88          double dPhi = .1;
89          srr = dr*dr;
90          sphiphi = dPhi*dPhi;
91          srphi = 0.;
92          
93          cov = CovarianceMatrixTransformer.rphi2xy(r, phi, srr, sphiphi, srphi);
94  //        for(int i=0; i<3; ++i) System.out.println("cov["+i+"]= "+cov[i]);
95          
96          
97          // dx should equal dy
98          assertEquals(cov[0], cov[1], eps);
99          // cov term should be neg
100         assertTrue(cov[2]<0);
101         // normalized cov term should be -1
102         assertEquals(cov[2]/cov[0], -1, eps);
103         
104         // now round-trip...
105         x = r*cos(phi);
106         y = r*sin(phi);
107         cov = CovarianceMatrixTransformer.xy2rphi(x, y, cov[0], cov[1], cov[2]);
108 //        for(int i=0; i<3; ++i) System.out.println("cov["+i+"]= "+cov[i]);
109         assertEquals(cov[0], srr, eps);
110         assertEquals(cov[1],sphiphi, eps);
111         assertEquals(cov[2], srphi, eps);
112     }
113 }