lcsim/test/org/lcsim/util/lcio
diff -u -r1.1 -r1.2
--- CalorimeterHitIOTest.java 24 Aug 2007 00:03:56 -0000 1.1
+++ CalorimeterHitIOTest.java 24 Aug 2007 19:56:42 -0000 1.2
@@ -15,12 +15,12 @@
import org.lcsim.util.test.TestUtil.TestOutputFile;
/**
- * Test to create a collection of {@link org.lcsim.event.CalorimeterHit} objects,
- * write out to a file, and read back, checking that the information returned
- * is consistent.
+ * Creates an event with {@link org.lcsim.event.CalorimeterHit} collections
+ * that have all permutations of the LCIO flag settings. This event is written
+ * to a file, read back in, and checked for correctness.
*
* @author Jeremy McCormick <[log in to unmask]>
- * @version $Id: CalorimeterHitIOTest.java,v 1.1 2007/08/24 00:03:56 jeremy Exp $
+ * @version $Id: CalorimeterHitIOTest.java,v 1.2 2007/08/24 19:56:42 jeremy Exp $
*/
public class CalorimeterHitIOTest extends TestCase
{
@@ -28,25 +28,24 @@
{
// Create an empty event.
EventHeader event = new BaseLCSimEvent(0,0,"sdjan03");
-
- // Create a test collection of hits.
- List<CalorimeterHit> hits = new ArrayList<CalorimeterHit>();
-
- // Add two identical test hits to the collection.
- TestCalorimeterHit testhit = new TestCalorimeterHit();
- hits.add(testhit);
- testhit = new TestCalorimeterHit();
- hits.add(testhit);
-
- // Set write flags.
- // TODO: Check that IO works with different flag setting.
- int flag = 1 << LCIOConstants.RCHBIT_ID1;
- flag |= 1 << LCIOConstants.RCHBIT_TIME;
- //flag |= 1 << LCIOConstants.RCHBIT_NO_PTR;
- flag |= 1 << LCIOConstants.CHBIT_LONG;
-
- // Add the hits collection to the event.
- event.put("TestCalorimeterHits", hits, CalorimeterHit.class, flag);
+
+ // 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 two identical test hits to the collection.
+ TestCalorimeterHit testhit = new TestCalorimeterHit();
+ hits.add(testhit);
+ testhit = new TestCalorimeterHit();
+ hits.add(testhit);
+
+ 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("CalorimeterHitIOTest.slcio");
@@ -58,33 +57,125 @@
LCIOReader reader = new LCIOReader(outfile);
EventHeader eventcheck = reader.read();
- // Sanity checks on TestCalorimeterHit read back from file.
- List<CalorimeterHit> hitscheck = eventcheck.get(CalorimeterHit.class,"TestCalorimeterHits");
- for (CalorimeterHit hit : hitscheck)
+ // Test hit to compare against.
+ TestCalorimeterHit testhit = new TestCalorimeterHit();
+
+ // 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(), testhit.getCellID());
+ }
+ else
+ {
+ assertEquals("partial cellId not equal", (int)hit.getCellID(), (int)testhit.getCellID());
+ }
+
+ // Check position if CHBIT_LONG is set.
+ if (LCIOUtil.bitTest(flag,LCIOConstants.CHBIT_LONG))
+ {
+ assertEquals("position[0] not equal", hit.getPosition()[0], testhit.getPosition()[0]);
+ assertEquals("position[1] not equal", hit.getPosition()[1], testhit.getPosition()[1]);
+ assertEquals("position[2] not equal", hit.getPosition()[2], testhit.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 raw energies are equal.
+ assertEquals("rawEnergy not equal", hit.getRawEnergy(), testhit.getRawEnergy());
+
+ // Check that times are equal.
+ if (LCIOUtil.bitTest(flag,LCIOConstants.RCHBIT_TIME))
+ {
+ assertEquals("time not equal", hit.getTime(), testhit.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);
+ }
+ }
+ }
+ }
+
+ private List<Integer> makeTestFlags()
+ {
+ List<Integer> flagList = new ArrayList<Integer>();
+
+ List<int[]> perm = new ArrayList<int[]>();
+ for (int i=0; i<16; i++)
{
- // Check all the values that do not depend on a valid LCMetaData or IDDecoder.
- assertEquals("cellId not equal", hit.getCellID(), testhit.getCellID());
- assertEquals("position[0] not equal", hit.getPosition()[0], testhit.getPosition()[0]);
- assertEquals("position[1] not equal", hit.getPosition()[1], testhit.getPosition()[1]);
- assertEquals("position[2] not equal", hit.getPosition()[2], testhit.getPosition()[2]);
- assertEquals("rawEnergy not equal", hit.getRawEnergy(), testhit.getRawEnergy());
- assertEquals("time not equal", hit.getTime(), testhit.getTime(), 1e-7);
+ 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;
}
class TestCalorimeterHit extends BaseCalorimeterHit
{
TestCalorimeterHit()
{
- this.id = 0;
+ this.id = Long.parseLong("800000001",16);
this.position = new double[] {1.0,2.0,3.0};
this.rawEnergy = 10.0;
this.time = 1.1;
}
-
- void setData(LCMetaData data)
- {
- this.data = data;
- }
}
}
\ No newline at end of file