View Javadoc

1   package org.lcsim.recon.tracking.magfield;
2   
3   import java.io.BufferedInputStream;
4   import java.io.BufferedReader;
5   import java.io.InputStream;
6   import java.io.InputStreamReader;
7   import java.util.StringTokenizer;
8   import junit.framework.TestCase;
9   import org.lcsim.recon.tracking.spacegeom.CartesianPoint;
10  import org.lcsim.recon.tracking.spacegeom.SpacePoint;
11  import org.lcsim.recon.tracking.spacegeom.SpacePointVector;
12  import static java.lang.Math.abs;
13  
14  /**
15   *
16   * @author Norman A Graf
17   *
18   * @version $Id:
19   */
20  public class Cartesian3DMagneticFieldMapTest extends TestCase
21  {
22  
23      private boolean debug = false;
24  
25      public void testCartesian3DMagneticFieldMap() throws Exception
26      {
27          InputStream is = this.getClass().getResourceAsStream("ThreeDFieldMap.dat");
28          double xOff = 0.0;
29          double yOff = 0.0;
30          double zOff = 0.0;
31          Cartesian3DMagneticFieldMap map = new Cartesian3DMagneticFieldMap(is, xOff, yOff, zOff);
32  
33          is.close();
34          // now read in field map again, and check that we get the correct values at the
35          // measured points
36          // does not test the interpolation per se but is a check.
37          BufferedReader myInput = new BufferedReader(new InputStreamReader(new BufferedInputStream(this.getClass().getResourceAsStream("ThreeDFieldMap.dat"))));
38          String thisLine;
39          int nx = 0;
40          int ny = 0;
41          int nz = 0;
42          //skip the first nine lines of metadata
43          for (int i = 0; i < 9; ++i) {
44              thisLine = myInput.readLine();
45              if (i == 1) // parse for number of grid point in x, y,x
46              {
47                  StringTokenizer st = new StringTokenizer(thisLine, " ");
48                  nx = Integer.parseInt(st.nextToken());
49                  ny = Integer.parseInt(st.nextToken());
50                  nz = Integer.parseInt(st.nextToken());
51              }
52          }
53          assertEquals(nx, 6);
54          assertEquals(ny, 6);
55          assertEquals(nz, 10);
56  
57          int nlines = nx * ny * nz;
58          // loop over the field points
59  
60          for (int i = 0; i < nlines; ++i) {
61              thisLine = myInput.readLine();
62              StringTokenizer st = new StringTokenizer(thisLine, " ");
63              double x = Double.parseDouble(st.nextToken());
64              double y = Double.parseDouble(st.nextToken());
65              double z = Double.parseDouble(st.nextToken());
66              double bx = Double.parseDouble(st.nextToken());
67              double by = Double.parseDouble(st.nextToken());
68              double bz = Double.parseDouble(st.nextToken());
69              SpacePointVector spv = map.field(new CartesianPoint(x+xOff, y+yOff, z+zOff));
70              assertEquals(bx, spv.v_x(), abs(.001 * bx));
71              assertEquals(by, spv.v_y(), abs(.001 * by));
72              assertEquals(bz, spv.v_z(), abs(.001 * bz));
73          }
74      }
75  }