View Javadoc

1   package org.lcsim.detector;
2   
3   import static org.lcsim.units.clhep.SystemOfUnits.m;
4   import hep.graphics.heprep.HepRep;
5   import hep.graphics.heprep.HepRepFactory;
6   import hep.graphics.heprep.HepRepInstance;
7   import hep.graphics.heprep.HepRepInstanceTree;
8   import hep.graphics.heprep.HepRepPoint;
9   import hep.graphics.heprep.HepRepTreeID;
10  import hep.graphics.heprep.HepRepType;
11  import hep.graphics.heprep.HepRepTypeTree;
12  import hep.graphics.heprep.HepRepWriter;
13  import hep.physics.vec.Hep3Vector;
14  
15  import java.awt.Color;
16  import java.io.FileOutputStream;
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  import org.lcsim.detector.converter.heprep.DetectorElementToHepRepConverter;
24  import org.lcsim.detector.material.IMaterial;
25  import org.lcsim.detector.material.MaterialElement;
26  import org.lcsim.detector.solids.Box;
27  import org.lcsim.util.test.TestUtil.TestOutputFile;
28  
29  /**
30   * Test case that writes out a heprep file called
31   * ShapeRotateTest.heprep which shows some
32   * DetectorElements that have translation and
33   * rotations.  Visually inspect to determine
34   * correctness.
35   * 
36   * @author Jeremy McCormick <jeremym@slac.stanford.edu>
37   *
38   */
39  public class ShapeRotateTest 
40  extends TestCase
41  {
42      private static IMaterial dummymat = new MaterialElement("dummymat",1,1,1.0);
43      private IPhysicalVolumeNavigator nav;
44  
45      List<Hep3Vector> points =
46          new ArrayList<Hep3Vector>();
47  
48      public ShapeRotateTest(String name)
49      {
50          super(name);
51      }
52  
53      public static junit.framework.Test suite()
54      {
55          return new TestSuite(ShapeRotateTest.class);
56      }
57  
58      public void testShapeRotate()
59      {        
60          try {
61              writeHepRep(new TestOutputFile("ShapeRotateTest.heprep").getAbsolutePath());
62          }
63          catch ( Throwable x )
64          {
65              throw new RuntimeException(x);
66          }
67      }
68  
69      public class TestDE
70      extends DetectorElement
71      {
72          TestDE(String name, IDetectorElement parent, String path)
73          {
74              super(name, parent, path);
75          }        
76      }
77      
78      public void setUp()
79      {
80          IPhysicalVolume world = createWorld();
81          nav = new PhysicalVolumeNavigator("default",world);
82          
83          Box box1 = new Box("box1",50,25,50);
84          ILogicalVolume lv1 = new LogicalVolume("test1",box1,dummymat);
85          
86          Box box2 = new Box("box2", 10,10,10);
87          ILogicalVolume lv2 = new LogicalVolume("test2",box2,dummymat);
88          ITranslation3D t1 = new Translation3D(25,0,0);
89          IRotation3D r1 = new RotationPassiveXYZ(0, 0, Math.PI/8);
90          
91          IPhysicalVolume pv1 = 
92              new PhysicalVolume(
93                      new Transform3D(t1,r1),
94                      "box2",
95                      lv2,
96                      lv1,
97                      0
98                      );
99          
100         double transX = 0;
101         double transIncr = 100;
102         
103         double rotZ = 0;
104         double rotIncr = Math.PI/2;
105         
106         for (int i=0; i<5; i++)
107         {
108             IRotation3D thisRot = new RotationPassiveXYZ(0, 0, rotZ);
109             
110             ITranslation3D thisTrans = new Translation3D(transX,0,0);
111             
112             IPhysicalVolume pv =
113                 new PhysicalVolume(
114                         new Transform3D(thisTrans, thisRot),
115                         "pv" + i,
116                         lv1,
117                         world.getLogicalVolume(),
118                         i);
119             
120             rotZ += rotIncr;
121             transX += transIncr;
122             
123             new TestDE("dau"+i,null,"/pv"+i+"/box2");
124             new TestDE("par"+i,null,"/pv"+i);
125         }
126         
127         /*
128         for (IDetectorElement de : DetectorElementStore.getInstance())
129         {
130             System.out.println(de.getName());
131             if ( de.hasGeometryInfo())
132             {
133                 System.out.println("     " + de.getGeometry().getPhysicalVolumePath());
134             }
135         }*/
136     }
137   
138     public final IPhysicalVolume createWorld()
139     {       
140         Box boxWorld = new Box(
141                 "world_box",
142                 10.0*m,
143                 10.0*m,
144                 10.0*m);
145 
146         LogicalVolume lvWorld = 
147             new LogicalVolume(
148                     "world",
149                     boxWorld,
150                     dummymat);
151 
152         IPhysicalVolume pvTop = 
153             new PhysicalVolume(
154                     null,
155                     "world",
156                     lvWorld,
157                     null,
158                     0);
159 
160         return pvTop;
161     }      
162 
163     public final static String HITS_LAYER = "Hits";
164     public final static String PARTICLES_LAYER = "Particles";
165    
166     private void writeHepRep(String filepath) throws Exception
167     {
168         HepRepFactory factory = HepRepFactory.create();
169         HepRep root = factory.createHepRep();        
170 
171         // detector
172         HepRepTreeID treeID = factory.createHepRepTreeID("DetectorType", "1.0");
173         HepRepTypeTree typeTree = factory.createHepRepTypeTree(treeID);
174         root.addTypeTree(typeTree);
175 
176         HepRepInstanceTree instanceTree = factory.createHepRepInstanceTree("Detector", "1.0", typeTree);
177         root.addInstanceTree(instanceTree);
178 
179         String detectorLayer = "Detector";
180         root.addLayer(detectorLayer);
181 
182         HepRepType barrel = factory.createHepRepType(typeTree, "Barrel");
183         barrel.addAttValue("layer", detectorLayer);
184         HepRepType endcap = factory.createHepRepType(typeTree, "Endcap");
185         endcap.addAttValue("layer", detectorLayer);
186 
187         //DetectorElementToHepRepConverter cnv = 
188         //    new DetectorElementToHepRepConverter();
189 
190         for (IDetectorElement de : DetectorElementStore.getInstance())
191         {
192             DetectorElementToHepRepConverter.convert(de, factory, root, -1, false, null);
193         }
194         // end detector
195 
196         root.addLayer(PARTICLES_LAYER);
197         root.addLayer(HITS_LAYER);
198         root.addLayer("axis");
199 
200         treeID = factory.createHepRepTreeID("EventType", "1.0");
201         typeTree = factory.createHepRepTypeTree(treeID);
202         root.addTypeTree(typeTree);
203         instanceTree = factory.createHepRepInstanceTree("Event", "1.0", typeTree);
204         root.addInstanceTree(instanceTree);  
205 
206         // axis
207         HepRepType axis = factory.createHepRepType(typeTree, "axis");
208         axis.addAttValue("drawAs","Line");
209         axis.addAttValue("layer", "axis");
210 
211         HepRepType xaxis = factory.createHepRepType(axis, "xaxis");
212         xaxis.addAttValue("color",Color.RED);
213         xaxis.addAttValue("fill",true);
214         xaxis.addAttValue("fillColor",Color.RED);
215         HepRepInstance x = factory.createHepRepInstance(instanceTree, xaxis);
216         factory.createHepRepPoint(x,0,0,0);
217         factory.createHepRepPoint(x,1000,0,0);
218 
219         HepRepType yaxis = factory.createHepRepType(axis, "yaxis");
220         yaxis.addAttValue("color",Color.GREEN);
221         yaxis.addAttValue("fill",true);
222         yaxis.addAttValue("fillColor",Color.GREEN);
223         HepRepInstance y = factory.createHepRepInstance(instanceTree, yaxis);
224         factory.createHepRepPoint(y,0,0,0);
225         factory.createHepRepPoint(y,0,1000,0);
226 
227         HepRepType zaxis = factory.createHepRepType(axis, "zaxis");
228         zaxis.addAttValue("color",Color.BLUE);
229         zaxis.addAttValue("fill",true);
230         zaxis.addAttValue("fillColor",Color.BLUE);
231         HepRepInstance z = factory.createHepRepInstance(instanceTree, zaxis);
232         factory.createHepRepPoint(z,0,0,0);
233         factory.createHepRepPoint(z,0,0,1000);
234         // done axis                              
235 
236         // points
237         HepRepType typeX = factory.createHepRepType(typeTree, "points");
238         typeX.addAttValue("layer",HITS_LAYER);
239         typeX.addAttValue("drawAs","Point");
240         typeX.addAttValue("color",Color.GREEN);
241         typeX.addAttValue("fill",true);
242         typeX.addAttValue("fillColor",Color.GREEN);
243         typeX.addAttValue("MarkName","Box");
244  
245         for (Hep3Vector p : points )
246         {
247             HepRepInstance instanceX = factory.createHepRepInstance(instanceTree, typeX);
248             HepRepPoint pp = factory.createHepRepPoint(instanceX,p.x(),p.y(),p.z());
249         }        
250         // done points
251 
252         HepRepWriter writer = 
253             HepRepFactory.create().createHepRepWriter(new FileOutputStream(filepath),false,false);
254         writer.write(root,"test");
255         writer.close();
256     }      
257 }