Commit in lcsim/test/org/lcsim/util/loop on MAIN
LCIODriverTest.java+108added 1.1
JM: test of LCIODriver collection filtering

lcsim/test/org/lcsim/util/loop
LCIODriverTest.java added at 1.1
diff -N LCIODriverTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ LCIODriverTest.java	18 Dec 2008 20:50:29 -0000	1.1
@@ -0,0 +1,108 @@
+package org.lcsim.util.loop;
+
+import java.io.File;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.List;
+
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.MCParticle;
+import org.lcsim.event.SimCalorimeterHit;
+import org.lcsim.util.Driver;
+import org.lcsim.util.cache.FileCache;
+import org.lcsim.util.test.TestUtil.TestOutputFile;
+
+/**
+ * Tests of the {@link org.lcsim.util.loop.LCIODriver}.
+ * @author jeremym
+ */
+public class LCIODriverTest 
+{
+	private final String writeCollection[] = {"MCParticle"};  
+	
+	/**
+	 * Test that setting write only collections works correctly.
+	 * @throws Exception
+	 */
+	public void testWriteOnlyCollection() throws Exception
+	{	
+		FileCache cache = new FileCache();
+		File inputFile = cache.getCachedFile(new URL("http://www.lcsim.org/test/lcio/pythiaZPoleuds-0-1000_SLIC-v2r3p7_geant4-v9r0p1_LCPhys_sid01.slcio"));
+		
+		LCSimLoop loopWrite = new LCSimLoop();
+		loopWrite.setLCIORecordSource(inputFile);
+		File outputFile = new TestOutputFile("WriteOnlyCollectionTest");
+		
+		List<String> writeOnlyList = Arrays.asList(writeCollection);
+		LCIODriver driver = new LCIODriver(outputFile, writeOnlyList);
+		driver.setWriteOnlyCollections(writeCollection);
+		loopWrite.add(driver);
+		
+		loopWrite.loop(1);
+		loopWrite.dispose();
+		
+		LCSimLoop loopRead = new LCSimLoop();
+		loopRead.setLCIORecordSource(outputFile);
+		loopRead.add(new WriteOnlyCollectionCheckDriver());
+		loopRead.loop(1);
+		loopRead.dispose();
+	}
+
+	class WriteOnlyCollectionCheckDriver extends Driver
+	{
+		public void process(EventHeader event)
+		{
+			if (event.getLists().size() > 1)
+			{
+				for (List list : event.getLists())
+				{
+					System.out.println(event.getMetaData(list).getName());
+				}
+				throw new RuntimeException("Too many collections in event.");
+			}
+			List<List<MCParticle>> mcparticle = event.get(MCParticle.class);
+			if (mcparticle.size() > 1)
+				throw new RuntimeException("Too many MCParticle collections in output.");
+		}
+	}
+	
+	/**
+	 * Test that setting ignore collections works correctly.
+	 * @throws Exception
+	 */
+	public void testIgnoreCollections() throws Exception
+	{
+		FileCache cache = new FileCache();
+		File inputFile = cache.getCachedFile(new URL("http://www.lcsim.org/test/lcio/pythiaZPoleuds-0-1000_SLIC-v2r3p7_geant4-v9r0p1_LCPhys_sid01.slcio"));
+		
+		LCSimLoop loopWrite = new LCSimLoop();
+		loopWrite.setLCIORecordSource(inputFile);
+		File outputFile = new TestOutputFile("IgnoreCollectionTest");
+		LCIODriver lciodriver = new LCIODriver(outputFile, Arrays.asList(new String[] {"EcalBarrHits"}));
+		loopWrite.add(lciodriver);
+		loopWrite.loop(1);
+		loopWrite.dispose();
+		
+		LCSimLoop loopRead = new LCSimLoop();
+		loopRead.setLCIORecordSource(outputFile);
+		loopRead.add(new IgnoreCollectionCheckDriver());
+		loopRead.loop(1);
+		loopRead.dispose();
+	}
+	
+	class IgnoreCollectionCheckDriver extends Driver
+	{
+		public void process(EventHeader event)
+		{
+			List<List<SimCalorimeterHit>> colls = event.get(SimCalorimeterHit.class);
+			for (List<SimCalorimeterHit> hits : colls)
+			{
+				if (event.getMetaData(hits).getName().equals("EcalBarrHits"))
+				{
+					throw new RuntimeException("Collection was not ignored!");
+				}
+			}
+		}
+	}
+	
+}
CVSspam 0.2.8