Commit in lcsim/test/org/lcsim/util/lcio on MAIN
CalorimeterHitIOTest.java+90added 1.1
JM: Add a simple test case to read/write CalorimeterHits.

lcsim/test/org/lcsim/util/lcio
CalorimeterHitIOTest.java added at 1.1
diff -N CalorimeterHitIOTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ CalorimeterHitIOTest.java	24 Aug 2007 00:03:56 -0000	1.1
@@ -0,0 +1,90 @@
+package org.lcsim.util.lcio;
+
+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.event.EventHeader.LCMetaData;
+import org.lcsim.util.event.BaseCalorimeterHit;
+import org.lcsim.util.event.BaseLCSimEvent;
+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.
+ * 
+ * @author Jeremy McCormick <[log in to unmask]>
+ * @version $Id: CalorimeterHitIOTest.java,v 1.1 2007/08/24 00:03:56 jeremy Exp $
+ */
+public class CalorimeterHitIOTest extends TestCase 
+{
+	public void testCalorimeterHitIO() throws IOException
+	{
+		// 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);		
+		
+		// Write out the event to a file.
+		File outfile = new TestOutputFile("CalorimeterHitIOTest.slcio");		
+		LCIOWriter writer = new LCIOWriter(outfile);
+		writer.write(event);
+		writer.close();
+		
+		// Read the event back.
+		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)
+		{
+			// 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);
+		}
+	}
+	
+	class TestCalorimeterHit extends BaseCalorimeterHit
+	{
+		TestCalorimeterHit()
+		{
+			this.id = 0;
+			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
CVSspam 0.2.8