lcsim/test/org/lcsim/detector/driver
diff -N DetectorIdHelperTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DetectorIdHelperTest.java 6 Sep 2007 18:15:48 -0000 1.1
@@ -0,0 +1,299 @@
+package org.lcsim.detector.driver;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.freehep.record.loop.LoopException;
+import org.lcsim.detector.identifier.IIdentifier;
+import org.lcsim.detector.identifier.Identifier;
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.SimCalorimeterHit;
+import org.lcsim.event.SimTrackerHit;
+import org.lcsim.event.EventHeader.LCMetaData;
+import org.lcsim.geometry.Subdetector;
+import org.lcsim.util.Driver;
+import org.lcsim.util.cache.FileCache;
+import org.lcsim.util.loop.LCSimLoop;
+import org.lcsim.detector.IDetectorElement;
+import org.lcsim.detector.DetectorIdHelper;
+
+/**
+ * Tests the functionality of {@link org.lcsim.detector.DetectorIdHelper}
+ * on an sid01 LCIO file.
+ *
+ * @author Jeremy McCormick <[log in to unmask]>
+ * @version $Id: DetectorIdHelperTest.java,v 1.1 2007/09/06 18:15:48 jeremy Exp $
+ */
+
+public class DetectorIdHelperTest extends TestCase
+{
+ public void testDetectorIdHelper()
+ {
+ File file = null;
+ try {
+ FileCache cache = new FileCache();
+ file = cache.getCachedFile(new URL("http://www.lcsim.org/test/lcio/muons10_Theta4-176_1-10GeV_SLIC_v2r2p1_sid01.slcio"));
+ LCSimLoop loop = new LCSimLoop();
+ loop.setLCIORecordSource(file);
+ loop.add(new TrackerDriver());
+ loop.add(new CalorimeterDriver());
+ loop.loop(1);
+ loop.dispose();
+ }
+ catch (IOException x)
+ {
+ throw new RuntimeException(x);
+ }
+ catch (LoopException x)
+ {
+ throw new RuntimeException(x);
+ }
+ }
+
+ class CalorimeterDriver extends Driver
+ {
+ protected void process(EventHeader event)
+ {
+ int ecalBarrel, ecalEndcap, hcalBarrel, hcalEndcap, forwardEcal, lumi, muonBarrel, muonEndcap;
+ ecalBarrel = ecalEndcap = hcalBarrel = hcalEndcap = forwardEcal = lumi = muonBarrel = muonEndcap = 0;
+ List<List<SimCalorimeterHit>> collections = event.get(SimCalorimeterHit.class);
+ for (List<SimCalorimeterHit> collection: collections)
+ {
+ LCMetaData meta = event.getMetaData(collection);
+ String collectionName = meta.getName();
+ Subdetector subdet = meta.getIDDecoder().getSubdetector();
+ IDetectorElement deSubdet = subdet.getDetectorElement();
+ DetectorIdHelper idHelper = (DetectorIdHelper)deSubdet.getIdentifierHelper();
+
+ for (SimCalorimeterHit hit : collection)
+ {
+ IIdentifier hitId = new Identifier(hit.getCellID());
+
+ assertTrue(idHelper.isCalorimeter(hitId));
+
+ if (subdet.isBarrel())
+ {
+ assertTrue(idHelper.isBarrel(hitId));
+ assertTrue(idHelper.isCalorimeterBarrel(hitId));
+ }
+
+ if (subdet.isEndcap())
+ {
+ assertTrue(idHelper.isEndcap(hitId));
+ assertTrue(idHelper.isCalorimeterEndcap(hitId));
+
+ if (hit.getPosition()[2] > 0)
+ {
+ assertTrue(idHelper.isCalorimeterEndcapPositive(hitId));
+ }
+ else
+ {
+ assertTrue(idHelper.isCalorimeterEndcapNegative(hitId));
+ }
+ }
+
+ if (collectionName.equals("EcalBarrHits"))
+ {
+ assertTrue(idHelper.isEcal(hitId));
+ assertTrue(idHelper.isBarrel(hitId));
+ ++ecalBarrel;
+ }
+
+ if (collectionName.equals("EcalEndcapHits"))
+ {
+ assertTrue(idHelper.isEcal(hitId));
+ assertTrue(idHelper.isEcalEndcap(hitId));
+ if (hit.getPosition()[2] > 0)
+ {
+ assertTrue(idHelper.isEcalEndcapPositive(hitId));
+ }
+ else
+ {
+ assertTrue(idHelper.isEcalEndcapNegative(hitId));
+ }
+ ++ecalEndcap;
+ }
+
+ if (collectionName.equals("HcalBarrHits"))
+ {
+ assertTrue(idHelper.isHcal(hitId));
+ assertTrue(idHelper.isHcalBarrel(hitId));
+ ++hcalBarrel;
+ }
+
+ if (collectionName.equals("HcalEndcapHits"))
+ {
+ assertTrue(idHelper.isHcal(hitId));
+ assertTrue(idHelper.isHcalEndcap(hitId));
+ if (hit.getPosition()[2] > 0)
+ {
+ assertTrue(idHelper.isHcalEndcapPositive(hitId));
+ }
+ else
+ {
+ assertTrue(idHelper.isHcalEndcapNegative(hitId));
+ }
+ ++hcalEndcap;
+ }
+
+ if (collectionName.equals("MuonBarrHits"))
+ {
+ assertTrue(idHelper.isMuon(hitId));
+ assertTrue(idHelper.isMuonBarrel(hitId));
+ ++muonBarrel;
+ }
+
+ if (collectionName.equals("MuonEndcapHits"))
+ {
+ assertTrue(idHelper.isMuon(hitId));
+ assertTrue(idHelper.isMuonEndcap(hitId));
+ if (hit.getPosition()[2] > 0)
+ {
+ assertTrue(idHelper.isMuonEndcapPositive(hitId));
+ }
+ else
+ {
+ assertTrue(idHelper.isMuonEndcapNegative(hitId));
+ }
+ ++muonEndcap;
+ }
+
+ // FIXME: This event does not have Lumi hits!
+ if (collectionName.equals("LuminosityMonitorHits"))
+ {
+ assertTrue(idHelper.isLumi(hitId));
+
+ if (hit.getPosition()[2] > 0)
+ {
+ assertTrue(idHelper.isLumiEndcapPositive(hitId));
+ }
+ else
+ {
+ assertTrue(idHelper.isLumiEndcapNegative(hitId));
+ }
+ ++lumi;
+ }
+
+ if (collectionName.equals("ForwardEcalEndcapHits"))
+ {
+ assertTrue(idHelper.isEcalForward(hitId));
+
+ if (hit.getPosition()[2] > 0)
+ {
+ assertTrue(idHelper.isEcalForwardEndcapPositive(hitId));
+ }
+ else
+ {
+ assertTrue(idHelper.isEcalForwardEndcapNegative(hitId));
+ }
+ ++forwardEcal;
+ }
+ }
+ }
+ assertEquals(ecalBarrel,173);
+ assertEquals(ecalEndcap,139);
+ assertEquals(hcalBarrel,209);
+ assertEquals(hcalEndcap,108);
+ assertEquals(muonBarrel,73);
+ assertEquals(muonEndcap,81);
+ assertEquals(lumi,0);
+ assertEquals(forwardEcal,30);
+ }
+ }
+
+ class TrackerDriver extends Driver
+ {
+ protected void process(EventHeader event)
+ {
+ event.getDetector();
+ int sitBarrel, sitEndcap, vtxBarrel, vtxEndcap;
+ sitBarrel = sitEndcap = vtxBarrel = vtxEndcap = 0;
+ List<List<SimTrackerHit>> collections = event.get(SimTrackerHit.class);
+ for (List<SimTrackerHit> collection : collections)
+ {
+ LCMetaData meta = event.getMetaData(collection);
+ String collectionName = meta.getName();
+ Subdetector subdet = meta.getIDDecoder().getSubdetector();
+ if (subdet == null)
+ throw new RuntimeException("Oops! Subdetector for collection " + meta.getName() + " is null!");
+ IDetectorElement deSubdet = subdet.getDetectorElement();
+ DetectorIdHelper idHelper = (DetectorIdHelper)deSubdet.getIdentifierHelper();
+
+ IIdentifier subdetId = deSubdet.getIdentifier();
+
+ for (SimTrackerHit hit : collection)
+ {
+ IIdentifier hitId = new Identifier(hit.getCellID());
+
+ assertTrue(idHelper.isTracker(hitId));
+
+ if (subdet.isBarrel())
+ {
+ assertTrue(idHelper.isBarrel(hitId));
+ assertTrue(idHelper.isTrackerBarrel(hitId));
+ }
+
+ if (subdet.isEndcap())
+ {
+ assertTrue(idHelper.isEndcap(hitId));
+ assertTrue(idHelper.isTrackerEndcap(hitId));
+ }
+
+ if (collectionName.equals("TkrBarrHits"))
+ {
+ assertTrue(idHelper.isSit(hitId));
+ assertTrue(idHelper.isSitBarrel(hitId));
+ ++sitBarrel;
+ }
+
+ if (collectionName.equals("TkrEndcapHits"))
+ {
+ assertTrue(idHelper.isSit(hitId));
+ assertTrue(idHelper.isSitEndcap(hitId));
+ if (hit.getPoint()[2] > 0)
+ {
+ assertTrue(idHelper.isSitEndcapPositive(hitId));
+ }
+ else
+ {
+ assertTrue(idHelper.isSitEndcapNegative(hitId));
+ }
+ ++sitEndcap;
+ }
+
+ if (collectionName.equals("VtxBarrHits"))
+ {
+ assertTrue(idHelper.isVtx(hitId));
+ assertTrue(idHelper.isVtxBarrel(hitId));
+ ++vtxBarrel;
+ }
+
+ if (collectionName.equals("VtxEndcapHits"))
+ {
+ assertTrue(idHelper.isVtx(hitId));
+ assertTrue(idHelper.isVtxEndcap(hitId));
+ if (hit.getPoint()[2] > 0)
+ {
+ assertTrue(idHelper.isVtxEndcapPositive(hitId));
+ }
+ else
+ {
+ assertTrue(idHelper.isVtxEndcapNegative(hitId));
+ }
+ ++vtxEndcap;
+ }
+ }
+ }
+
+ // Check that the correct number of SimTrackerHits were processed.
+ assertEquals(sitBarrel,33);
+ assertEquals(sitEndcap, 26);
+ assertEquals(vtxBarrel,35);
+ assertEquals(vtxEndcap,12);
+ }
+ }
+}
\ No newline at end of file