lcsim/test/org/lcsim/calorimeter
diff -u -r1.1 -r1.2
--- CalorimeterHitDetectorElementTest.java 18 Mar 2009 03:43:13 -0000 1.1
+++ CalorimeterHitDetectorElementTest.java 18 Mar 2009 04:05:10 -0000 1.2
@@ -7,6 +7,7 @@
import junit.framework.TestCase;
import org.lcsim.detector.identifier.IIdentifierHelper;
+import org.lcsim.detector.solids.Inside;
import org.lcsim.event.CalorimeterHit;
import org.lcsim.event.EventHeader;
import org.lcsim.util.Driver;
@@ -14,87 +15,115 @@
import org.lcsim.util.loop.LCSimLoop;
/**
- * Tests that the CalorimeterHitDetectorElementDriver sets up DetectorElements
- * for all CalorimeterHits.
+ * Tests that all CalorimeterHits have valid DetectorElements associated with
+ * them for an sid02 event.
+ *
* @author Jeremy McCormick <[log in to unmask]>
*/
-public class CalorimeterHitDetectorElementTest extends TestCase
+public class CalorimeterHitDetectorElementTest extends TestCase
{
- private static final boolean DEBUG = false;
-
- public void testHits() throws Exception
- {
- URL url = new URL("http://www.lcsim.org/test/lcio/panpyWW-0-500_SLIC-v2r5p4_geant4-v9r2p0_LCPhys_sid02.slcio");
- FileCache cache = new FileCache();
- File testFile = cache.getCachedFile(url);
- LCSimLoop loop = new LCSimLoop();
- loop.setLCIORecordSource(testFile);
- loop.add(new CalorimeterTestDriver());
- loop.loop(-1,null);
- loop.dispose();
- }
-
- class CalorimeterTestDriver extends Driver
- {
- CalorimeterTestDriver()
- {}
-
- public void process(EventHeader event)
- {
- // First time how long it takes to setup DetectorElements
- // for the curious and/or skeptical.
- long start = System.currentTimeMillis();
- int nhits = 0;
- for (List<CalorimeterHit> coll : event.get(CalorimeterHit.class))
- {
- for (CalorimeterHit hit : coll)
- {
- hit.getDetectorElement();
- ++nhits;
- }
- }
- long elapsed = System.currentTimeMillis() - start;
- System.out.println("Took " + elapsed + " ms to find DetectorElements for " + nhits + " hits.");
- System.out.println("ms / hit = " + ((double)elapsed)/((double)nhits));
-
- // Now process the collections again and perform some assertions to
- // check the validity of the DetectorElement association.
- int checkedHits=0;
- for (List<CalorimeterHit> coll : event.get(CalorimeterHit.class))
- {
- for (CalorimeterHit hit : coll)
- {
- hit.getDetectorElement();
-
- // Hit must have a DetectorElement.
- assertTrue(hit.getDetectorElement() != null);
-
- IIdentifierHelper helper = hit.getIdentifierHelper();
-
- // Verbose debug print of DetectorElement name and hit's expanded identifier.
- if (DEBUG)
- System.out.println(hit.getDetectorElement().getName() + " - " + hit.getIdentifierHelper().unpack(hit.getIdentifier()));
-
- // CalorimeterHits are currently linked to their layer,
- // so layer number should match the DetectorElement's.
- int layern = helper.getValue(hit.getIdentifier(), "layer");
-
- // TODO: Replace with comparison against DetectorElement's identifier
- // once CylindricalCalorimeter sensors have them.
- if (!hit.getDetectorElement().getName().contains("layer"+layern))
- {
- throw new RuntimeException(
- "The layer number <" + layern + "> of the hit and does not match DetectorElement <" + hit.getDetectorElement().getName() + ">.");
- }
-
- ++checkedHits;
- }
- }
- assertEquals(
- "The number of hits that were setup and those that were checked do not match.",
- nhits,
- checkedHits);
- System.out.println("Checked " + checkedHits + " for valid DetectorElement setting.");
- }
- }
+ private static final boolean DEBUG = false;
+
+ public void testHits() throws Exception
+ {
+ URL url = new URL(
+ "http://www.lcsim.org/test/lcio/panpyWW-0-500_SLIC-v2r5p4_geant4-v9r2p0_LCPhys_sid02.slcio");
+ FileCache cache = new FileCache();
+ File testFile = cache.getCachedFile(url);
+ LCSimLoop loop = new LCSimLoop();
+ loop.setLCIORecordSource(testFile);
+ loop.add(new CalorimeterTestDriver());
+ loop.loop(-1, null);
+ loop.dispose();
+ }
+
+ class CalorimeterTestDriver extends Driver
+ {
+ CalorimeterTestDriver()
+ {
+ }
+
+ public void process(EventHeader event)
+ {
+ int nhits = 0;
+
+ // For the curious and/or skeptical, time how long
+ // it takes to setup the DetectorElements.
+ long start = System.currentTimeMillis();
+ for (List<CalorimeterHit> coll : event.get(CalorimeterHit.class))
+ {
+ for (CalorimeterHit hit : coll)
+ {
+ // This causes the DetectorElement reference to
+ // be cached in the hit.
+ hit.getDetectorElement();
+
+ // FIXME: This adds slightly to the elapsed time.
+ // Maybe count in second loop only.
+ ++nhits;
+ }
+ }
+
+ // Print statistics.
+ long elapsed = System.currentTimeMillis() - start;
+ System.out.println("Took " + elapsed
+ + " ms to find DetectorElements for " + nhits + " hits.");
+ System.out.println("ms / hit = " + ((double) elapsed)
+ / ((double) nhits));
+
+ // Now process the collections again and check the validity
+ // of the DetectorElement association.
+ int checkedHits = 0;
+ for (List<CalorimeterHit> coll : event.get(CalorimeterHit.class))
+ {
+ for (CalorimeterHit hit : coll)
+ {
+ // Verbose debug print of DetectorElement name and hit's
+ // expanded identifier.
+ if (DEBUG)
+ System.out.println(hit.getDetectorElement().getName()
+ + " - "
+ + hit.getIdentifierHelper().unpack(
+ hit.getIdentifier()));
+
+ hit.getDetectorElement();
+
+ // Hit must have a DetectorElement.
+ assertTrue(hit.getDetectorElement() != null);
+
+ // Hit position must be inside DetectorElement.
+ assertEquals("Hit is not inside its DetectorElement.",
+ hit.getDetectorElement().getGeometry().inside(
+ hit.getPositionVec()), Inside.INSIDE);
+
+ IIdentifierHelper helper = hit.getIdentifierHelper();
+
+ // CalorimeterHits are currently linked to their layer,
+ // so layer number should match the DetectorElement's.
+ int layern = helper.getValue(hit.getIdentifier(), "layer");
+
+ // TODO: Replace with comparison against DetectorElement's
+ // identifier once CylindricalCalorimeter sensors have them.
+ if (!hit.getDetectorElement().getName().contains(
+ "layer" + layern))
+ {
+ throw new RuntimeException(
+ "The layer number <"
+ + layern
+ + "> of the hit and does not match DetectorElement <"
+ + hit.getDetectorElement().getName()
+ + ">.");
+ }
+
+ ++checkedHits;
+ }
+ }
+
+ assertEquals(
+ "The number of hits that were setup and those that were checked do not match.",
+ nhits, checkedHits);
+
+ System.out.println("Found valid DetectorElements for " + checkedHits + " hits.");
+ }
+ }
}
\ No newline at end of file