lcsim/test/org/lcsim/event/base
diff -N CalorimeterHitImplTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CalorimeterHitImplTest.java 5 May 2010 15:59:52 -0000 1.1
@@ -0,0 +1,226 @@
+package org.lcsim.event.base;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import junit.framework.TestCase;
+import org.lcsim.event.CalorimeterHit;
+import org.lcsim.event.EventHeader;
+import org.lcsim.util.lcio.LCIOConstants;
+import org.lcsim.util.lcio.LCIOReader;
+import org.lcsim.util.lcio.LCIOUtil;
+import org.lcsim.util.lcio.LCIOWriter;
+import org.lcsim.util.test.TestUtil.TestOutputFile;
+
+/**
+ *
+ * @author Norman A. Graf
+ *
+ * @version $Id:
+ */
+public class CalorimeterHitImplTest extends TestCase
+{
+
+ public void testCalorimeterHitImpl() throws IOException
+ {
+ // create a hit
+ long id = Long.parseLong("800000001", 16);
+ double[] position = new double[]
+ {
+ 1.0, 2.0, 3.0
+ };
+ double energy = 100.;
+ double time = 1.1;
+ int type = 137;
+
+ CalorimeterHit myHit = new CalorimeterHitImpl(energy, position, time, id, type);
+ System.out.println(myHit);
+
+ // Create an empty event.
+ EventHeader event = new BaseLCSimEvent(0, 0, "sidloi");
+
+ // Create a list of test flags, all permutations of 4 settings for 16 total.
+ List<Integer> testFlags = makeTestFlags();
+
+ // Add a hits collection to the event for every permutation of the flag settings.
+ for (int i = 0; i < testFlags.size(); i++)
+ {
+ // Create a test collection of hits.
+ List<CalorimeterHit> hits = new ArrayList<CalorimeterHit>();
+ // add the hit to the collection
+ hits.add(myHit);
+ // add the collection to the event
+ event.put("TestCalorimeterHits_" + Integer.toHexString(testFlags.get(i)), hits, CalorimeterHit.class, testFlags.get(i));
+ }
+
+ // Write out the event to a file.
+ File outfile = new TestOutputFile("CalorimeterHitImplIOTest.slcio");
+ LCIOWriter writer = new LCIOWriter(outfile);
+ writer.write(event);
+ writer.close();
+
+ // Read the event back.
+ LCIOReader reader = new LCIOReader(outfile);
+ EventHeader eventcheck = reader.read();
+
+ // Loop over all the different flag settings.
+ for (int i=0; i<testFlags.size(); i++)
+ {
+ int flag = testFlags.get(i);
+
+ // Get the collection with these flag settings.
+ List<CalorimeterHit> hitscheck = eventcheck.get(CalorimeterHit.class,"TestCalorimeterHits_"+Integer.toHexString(flag));
+
+ // Check all from CalorimeterHit that do not depend on a valid LCMetaData or IDDecoder.
+ for (CalorimeterHit hit : hitscheck)
+ {
+ // Check that cellIds are equal if full Id is stored.
+ if (LCIOUtil.bitTest(flag,LCIOConstants.RCHBIT_ID1))
+ {
+ assertEquals("cellId not equal", hit.getCellID(), myHit.getCellID());
+ }
+ else
+ {
+ assertEquals("partial cellId not equal", (int)hit.getCellID(), (int)myHit.getCellID());
+ }
+
+ // Check position if CHBIT_LONG is set.
+ if (LCIOUtil.bitTest(flag,LCIOConstants.CHBIT_LONG))
+ {
+ assertEquals("position[0] not equal", hit.getPosition()[0], myHit.getPosition()[0]);
+ assertEquals("position[1] not equal", hit.getPosition()[1], myHit.getPosition()[1]);
+ assertEquals("position[2] not equal", hit.getPosition()[2], myHit.getPosition()[2]);
+ }
+ // Check that an exception is thrown when trying to access position when CHBIT_LONG is not set.
+ else
+ {
+ try
+ {
+ hit.getPosition();
+ assertTrue("hit access should have thrown an exception",0==1);
+ }
+ catch (Exception x)
+ {
+ // OK.
+ }
+ }
+
+ // Check that the energies are equal.
+ assertEquals("Energy not equal", hit.getCorrectedEnergy(), myHit.getCorrectedEnergy());
+
+ // Check that times are equal.
+ if (LCIOUtil.bitTest(flag,LCIOConstants.RCHBIT_TIME))
+ {
+ assertEquals("time not equal", hit.getTime(), myHit.getTime(), 1e-7);
+ }
+ // Check that the time is 0.0 if RCHBIT_TIME is not set.
+ else
+ {
+ assertEquals("time should be zero", hit.getTime(), 0.0);
+ }
+ assertEquals(hit.getType(), 137);
+ }
+ }
+
+ }
+
+ private List<Integer> makeTestFlags()
+ {
+ List<Integer> flagList = new ArrayList<Integer>();
+
+ List<int[]> perm = new ArrayList<int[]>();
+ for (int i = 0; i < 16; i++)
+ {
+ perm.add(new int[0]);
+ }
+
+ perm.set(0, new int[]
+ {
+ 0, 0, 0, 0
+ });
+ perm.set(1, new int[]
+ {
+ 1, 0, 0, 0
+ });
+ perm.set(2, new int[]
+ {
+ 0, 1, 0, 0
+ });
+ perm.set(3, new int[]
+ {
+ 0, 0, 1, 0
+ });
+ perm.set(4, new int[]
+ {
+ 0, 0, 0, 1
+ });
+ perm.set(5, new int[]
+ {
+ 1, 1, 0, 0
+ });
+ perm.set(6, new int[]
+ {
+ 1, 1, 1, 0
+ });
+ perm.set(7, new int[]
+ {
+ 1, 1, 0, 1
+ });
+ perm.set(8, new int[]
+ {
+ 0, 1, 1, 0
+ });
+ perm.set(9, new int[]
+ {
+ 0, 1, 1, 1
+ });
+ perm.set(10, new int[]
+ {
+ 1, 0, 1, 0
+ });
+ perm.set(11, new int[]
+ {
+ 1, 0, 1, 1
+ });
+ perm.set(12, new int[]
+ {
+ 0, 0, 1, 1
+ });
+ perm.set(13, new int[]
+ {
+ 1, 0, 0, 1
+ });
+ perm.set(14, new int[]
+ {
+ 1, 1, 1, 1
+ });
+ perm.set(15, new int[]
+ {
+ 0, 1, 0, 1
+ });
+
+ for (int i = 0; i < 16; i++)
+ {
+ int flag = 0;
+
+ int[] x = perm.get(i);
+
+ if (x[0] == 1)
+ flag |= 1 << LCIOConstants.RCHBIT_ID1;
+
+ if (x[1] == 1)
+ flag |= 1 << LCIOConstants.RCHBIT_TIME;
+
+ if (x[2] == 1)
+ flag |= 1 << LCIOConstants.RCHBIT_NO_PTR;
+
+ if (x[3] == 1)
+ flag |= 1 << LCIOConstants.CHBIT_LONG;
+
+ flagList.add(flag);
+ }
+
+ return flagList;
+ }
+}