1 package org.lcsim.geometry.compact.converter.lcdd;
2
3 import org.jdom.Attribute;
4 import org.jdom.DataConversionException;
5 import org.jdom.Element;
6 import org.jdom.JDOMException;
7 import org.lcsim.geometry.compact.converter.lcdd.util.Box;
8 import org.lcsim.geometry.compact.converter.lcdd.util.Define;
9 import org.lcsim.geometry.compact.converter.lcdd.util.LCDD;
10 import org.lcsim.geometry.compact.converter.lcdd.util.Material;
11 import org.lcsim.geometry.compact.converter.lcdd.util.PhysVol;
12 import org.lcsim.geometry.compact.converter.lcdd.util.Position;
13 import org.lcsim.geometry.compact.converter.lcdd.util.Rotation;
14 import org.lcsim.geometry.compact.converter.lcdd.util.SensitiveDetector;
15 import org.lcsim.geometry.compact.converter.lcdd.util.Solids;
16 import org.lcsim.geometry.compact.converter.lcdd.util.Structure;
17 import org.lcsim.geometry.compact.converter.lcdd.util.Volume;
18 import org.lcsim.geometry.layer.LayerFromCompactCnv;
19
20
21
22
23
24 abstract class AbstractTestBeam extends LCDDSubdetector {
25
26 AbstractTestBeam(Element node) throws JDOMException {
27 super(node);
28 }
29
30 public void addToLCDD(LCDD lcdd, SensitiveDetector sens) throws JDOMException {
31 String detectorName = node.getAttributeValue("name");
32 int id = node.getAttribute("id").getIntValue();
33
34 Material air = lcdd.getMaterial("Air");
35 Solids solids = lcdd.getSolids();
36 Structure structure = lcdd.getStructure();
37 Volume motherVolume = lcdd.pickMotherVolume(this);
38 Define define = lcdd.getDefine();
39
40 Element dimensions = node.getChild("dimensions");
41 double xdim = dimensions.getAttribute("x").getDoubleValue();
42 double ydim = dimensions.getAttribute("y").getDoubleValue();
43 double zdim = LayerFromCompactCnv.computeDetectorTotalThickness(node);
44
45 Box envelopeBox = new Box(detectorName + "_box");
46 envelopeBox.setX(xdim);
47 envelopeBox.setY(ydim);
48 envelopeBox.setZ(zdim);
49 solids.addSolid(envelopeBox);
50 Volume envelopeVolume = new Volume(detectorName + "_envelope");
51 envelopeVolume.setSolid(envelopeBox);
52 envelopeVolume.setMaterial(air);
53
54
55 Position envelopePosition = createPosition(detectorName);
56 define.addPosition(envelopePosition);
57
58
59 Rotation envelopeRotation = createRotation(detectorName);
60 if (envelopeRotation != null) {
61 define.addRotation(envelopeRotation);
62 }
63
64 double layerZPos = -zdim / 2;
65
66 int layerCount = 0;
67 int layerTypeCount = 0;
68 for (Object lo : node.getChildren("layer")) {
69 Element layer = (Element) lo;
70
71 double layerX = xdim;
72 Attribute xattrib = layer.getAttribute("x");
73 if (xattrib != null) {
74 layerX = xattrib.getDoubleValue();
75 }
76
77 double layerY = ydim;
78 Attribute yattrib = layer.getAttribute("y");
79 if (yattrib != null) {
80 layerY = yattrib.getDoubleValue();
81 }
82
83 double layerZ = LayerFromCompactCnv.computeSingleLayerThickness(layer);
84
85 int repeat = 1;
86 Attribute repeatAttrib = layer.getAttribute("repeat");
87 if (repeatAttrib != null) {
88 repeat = repeatAttrib.getIntValue();
89 }
90
91 String layerVolumeName = detectorName + "_layerType" + layerTypeCount;
92 Box layerBox = new Box(layerVolumeName + "_box");
93 layerBox.setX(layerX);
94 layerBox.setY(layerY);
95 layerBox.setZ(layerZ);
96 solids.addSolid(layerBox);
97
98 Volume layerVolume = new Volume(layerVolumeName);
99 layerVolume.setMaterial(air);
100 layerVolume.setSolid(layerBox);
101
102 int sliceCount = 0;
103 double slicePosZ = -layerZ / 2;
104
105 for (Object so : layer.getChildren("slice")) {
106 Element slice = (Element) so;
107 double sliceX = layerX;
108 double sliceY = layerY;
109 xattrib = slice.getAttribute("x");
110 if (xattrib != null) {
111 sliceX = xattrib.getDoubleValue();
112 }
113
114 yattrib = slice.getAttribute("y");
115 if (yattrib != null) {
116 sliceY = yattrib.getDoubleValue();
117 }
118 double sliceZ = slice.getAttribute("thickness").getDoubleValue();
119
120 Attribute s = slice.getAttribute("sensitive");
121 boolean sensitive = s != null && s.getBooleanValue();
122
123 String sliceName = layerVolumeName + "_slice" + sliceCount;
124
125 Box sliceBox = new Box(sliceName + "_box");
126 sliceBox.setX(sliceX);
127 sliceBox.setY(sliceY);
128 sliceBox.setZ(sliceZ);
129 solids.addSolid(sliceBox);
130
131 Volume sliceVolume = new Volume(sliceName);
132 sliceVolume.setSolid(sliceBox);
133 Material sliceMaterial = lcdd.getMaterial(slice.getAttributeValue("material"));
134 sliceVolume.setMaterial(sliceMaterial);
135 if (sensitive)
136 sliceVolume.setSensitiveDetector(sens);
137
138
139
140
141
142 setRegion(lcdd, slice, sliceVolume);
143 setLimitSet(lcdd, slice, sliceVolume);
144 setVisAttributes(lcdd, node, sliceVolume);
145
146 structure.addVolume(sliceVolume);
147
148 PhysVol slicePhysVol = new PhysVol(sliceVolume);
149 slicePhysVol.addPhysVolID("slice", sliceCount);
150 Position slicePosition = new Position(sliceName + "_position");
151 slicePosZ += sliceZ / 2;
152
153 slicePosition.setZ(slicePosZ);
154 slicePosZ += sliceZ / 2;
155 define.addPosition(slicePosition);
156 slicePhysVol.setPosition(slicePosition);
157 layerVolume.addPhysVol(slicePhysVol);
158
159 ++sliceCount;
160 }
161
162 setVisAttributes(lcdd, node, layerVolume);
163 structure.addVolume(layerVolume);
164
165 for (int i = 0; i < repeat; i++) {
166 String layerPhysVolName = detectorName + "_layer" + layerCount;
167
168 PhysVol layerPhysVol = new PhysVol(layerVolume);
169 layerPhysVol.addPhysVolID("layer", layerCount);
170
171 layerZPos += layerZ / 2;
172 Position layerPosition = new Position(layerPhysVolName + "_position");
173 define.addPosition(layerPosition);
174 layerPosition.setZ(layerZPos);
175 layerPhysVol.setPosition(layerPosition);
176 layerZPos += layerZ / 2;
177
178 envelopeVolume.addPhysVol(layerPhysVol);
179
180 ++layerCount;
181 }
182 ++layerTypeCount;
183 }
184 setVisAttributes(lcdd, node, envelopeVolume);
185 structure.addVolume(envelopeVolume);
186 PhysVol envelopePhysVol = new PhysVol(envelopeVolume);
187 envelopePhysVol.addPhysVolID("system", id);
188 envelopePhysVol.setPosition(envelopePosition);
189 if (envelopeRotation != null)
190 envelopePhysVol.setRotation(envelopeRotation);
191 motherVolume.addPhysVol(envelopePhysVol);
192 }
193
194 private Position createPosition(String detectorName) throws DataConversionException {
195 double xpos = 0;
196 double ypos = 0;
197 double zpos = 0;
198
199 Element positionElement = node.getChild("position");
200
201 if (positionElement != null) {
202 Attribute posAttribute = positionElement.getAttribute("x");
203 if (posAttribute != null) {
204 xpos = posAttribute.getDoubleValue();
205 }
206
207 posAttribute = positionElement.getAttribute("y");
208 if (posAttribute != null) {
209 ypos = posAttribute.getDoubleValue();
210 }
211
212 posAttribute = positionElement.getAttribute("z");
213 if (posAttribute != null) {
214 zpos = posAttribute.getDoubleValue();
215 }
216 }
217 Position envelopePosition = new Position(detectorName + "_position");
218 envelopePosition.setX(xpos);
219 envelopePosition.setY(ypos);
220 envelopePosition.setZ(zpos);
221 return envelopePosition;
222 }
223
224 private Rotation createRotation(String detectorName) throws DataConversionException {
225 double rx, ry, rz;
226 rx = ry = rz = 0;
227 Element rotationElement = node.getChild("rotation");
228 Rotation envelopeRotation = null;
229 if (rotationElement != null) {
230 Attribute rotationAttribute = rotationElement.getAttribute("x");
231 if (rotationAttribute != null) {
232 rx = rotationAttribute.getDoubleValue();
233 }
234 rotationAttribute = rotationElement.getAttribute("y");
235 if (rotationAttribute != null) {
236 ry = rotationAttribute.getDoubleValue();
237 }
238 rotationAttribute = rotationElement.getAttribute("z");
239 if (rotationAttribute != null) {
240 rz = rotationAttribute.getDoubleValue();
241 }
242 envelopeRotation = new Rotation(detectorName + "_rotation");
243 envelopeRotation.setX(rx);
244 envelopeRotation.setY(ry);
245 envelopeRotation.setZ(rz);
246 }
247 return envelopeRotation;
248 }
249 }