GeomConverter/src/org/lcsim/geometry/compact/converter/lcdd
diff -N CartesianGridXZ.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CartesianGridXZ.java 21 Sep 2009 21:13:51 -0000 1.1
@@ -0,0 +1,48 @@
+package org.lcsim.geometry.compact.converter.lcdd;
+
+import org.jdom.DataConversionException;
+import org.jdom.Element;
+import org.jdom.Attribute;
+import org.jdom.JDOMException;
+import org.lcsim.geometry.compact.converter.lcdd.util.Calorimeter;
+
+/**
+ *
+ * LCDD binding for CartesianGridXZ segmentation.
+ *
+ * @author jeremym
+ */
+public class CartesianGridXZ extends LCDDSegmentation
+{
+ private double gridSizeX;
+ private double gridSizeZ;
+
+ CartesianGridXZ(Element node) throws DataConversionException, JDOMException
+ {
+ super(node);
+
+ Attribute attrib = node.getAttribute("gridSizeX");
+ if ( attrib == null )
+ {
+ throw new JDOMException("Required attribute gridSizePhi was not found.");
+ }
+ gridSizeX = attrib.getDoubleValue();
+
+ attrib = node.getAttribute("gridSizeZ");
+
+ if ( attrib == null )
+ {
+ throw new JDOMException("Required attribute gridSizeZ was not found.");
+ }
+ gridSizeZ = attrib.getDoubleValue();
+ }
+
+ void setSegmentation(Calorimeter cal)
+ {
+ org.lcsim.geometry.compact.converter.lcdd.util.GridXYZ seg =
+ new org.lcsim.geometry.compact.converter.lcdd.util.GridXYZ();
+ seg.setGridSizeX(gridSizeX);
+ seg.setGridSizeZ(gridSizeZ);
+ cal.setSegmentation(seg);
+ }
+}
\ No newline at end of file
GeomConverter/src/org/lcsim/geometry/segmentation
diff -N CartesianGridXZ.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CartesianGridXZ.java 21 Sep 2009 21:13:51 -0000 1.1
@@ -0,0 +1,353 @@
+package org.lcsim.geometry.segmentation;
+
+import hep.physics.vec.BasicHep3Vector;
+import hep.physics.vec.Hep3Vector;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jdom.DataConversionException;
+import org.jdom.Element;
+import org.lcsim.detector.IDetectorElement;
+import org.lcsim.detector.IDetectorElementContainer;
+import org.lcsim.detector.identifier.ExpandedIdentifier;
+import org.lcsim.detector.identifier.IExpandedIdentifier;
+import org.lcsim.detector.identifier.IIdentifier;
+import org.lcsim.detector.identifier.IIdentifierHelper;
+import org.lcsim.detector.identifier.Identifier;
+import org.lcsim.detector.solids.Box;
+import org.lcsim.detector.solids.Inside;
+import org.lcsim.detector.solids.Trd;
+import org.lcsim.geometry.util.IDDescriptor;
+
+/**
+ * Simplified XY grid based on GridXYZ segmentation.
+ *
+ * @author jeremym
+ */
+public class CartesianGridXZ extends SegmentationBase
+{
+ private double gridSizeX = 0;
+ private double gridSizeZ = 0;
+
+ private int xIndex = -1;
+ private int zIndex = -1;
+ private int layerIndex = -1;
+
+ private double[] localPosition =
+ { 0, 0, 0 };
+
+ private double[] globalPosition =
+ { 0, 0, 0 };
+
+ private boolean needsCompute = true;
+
+ List<Integer> geomFields;
+
+ public CartesianGridXZ(Element node) throws DataConversionException
+ {
+ super(node);
+
+ if (node.getAttribute("gridSizeX") != null)
+ {
+ gridSizeX = node.getAttribute("gridSizeX").getDoubleValue();
+ }
+ else
+ {
+ throw new RuntimeException("Missing gridSizeX parameter.");
+ }
+
+ if (node.getAttribute("gridSizeZ") != null)
+ {
+ gridSizeZ = node.getAttribute("gridSizeZ").getDoubleValue();
+ }
+ else
+ {
+ throw new RuntimeException("Missing gridSizeZ parameter.");
+ }
+ }
+
+ public boolean supportsNeighbours()
+ {
+ return true;
+ }
+
+ // TODO: Bounds check on x and y indices.
+ // TODO: Put into util or superclass.
+ public long[] getNeighbourIDs(int layerRange, int xRange, int zRange)
+ {
+ // Get the IdentifierHelper.
+ IIdentifierHelper helper = detector.getDetectorElement().getIdentifierHelper();
+
+ // Get number of layers.
+ int nlayers = this.getNumberOfLayers();
+
+ // Current packed id.
+ IIdentifier currId = new Identifier(this.getDecoder().getID());
+
+ // Set values for current id.
+ int currLayer = helper.getValue(currId, layerIndex);
+ int currX = helper.getValue(currId, xIndex);
+ int currZ = helper.getValue(currId, zIndex);
+
+ // Create an ExpandedIdentifier for the current id.
+ IExpandedIdentifier thisId = helper.unpack(currId);
+
+ // Create return array.
+ List<Long> neighbors = new ArrayList<Long>();
+
+ // Loop over layer range.
+ for (int ilayer=-layerRange; ilayer<=layerRange; ilayer++)
+ {
+ // Loop over X range.
+ for (int ix=-xRange; ix<=xRange; ix++)
+ {
+ // Loop over Y range.
+ for (int iz=-zRange; iz<=zRange; iz++)
+ {
+ // Compute layer value.
+ int neighborLayer = currLayer + ilayer;
+
+ if (neighborLayer >= 0 && neighborLayer < nlayers)
+ {
+ // Compute x value.
+ int neighborX = currX + ix;
+
+ // Compute y value.
+ int neighborY = currZ + iz;
+
+ // Create a new ExpandedIdenfier from the base Id.
+ ExpandedIdentifier neighborId = new ExpandedIdentifier(thisId);
+
+ // Set the neighbor fields.
+ neighborId.setValue(layerIndex, neighborLayer);
+ neighborId.setValue(xIndex, neighborX);
+ neighborId.setValue(zIndex, neighborY);
+
+ // Add the neighbor id to the return array.
+ neighbors.add(helper.pack(neighborId).getValue());
+ }
+ }
+ }
+ }
+
+ long result[] = new long[neighbors.size()];
+ int i = 0;
+ for (Long id : neighbors)
+ {
+ result[i] = id;
+ i++;
+ }
+ return result;
+ }
+
+ // TODO: Put into util or superclass.
+ public long findCellContainingXYZ(double x, double y, double z)
+ {
+ Hep3Vector pos = new BasicHep3Vector(x,y,z);
+ IDetectorElement de =
+ getSubdetector().getDetectorElement().findDetectorElement(pos);
+ if (!de.getGeometry().getPhysicalVolume().isSensitive())
+ {
+ throw new RuntimeException("The volume " + de.getName() + " is not sensitive.");
+ }
+ Hep3Vector localPosition = de.getGeometry().transformGlobalToLocal(pos);
+ ExpandedIdentifier geomId = new ExpandedIdentifier(de.getExpandedIdentifier());
+ geomId.setValue(xIndex, getXBin(localPosition.x()));
+ geomId.setValue(zIndex, getZBin(localPosition.y()));
+ return getSubdetector().getDetectorElement().getIdentifierHelper().pack(geomId).getValue();
+ }
+
+ // FIXME: Copied from GridXYZ.
+ public int getBin(double u, double gridSizeU)
+ {
+ double u0 = gridSizeU / 2;
+ int iu = (int) Math.floor((u - u0) / gridSizeU + 0.5);
+ return iu;
+ }
+
+ // FIXME: Copied from GridXYZ.
+ public int getXBin(double x)
+ {
+ return getBin(x, gridSizeX);
+ }
+
+ // FIXME: Copied from GridXYZ.
+ public int getZBin(double z)
+ {
+ return getBin(z, gridSizeZ);
+ }
+
+ public double computePosition(int binValue, double gridSize)
+ {
+ return (((double) binValue) + 0.5) * gridSize;
+ }
+
+ public boolean boundsCheck(long rawId)
+ {
+ IIdentifier geomId = makeGeometryIdentifier(rawId);
+ IIdentifierHelper helper = getSubdetector().getDetectorElement().getIdentifierHelper();
+ IIdentifier id = new Identifier(rawId);
+ int xVal = helper.getValue(id, xIndex);
+ int zVal = helper.getValue(id, zIndex);
+ IDetectorElementContainer deSrch = getSubdetector().getDetectorElement().findDetectorElement(geomId);
+ if (deSrch == null || deSrch.size() == 0)
+ {
+ return false;
+ }
+ IDetectorElement de = deSrch.get(0);
+ double xPos = computePosition(xVal, gridSizeX);
+ double zPos = computePosition(zVal, gridSizeZ);
+ if (de.getGeometry().getLogicalVolume().getSolid() instanceof Box)
+ {
+ Box sensorBox = (Box)de.getGeometry().getLogicalVolume().getSolid();
+ // Check coordinate values against box bounds.
+ if (sensorBox.inside(new BasicHep3Vector(xPos, 0, zPos)) == Inside.INSIDE)
+ {
+ return true;
+ }
+ // TODO: Handle edge case with partial cells. (How???)
+ else
+ {
+ return false;
+ }
+ }
+ // TODO: Implement Trapezoid bounds check here.
+ else if (de.getGeometry().getLogicalVolume().getSolid() instanceof Trd)
+ {
+ Trd sensorTrd = (Trd)de.getGeometry().getLogicalVolume().getSolid();
+ // Check coordinate values against trd bounds.
+ if (sensorTrd.inside(new BasicHep3Vector(xPos, 0, zPos)) == Inside.INSIDE)
+ {
+ return true;
+ }
+ // TODO: Handle edge case with partial cells. (How???)
+ else
+ {
+ return false;
+ }
+ }
+ else
+ {
+ throw new RuntimeException("Don't know how to bounds check solid " + de.getGeometry().getLogicalVolume().getSolid().getName() + ".");
+ }
+ }
+
+ public double[] getPosition()
+ {
+ if (needsCompute)
+ {
+ computePosition();
+ needsCompute = false;
+ }
+ return globalPosition;
+ }
+
+ private void computeGlobalPosition()
+ {
+ // Make an id only containing geometric fields and no segmentation fields.
+ IExpandedIdentifier geomIdExp = detector.getDetectorElement().getIdentifierHelper().unpack(new Identifier(this.getDecoder().getID()), geomFields);
+ IIdentifier geomId = detector.getDetectorElement().getIdentifierHelper().pack(geomIdExp);
+
+ // Search for the the DetectorElement associated with the geometry id.
+ List<IDetectorElement> deSearch = detector.getDetectorElement().findDetectorElement(geomId);
+
+ // Check if the lookup failed.
+ if (deSearch == null || deSearch.size() == 0)
+ {
+ throw new RuntimeException("Failed to find DetectorElement with geometry id <" + geomIdExp.toString() + "> !");
+ }
+
+ // Set the DetectorElement to use for local to global transform.
+ IDetectorElement sensor = deSearch.get(0);
+
+ //System.out.println(sensor.getName() + " - " + sensor.getGeometry().getPosition());
+
+ // Create a vector of the local position.
+ Hep3Vector posVecLocal = new BasicHep3Vector(localPosition[0], localPosition[1], localPosition[2]);
+ //System.out.println("posVecLocal: " + posVecLocal.toString());
+
+ // Compute the global position of the hit using the DetectorElement.
+ Hep3Vector posVecGlobal = sensor.getGeometry().transformLocalToGlobal(posVecLocal);
+
+ // Set the internal global position array.
+ globalPosition[0] = posVecGlobal.x();
+ globalPosition[1] = posVecGlobal.y();
+ globalPosition[2] = posVecGlobal.z();
+ }
+
+ public void setID(long id)
+ {
+ super.setID(id);
+ needsCompute = true;
+ }
+
+ public void setIDDescription(IDDescriptor id)
+ {
+ super.setIDDescription(id);
+
+ if (geomFields == null)
+ {
+ geomFields = new ArrayList<Integer>();
+
+ xIndex = id.indexOf("x");
+ zIndex = id.indexOf("z");
+ layerIndex = id.indexOf("layer");
+
+ // Set geometry field list.
+ for (int i=0; i < id.fieldCount(); i++)
+ {
+ String fname = id.fieldName(i);
+ if (!fname.equals("x") && !fname.equals("z"))
+ {
+ geomFields.add(i);
+ }
+ }
+ }
+ }
+
+ public double getX()
+ {
+ return getPosition()[0];
+ }
+
+ public double getY()
+ {
+ return getPosition()[1];
+ }
+
+ public double getZ()
+ {
+ return getPosition()[2];
+ }
+
+ private void computeLocalX()
+ {
+ localPosition[0] = (((double) getValue(xIndex)) + 0.5) * gridSizeX;
+ }
+
+ private void computeLocalZ()
+ {
+ localPosition[2] = (((double) getValue(zIndex)) + 0.5) * gridSizeZ;
+ }
+
+ private void computePosition()
+ {
+ computeLocalPosition();
+ computeGlobalPosition();
+ }
+
+ private void computeLocalPosition()
+ {
+ localPosition[0] = localPosition[1] = localPosition[2] = 0.0;
+ computeLocalX();
+ computeLocalZ();
+ }
+
+ private IIdentifier makeGeometryIdentifier(long id)
+ {
+ IExpandedIdentifier geomIdExp = detector.getDetectorElement().getIdentifierHelper().unpack(new Identifier(id), geomFields);
+ return detector.getDetectorElement().getIdentifierHelper().pack(geomIdExp);
+ }
+
+}
\ No newline at end of file