1 package org.lcsim.detector.converter.compact;
2
3 import static org.lcsim.units.clhep.SystemOfUnits.cm;
4 import hep.physics.vec.BasicHep3Vector;
5 import hep.physics.vec.Hep3Vector;
6
7 import java.io.InputStream;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13
14 import org.lcsim.detector.IDetectorElement;
15 import org.lcsim.detector.IGeometryInfo;
16 import org.lcsim.detector.IPhysicalVolume;
17 import org.lcsim.detector.IPhysicalVolumeNavigator;
18 import org.lcsim.detector.PhysicalVolumeNavigatorStore;
19 import org.lcsim.detector.solids.Inside;
20 import org.lcsim.detector.solids.Tube;
21 import org.lcsim.geometry.Detector;
22 import org.lcsim.geometry.GeometryReader;
23
24 public class DetectorConverterTest
25 extends TestCase
26 {
27 private Detector detector;
28
29 public DetectorConverterTest(String name)
30 {
31 super(name);
32 }
33
34 public static junit.framework.Test suite()
35 {
36 return new TestSuite(DetectorConverterTest.class);
37 }
38
39 private static final String resource = "/org/lcsim/detector/converter/compact/DetectorConverterTest.xml";
40 public void setUp()
41 {
42 InputStream in =
43 this.getClass().
44 getResourceAsStream(resource);
45 GeometryReader reader = new GeometryReader();
46 try {
47 detector = reader.read(in);
48 }
49 catch ( Throwable x )
50 {
51 throw new RuntimeException(x);
52 }
53 }
54
55 public void testGeometry() throws Exception
56 {
57 IPhysicalVolumeNavigator nav =
58 PhysicalVolumeNavigatorStore.getInstance().getDefaultNavigator();
59 IPhysicalVolume world = nav.getTopPhysicalVolume();
60
61
62 assertTrue("Top volume is not the world!", world.getName().equals("world"));
63
64
65 assertTrue("World volume has a mother!", world.getMotherLogicalVolume() == null);
66
67 IDetectorElement deDetector = detector.getDetectorElement();
68
69 String des[] = new String[]{
70 "ecal_barrel",
71 "ecal_endcap",
72 "tracker"};
73
74
75 for ( String de : des )
76 {
77 IDetectorElement search = deDetector.getChildren().find(de).get(0);
78
79
80 assertTrue("The expected DetectorElement <" + de + "> is missing!", search != null);
81
82
83 List<DeDetector> detectorSearch = search.findAncestors(DeDetector.class);
84 assertTrue( detectorSearch.size() == 1);
85 assertTrue( detectorSearch.get(0).getName().equals("test_detector"));
86 }
87 }
88
89
90
91
92
93
94 public void testIsInside() throws Exception
95 {
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 List<Hep3Vector> points = new ArrayList<Hep3Vector>();
117
118
119 points.add(new BasicHep3Vector(0,0,255*cm));
120
121 points.add(new BasicHep3Vector(0,0,-255.0*cm));
122
123
124 IPhysicalVolumeNavigator nav =
125 PhysicalVolumeNavigatorStore.getInstance().getDefaultNavigator();
126
127
128
129
130
131 for ( IDetectorElement child : detector.getDetectorElement().getChildren() )
132 {
133 for ( IDetectorElement sensor : child.getChildren() )
134 {
135 IGeometryInfo sensorGeo = sensor.getGeometry();
136
137 Tube sensorTube = (Tube)sensorGeo.getLogicalVolume().getSolid();
138
139 double zsensor = sensorGeo.getPosition().z();
140 double zwidth = sensorTube.getZHalfLength();
141
142 for ( Hep3Vector point : points )
143 {
144 double zpoint = point.z();
145 double ypoint = point.y();
146
147
148 if ( sensor.getName().contains("barrel") )
149 {
150 if ( ypoint > sensorTube.getInnerRadius() &&
151 ypoint < sensorTube.getOuterRadius() )
152 {
153 assertEquals(sensorGeo.inside(point),Inside.INSIDE);
154 }
155 }
156
157 else if ( child.getName().contains("endcap_positive") )
158 {
159
160
161
162
163
164
165
166
167
168
169
170
171
172 if ( zpoint > (zsensor - zwidth ) &&
173 zpoint < (zsensor + zwidth ) )
174 {
175 assertEquals(sensorGeo.inside(point),Inside.INSIDE);
176 }
177 }
178
179 else if ( child.getName().contains("ecal_endcap_negative") )
180 {
181
182
183
184 if ( zpoint < (zsensor + zwidth ) &&
185 zpoint > (zsensor - zwidth ) )
186 {
187 sensorGeo.getDetectorElement();
188 sensorGeo.getDetectorElement().getParent();
189 sensorGeo.getDetectorElement().getParent().getName();
190
191
192
193 assertEquals(sensorGeo.inside(point),Inside.INSIDE);
194 }
195 }
196 }
197 }
198 }
199 }
200 }