1 package org.lcsim.geometry.segmentation;
2
3 import hep.physics.vec.BasicHep3Vector;
4 import hep.physics.vec.Hep3Vector;
5
6 import java.util.ArrayList;
7
8 import org.jdom.DataConversionException;
9 import org.jdom.Element;
10 import org.lcsim.detector.IDetectorElement;
11 import org.lcsim.detector.IDetectorElementContainer;
12 import org.lcsim.detector.identifier.IExpandedIdentifier;
13 import org.lcsim.detector.identifier.IIdentifier;
14 import org.lcsim.detector.identifier.IIdentifierHelper;
15 import org.lcsim.detector.identifier.Identifier;
16 import org.lcsim.detector.solids.Inside;
17 import org.lcsim.detector.solids.Trd;
18 import org.lcsim.geometry.util.IDDescriptor;
19
20
21
22
23
24
25 public class CartesianGridXZ extends AbstractCartesianGrid
26 {
27 private int xIndex = -1;
28 private int zIndex = -1;
29 private static final String[] fieldNames = {"x", "z"};
30
31 public CartesianGridXZ(Element node) throws DataConversionException
32 {
33 super(node);
34
35 if (node.getAttribute("gridSizeX") != null)
36 {
37 gridSizeX = node.getAttribute("gridSizeX").getDoubleValue();
38 cellSizes.add(0, gridSizeX);
39 }
40 else
41 {
42 throw new RuntimeException("Missing gridSizeX parameter.");
43 }
44
45 if (node.getAttribute("gridSizeZ") != null)
46 {
47 gridSizeZ = node.getAttribute("gridSizeZ").getDoubleValue();
48 cellSizes.add(1, gridSizeZ);
49 }
50 else
51 {
52 throw new RuntimeException("Missing gridSizeZ parameter.");
53 }
54 }
55
56 public String[] getSegmentationFieldNames() {
57 return fieldNames;
58 }
59
60 public long[] getNeighbourIDs(int layerRange, int xRange, int zRange)
61 {
62 return getNeighbourIDs(layerRange, xRange, zRange, xIndex, zIndex);
63 }
64
65 protected void setSegmentationValues(IExpandedIdentifier geomId, Hep3Vector localPositionVec)
66 {
67 geomId.setValue(xIndex, getXBin(localPositionVec.x()));
68 geomId.setValue(zIndex, getZBin(localPositionVec.z()));
69 }
70
71 public int getXBin(double x)
72 {
73 return getBin(x, gridSizeX);
74 }
75
76 public int getZBin(double z)
77 {
78 return getBin(z, gridSizeZ);
79 }
80
81 public boolean boundsCheck(long rawId)
82 {
83 IIdentifier geomId = makeGeometryIdentifier(rawId);
84 IIdentifierHelper helper = getSubdetector().getDetectorElement().getIdentifierHelper();
85 IIdentifier id = new Identifier(rawId);
86 int xVal = helper.getValue(id, xIndex);
87 int zVal = helper.getValue(id, zIndex);
88 IDetectorElementContainer deSrch = getSubdetector().getDetectorElement().findDetectorElement(geomId);
89 if (deSrch == null || deSrch.size() == 0)
90 {
91 return false;
92 }
93 IDetectorElement de = deSrch.get(0);
94 double xPos = computeCoordinate(xVal, gridSizeX);
95 double zPos = computeCoordinate(zVal, gridSizeZ);
96 if (de.getGeometry().getLogicalVolume().getSolid() instanceof Trd)
97 {
98 Trd sensorTrd = (Trd)de.getGeometry().getLogicalVolume().getSolid();
99
100 if (sensorTrd.inside(new BasicHep3Vector(xPos, 0, zPos)) == Inside.INSIDE)
101 {
102 return true;
103 }
104
105 else
106 {
107 return false;
108 }
109 }
110 else
111 {
112 throw new RuntimeException("Don't know how to bounds check solid " + de.getGeometry().getLogicalVolume().getSolid().getName() + ".");
113 }
114 }
115
116 protected void setupGeomFields(IDDescriptor id)
117 {
118 if (geomFields == null)
119 {
120 geomFields = new ArrayList<Integer>();
121
122 xIndex = id.indexOf("x");
123 zIndex = id.indexOf("z");
124 layerIndex = id.indexOf("layer");
125 try {
126 sliceIndex = id.indexOf("slice");
127 }
128 catch (IllegalArgumentException x)
129 {
130 System.err.println("WARNING: The slice field does not exist in this IDDecoder!");
131 sliceIndex = -1;
132 }
133
134
135
136 for (int i=0; i < id.fieldCount(); i++)
137 {
138 String fname = id.fieldName(i);
139 if (!fname.equals("x") && !fname.equals("z"))
140 {
141 geomFields.add(i);
142 }
143 }
144 }
145 }
146
147 private void computeLocalX()
148 {
149 localPosition[0] = (((double) getValue(xIndex)) + 0.5) * gridSizeX;
150 }
151
152 private void computeLocalZ()
153 {
154 localPosition[2] = (((double) getValue(zIndex)) + 0.5) * gridSizeZ;
155 }
156
157 protected void computeLocalPosition()
158 {
159 localPosition[0] = localPosition[1] = localPosition[2] = 0.0;
160 computeLocalX();
161 computeLocalZ();
162 }
163 }