Commit in lcsim on MAIN
src/org/lcsim/detector/driver/SimTrackerIdentifierReadoutDriver.java+49added 1.1
test/org/lcsim/detector/driver/SimTrackerHitIdentifierReadoutDriverTest.java+145added 1.1
+194
2 added files
JM: Add id-based readout of SimTrackerHits.  Should be much faster than using position.

lcsim/src/org/lcsim/detector/driver
SimTrackerIdentifierReadoutDriver.java added at 1.1
diff -N SimTrackerIdentifierReadoutDriver.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ SimTrackerIdentifierReadoutDriver.java	22 May 2007 22:43:25 -0000	1.1
@@ -0,0 +1,49 @@
+package org.lcsim.detector.driver;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.lcsim.detector.DetectorElement;
+import org.lcsim.detector.DetectorElementStore;
+import org.lcsim.detector.IDetectorElement;
+import org.lcsim.detector.IReadout;
+import org.lcsim.detector.identifier.Identifier;
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.SimTrackerHit;
+
+public class SimTrackerIdentifierReadoutDriver 
+extends CollectionHandler
+{
+    public SimTrackerIdentifierReadoutDriver(List<String> collectionNames)
+    {
+        super(collectionNames);
+        add( new ReadoutCleanupDriver( collectionNames ) );
+    }
+
+    public SimTrackerIdentifierReadoutDriver(String[] collectionNames)
+    {        
+        super(collectionNames);
+        add( new ReadoutCleanupDriver( collectionNames ) );
+    }
+
+    protected void process(EventHeader header)
+    {           
+        super.process(header);
+        List<List<SimTrackerHit>> collections = header.get(SimTrackerHit.class);
+        for ( List<SimTrackerHit> collection : collections )
+        {
+            if ( canHandle( header.getMetaData( collection ).getName() ) )
+            {
+                for ( SimTrackerHit hit : collection )
+                {
+                    IDetectorElement deSubdet = hit.getSubdetector().getDetectorElement();
+                    IDetectorElement deHit = 
+                        DetectorElementStore.getInstance().findDetectorElement(new Identifier(hit.getCellID()));
+                    hit.setDetectorElement( deHit );
+                    IReadout ro = deHit.getReadout();
+                    ro.addHit( hit );                    
+                }
+            }
+        }        
+    }                     
+}

lcsim/test/org/lcsim/detector/driver
SimTrackerHitIdentifierReadoutDriverTest.java added at 1.1
diff -N SimTrackerHitIdentifierReadoutDriverTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ SimTrackerHitIdentifierReadoutDriverTest.java	22 May 2007 22:43:26 -0000	1.1
@@ -0,0 +1,145 @@
+package org.lcsim.detector.driver;
+
+import hep.physics.vec.BasicHep3Vector;
+
+import java.io.File;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.lcsim.detector.IDetectorElement;
+import org.lcsim.detector.IReadout;
+import org.lcsim.detector.identifier.Identifier;
+import org.lcsim.detector.identifier.IIdentifierHelper;
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.SimTrackerHit;
+import org.lcsim.geometry.compact.Subdetector;
+import org.lcsim.util.Driver;
+import org.lcsim.util.cache.FileCache;
+import org.lcsim.util.loop.LCSimLoop;
+
+/**
+ * Reads 2 muon event generated in sid01_polyhedra, which uses
+ * SiTrackerBarrel.  For DetectorElements where there is a 
+ * Readout containing one hit, check that the hit from the event
+ * and the DE's have matching cell id. 
+ */
+public class SimTrackerHitIdentifierReadoutDriverTest extends TestCase
+{
+    public SimTrackerHitIdentifierReadoutDriverTest(String testName)
+    {
+        super(testName);
+    }
+
+    public static Test suite()
+    {
+        return new TestSuite(SimTrackerHitIdentifierReadoutDriverTest.class);
+    }
+
+    public void setUp()
+    {
+        java.lang.Runtime.getRuntime().gc();
+    }
+
+    public void testReadout() throws Exception
+    {        
+        URL url = 
+            new URL("http://www.lcsim.org/test/lcio/mu-_10GeV_10_SLIC_v2r1p7_geant4-v8r2p0_SiTrackerBarrelTest00.slcio");
+        FileCache cache = new FileCache();
+        File file = cache.getCachedFile(url);        
+
+        LCSimLoop loop = new LCSimLoop();
+        loop.setLCIORecordSource(file);
+        loop.add( new SimTrackerHitPositionalReadoutDriver(new String[] {"SiTrackerBarrel_RO"} ) );
+        loop.add( new TestDriver() );
+        loop.loop(2);
+        loop.dispose();
+    }     
+
+    class TestDriver
+    extends Driver
+    {
+        protected void process(EventHeader header)
+        {      
+            List<SimTrackerHit> hitsFromRO = new ArrayList<SimTrackerHit>();
+            Subdetector tkr = header.getDetector().getSubdetector("SiTrackerBarrel");
+            //IIdentifierHelper helper = tkr.getDetectorElement().getIdentifierHelper();
+            for ( IDetectorElement layer : tkr.getDetectorElement().getChildren() )
+            {
+                for ( IDetectorElement module : layer.getChildren() )
+                {
+                    for ( IDetectorElement sensor : module.getChildren() )
+                    {
+                        IReadout ro = sensor.getReadout();
+                                
+                        for ( SimTrackerHit rohit : ro.getHits(SimTrackerHit.class) )
+                        {
+                        		hitsFromRO.add( rohit );
+                        }
+                    }
+                }
+            }
+                         
+            List<SimTrackerHit> eventHits = header.get(SimTrackerHit.class, "SiTrackerBarrel_RO");
+            
+            assertEquals( "Collection and detector readout have different number of hits!", eventHits.size(), hitsFromRO.size() );
+            
+            for ( SimTrackerHit hit : eventHits )
+            {                               
+                IDetectorElement de = hit.getDetectorElement();
+
+                // Check that DetectorElement and hit have same id.
+
+                //System.out.println("deId = " + helper.unpack( de.getIdentifier() ).toString() );
+                //System.out.println("hitId = " + helper.unpack( new Identifier( hit.getCellID() ) ).toString() );
+                //System.out.println();
+                
+                assertEquals( 
+                        "DetectorElement id and hit id are different!",
+                        de.getIdentifier(), 
+                        new Identifier( hit.getCellID() ) );
+                
+                // Get the Readoout.
+                IReadout ro = de.getReadout();
+                
+                // Get the hits from the Readout.
+                List<SimTrackerHit> hits = ro.getHits( SimTrackerHit.class );
+                
+                // Loop over the hits in the Readout.
+                for ( SimTrackerHit deHit : hits )
+                {		
+                    // Check that the cellIDs are the same for the event hit and the Readout hit.
+                    assertEquals( "CellIDs do not match!", hit.getCellID(), deHit.getCellID() );
+                    
+                    // Check that the SimTrackerHit is inside the DetectorElement it is assigned to.
+                    assertTrue( "SimTrackerHit not inside its DetectorElement!", de.getGeometry().isInside( new BasicHep3Vector( hit.getPoint() ) ) );
+                    
+                    // Check that the hit can be looked up by position in the Readout hits.
+                    SimTrackerHit lkpHit = findHitByPosition( hit, hits );
+                    assertTrue( "Didn't find hit by position on the Readout!", lkpHit != null );                                       
+                }
+            } 
+        }
+        
+        private SimTrackerHit findHitByPosition( SimTrackerHit findHit, List<SimTrackerHit> hits)
+        {
+            double[] findHitPos = findHit.getPoint();
+            for ( SimTrackerHit hit : hits )
+            {
+                double[] hitPos = hit.getPoint();
+                if ( findHitPos[0] == hitPos[0] && 
+                        findHitPos[1] == hitPos[1] && 
+                        findHitPos[2] == hitPos[2] )
+                {
+                    return hit;
+                }
+            }
+            return null;
+        }
+    }
+}
+
CVSspam 0.2.8