Author: [log in to unmask]
Date: Wed Mar 25 13:55:33 2015
New Revision: 3575
Log:
Add support for downloading and caching a fieldmap file from a URL.
Conversion to LCDD not yet supported.
Added:
projects/lcsim/trunk/detector-framework/src/test/resources/org/lcsim/geometry/field/FieldMap3DURLTest.xml (contents, props changed)
- copied, changed from r3567, projects/lcsim/trunk/detector-framework/src/test/resources/org/lcsim/geometry/field/FieldMap3DTest.xml
Modified:
projects/lcsim/trunk/detector-framework/src/main/java/org/lcsim/geometry/field/FieldMap3D.java (contents, props changed)
projects/lcsim/trunk/detector-framework/src/test/java/org/lcsim/geometry/field/FieldMap3DTest.java (contents, props changed)
Modified: projects/lcsim/trunk/detector-framework/src/main/java/org/lcsim/geometry/field/FieldMap3D.java
=============================================================================
--- projects/lcsim/trunk/detector-framework/src/main/java/org/lcsim/geometry/field/FieldMap3D.java (original)
+++ projects/lcsim/trunk/detector-framework/src/main/java/org/lcsim/geometry/field/FieldMap3D.java Wed Mar 25 13:55:33 2015
@@ -3,16 +3,19 @@
import hep.physics.vec.BasicHep3Vector;
import hep.physics.vec.Hep3Vector;
import java.io.BufferedReader;
+import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static java.lang.Math.sqrt;
+import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.jdom.Element;
import org.jdom.JDOMException;
+import org.lcsim.util.cache.FileCache;
/**
*
@@ -22,7 +25,6 @@
*/
public class FieldMap3D extends AbstractFieldMap
{
-
private double[][][] _xField;
private double[][][] _yField;
private double[][][] _zField;
@@ -40,119 +42,125 @@
private double _bMax;
private double[] _Bfield = new double[3];
String _filename;
-
+
public FieldMap3D(Element node) throws JDOMException
{
super(node);
_xOffset = node.getAttribute("xoffset").getDoubleValue();
_yOffset = node.getAttribute("yoffset").getDoubleValue();
_zOffset = node.getAttribute("zoffset").getDoubleValue();
-
- setup(node.getAttributeValue("filename"));
- }
-
- private void setup(String filename)
- {
+ _filename = node.getAttributeValue("filename");
+ System.out.println("Field Map location: " + _filename);
+ try {
+ setup(_filename);
+ } catch (Exception e) {
+ throw new RuntimeException("Error reading field map from " + _filename, e);
+ }
+ }
+
+ private void setup(String filename) throws IOException
+ {
+ InputStream fis;
+ BufferedReader br;
+ String line;
+
+ //FIXME Should specify either filename or url in the xml. Needs change to schema.
+ if (filename.startsWith("http")) {
+ FileCache cache = new FileCache();
+ File file = cache.getCachedFile(new URL(filename));
+ fis = new FileInputStream(file);
+ } else {
+ fis = new FileInputStream(filename);
+ }
+
System.out.println("-----------------------------------------------------------");
System.out.println("FieldMap3D ");
System.out.println("-----------------------------------------------------------");
-
System.out.println("Reading the field grid from " + filename + " ... ");
- InputStream fis;
- BufferedReader br;
- String line;
-
- try {
- fis = new FileInputStream(filename);
- br = new BufferedReader(new InputStreamReader(fis));
- // ignore the first blank line
+ br = new BufferedReader(new InputStreamReader(fis));
+ // ignore the first blank line
+ line = br.readLine();
+ // next line has table dimensions
+ line = br.readLine();
+ // read in the table dimensions of the file
+ StringTokenizer st = new StringTokenizer(line, " ");
+ _nx = Integer.parseInt(st.nextToken());
+ _ny = Integer.parseInt(st.nextToken());
+ _nz = Integer.parseInt(st.nextToken());
+
+ // Set up storage space for table
+ _xField = new double[_nx + 1][_ny + 1][_nz + 1];
+ _yField = new double[_nx + 1][_ny + 1][_nz + 1];
+ _zField = new double[_nx + 1][_ny + 1][_nz + 1];
+
+ // Ignore other header information
+ // The first line whose second character is '0' is considered to
+ // be the last line of the header.
+ do {
line = br.readLine();
- // next line has table dimensions
- line = br.readLine();
- // read in the table dimensions of the file
- StringTokenizer st = new StringTokenizer(line, " ");
- _nx = Integer.parseInt(st.nextToken());
- _ny = Integer.parseInt(st.nextToken());
- _nz = Integer.parseInt(st.nextToken());
-
-
- // Set up storage space for table
- _xField = new double[_nx + 1][_ny + 1][_nz + 1];
- _yField = new double[_nx + 1][_ny + 1][_nz + 1];
- _zField = new double[_nx + 1][_ny + 1][_nz + 1];
-
- // Ignore other header information
- // The first line whose second character is '0' is considered to
- // be the last line of the header.
- do {
- line = br.readLine();
- System.out.println(line);
- st = new StringTokenizer(line, " ");
- } while (!st.nextToken().trim().equals("0"));
-
- // now ready to read in the values in the table
- // format is:
- // x y z Bx By Bz
- //
- int ix, iy, iz;
- double xval = 0.;
- double yval = 0.;
- double zval = 0.;
- double bx, by, bz;
- for (ix = 0; ix < _nx; ix++) {
- for (iy = 0; iy < _ny; iy++) {
- for (iz = 0; iz < _nz; iz++) {
- line = br.readLine();
- st = new StringTokenizer(line, " ");
- xval = Double.parseDouble(st.nextToken());
- yval = Double.parseDouble(st.nextToken());
- zval = Double.parseDouble(st.nextToken());
- bx = Double.parseDouble(st.nextToken());
- by = Double.parseDouble(st.nextToken());
- bz = Double.parseDouble(st.nextToken());
- if (ix == 0 && iy == 0 && iz == 0) {
- _minx = xval;
- _miny = yval;
- _minz = zval;
- }
- _xField[ix][iy][iz] = bx;
- _yField[ix][iy][iz] = by;
- _zField[ix][iy][iz] = bz;
- double b = bx * bx + by * by + bz * bz;
- if (b > _bMax) {
- _bMax = b;
- }
+ System.out.println(line);
+ st = new StringTokenizer(line, " ");
+ } while (!st.nextToken().trim().equals("0"));
+
+ // now ready to read in the values in the table
+ // format is:
+ // x y z Bx By Bz
+ //
+ int ix, iy, iz;
+ double xval = 0.;
+ double yval = 0.;
+ double zval = 0.;
+ double bx, by, bz;
+ for (ix = 0; ix < _nx; ix++) {
+ for (iy = 0; iy < _ny; iy++) {
+ for (iz = 0; iz < _nz; iz++) {
+ line = br.readLine();
+ st = new StringTokenizer(line, " ");
+ xval = Double.parseDouble(st.nextToken());
+ yval = Double.parseDouble(st.nextToken());
+ zval = Double.parseDouble(st.nextToken());
+ bx = Double.parseDouble(st.nextToken());
+ by = Double.parseDouble(st.nextToken());
+ bz = Double.parseDouble(st.nextToken());
+ if (ix == 0 && iy == 0 && iz == 0) {
+ _minx = xval;
+ _miny = yval;
+ _minz = zval;
+ }
+ _xField[ix][iy][iz] = bx;
+ _yField[ix][iy][iz] = by;
+ _zField[ix][iy][iz] = bz;
+ double b = bx * bx + by * by + bz * bz;
+ if (b > _bMax) {
+ _bMax = b;
}
}
}
- _bMax = sqrt(_bMax);
-
- _maxx = xval;
- _maxy = yval;
- _maxz = zval;
-
- System.out.println("\n ---> ... done reading ");
- System.out.println(" ---> assumed the order: x, y, z, Bx, By, Bz "
- + "\n ---> Min values x,y,z: "
- + _minx + " " + _miny + " " + _minz
- + "\n ---> Max values x,y,z: "
- + _maxx + " " + _maxy + " " + _maxz
- + "\n Maximum Field strength: " + _bMax + " "
- + "\n ---> The field will be offset by " + _xOffset + " " + _yOffset + " " + _zOffset);
-
- _dx = _maxx - _minx;
- _dy = _maxy - _miny;
- _dz = _maxz - _minz;
- System.out.println("\n ---> Range of values x,y,z: "
- + _dx + " " + _dy + " " + _dz
- + "\n-----------------------------------------------------------");
-
- br.close();
- } catch (IOException x) {
- throw new RuntimeException(x);
- }
-
+ }
+ _bMax = sqrt(_bMax);
+
+ _maxx = xval;
+ _maxy = yval;
+ _maxz = zval;
+
+ System.out.println("\n ---> ... done reading ");
+ System.out.println(" ---> assumed the order: x, y, z, Bx, By, Bz "
+ + "\n ---> Min values x,y,z: "
+ + _minx + " " + _miny + " " + _minz
+ + "\n ---> Max values x,y,z: "
+ + _maxx + " " + _maxy + " " + _maxz
+ + "\n Maximum Field strength: " + _bMax + " "
+ + "\n ---> The field will be offset by " + _xOffset + " " + _yOffset + " " + _zOffset);
+
+ _dx = _maxx - _minx;
+ _dy = _maxy - _miny;
+ _dz = _maxz - _minz;
+ System.out.println("\n ---> Range of values x,y,z: "
+ + _dx + " " + _dy + " " + _dz
+ + "\n-----------------------------------------------------------");
+
+ br.close();
}
@Override
@@ -207,7 +215,6 @@
double zfraction = (z - _minz) / _dz;
//double xdindex, ydindex, zdindex;
-
// Position of the point within the cuboid defined by the
// nearest surrounding tabulated points
double[] xmodf = modf(xfraction * (_nx - 1));
@@ -216,7 +223,6 @@
// The indices of the nearest tabulated point whose coordinates
// are all less than those of the given point
-
int xindex = (int) xmodf[0];
int yindex = (int) ymodf[0];
int zindex = (int) zmodf[0];
@@ -224,8 +230,8 @@
double ylocal = ymodf[1];
double zlocal = zmodf[1];
// bilinear interpolation
- _Bfield[0] =
- _xField[xindex][yindex][zindex] * (1 - xlocal) * (1 - ylocal) * (1 - zlocal)
+ _Bfield[0]
+ = _xField[xindex][yindex][zindex] * (1 - xlocal) * (1 - ylocal) * (1 - zlocal)
+ _xField[xindex][yindex][zindex + 1] * (1 - xlocal) * (1 - ylocal) * zlocal
+ _xField[xindex][yindex + 1][zindex] * (1 - xlocal) * ylocal * (1 - zlocal)
+ _xField[xindex][yindex + 1][zindex + 1] * (1 - xlocal) * ylocal * zlocal
@@ -233,8 +239,8 @@
+ _xField[xindex + 1][yindex][zindex + 1] * xlocal * (1 - ylocal) * zlocal
+ _xField[xindex + 1][yindex + 1][zindex] * xlocal * ylocal * (1 - zlocal)
+ _xField[xindex + 1][yindex + 1][zindex + 1] * xlocal * ylocal * zlocal;
- _Bfield[1] =
- _yField[xindex][yindex][zindex] * (1 - xlocal) * (1 - ylocal) * (1 - zlocal)
+ _Bfield[1]
+ = _yField[xindex][yindex][zindex] * (1 - xlocal) * (1 - ylocal) * (1 - zlocal)
+ _yField[xindex][yindex][zindex + 1] * (1 - xlocal) * (1 - ylocal) * zlocal
+ _yField[xindex][yindex + 1][zindex] * (1 - xlocal) * ylocal * (1 - zlocal)
+ _yField[xindex][yindex + 1][zindex + 1] * (1 - xlocal) * ylocal * zlocal
@@ -242,8 +248,8 @@
+ _yField[xindex + 1][yindex][zindex + 1] * xlocal * (1 - ylocal) * zlocal
+ _yField[xindex + 1][yindex + 1][zindex] * xlocal * ylocal * (1 - zlocal)
+ _yField[xindex + 1][yindex + 1][zindex + 1] * xlocal * ylocal * zlocal;
- _Bfield[2] =
- _zField[xindex][yindex][zindex] * (1 - xlocal) * (1 - ylocal) * (1 - zlocal)
+ _Bfield[2]
+ = _zField[xindex][yindex][zindex] * (1 - xlocal) * (1 - ylocal) * (1 - zlocal)
+ _zField[xindex][yindex][zindex + 1] * (1 - xlocal) * (1 - ylocal) * zlocal
+ _zField[xindex][yindex + 1][zindex] * (1 - xlocal) * ylocal * (1 - zlocal)
+ _zField[xindex][yindex + 1][zindex + 1] * (1 - xlocal) * ylocal * zlocal
Modified: projects/lcsim/trunk/detector-framework/src/test/java/org/lcsim/geometry/field/FieldMap3DTest.java
=============================================================================
--- projects/lcsim/trunk/detector-framework/src/test/java/org/lcsim/geometry/field/FieldMap3DTest.java (original)
+++ projects/lcsim/trunk/detector-framework/src/test/java/org/lcsim/geometry/field/FieldMap3DTest.java Wed Mar 25 13:55:33 2015
@@ -24,7 +24,11 @@
public void testRead() throws Exception
{
- InputStream in = this.getClass().getResourceAsStream("/org/lcsim/geometry/field/FieldMap3DTest.xml");
+ String[] files = {"/org/lcsim/geometry/field/FieldMap3DTest.xml", "/org/lcsim/geometry/field/FieldMap3DURLTest.xml"};
+ for(int f=0; f<files.length; ++f)
+ {
+ System.out.println("testing "+files[f]);
+ InputStream in = this.getClass().getResourceAsStream(files[f]);
GeometryReader reader = new GeometryReader();
Detector det = reader.read(in);
FieldMap map = det.getFieldMap();
@@ -67,5 +71,6 @@
assertEquals(offsets[0], off[0]);
assertEquals(offsets[1], off[1]);
assertEquals(offsets[2], off[2]);
+ }
}
}
Copied: projects/lcsim/trunk/detector-framework/src/test/resources/org/lcsim/geometry/field/FieldMap3DURLTest.xml (from r3567, projects/lcsim/trunk/detector-framework/src/test/resources/org/lcsim/geometry/field/FieldMap3DTest.xml)
=============================================================================
--- projects/lcsim/trunk/detector-framework/src/test/resources/org/lcsim/geometry/field/FieldMap3DTest.xml (original)
+++ projects/lcsim/trunk/detector-framework/src/test/resources/org/lcsim/geometry/field/FieldMap3DURLTest.xml Wed Mar 25 13:55:33 2015
@@ -28,7 +28,7 @@
<fields>
<field type="FieldMap3D" name="FieldMap3DTest"
- filename="src/test/resources/org/lcsim/geometry/field/HPS_b18d36_unfolded.dat"
+ filename="http://www.lcsim.org/resources/hps/fieldmap/HPS_b18d36_unfolded.dat"
xoffset="2.117"
yoffset="0.0"
zoffset="45.72" />
########################################################################
Use REPLY-ALL to reply to list
To unsubscribe from the LCDET-SVN list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCDET-SVN&A=1
|