lcsim/src/org/lcsim/contrib/CosminDeaconu
diff -N DisplayHitModules.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DisplayHitModules.java 27 Dec 2007 20:53:04 -0000 1.1
@@ -0,0 +1,117 @@
+/*
+ * DisplayHitModules.java
+ *
+ * Created on December 14, 2007, 1:43 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+package org.lcsim.contrib.CosminDeaconu;
+
+import hep.graphics.heprep.HepRepFactory;
+import hep.graphics.heprep.HepRepInstance;
+import hep.graphics.heprep.HepRepInstanceTree;
+import hep.graphics.heprep.HepRepType;
+import hep.graphics.heprep.HepRepTypeTree;
+import hep.physics.vec.Hep3Vector;
+import java.awt.Color;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.lcsim.detector.DetectorElementStore;
+import org.lcsim.detector.IDetectorElement;
+import org.lcsim.detector.IDetectorElementContainer;
+import org.lcsim.detector.IGeometryInfo;
+import org.lcsim.detector.identifier.IIdentifier;
+import org.lcsim.detector.identifier.Identifier;
+import org.lcsim.detector.solids.IPolyhedron;
+import org.lcsim.detector.solids.ISolid;
+import org.lcsim.detector.solids.Point3D;
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.SimTrackerHit;
+import org.lcsim.util.heprep.HepRepCollectionConverter;
+import org.lcsim.util.heprep.LCSimHepRepConverter;
+
+/**
+ * Converter that draws modules and sensors that have hits in them.
+ * Everything is drawn in the event layer.
+ *
+ * To enable, modify org.lcsim.util.heprep.LCSimHepRepConverter so that it
+ * contains "register(new DisplayHitModules());" along with all the other
+ * register statements (and you'll have to import this class too).
+ "
+ * The string DETECTOR_NAME gives the name of the detector for which this will activate
+ *
+ * @author cozzy
+ */
+public class DisplayHitModules implements HepRepCollectionConverter{
+
+ private static final String DETECTOR_NAME = "SiTrackerBarrelTest00"; //only accept this type of detector
+
+ /** Creates a new instance of DisplayHitModules */
+ public DisplayHitModules() {
+ }
+
+
+ public boolean canHandle(Class k) {
+ return (SimTrackerHit.class.isAssignableFrom(k));
+ }
+
+ public void convert(EventHeader event, List collection, HepRepFactory factory, HepRepTypeTree typeTree, HepRepInstanceTree instanceTree) {
+
+ if (!event.getDetectorName().equals(DETECTOR_NAME)) return; //make sure it's the right detector
+
+ //sensor type
+ HepRepType typeS = factory.createHepRepType(typeTree,"HitSensor");
+ typeS.addAttValue("layer",LCSimHepRepConverter.HITS_LAYER);
+ typeS.addAttValue("drawAs","prism");
+ typeS.addAttValue("color",Color.WHITE);
+
+ //module type
+ HepRepType typeM = factory.createHepRepType(typeTree,"HitModule");
+ typeM.addAttValue("layer",LCSimHepRepConverter.HITS_LAYER);
+ typeM.addAttValue("drawAs","prism");
+ typeM.addAttValue("color",Color.WHITE);
+
+ Set<Integer> alreadyMade = new HashSet<Integer>(); //don't want to draw the same thing multiple times if there are multiple SimHits in a sensor.
+
+ for (SimTrackerHit h : (List<SimTrackerHit>) collection) {
+
+ //get the right sensor
+ IIdentifier id = new Identifier(h.getCellID());
+ IDetectorElementContainer de = DetectorElementStore.getInstance().find(id);
+ IDetectorElement sensor = de.get(0);
+
+ //make sure we don't have repeats
+ Integer hash = Integer.valueOf(sensor.hashCode());
+ if (alreadyMade.contains(hash)) continue;
+ alreadyMade.add(hash);
+
+ //get the module from the sensor
+ IDetectorElement module = sensor.getParent();
+
+ drawPolyhedron(sensor,typeS,instanceTree,factory);
+ drawPolyhedron(module,typeM,instanceTree,factory);
+
+ }
+ }
+
+ private void drawPolyhedron(IDetectorElement detelem, HepRepType type,HepRepInstanceTree instanceTree, HepRepFactory factory) {
+
+ ISolid solid = detelem.getGeometry().getLogicalVolume().getSolid();
+ if (!(solid instanceof IPolyhedron)) return;
+ IPolyhedron poly = (IPolyhedron) solid;
+
+ List<Point3D> points = poly.getVertices();
+ int[] point_ordering = poly.getHepRepVertexOrdering();
+
+ HepRepInstance instance = factory.createHepRepInstance(instanceTree,type);
+
+ for (int i = 0; i< point_ordering.length; i++) {
+ Hep3Vector p = detelem.getGeometry().transformLocalToGlobal(points.get(point_ordering[i]));
+ factory.createHepRepPoint(instance,p.x(),p.y(),p.z());
+ }
+ }
+
+}