View Javadoc

1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
5    */
6   package org.lcsim.recon.tracking.magfield;
7   
8   import java.io.BufferedInputStream;
9   import java.io.BufferedReader;
10  import java.io.InputStream;
11  import java.io.InputStreamReader;
12  import java.util.ArrayList;
13  import java.util.List;
14  import java.util.StringTokenizer;
15  import junit.framework.TestCase;
16  import org.lcsim.recon.tracking.spacegeom.CartesianPoint;
17  import org.lcsim.recon.tracking.spacegeom.SpacePoint;
18  import org.lcsim.recon.tracking.spacegeom.SpacePointVector;
19  
20  /**
21   *
22   * @author ngraf
23   */
24  public class SplineFit1DMagneticFieldMapTest extends TestCase
25  {
26  
27      private boolean debug = false;
28  
29      /**
30       * Test of field method, of class SplineFit1DMagneticFieldMap.
31       */
32      public void testSplineFit1DMagneticFieldMap()
33      {
34          List<Double> pos = new ArrayList<Double>();
35          List<Double> bval = new ArrayList<Double>();
36  
37          InputStream is = this.getClass().getResourceAsStream("HPS_b18d36_By_0_0_z.dat");
38          try {
39              BufferedReader myInput = new BufferedReader(new InputStreamReader(new BufferedInputStream(is)));
40  
41              String thisLine;
42  
43              while ((thisLine = myInput.readLine()) != null) {
44                  //System.out.println(thisLine);
45                  StringTokenizer st = new StringTokenizer(thisLine, " ");
46                  pos.add(Double.parseDouble(st.nextToken()));
47                  bval.add(Double.parseDouble(st.nextToken()));
48              }
49              myInput.close();
50          } catch (Exception e) {
51              e.printStackTrace();
52          }
53          int size = pos.size();
54          double[] z = new double[size];
55          double[] By = new double[size];
56          for (int i = 0; i < size; ++i) {
57              z[i] = pos.get(i);
58              By[i] = bval.get(i);
59          }
60          double scale = 0.5;
61          SplineFit1DMagneticFieldMap map = new SplineFit1DMagneticFieldMap(SplineFit1DMagneticFieldMap.COORDINATE.Z, z, SplineFit1DMagneticFieldMap.BVAL.BY, By, scale);
62          if (debug) {
63              System.out.println(map);
64          }
65          for (int i = 0; i < size; ++i) {
66              SpacePoint p = new CartesianPoint(0., 0., z[i]);
67              SpacePointVector result = map.field(p);
68              if (debug) {
69                  System.out.println("File z: " + z[i] + " By: " + By[i] + " map By: " + result.v_y());
70              }
71              assertEquals(scale*By[i], result.v_y());
72          }
73          if (debug) {
74              double min = z[0];
75              double max = z[size - 1];
76              double du = (max - min) / (10. * (size - 1));
77              System.out.println("min: " + min + " max: " + max + " size: " + size + " du: " + du);
78              for (double u = min; u <= max; u += du) {
79                  SpacePoint p = new CartesianPoint(0., 0., u);
80                  SpacePointVector result = map.field(p);
81                  System.out.println(u + " " + result.v_y());
82              }
83          }
84      }
85  }