View Javadoc

1   package org.lcsim.detector;
2   
3   import static org.lcsim.units.clhep.SystemOfUnits.m;
4   import hep.physics.vec.BasicHep3Vector;
5   import hep.physics.vec.Hep3Vector;
6   import junit.framework.TestCase;
7   import junit.framework.TestSuite;
8   
9   import org.lcsim.detector.material.IMaterial;
10  import org.lcsim.detector.material.MaterialElement;
11  import org.lcsim.detector.solids.Box;
12  import org.lcsim.detector.solids.Inside;
13  import org.lcsim.detector.solids.Tube;
14  
15  public class SimpleDetectorTest extends TestCase
16  {		
17  	private IPhysicalVolume world = null;
18  	private static IMaterial dummymat = new MaterialElement("dummymat",1,1,1.0);
19  	
20      public SimpleDetectorTest(String name)
21      {
22          super(name);
23      }
24      
25      public static junit.framework.Test suite()
26      {
27          return new TestSuite(SimpleDetectorTest.class);
28      }
29      
30      protected void setUp() throws Exception
31      {
32      	world = createTestGeometry();
33      }
34      
35      public void testBasicAccess()
36      {
37      	//System.out.println("pv world = " + world.getName());
38      	ILogicalVolume lvWorld = world.getLogicalVolume();
39      	//System.out.println("lv world = " + lvWorld.getName());
40      	IPhysicalVolume pvDau = lvWorld.getDaughter(0);    	
41      	//System.out.println("pv dau = " + pvDau.getName());
42      	ILogicalVolume lvDau = pvDau.getLogicalVolume();
43      	//System.out.println("lv dau = " + lvDau.getName());
44      	Box boxDau = (Box)lvDau.getSolid();
45      	//System.out.println("box dau = " + boxDau.getName() + "; halfx halfy halfz = " + boxDau.getXHalfLength() + " " + boxDau.getYHalfLength() + " " + boxDau.getZHalfLength());
46      	IMaterial material = lvDau.getMaterial();
47      	//System.out.println("mat dau = " + material.getName());
48      }
49  
50      static double [] xpoints = {10.0,10.1,9.9,5.1,4.9,2.6,2.4,1.1,.9,.6,.4,.2,.1,0};
51  	private String[] testnames = {"/box1","box1","box1/","/box1"};
52      
53      /*
54      public void testIsInside()
55      {
56          IPhysicalVolumeNavigator nav = PhysicalVolumeNavigatorStore.getInstance().createDefault(world);
57          
58          List<Hep3Vector> testpoints = new ArrayList<Hep3Vector>();
59          testpoints.add(new BasicHep3Vector(0,0,0));
60          testpoints.add(new BasicHep3Vector(50,0,0));
61          testpoints.add(new BasicHep3Vector(0,50,0));
62          
63          for ( Hep3Vector point : testpoints )
64          {
65              IPhysicalVolumePath path = nav.getPath(point);
66          }        
67      }*/
68      
69  	public void testNavigator()
70      {
71      	IPhysicalVolumeNavigator nav = 
72              PhysicalVolumeNavigatorStore.getInstance().createDefault(world); 	
73      	
74      	// The string "/" should give back a reference
75      	// to the top volume, which is encoded by a
76      	// single "/".
77      	assertTrue("/".equals(nav.getPath("/").toString()));
78      	
79      	// The navigator should normalize all the test names to "/box1".
80      	for (String testname : testnames)
81      	{    	
82      		IPhysicalVolumePath path = nav.getPath(testname);
83      		    		
84      		assertEquals(path.size(),2);
85              
86      		assertTrue("/box1".equals(path.toString()                ));
87      		assertTrue( "box1".equals(path.getLeafVolume().getName() ));
88          	assertTrue("world".equals(path.getTopVolume().getName()  ));
89      	}
90      	    	    	    
91      	IPhysicalVolumePath path = nav.getPath("/box1");
92      	    	    	      
93          // Check isInside for positive points on "box1". 
94      	for (double x : xpoints)
95      	{
96      		path = nav.getPath(new BasicHep3Vector(x,0,0));
97      		
98      		if (x<5.0)
99      		{
100     			assertTrue("/box1".equals(path.toString()));
101     		}
102     		else {
103     			assertTrue("/".equals(path.toString()));
104     		}
105     	}
106     	
107         // Check isInside for negative points on "box1".
108     	for (double x : xpoints)
109     	{
110     		IPhysicalVolumePath path3 = nav.getPath(new BasicHep3Vector(-x,0,0));
111     		    		
112     		if (-x > -5.0)
113     		{
114     			assertTrue("/box1".equals(path3.toString()));
115     		}
116     		else {
117     			assertTrue("/".equals(path3.toString()));
118     		}    		    	
119     	}
120     	
121         // Check isInside for some positive x points on "box2".
122     	for (double x : new double[] {44.9,45.1,47.9,48.1,51.9,52.1,49.9,54.9,55.1})
123     	{
124     		IPhysicalVolumePath path4 = nav.getPath(new BasicHep3Vector(x,0,0));    	
125     		if (x>45 && x<55)
126     		{
127     			assertTrue("/box2".equals(path4.toString()));
128     		}
129     	}
130     	    	
131     	// Create a dummy DE that has the "box1" volume as its node.
132     	IDetectorElement dummyDE = new DummyDE(nav.getPath("/box1"));
133     	IGeometryInfo gi = dummyDE.getGeometry();
134     	assertTrue("/box1".equals(gi.getPath().toString()));
135     	
136     	// Check isInside for positive points on the "box1" DE.
137     	for (double x : xpoints)
138     	{
139     		Hep3Vector thisx = new BasicHep3Vector(x,0,0);
140     		Inside inside = gi.inside(thisx);
141     		    		    	
142     		if (x<5.0)
143     		{
144     			assertEquals(inside,Inside.INSIDE);
145     		}    		
146     	}
147     	
148         // Check isInside for various geometry objects.
149     	for (double y : new double[] {44.9,45.1,47.9,48.1,51.9,52.1,49.9,54.9,55.1})
150     	{
151     		Hep3Vector point = new BasicHep3Vector(0,y,0);
152     		IPhysicalVolumePath path5 = nav.getPath(point);
153     		
154     		if ( y < 45.0 || y > 55.0)
155     		{
156     			assertTrue(path5.size()==1);
157     			assertTrue("/".equals(path5.toString()));
158     		}
159     		
160     		if ((y > 45.0 && y < 48.0) || (y > 52.0 && y < 55.0))
161     		{
162     			assertTrue("/box3".equals(path5.toString()));
163     		}
164     		else if (y > 48.0 && y < 52.0)
165     		{
166     			assertTrue("/box3/box4".equals(path5.toString()));
167     		}    		    	
168     	}
169     	
170     	IPhysicalVolumePath path6 = nav.getPath(new BasicHep3Vector(101,0,0));
171     	assertTrue("/tube1".equals(path6.toString()));
172     	path6 = nav.getPath("/tube1");
173     	assertTrue("/tube1".equals(path6.toString()));
174     	path6 = nav.getPath(new BasicHep3Vector(111.0,0,0));
175     	assertTrue("/tube1/tube2".equals(path6.toString()));
176     }
177 	
178 	public class VisitorTest 
179 	implements IPhysicalVolumeVisitor
180 	{
181 		public void visit(IPhysicalVolume volume)
182 		{
183 			System.out.println("visiting node " + volume.getName());
184 		}
185         
186         public boolean isDone()
187         {
188             return false;
189         }
190 	}
191 	
192     /*
193 	public void testTraverse()
194 	{
195 		IPhysicalVolumeNavigator nav = new PhysicalVolumeNavigator("nav2", world); 	
196 		VisitorTest visitorTest = new VisitorTest();
197 		
198 		System.out.println("---PreOrder Traversal---");
199 		nav.traversePreOrder(visitorTest);
200 		System.out.println('\n');
201 		
202 		System.out.println("---PostOrder Traversal---");
203 		nav.traversePostOrder(visitorTest);
204 	}*/
205     
206 	public IPhysicalVolume createTestGeometry()
207 	{
208 		IPhysicalVolume world = createWorld();
209 		createTestSolids(world);
210 		return world;
211 	}
212 	
213 	public final void createTestSolids(IPhysicalVolume mom)
214 	{
215 		// 10 mm box at 0,0,0 
216 		Box box = new Box("test_box1",5.0,5.0,5.0);
217 		LogicalVolume lvTest = new LogicalVolume("lvTest",box,dummymat);
218 		new PhysicalVolume(
219 				new Transform3D(),
220 				"box1",
221 				lvTest,
222 				mom.getLogicalVolume(),
223 				0);
224 
225 		// 10 mm box at 50,0,0
226 		Box box2 = new Box("test_box2",5.0,5.0,5.0);
227 		LogicalVolume lvTest2 = new LogicalVolume("lvTest2",box2,dummymat);
228 		new PhysicalVolume(
229 				new Transform3D(new Translation3D(50.0,0,0)),
230 				"box2",
231 				lvTest2,
232 				mom.getLogicalVolume(),
233 				1);		
234 		
235 		// 10 mm box at 0,50,0
236 		Box box3 = new Box("test_box3",5.0,5.0,5.0);
237 		LogicalVolume lvTest3 = new LogicalVolume("lvTest3",box3,dummymat);
238 		new PhysicalVolume(
239 				new Transform3D(new Translation3D(0,50.0,0)),
240 				"box3",
241 				lvTest3,
242 				mom.getLogicalVolume(),
243 				2);		
244 		
245 		// A 2 mm box inside of box3.
246 		Box box4 = new Box("test_box4",2.0,2.0,2.0);
247 		LogicalVolume lvTest4 = new LogicalVolume("lvTest4",box4,dummymat);
248 		new PhysicalVolume(
249 				null,
250 				"box4",
251 				lvTest4,
252 				lvTest3,
253 				0);				
254 		
255 		Tube tube1 = new Tube("test_tube1",100.0,200.0,1000.0);
256 		LogicalVolume lvTest5 = new LogicalVolume("lvTest5",tube1,dummymat);
257 		new PhysicalVolume(
258 				null,
259 				"tube1",
260 				lvTest5,
261 				mom.getLogicalVolume(),
262 				6);
263 		
264 		Tube tube2 = new Tube("test_tube2",110.0,120.0,1000.0);
265 		LogicalVolume lvTest6 = new LogicalVolume("lvTest6",tube2,dummymat);
266 		new PhysicalVolume(
267 				null,
268 				"tube2",
269 				lvTest6,
270 				lvTest5,
271 				7);
272 	}
273 	
274 	public final IPhysicalVolume createWorld()
275 	{		
276 		Box boxWorld = new Box(
277 				"world_box",
278 				10.0*m,
279 				10.0*m,
280 				10.0*m);
281 		
282 		LogicalVolume lvWorld = 
283 			new LogicalVolume(
284 					"world",
285 					boxWorld,
286 					dummymat);
287 		
288 		IPhysicalVolume pvTop = 
289 			new PhysicalVolume(
290 					null,
291 					"world",
292 					lvWorld,
293 					null,
294 					0);
295 		
296 		return pvTop;
297 	}   
298 	
299 	public class DummyDE
300 	extends DetectorElement
301 	{
302 		DummyDE(IPhysicalVolumePath support)
303 		{
304             super("dummy");
305             setSupport(support);
306 		}
307 	}
308 }