Commit in hps-java/src/test/java/org/lcsim/hps/evio on MAIN | |||
CompositeDataTest.java | +149 | added 1.1 | |
Evio4ReadTest.java | +117 | added 1.1 | |
EvioToLcioTest.java | +837 | added 1.1 | |
RawTrackerHitToEvio4Converter.java | +225 | added 1.1 | |
RawTrackerHitToEvio4Converter_Test.java | +9 | added 1.1 | |
Evio2LcioTest.java | -848 | 1.2 removed | |
+1337 | -848 |
checkpointing some work on EVIO v4
diff -N CompositeDataTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ CompositeDataTest.java 1 Mar 2012 22:32:30 -0000 1.1 @@ -0,0 +1,149 @@
+package org.lcsim.hps.evio; + +import java.nio.ByteOrder; +import java.util.List; + +import junit.framework.TestCase; + +import org.jlab.coda.jevio.ByteDataTransformer; +import org.jlab.coda.jevio.CompositeData; +import org.jlab.coda.jevio.DataType; +import org.jlab.coda.jevio.EvioException; + +public class CompositeDataTest extends TestCase { + + public void testMe() { + doIt(); + } + + public void doIt() { + + int[] bank = new int[24]; + + /***********************/ + /* bank of tagsegments */ + /***********************/ + bank[0] = 23; // bank length + bank[1] = 6 << 16 | 0xF << 8 | 3; // tag = 6, bank contains composite type, num = 3 + + // N(I,D,F,2S,8a) + // first part of composite type (for format) = tagseg (tag & type ignored, len used) + bank[2] = 5 << 20 | 0x3 << 16 | 4; // tag = 5, seg has char data, len = 4 + // ASCII chars values in latest evio string (array) format, N(I,D,F,2S,8a) with N=2 + bank[3] = 0x4E << 24 | 0x28 << 16 | 0x49 << 8 | 0x2C; // N ( I , + bank[4] = 0x44 << 24 | 0x2C << 16 | 0x46 << 8 | 0x2C; // D , F , + bank[5] = 0x32 << 24 | 0x53 << 16 | 0x2C << 8 | 0x38 ; // 2 S , 8 + bank[6] = 0x61 << 24 | 0x29 << 16 | 0x00 << 8 | 0x04 ; // a ) \0 \4 + + // second part of composite type (for data) = bank (tag, num, type ignored, len used) + bank[7] = 16; + bank[8] = 6 << 16 | 0xF << 8 | 1; + bank[9] = 0x2; // N + bank[10] = 0x00001111; // I + + // Double + double d = Math.PI * (-1.e-100); + long dl = Double.doubleToLongBits(d); + bank[11] = (int) (dl >>> 32); // higher 32 bits + bank[12] = (int) dl; // lower 32 bits + + // Float + float f = (float)(Math.PI*(-1.e-24)); + int fi = Float.floatToIntBits(f); + bank[13] = fi; + + bank[14] = 0x11223344; // 2S + + bank[15] = 0x48 << 24 | 0x49 << 16 | 0x00 << 8 | 0x48; // H I \0 H + bank[16] = 0x4F << 24 | 0x00 << 16 | 0x04 << 8 | 0x04; // 0 \ 0 \4 \4 + + // duplicate data + for (int i=0; i < 7; i++) { + bank[17+i] = bank[10+i]; + } + + // all composite including headers + int[] allData = new int[22]; + for (int i=0; i < 22; i++) { + allData[i] = bank[i+2]; + } + + try { + // change int array into byte array + byte[] byteArray = ByteDataTransformer.toBytes(allData, ByteOrder.BIG_ENDIAN); + + // Create composite object + CompositeData cData = new CompositeData(byteArray, ByteOrder.BIG_ENDIAN); + + // print out general data + printCompositeDataObject(cData); + } + catch (EvioException e) { + e.printStackTrace(); + } + + } + + + /** + * Print the data from a CompositeData object in a user-friendly form. + * @param cData CompositeData object + */ + public static void printCompositeDataObject(CompositeData cData) { + + // Get lists of data items & their types from composite data object + List<Object> items = cData.getItems(); + List<DataType> types = cData.getTypes(); + + // Use these list to print out data of unknown format + DataType type; + int len = items.size(); + for (int i=0; i < len; i++) { + type = types.get(i); + System.out.print(String.format("type = %9s, val = ", type)); + switch (type) { + case INT32: + case UINT32: + case UNKNOWN32: + int j = (Integer)items.get(i); + System.out.println("0x"+Integer.toHexString(j)); + break; + case LONG64: + case ULONG64: + long l = (Long)items.get(i); + System.out.println("0x"+Long.toHexString(l)); + break; + case SHORT16: + case USHORT16: + short s = (Short)items.get(i); + System.out.println("0x"+Integer.toHexString(s)); + break; + case CHAR8: + case UCHAR8: + byte b = (Byte)items.get(i); + System.out.println("0x"+Integer.toHexString(b)); + break; + case FLOAT32: + float ff = (Float)items.get(i); + System.out.println(""+ff); + break; + case DOUBLE64: + double dd = (Double)items.get(i); + System.out.println(""+dd); + break; + case CHARSTAR8: + String[] strs = (String[])items.get(i); + for (String ss : strs) { + System.out.print(ss + ", "); + } + System.out.println(); + break; + default: + } + } + + } + + + +}
diff -N Evio4ReadTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ Evio4ReadTest.java 1 Mar 2012 22:32:30 -0000 1.1 @@ -0,0 +1,117 @@
+package org.lcsim.hps.evio; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.List; + +import junit.framework.TestCase; + +import org.jlab.coda.jevio.BaseStructure; +import org.jlab.coda.jevio.CompositeData; +import org.jlab.coda.jevio.DataType; +import org.jlab.coda.jevio.EvioEvent; +import org.jlab.coda.jevio.EvioException; +import org.jlab.coda.jevio.EvioReader; +import org.jlab.coda.jevio.test.CompositeTester; +import org.lcsim.util.cache.FileCache; + +/** + * Read in an EVIO 4 sample file and print the contents. + * + * @author Jeremy McCormick <[log in to unmask]> + * @version $Id: Evio4ReadTest.java,v 1.1 2012/03/01 22:32:30 jeremy Exp $ + */ +public class Evio4ReadTest extends TestCase { + + private static final String testFileName = "evio4_sample.dat"; + private static final String testFileUrl = "http://www.lcsim.org/test/hps/" + testFileName; + + public void testEvio() throws IOException, EvioException { + + // Get test file to local cache. + FileCache cache = new FileCache(); + cache.setCacheDirectory(new File(".")); + File file = cache.getCachedFile(new URL(testFileUrl)); + + // Open the EVIO file for reading. + EvioReader reader = new EvioReader(file); + + // Write to XML before looping. + //reader.toXMLFile((new File(testFileName + ".xml")).getCanonicalPath()); + + // Loop over events. + EvioEvent event = reader.parseNextEvent(); + int nread = 0; + while (event != null) { + ++nread; + System.out.println("read event #" + event.getEventNumber() + " with size " + event.getTotalBytes() + " bytes"); + for (BaseStructure b1 : event.getChildren()) { + CompositeData cData = b1.getCompositeData(); + //CompositeTester.printCompositeDataObject(cData); + + // Get lists of data items & their types from composite data object + List<Object> items = cData.getItems(); + List<DataType> types = cData.getTypes(); + + System.out.println("# items = " + items.size()); + System.out.println("# types = " + types.size()); + DataType type; + int len = items.size(); + for (int i=0; i < len; i++) { + type = types.get(i); + System.out.print(String.format("type = %9s, val = ", type)); + switch (type) { + case INT32: + case UINT32: + case UNKNOWN32: + int j = (Integer) items.get(i); + System.out.println("0x" + Integer.toHexString(j)); + break; + case LONG64: + case ULONG64: + long l = (Long) items.get(i); + System.out.println("0x" + Long.toHexString(l)); + break; + case SHORT16: + case USHORT16: + short s = (Short) items.get(i); + System.out.println("0x" + Integer.toHexString(s)); + break; + case CHAR8: + case UCHAR8: + System.out.println("BORK"); + break; + // FIXME Broken? + // byte b = (Byte) items.get(i); + // System.out.println("0x" + Integer.toHexString(b)); + // break; + case FLOAT32: + float ff = (Float) items.get(i); + System.out.println("" + ff); + break; + case DOUBLE64: + double dd = (Double) items.get(i); + System.out.println("" + dd); + break; + case CHARSTAR8: + String[] strs = (String[]) items.get(i); + for (String ss : strs) { + System.out.print(ss + ", "); + } + System.out.println(); + break; + default: + } + } + } + + // DEBUG + if (true) break; + + event = reader.parseNextEvent(); + } + System.out.println("read " + nread + " events"); + reader.close(); + } +}
diff -N EvioToLcioTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ EvioToLcioTest.java 1 Mar 2012 22:32:30 -0000 1.1 @@ -0,0 +1,837 @@
+package org.lcsim.hps.evio; + +import static java.lang.System.arraycopy; + +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.freehep.record.loop.LoopException; +import org.jlab.coda.jevio.BaseStructure; +import org.jlab.coda.jevio.BaseStructureHeader; +import org.jlab.coda.jevio.EvioEvent; +import org.jlab.coda.jevio.EvioException; +import org.jlab.coda.jevio.EvioFile; +import org.lcsim.conditions.ConditionsManager; +import org.lcsim.conditions.ConditionsManagerImplementation; +import org.lcsim.conditions.ConditionsReader; +import org.lcsim.event.EventHeader; +import org.lcsim.event.GenericObject; +import org.lcsim.event.LCRelation; +import org.lcsim.event.base.BaseLCRelation; +import org.lcsim.event.base.BaseLCSimEvent; +import org.lcsim.util.Driver; +import org.lcsim.util.cache.FileCache; +import org.lcsim.util.lcio.LCIOWriter; +import org.lcsim.util.loop.DummyConditionsConverter; +import org.lcsim.util.loop.DummyDetector; +import org.lcsim.util.loop.LCSimLoop; + +/** + * Test that generically converts an EVIO v3 file into a collection of LCIO GenericObjects. + * Each data bank is copied into a corresponding GenericObject which has an associated object with its bank metadata. + * No type conversion to LCIO classes is performed. This can be done in a subsequent step within the LCSim event loop. + * + * @author Jeremy McCormick + * @version $Id: EvioToLcioTest.java,v 1.1 2012/03/01 22:32:30 jeremy Exp $ + */ +public class EvioToLcioTest extends TestCase +{ + // Control flags for debugging. Change these by hand and recompile to use. + static private final boolean DEBUG = false; // Set to true for a lot of debug prints. + static private final boolean BREAK_AFTER_SINGLE_EVENT = false; // Set to true to stop after one event. + static private final boolean WRITE_XML = false; // Set to true to write the EVIO file as XML before the conversion begins. + + // Converted LCIO file name. + static final private String outputFilePath = "./EvioCnvTest.slcio"; + + // URL pointing to test data from Maurik, which was generated by the GEMC simulation. + static final private String testFileUrl = "http://www.lcsim.org/test/hps/hps_test_data_2011_nov_18_1.evio"; + + /** + * Convert an EVIO file to LCIO and then read them back in parallel to check the output. + */ + public void testEvio2Lcio() + { + // Setup the file cache. + FileCache cache = null; + File testFile = null; + try + { + cache = new FileCache(new File(".")); + } + catch (IOException x) + { + throw new RuntimeException(x); + } + + // Get the test file to the local machine. + try + { + testFile = cache.getCachedFile(new URL(testFileUrl)); + } + catch (Exception x) + { + throw new RuntimeException(x); + } + + // Convert a test EVIO file to LCIO. + String[] args = new String[2]; + try + { + args[0] = testFile.getCanonicalPath(); + } + catch (IOException x) + { + throw new RuntimeException(x); + } + args[1] = outputFilePath; + (new Evio2LcioConverter()).main(args); + + if (DEBUG) + { + System.out.println("-----------------------"); + } + + // Test that the LCIO data matches the EVIO. + readBackLcio(new File(outputFilePath), testFile); + } + + /** + * Example class for generically converting EVIO data to LCIO. + */ + private static class Evio2LcioConverter + { + String bankInfoCollName = "BankInfo"; + String rawDataCollName = "RawDataBank"; + String relCollName = "BankInfoRelations"; + + public Evio2LcioConverter() + {} + + public void main(String[] args) + { + // Use dummy detector for LCSim conditions to avoid errors. + setDummyDetector("DUMMY"); + + // Check and set user arguments. + if (args.length < 2) + throw new RuntimeException("Not enough arguments."); + String inFilePath = args[0]; + String outputFilePath = args[1]; + + // Open the EVIO file for reading. + EvioFile evioFile = null; + try + { + evioFile = new EvioFile(inFilePath); + } + catch (IOException x) + { + throw new RuntimeException(x); + } + + // Write out test XML file with EVIO structure and data. + if (WRITE_XML) + { + System.out.println("Writing EVIO data to XML file ..."); + evioFile.toXMLFile("EvioTest.xml"); + } + + // Read the first EVIO event outside the processing loop. + EvioEvent evioEvent = null; + int nread = 0; + try + { + evioEvent = evioFile.parseNextEvent(); + ++nread; + } + catch (EvioException x) + { + throw new RuntimeException(x); + } + + if (DEBUG) + { + System.out.println("Read first EVIO event OKAY."); + } + + // Open new LCIOWriter for converted output. + LCIOWriter writer = null; + try + { + writer = new LCIOWriter(outputFilePath); + } + catch (IOException x) + { + throw new RuntimeException(x); + } + + if (DEBUG) + System.out.println(); + + // Loop over EVIO events. + while (evioEvent != null) + { + // Make a new LCIO event. + // FIXME EVIO event numbers all seem to be zero in the test file. + // FIXME Set run number. + BaseLCSimEvent lcioEvent = new BaseLCSimEvent(0, evioEvent.getEventNumber(), "DUMMY"); + + // GenericObject collection to fill with the raw data. + List<GenericObject> objectColl = new ArrayList<GenericObject>(); + + // LCRelation list to connect bank info with data. + List<LCRelation> bankRelColl = new ArrayList<LCRelation>(); + + // Map to represent bank tag and number info. + Map<String, GenericObject> bankIds = new HashMap<String, GenericObject>(); + + // Loop over the EVIO data banks. + Vector<BaseStructure> children = evioEvent.getChildren(); + for (BaseStructure topBank : children) + { + BaseStructureHeader topHeader = topBank.getHeader(); + if (topBank.getChildCount() > 0) + { + for (BaseStructure subBank : topBank.getChildren()) + { + BaseStructureHeader subHeader = subBank.getHeader(); + if (subBank.getChildCount() > 0) + { + for (BaseStructure dataBank : subBank.getChildren()) + { + BaseStructureHeader dataBankHeader = dataBank.getHeader(); + + // Make a concatenated ID for this bank. + String bankId = makeHeaderId(topHeader, subHeader, dataBankHeader); + + // Make a new GenericObject for this bank's info. + if (bankIds.get(bankId) == null) + { + int[] bankInfo = new int[6]; + bankInfo[0] = topHeader.getTag(); + bankInfo[1] = topHeader.getNumber(); + bankInfo[2] = subHeader.getTag(); + bankInfo[3] = subHeader.getNumber(); + bankInfo[4] = dataBankHeader.getTag(); + bankInfo[5] = dataBankHeader.getNumber(); + bankIds.put(bankId, new LcioDataBankInfo(bankInfo)); + } + + // Get the bank info. + GenericObject bankInfo = bankIds.get(bankId); + + //System.out.println("BankInfo: tag = " + bankInfo.getIntVal(0) + "; number = " + bankInfo.getIntVal(1)); + + if (DEBUG) + { + printOut(dataBank, System.out); + } + + // LCIO object to represent one bank of data. + LcioDataBank lcioData = null; + + // Float data. + if (dataBank.getFloatData() != null) + { + lcioData = new LcioDataBank(dataBank.getFloatData()); + } + // Int data. + if (dataBank.getIntData() != null) + { + lcioData = new LcioDataBank(dataBank.getIntData()); + } + // Double data. + if (dataBank.getDoubleData() != null) + { + lcioData = new LcioDataBank(dataBank.getDoubleData()); + } + // FIXME: How to handle raw byte data? Copy to int array with values between 0 and 255? + // Could also pack the data 4 to a 32-bit int or 8 to 64. + + // Setup relation pointing from bank data to its info. + if (lcioData != null) + { + objectColl.add(lcioData); + LCRelation relation = new BaseLCRelation(lcioData, bankInfo); + bankRelColl.add(relation); + } + + if (DEBUG) + { + System.out.println(); + } + } + } + } + } + } + + // Put the data bank info into the LCIO event. + List<GenericObject> bankInfoList = new ArrayList<GenericObject>(bankIds.values()); + lcioEvent.put(bankInfoCollName, bankInfoList, GenericObject.class, 0); + + // Put the GenericObject object collection containing the block data into the event. + lcioEvent.put(rawDataCollName, objectColl, GenericObject.class, 0); + + // Put the relations into the event. + lcioEvent.put(relCollName, bankRelColl, LCRelation.class, 0); + + // Write out the LCIO event. + try + { + writer.write(lcioEvent); + } + catch (IOException x) + { + throw new RuntimeException(x); + } + + // Check if need to break on one event. + if (BREAK_AFTER_SINGLE_EVENT) + break; + + // Read next EVIO event. + try + { + evioEvent = evioFile.parseNextEvent(); + ++nread; + } + catch (EvioException x) + { + throw new RuntimeException(x); + } + } + + try + { + writer.close(); + } + catch (IOException x) + { + throw new RuntimeException(x); + } + + if (DEBUG) + { + System.out.println("Read " + nread + " EVIO events OKAY."); + } + } + } + + private static void readBackLcio(File lcioFile, File evioFile) + { + LCSimLoop loop = new LCSimLoop(); + loop.setDummyDetector("DUMMY"); + try + { + loop.setLCIORecordSource(lcioFile); + } + catch (IOException x) + { + throw new RuntimeException(x); + } + LcioCheckDriver checkDriver = new LcioCheckDriver(); + try + { + checkDriver.setEvioFilePath(evioFile.getCanonicalPath()); + } + catch (IOException x) + { + throw new RuntimeException(x); + } + loop.add(checkDriver); + try + { + loop.loop(-1, null); + } + catch (LoopException x) + { + throw new RuntimeException(x); + } + catch (IOException x) + { + throw new RuntimeException(x); + } + } + + /** + * Read back the LCIO event and check that the data matches the corresponding EVIO file. + */ + static private class LcioCheckDriver extends Driver + { + // Number of events read. + int nread = 0; + + // Settable path to EVIO file. + String evioFilePath = null; + + // The EVIO file for comparison. + EvioFile evioFile = null; + + // The current EVIO event. + EvioEvent evioEvent = null; + + public LcioCheckDriver() + {} + + /** + * Set the path of the EVIO file to be checked against. + * @param evioFilePath The path to the EVIO file. + */ + public void setEvioFilePath(String evioFilePath) + { + this.evioFilePath = evioFilePath; + } + + public void startOfData() + { + // Open the EVIO file for reading. + if (evioFilePath == null) + { + throw new RuntimeException("The EVIO file path was not set!"); + } + try + { + evioFile = new EvioFile(evioFilePath); + } + catch (IOException x) + { + throw new RuntimeException(x); + } + nread = 0; + } + + // FIXME The names of the LCIO collections are hard-coded and correspond to the defaults from the converter class. + public void process(EventHeader event) + { + if (DEBUG) + { + System.out.println("Processing event #" + event.getEventNumber()); + } + + // Read in next EVIO event which should match up with this LCIO event. + try + { + evioEvent = evioFile.parseNextEvent(); + } + catch (EvioException x) + { + throw new RuntimeException(x); + } + + // Make a list of EVIO banks to check. Only the leaf banks with data are added to the list. + Vector<BaseStructure> children = evioEvent.getChildren(); + List<BaseStructure> evioBanks = new ArrayList<BaseStructure>(); + for (BaseStructure topBank : children) + { + if (topBank.getChildCount() > 0) + { + for (BaseStructure subBank : topBank.getChildren()) + { + if (subBank.getChildCount() > 0) + { + for (BaseStructure dataBank : subBank.getChildren()) + { + // Only add leaf banks containing some data. + if (dataBank.getFloatData() != null || dataBank.getDoubleData() != null || dataBank.getIntData() != null) + { + evioBanks.add(dataBank); + } + } + } + } + } + } + + if (DEBUG) + { + System.out.println("EVIO event has " + evioBanks.size() + " data banks."); + } + + // Get the GenericObject collection containing the bank data. + List<GenericObject> lcioDataColl = event.get(GenericObject.class, "RawDataBank"); + + // Get the list of relations to bank info. + List<LCRelation> infoRel = event.get(LCRelation.class, "BankInfoRelations"); + + if (DEBUG) + { + System.out.println("LCIO collection RawDataBank has " + lcioDataColl.size() + " generic objects."); + } + + // Check that number of banks and GenericObjects is the same. + assertEquals("Number of EVIO data banks and LCIO GenericObjects does not match.", evioBanks.size(), lcioDataColl.size()); + + // Loop over the LCIO GenericObject collection. + int ibank = 0; + for (GenericObject lcioObj : lcioDataColl) + { + // Get the next bank from the EVIO file. + BaseStructure evioBank = evioBanks.get(ibank); + + if (DEBUG) + { + System.out.println("GenericObject ..."); + System.out.println(" ndoubles = " + lcioObj.getNDouble()); + System.out.println(" nfloats = " + lcioObj.getNFloat()); + System.out.println(" nints = " + lcioObj.getNInt()); + printOut(lcioObj, System.out); + } + + // Find the info relation for this data. + LCRelation rel = findRelation(infoRel, lcioObj); + + // Check that the relation exists. + TestCase.assertTrue("Could not find corresponding bank info for GenericObject.", rel != null); + + // Get the bank info. + GenericObject bankInfo = (GenericObject)rel.getTo(); + + if (DEBUG) + { + System.out.println("LCIO BankInfo ..."); + printOut(bankInfo, System.out); + System.out.println("EVIO Header; tag = " + evioBank.getHeader().getTag() + "; number = " + evioBank.getHeader().getNumber()); + } + + // Check that data bank info matches the EVIO file. This just checks the last tag and number. + assertEquals("Bank tag does not match EVIO.", evioBank.getHeader().getTag(), bankInfo.getIntVal(4)); + assertEquals("Bank number does not match EVIO.", evioBank.getHeader().getNumber(), bankInfo.getIntVal(5)); + + // Check double values. + if (lcioObj.getNDouble() != 0) + { + double[] evioDoubleData = evioBank.getDoubleData(); + int ndouble = evioDoubleData.length; + assertEquals("Number of doubles in GenericObject and EVIO bank don't match.", ndouble, lcioObj.getNDouble()); + for (int i=0; i<ndouble; i++) + { + // Seems that we need a big tolerance for this to pass! + assertEquals("EVIO double value does not match LCIO.", evioDoubleData[i], lcioObj.getDoubleVal(i), 1e-2); + } + } + + // Check int values. + if (lcioObj.getNInt() != 0) + { + int[] evioIntData = evioBank.getIntData(); + int nint = evioIntData.length; + assertEquals("Number of ints in GenericObject and EVIO bank don't match.", nint, lcioObj.getNInt()); + for (int i=0; i<nint; i++) + { + assertEquals("EVIO int value does not match LCIO.", evioIntData[i], lcioObj.getIntVal(i), 1e-2); + } + } + + // Check float values. + if (lcioObj.getNFloat() != 0) + { + float[] evioFloatData = evioBank.getFloatData(); + int nfloat = evioFloatData.length; + assertEquals("Number of floats in GenericObject and EVIO bank don't match.", nfloat, lcioObj.getNInt()); + for (int i=0; i<nfloat; i++) + { + assertEquals("EVIO float value does not match LCIO.", evioFloatData[i], lcioObj.getDoubleVal(i)); + } + } + + // Increment EVIO bank number. + ++ibank; + } + if (DEBUG) + { + System.out.println("Read " + event.getEventNumber() + " LCIO events OKAY."); + } + ++nread; + } + + public void endOfData() + { + if (DEBUG) + { + System.out.println(this.getClass().getSimpleName() + " read " + nread + " events OKAY."); + } + evioFile.close(); + } + } + + /** + * Represents the tag and number values from a set of EVIO banks. + */ + private static class LcioDataBankInfo implements GenericObject + { + private static final int BANK_INFO_SIZE = 6; + int[] bankInfo = new int[BANK_INFO_SIZE]; + + public LcioDataBankInfo(int[] bankInfo) + { + this.bankInfo = bankInfo; + } + + public int getNInt() + { + return BANK_INFO_SIZE; + } + + public int getNFloat() + { + return 0; + } + + public int getNDouble() + { + return 0; + } + + public int getIntVal(int index) + { + return bankInfo[index]; + } + + public float getFloatVal(int index) + { + return 0; + } + + public double getDoubleVal(int index) + { + return 0; + } + + public boolean isFixedSize() + { + return true; + } + } + + /** + * Implementation of GenericObject to represent an EVIO data bank in LCIO. + */ + private static class LcioDataBank implements GenericObject + { + float[] floatVals = null; + double[] doubleVals = null; + int[] intVals = null; + + int nint = 0; + int nfloat = 0; + int ndouble = 0; + + public LcioDataBank(float[] floatVals) + { + this.floatVals = new float[floatVals.length]; + arraycopy(floatVals, 0, this.floatVals, 0, floatVals.length); + nfloat = this.floatVals.length; + if (DEBUG) + { + System.out.println("Made LcioDataBank with " + nfloat + " floats."); + } + } + + public LcioDataBank(int[] intVals) + { + this.intVals = new int[intVals.length]; + arraycopy(intVals, 0, this.intVals, 0, intVals.length); + nint = this.intVals.length; + if (DEBUG) + { + System.out.println("Made LcioDataBank with " + nint + " ints."); + } + } + + public LcioDataBank(double[] doubleVals) + { + this.doubleVals = new double[doubleVals.length]; + arraycopy(doubleVals, 0, this.doubleVals, 0, doubleVals.length); + ndouble = this.doubleVals.length; + if (DEBUG) + { + System.out.println("Made LcioDataBank with " + ndouble + " doubles."); + } + } + + public int getNInt() + { + return nint; + } + + public int getNFloat() + { + return nfloat; + } + + public int getNDouble() + { + return ndouble; + } + + public int getIntVal(int index) + { + return intVals[index]; + } + + public float getFloatVal(int index) + { + return floatVals[index]; + } + + public double getDoubleVal(int index) + { + return doubleVals[index]; + } + + public boolean isFixedSize() + { + return false; + } + } + + /** + * Print out GenericObject values. + * @param lcioObj The GenericObject to be printed. + * @param ps The PrintStream to be used. + */ + private static void printOut(GenericObject lcioObj, PrintStream ps) + { + if (lcioObj.getNDouble() != 0) + { + int ndouble = lcioObj.getNDouble(); + ps.print("[ "); + for (int i=0; i<ndouble; i++) + { + ps.print(lcioObj.getDoubleVal(i) + " "); + } + ps.println("]"); + } + if (lcioObj.getNInt() != 0) + { + int nint = lcioObj.getNInt(); + ps.print("[ "); + for (int i=0; i<nint; i++) + { + ps.print(lcioObj.getIntVal(i) + " "); + } + ps.println("]"); + } + if (lcioObj.getNFloat() != 0) + { + int nfloat = lcioObj.getNFloat(); + ps.print("[ "); + for (int i=0; i<nfloat; i++) + { + ps.print(lcioObj.getFloatVal(i) + " "); + } + ps.println("]"); + } + } + + /** + * Makes an ID hash from three EVIO headers. + * @param h1 The first header. + * @param h2 The second header. + * @param h3 The third header. + * @return An ID that sequentially concatenates the tag and number values of the headers. + */ + private static String makeHeaderId(BaseStructureHeader h1, BaseStructureHeader h2, BaseStructureHeader h3) + { + return Integer.toString(h1.getTag()) + Integer.toString(h1.getNumber()) + + Integer.toString(h2.getTag()) + Integer.toString(h2.getNumber()) + + Integer.toString(h3.getTag()) + Integer.toString(h3.getNumber()); + } + + /** + * Print out the values from an EVIO data bank. + * @param dataBank The EVIO bank. + * @param ps The PrintStream to use. + */ + private static void printOut(BaseStructure dataBank, PrintStream ps) + { + if (dataBank.getFloatData() != null) + { + ps.println("Data bank has " + dataBank.getFloatData().length + " floats."); + float[] floatData = dataBank.getFloatData(); + ps.print("[ "); + for (int i=0; i<floatData.length; i++) + { + ps.print(floatData[i] + " "); + } + ps.println("]"); + } + if (dataBank.getIntData() != null) + { + ps.println("Data bank has " + dataBank.getIntData().length + " ints."); + int[] intData = dataBank.getIntData(); + ps.print("[ "); + for (int i=0; i<intData.length; i++) + { + ps.print(intData[i] + " "); + } + ps.println("]"); + } + if (dataBank.getDoubleData() != null) + { + ps.println("Data bank has " + dataBank.getDoubleData().length + " doubles."); + double[] doubleData = dataBank.getDoubleData(); + ps.print("[ "); + for (int i=0; i<doubleData.length; i++) + { + ps.print(doubleData[i] + " "); + } + ps.println("]"); + } + if (dataBank.getByteData() != null) + { + ps.println("Data bank has " + dataBank.getByteData().length + " bytes."); + byte[] byteData = dataBank.getByteData(); + ps.print("[ "); + for (int i = 0; i < byteData.length; i++) + { + ps.print(byteData[i] + " "); + } + ps.println("]"); + } + } + + /** + * An (inefficient) method for finding a relation by its 'from' object. + * @param relations The list of all relations to check. + * @param o1 The from object to find. + * @return The matching LCRelation or null if does not exist. + */ + private static LCRelation findRelation(List<LCRelation> relations, Object o1) + { + LCRelation fnd = null; + for (LCRelation rel : relations) + { + if (rel.getFrom() == o1) + { + fnd = rel; + break; + } + } + return fnd; + } + + /** + * Copied from conditions system to setup dummy detector. + * @param detectorName The name of the detector (shouldn't actually matter). + */ + private static void setDummyDetector(String detectorName) + { + ConditionsManager cond = ConditionsManager.defaultInstance(); + ConditionsReader dummyReader = ConditionsReader.createDummy(); + ((ConditionsManagerImplementation)cond).setConditionsReader(dummyReader, detectorName); + DummyDetector detector = new DummyDetector(detectorName); + cond.registerConditionsConverter(new DummyConditionsConverter(detector)); + } +}
\ No newline at end of file
diff -N RawTrackerHitToEvio4Converter.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ RawTrackerHitToEvio4Converter.java 1 Mar 2012 22:32:30 -0000 1.1 @@ -0,0 +1,225 @@
+package org.lcsim.hps.evio; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteOrder; +import java.util.List; + +import org.freehep.record.loop.LoopException; +import org.jlab.coda.jevio.ByteDataTransformer; +import org.jlab.coda.jevio.CompositeData; +import org.jlab.coda.jevio.EvioException; +import org.lcsim.event.EventHeader; +import org.lcsim.event.RawTrackerHit; +import org.lcsim.util.Driver; +import org.lcsim.util.loop.LCSimLoop; + +/** + * Test class that converts RawTrackerHit output from SeedTracker into EVIO v4 CompositeData objects + * and compares the two to make sure they match. + * + * @author Jeremy McCormick <[log in to unmask]> + * @version $Id: RawTrackerHitToEvio4Converter.java,v 1.1 2012/03/01 22:32:30 jeremy Exp $ + */ +public class RawTrackerHitToEvio4Converter extends Driver { + + private final String format = "N(L,I,I)"; + private String rawHitCollectionName = "RawTrackerHitMaker_RawTrackerHits"; + private static String lcioFileName = "/u1/hps/trackerTest/ap2.2gev050mevsel_SLIC-v2r11p1_geant4-v9r3p2_QGSP_BERT_HPS-Test-JLAB-v4pt0_trackRecon.slcio"; + + public static void testMe() throws IOException, LoopException { + File file = new File(lcioFileName); + LCSimLoop loop = new LCSimLoop(); + loop.setLCIORecordSource(file); + loop.add(new RawTrackerHitToEvio4Converter()); + loop.loop(1); + } + + public RawTrackerHitToEvio4Converter() {} + + private static final int BANK_SIZE = 11; + + // Convert a single RawTrackerHit into a CompositeData object. The real converter would put N hits into each CompositeData but + // this method just contains one hit per composite block (for testing purposes). + private CompositeData convertToCompositeData(RawTrackerHit hit) { + + // Data bank which will include both header and data. + int[] bank = new int[BANK_SIZE]; + + // First part of composite type containing the formatting string. + // tagseg (tag & type ignored, len used) + bank[0] = 5 << 20 | 0x3 << 16 | 3; // tag = 5, seg has char data, len = 3 + + // Pack the format string into 3 integers. + bank[1] = (int)format.charAt(0) << 24 | (int)format.charAt(1) << 16 | (int)format.charAt(2) << 8 | (int)format.charAt(3); // N ( L , + bank[2] = (int)format.charAt(4) << 24 | (int)format.charAt(5) << 16 | (int)format.charAt(6) << 8 | (int)format.charAt(7); // I , I ) + bank[3] = 0x00 << 24 | 0x04 << 16 | 0x00 << 8 | 0x04; // Terminate with a couple ASCII '4' characters. (Needed???) + + // bank (length, tag, composite type, number) + bank[4] = 6; // This is the length of the data segment. This has to be 6 and not 5 but not sure why. + bank[5] = 6 << 16 | 0xF << 8 | 1; + + // N to repeat. Here 1 is used for testing. + bank[6] = 0x1; // N + + // ID --> L + long id = hit.getCellID(); + bank[7] = (int) (id >>> 32); // higher 32 + bank[8] = (int) id; // lower 32 bits + + // time --> I + bank[9] = hit.getTime(); + + // adcValue --> I + bank[10] = (int)hit.getADCValues()[0]; // Store this as an int instead of a short for testing. + + // Make the byte array and put into a composite data structure. + byte[] byteArray = ByteDataTransformer.toBytes(bank, ByteOrder.BIG_ENDIAN); + try { + return new CompositeData(byteArray, ByteOrder.BIG_ENDIAN); + } catch (EvioException x) { + throw new RuntimeException(x); + } + } + + + public void setRawHitCollectionName(String rawHitCollectionName) { + this.rawHitCollectionName = rawHitCollectionName; + } + + public void startOfData() { + if (this.rawHitCollectionName == null) { + throw new RuntimeException("The rawHitCollectName parameter was not set."); + } + } + + public void process(EventHeader event) { + List<RawTrackerHit> hits = event.get(RawTrackerHit.class, rawHitCollectionName); + CompositeData cData = null; + + for (RawTrackerHit hit : hits) { + + System.out.println("Converting RawTrackerHit: "); + System.out.println(" cellID = " + hit.getCellID()); + System.out.println(" time = " + hit.getTime()); + System.out.println(" adc = " + hit.getADCValues()[0]); + + cData = this.convertToCompositeData(hit); + + System.out.println("Converted to EVIO: "); + long cellID = cData.getLong(); + System.out.println(" cellID = " + cellID); + int time = cData.getInt(); + System.out.println(" time = " + time); + int adc = cData.getInt(); + System.out.println(" adc = " + adc); + + // Poor man's test assertions... + + if (hit.getCellID() != cellID) { + throw new RuntimeException("CellID doesn't match."); + } + if (hit.getTime() != time) { + throw new RuntimeException("Time doesn't match."); + } + if (hit.getADCValues()[0] != adc) { + throw new RuntimeException("ADC value doesn't match."); + } + System.out.println("----------------------------"); + } + + //CompositeTester.printCompositeDataObject(cData); + } + + /* + private CompositeData convertToCompositeData(List<RawTrackerHit> hits) throws EvioException { + + // ----------------------------------------------------------- + + // Need to put in format tag and bank info as below.... + + //bank[0] = 23; // bank length + //bank[1] = 6 << 16 | 0xF << 8 | 3; // tag = 6, bank contains composite type, num = 3 + + // N(I,D,F,2S,8a) + // first part of composite type (for format) = tagseg (tag & type ignored, len used) + //bank[2] = 5 << 20 | 0x3 << 16 | 4; // tag = 5, seg has char data, len = 4 + + // ASCII chars values in latest evio string (array) format, N(I,D,F,2S,8a) with N=2 + //bank[3] = 0x4E << 24 | 0x28 << 16 | 0x49 << 8 | 0x2C; // N ( I , + //bank[4] = 0x44 << 24 | 0x2C << 16 | 0x46 << 8 | 0x2C; // D , F , + //bank[5] = 0x32 << 24 | 0x53 << 16 | 0x2C << 8 | 0x38 ; // 2 S , 8 + //bank[6] = 0x61 << 24 | 0x29 << 16 | 0x00 << 8 | 0x04 ; // a ) \0 \4 + + // second part of composite type (for data) = bank (tag, num, type ignored, len used) + //bank[7] = 16; + //bank[8] = 6 << 16 | 0xF << 8 | 1; + //bank[9] = 0x2; // N + //bank[10] = 0x00001111; // I + + // ----------------------------------------------------------- + + // Need 4 int values for each hit. + int nhits = hits.size(); + + // Total bank size. + int bankSize = headerLen + (nhits * hitSize); + + // Create bank with space for header and data. + int[] bank = new int[bankSize]; + + // ??????? + + // Bank size. + bank[0] = bankSize; // ? + + // Make the bank header. + // FIXME Are these settings right??? + bank[1] = 6 << 16 | 0xF << 8 | 3; // tag = 6, bank contains composite type, num = 3 + bank[2] = 5 << 20 | 0x3 << 16 | 2; // tag = 5, seg has char data, len = 2 + + // Format string. + // N(I, + // L,S) + bank[3] = (int)format.charAt(0) << 24 | (int)format.charAt(1) << 16 | (int)format.charAt(2) << 8 | (int)format.charAt(3); + bank[4] = (int)format.charAt(4) << 24 | (int)format.charAt(5) << 16 | (int)format.charAt(6) << 8 | (int)format.charAt(7); + + // second part of composite type (for data) = bank (tag, num, type ignored, len used) + bank[5] = 16; + bank[6] = 6 << 16 | 0xF << 8 | 1; + // FIXME Is this right? + bank[7] = 0x2; // N + bank[8] = 0x00001111; // I + + // ?????????? + + // Loop over byte array to pack hit data, starting after header. + int ihit = 0; + for (int i = headerLen; i < bank.length; i += 4) { + + // Get hit. + RawTrackerHit hit = hits.get(ihit); + + // 64-bit ID. + long id = hit.getCellID(); + bank[i] = (int) (id >>> 32); // higher 32 + bank[i+1] = (int) id; // lower 32 bits + + // Time. + int time = hit.getTime(); + bank[i+2] = time; + + // ADC value. + short adcValue = hit.getADCValues()[0]; + bank[i+3] = adcValue; + + // Next hit index. + ++ihit; + } + + byte[] byteArray = ByteDataTransformer.toBytes(bank, ByteOrder.BIG_ENDIAN); + return new CompositeData(byteArray, ByteOrder.BIG_ENDIAN); + } + */ + +}
\ No newline at end of file
diff -N RawTrackerHitToEvio4Converter_Test.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ RawTrackerHitToEvio4Converter_Test.java 1 Mar 2012 22:32:30 -0000 1.1 @@ -0,0 +1,9 @@
+package org.lcsim.hps.evio; + +import junit.framework.TestCase; + +public class RawTrackerHitToEvio4Converter_Test extends TestCase { + public void testIt() throws Exception { + RawTrackerHitToEvio4Converter.testMe(); + } +}
\ No newline at end of file
diff -N Evio2LcioTest.java --- Evio2LcioTest.java 11 Feb 2012 00:38:35 -0000 1.2 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,848 +0,0 @@
-package org.lcsim.hps.evio; - -import static java.lang.System.arraycopy; - -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; -import java.net.URL; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Vector; - -import junit.framework.TestCase; - -import org.freehep.record.loop.LoopException; -import org.jlab.coda.jevio.BaseStructure; -import org.jlab.coda.jevio.BaseStructureHeader; -import org.jlab.coda.jevio.EvioEvent; -import org.jlab.coda.jevio.EvioException; -import org.jlab.coda.jevio.EvioFile; -import org.lcsim.conditions.ConditionsManager; -import org.lcsim.conditions.ConditionsManagerImplementation; -import org.lcsim.conditions.ConditionsReader; -import org.lcsim.event.EventHeader; -import org.lcsim.event.GenericObject; -import org.lcsim.event.LCRelation; -import org.lcsim.event.base.BaseLCRelation; -import org.lcsim.event.base.BaseLCSimEvent; -import org.lcsim.util.Driver; -import org.lcsim.util.cache.FileCache; -import org.lcsim.util.lcio.LCIOWriter; -import org.lcsim.util.loop.DummyConditionsConverter; -import org.lcsim.util.loop.DummyDetector; -import org.lcsim.util.loop.LCSimLoop; - -/** - * Test that generically converts an EVIO v3 file into a collection of LCIO GenericObjects. - * Each data bank is copied into a corresponding GenericObject which has an associated object with its bank metadata. - * No type conversion to LCIO classes is performed. This can be done in a subsequent step within the LCSim event loop. - * - * @author Jeremy McCormick - * @version $Id: Evio2LcioTest.java,v 1.2 2012/02/11 00:38:35 jeremy Exp $ - */ -public class Evio2LcioTest extends TestCase -{ - // Control flags for debugging. Change these by hand and recompile to use. - static private final boolean DEBUG = false; // Set to true for a lot of debug prints. - static private final boolean BREAK_AFTER_SINGLE_EVENT = false; // Set to true to stop after one event. - static private final boolean WRITE_XML = false; // Set to true to write the EVIO file as XML before the conversion begins. - - // Converted LCIO file name. - static final private String outputFilePath = "./EvioCnvTest.slcio"; - - // URL pointing to test data from Maurik, which was generated by the GEMC simulation. - static final private String testFileUrl = "http://www.lcsim.org/test/hps/hps_test_data_2011_nov_18_1.evio"; - - /** - * Convert an EVIO file to LCIO and then read them back in parallel to check the output. - */ - public void testEvio2Lcio() - { - // Setup the file cache. - FileCache cache = null; - File testFile = null; - try - { - cache = new FileCache(new File(".")); - } - catch (IOException x) - { - throw new RuntimeException(x); - } - - // Get the test file to the local machine. - try - { - testFile = cache.getCachedFile(new URL(testFileUrl)); - } - catch (Exception x) - { - throw new RuntimeException(x); - } - - // Convert a test EVIO file to LCIO. - String[] args = new String[2]; - try - { - args[0] = testFile.getCanonicalPath(); - } - catch (IOException x) - { - throw new RuntimeException(x); - } - args[1] = outputFilePath; - (new Evio2LcioConverter()).main(args); - - if (DEBUG) - { - System.out.println("-----------------------"); - } - - // Test that the LCIO data matches the EVIO. - readBackLcio(new File(outputFilePath), testFile); - } - - /** - * Example class for generically converting EVIO data to LCIO. - */ - private static class Evio2LcioConverter - { - String bankInfoCollName = "BankInfo"; - String rawDataCollName = "RawDataBank"; - String relCollName = "BankInfoRelations"; - - public Evio2LcioConverter() - {} - - /** - * Would use this constructor to change the names of the LCIO collections that are created. (Not currently used.) - * @param bankInfoCollName - * @param rawDataCollName - * @param relCollName - */ - public Evio2LcioConverter(String bankInfoCollName, String rawDataCollName, String relCollName) - { - this.bankInfoCollName = bankInfoCollName; - this.rawDataCollName = rawDataCollName; - this.relCollName = relCollName; - } - - public void main(String[] args) - { - // Use dummy detector for LCSim conditions to avoid errors. - setDummyDetector("DUMMY"); - - // Check and set user arguments. - if (args.length < 2) - throw new RuntimeException("Not enough arguments."); - String inFilePath = args[0]; - String outputFilePath = args[1]; - - // Open the EVIO file for reading. - EvioFile evioFile = null; - try - { - evioFile = new EvioFile(inFilePath); - } - catch (IOException x) - { - throw new RuntimeException(x); - } - - // Write out test XML file with EVIO structure and data. - if (WRITE_XML) - { - System.out.println("Writing EVIO data to XML file ..."); - evioFile.toXMLFile("EvioTest.xml"); - } - - // Read the first EVIO event outside the processing loop. - EvioEvent evioEvent = null; - int nread = 0; - try - { - evioEvent = evioFile.parseNextEvent(); - ++nread; - } - catch (EvioException x) - { - throw new RuntimeException(x); - } - - if (DEBUG) - { - System.out.println("Read first EVIO event OKAY."); - } - - // Open new LCIOWriter for converted output. - LCIOWriter writer = null; - try - { - writer = new LCIOWriter(outputFilePath); - } - catch (IOException x) - { - throw new RuntimeException(x); - } - - if (DEBUG) - System.out.println(); - - // Loop over EVIO events. - while (evioEvent != null) - { - // Make a new LCIO event. - // FIXME EVIO event numbers all seem to be zero in the test file. - // FIXME Set run number. - BaseLCSimEvent lcioEvent = new BaseLCSimEvent(0, evioEvent.getEventNumber(), "DUMMY"); - - // GenericObject collection to fill with the raw data. - List<GenericObject> objectColl = new ArrayList<GenericObject>(); - - // LCRelation list to connect bank info with data. - List<LCRelation> bankRelColl = new ArrayList<LCRelation>(); - - // Map to represent bank tag and number info. - Map<String, GenericObject> bankIds = new HashMap<String, GenericObject>(); - - // Loop over the EVIO data banks. - Vector<BaseStructure> children = evioEvent.getChildren(); - for (BaseStructure topBank : children) - { - BaseStructureHeader topHeader = topBank.getHeader(); - if (topBank.getChildCount() > 0) - { - for (BaseStructure subBank : topBank.getChildren()) - { - BaseStructureHeader subHeader = subBank.getHeader(); - if (subBank.getChildCount() > 0) - { - for (BaseStructure dataBank : subBank.getChildren()) - { - BaseStructureHeader dataBankHeader = dataBank.getHeader(); - - // Make a concatenated ID for this bank. - String bankId = makeHeaderId(topHeader, subHeader, dataBankHeader); - - // Make a new GenericObject for this bank's info. - if (bankIds.get(bankId) == null) - { - int[] bankInfo = new int[6]; - bankInfo[0] = topHeader.getTag(); - bankInfo[1] = topHeader.getNumber(); - bankInfo[2] = subHeader.getTag(); - bankInfo[3] = subHeader.getNumber(); - bankInfo[4] = dataBankHeader.getTag(); - bankInfo[5] = dataBankHeader.getNumber(); - bankIds.put(bankId, new LcioDataBankInfo(bankInfo)); - } - - // Get the bank info. - GenericObject bankInfo = bankIds.get(bankId); - - //System.out.println("BankInfo: tag = " + bankInfo.getIntVal(0) + "; number = " + bankInfo.getIntVal(1)); - - if (DEBUG) - { - printOut(dataBank, System.out); - } - - // LCIO object to represent one bank of data. - LcioDataBank lcioData = null; - - // Float data. - if (dataBank.getFloatData() != null) - { - lcioData = new LcioDataBank(dataBank.getFloatData()); - } - // Int data. - if (dataBank.getIntData() != null) - { - lcioData = new LcioDataBank(dataBank.getIntData()); - } - // Double data. - if (dataBank.getDoubleData() != null) - { - lcioData = new LcioDataBank(dataBank.getDoubleData()); - } - - // Setup relation pointing from bank data to its info. - if (lcioData != null) - { - objectColl.add(lcioData); - LCRelation relation = new BaseLCRelation(lcioData, bankInfo); - bankRelColl.add(relation); - } - - if (DEBUG) - { - System.out.println(); - } - } - } - } - } - } - - // Put the data bank info into the LCIO event. - List<GenericObject> bankInfoList = new ArrayList<GenericObject>(bankIds.values()); - lcioEvent.put(bankInfoCollName, bankInfoList, GenericObject.class, 0); - - // Put the GenericObject object collection containing the block data into the event. - lcioEvent.put(rawDataCollName, objectColl, GenericObject.class, 0); - - // Put the relations into the event. - lcioEvent.put(relCollName, bankRelColl, LCRelation.class, 0); - - // Write out the LCIO event. - try - { - writer.write(lcioEvent); - } - catch (IOException x) - { - throw new RuntimeException(x); - } - - // Check if need to break on one event. - if (BREAK_AFTER_SINGLE_EVENT) - break; - - // Read next EVIO event. - try - { - evioEvent = evioFile.parseNextEvent(); - ++nread; - } - catch (EvioException x) - { - throw new RuntimeException(x); - } - } - - try - { - writer.close(); - } - catch (IOException x) - { - throw new RuntimeException(x); - } - - if (DEBUG) - { - System.out.println("Read " + nread + " EVIO events OKAY."); - } - } - } - - private static void readBackLcio(File lcioFile, File evioFile) - { - LCSimLoop loop = new LCSimLoop(); - loop.setDummyDetector("DUMMY"); - try - { - loop.setLCIORecordSource(lcioFile); - } - catch (IOException x) - { - throw new RuntimeException(x); - } - LcioCheckDriver checkDriver = new LcioCheckDriver(); - try - { - checkDriver.setEvioFilePath(evioFile.getCanonicalPath()); - } - catch (IOException x) - { - throw new RuntimeException(x); - } - loop.add(checkDriver); - try - { - loop.loop(-1, null); - } - catch (LoopException x) - { - throw new RuntimeException(x); - } - catch (IOException x) - { - throw new RuntimeException(x); - } - } - - /** - * Read back the LCIO event and check that the data matches the corresponding EVIO file. - */ - static private class LcioCheckDriver extends Driver - { - // Number of events read. - int nread = 0; - - // Settable path to EVIO file. - String evioFilePath = null; - - // The EVIO file for comparison. - EvioFile evioFile = null; - - // The current EVIO event. - EvioEvent evioEvent = null; - - public LcioCheckDriver() - {} - - /** - * Set the path of the EVIO file to be checked against. - * @param evioFilePath The path to the EVIO file. - */ - public void setEvioFilePath(String evioFilePath) - { - this.evioFilePath = evioFilePath; - } - - public void startOfData() - { - // Open the EVIO file for reading. - if (evioFilePath == null) - { - throw new RuntimeException("The EVIO file path was not set!"); - } - try - { - evioFile = new EvioFile(evioFilePath); - } - catch (IOException x) - { - throw new RuntimeException(x); - } - nread = 0; - } - - // FIXME The names of the LCIO collections are hard-coded and correspond to the defaults from the converter class. - public void process(EventHeader event) - { - if (DEBUG) - { - System.out.println("Processing event #" + event.getEventNumber()); - } - - // Read in next EVIO event which should match up with this LCIO event. - try - { - evioEvent = evioFile.parseNextEvent(); - } - catch (EvioException x) - { - throw new RuntimeException(x); - } - - // Make a list of EVIO banks to check. Only the leaf banks with data are added to the list. - Vector<BaseStructure> children = evioEvent.getChildren(); - List<BaseStructure> evioBanks = new ArrayList<BaseStructure>(); - for (BaseStructure topBank : children) - { - if (topBank.getChildCount() > 0) - { - for (BaseStructure subBank : topBank.getChildren()) - { - if (subBank.getChildCount() > 0) - { - for (BaseStructure dataBank : subBank.getChildren()) - { - // Only add leaf banks containing some data. - if (dataBank.getFloatData() != null || dataBank.getDoubleData() != null || dataBank.getIntData() != null) - { - evioBanks.add(dataBank); - } - } - } - } - } - } - - if (DEBUG) - { - System.out.println("EVIO event has " + evioBanks.size() + " data banks."); - } - - // Get the GenericObject collection containing the bank data. - List<GenericObject> lcioDataColl = event.get(GenericObject.class, "RawDataBank"); - - // Get the list of relations to bank info. - List<LCRelation> infoRel = event.get(LCRelation.class, "BankInfoRelations"); - - if (DEBUG) - { - System.out.println("LCIO collection RawDataBank has " + lcioDataColl.size() + " generic objects."); - } - - // Check that number of banks and GenericObjects is the same. - assertEquals("Number of EVIO data banks and LCIO GenericObjects does not match.", evioBanks.size(), lcioDataColl.size()); - - // Loop over the LCIO GenericObject collection. - int ibank = 0; - for (GenericObject lcioObj : lcioDataColl) - { - // Get the next bank from the EVIO file. - BaseStructure evioBank = evioBanks.get(ibank); - - if (DEBUG) - { - System.out.println("GenericObject ..."); - System.out.println(" ndoubles = " + lcioObj.getNDouble()); - System.out.println(" nfloats = " + lcioObj.getNFloat()); - System.out.println(" nints = " + lcioObj.getNInt()); - printOut(lcioObj, System.out); - } - - // Find the info relation for this data. - LCRelation rel = findRelation(infoRel, lcioObj); - - // Check that the relation exists. - TestCase.assertTrue("Could not find corresponding bank info for GenericObject.", rel != null); - - // Get the bank info. - GenericObject bankInfo = (GenericObject)rel.getTo(); - - if (DEBUG) - { - System.out.println("LCIO BankInfo ..."); - printOut(bankInfo, System.out); - System.out.println("EVIO Header; tag = " + evioBank.getHeader().getTag() + "; number = " + evioBank.getHeader().getNumber()); - } - - // Check that data bank info matches the EVIO file. This just checks the last tag and number. - assertEquals("Bank tag does not match EVIO.", evioBank.getHeader().getTag(), bankInfo.getIntVal(4)); - assertEquals("Bank number does not match EVIO.", evioBank.getHeader().getNumber(), bankInfo.getIntVal(5)); - - // Check double values. - if (lcioObj.getNDouble() != 0) - { - double[] evioDoubleData = evioBank.getDoubleData(); - int ndouble = evioDoubleData.length; - assertEquals("Number of doubles in GenericObject and EVIO bank don't match.", ndouble, lcioObj.getNDouble()); - for (int i=0; i<ndouble; i++) - { - // Seems that we need a big tolerance for this to pass! - assertEquals("EVIO double value does not match LCIO.", evioDoubleData[i], lcioObj.getDoubleVal(i), 1e-2); - } - } - - // Check int values. - if (lcioObj.getNInt() != 0) - { - int[] evioIntData = evioBank.getIntData(); - int nint = evioIntData.length; - assertEquals("Number of ints in GenericObject and EVIO bank don't match.", nint, lcioObj.getNInt()); - for (int i=0; i<nint; i++) - { - assertEquals("EVIO int value does not match LCIO.", evioIntData[i], lcioObj.getIntVal(i), 1e-2); - } - } - - // Check float values. - if (lcioObj.getNFloat() != 0) - { - float[] evioFloatData = evioBank.getFloatData(); - int nfloat = evioFloatData.length; - assertEquals("Number of floats in GenericObject and EVIO bank don't match.", nfloat, lcioObj.getNInt()); - for (int i=0; i<nfloat; i++) - { - assertEquals("EVIO float value does not match LCIO.", evioFloatData[i], lcioObj.getDoubleVal(i)); - } - } - - // Increment EVIO bank number. - ++ibank; - } - if (DEBUG) - { - System.out.println("Read " + event.getEventNumber() + " LCIO events OKAY."); - } - ++nread; - } - - public void endOfData() - { - if (DEBUG) - { - System.out.println(this.getClass().getSimpleName() + " read " + nread + " events OKAY."); - } - evioFile.close(); - } - } - - /** - * Represents the tag and number values from a set of EVIO banks. - */ - private static class LcioDataBankInfo implements GenericObject - { - private static final int BANK_INFO_SIZE = 6; - int[] bankInfo = new int[BANK_INFO_SIZE]; - - public LcioDataBankInfo(int[] bankInfo) - { - this.bankInfo = bankInfo; - } - - public int getNInt() - { - return BANK_INFO_SIZE; - } - - public int getNFloat() - { - return 0; - } - - public int getNDouble() - { - return 0; - } - - public int getIntVal(int index) - { - return bankInfo[index]; - } - - public float getFloatVal(int index) - { - return 0; - } - - public double getDoubleVal(int index) - { - return 0; - } - - public boolean isFixedSize() - { - return true; - } - } - - /** - * Implementation of GenericObject to represent an EVIO data bank in LCIO. - */ - private static class LcioDataBank implements GenericObject - { - float[] floatVals = null; - double[] doubleVals = null; - int[] intVals = null; - - int nint = 0; - int nfloat = 0; - int ndouble = 0; - - public LcioDataBank(float[] floatVals) - { - this.floatVals = new float[floatVals.length]; - arraycopy(floatVals, 0, this.floatVals, 0, floatVals.length); - nfloat = this.floatVals.length; - if (DEBUG) - { - System.out.println("Made LcioDataBank with " + nfloat + " floats."); - } - } - - public LcioDataBank(int[] intVals) - { - this.intVals = new int[intVals.length]; - arraycopy(intVals, 0, this.intVals, 0, intVals.length); - nint = this.intVals.length; - if (DEBUG) - { - System.out.println("Made LcioDataBank with " + nint + " ints."); - } - } - - public LcioDataBank(double[] doubleVals) - { - this.doubleVals = new double[doubleVals.length]; - arraycopy(doubleVals, 0, this.doubleVals, 0, doubleVals.length); - ndouble = this.doubleVals.length; - if (DEBUG) - { - System.out.println("Made LcioDataBank with " + ndouble + " doubles."); - } - } - - public int getNInt() - { - return nint; - } - - public int getNFloat() - { - return nfloat; - } - - public int getNDouble() - { - return ndouble; - } - - public int getIntVal(int index) - { - return intVals[index]; - } - - public float getFloatVal(int index) - { - return floatVals[index]; - } - - public double getDoubleVal(int index) - { - return doubleVals[index]; - } - - public boolean isFixedSize() - { - return false; - } - } - - /** - * Print out GenericObject values. - * @param lcioObj The GenericObject to be printed. - * @param ps The PrintStream to be used. - */ - private static void printOut(GenericObject lcioObj, PrintStream ps) - { - if (lcioObj.getNDouble() != 0) - { - int ndouble = lcioObj.getNDouble(); - ps.print("[ "); - for (int i=0; i<ndouble; i++) - { - ps.print(lcioObj.getDoubleVal(i) + " "); - } - ps.println("]"); - } - if (lcioObj.getNInt() != 0) - { - int nint = lcioObj.getNInt(); - ps.print("[ "); - for (int i=0; i<nint; i++) - { - ps.print(lcioObj.getIntVal(i) + " "); - } - ps.println("]"); - } - if (lcioObj.getNFloat() != 0) - { - int nfloat = lcioObj.getNFloat(); - ps.print("[ "); - for (int i=0; i<nfloat; i++) - { - ps.print(lcioObj.getFloatVal(i) + " "); - } - ps.println("]"); - } - } - - /** - * Makes an ID hash from three EVIO headers. - * @param h1 The first header. - * @param h2 The second header. - * @param h3 The third header. - * @return An ID that sequentially concatenates the tag and number values of the headers. - */ - private static String makeHeaderId(BaseStructureHeader h1, BaseStructureHeader h2, BaseStructureHeader h3) - { - return Integer.toString(h1.getTag()) + Integer.toString(h1.getNumber()) + - Integer.toString(h2.getTag()) + Integer.toString(h2.getNumber()) + - Integer.toString(h3.getTag()) + Integer.toString(h3.getNumber()); - } - - /** - * Print out the values from an EVIO data bank. - * @param dataBank The EVIO bank. - * @param ps The PrintStream to use. - */ - private static void printOut(BaseStructure dataBank, PrintStream ps) - { - if (dataBank.getFloatData() != null) - { - ps.println("Data bank has " + dataBank.getFloatData().length + " floats."); - float[] floatData = dataBank.getFloatData(); - ps.print("[ "); - for (int i=0; i<floatData.length; i++) - { - ps.print(floatData[i] + " "); - } - ps.println("]"); - } - if (dataBank.getIntData() != null) - { - ps.println("Data bank has " + dataBank.getIntData().length + " ints."); - int[] intData = dataBank.getIntData(); - ps.print("[ "); - for (int i=0; i<intData.length; i++) - { - ps.print(intData[i] + " "); - } - ps.println("]"); - } - if (dataBank.getDoubleData() != null) - { - ps.println("Data bank has " + dataBank.getDoubleData().length + " doubles."); - double[] doubleData = dataBank.getDoubleData(); - ps.print("[ "); - for (int i=0; i<doubleData.length; i++) - { - ps.print(doubleData[i] + " "); - } - ps.println("]"); - } - if (dataBank.getByteData() != null) - { - ps.println("Data bank has " + dataBank.getByteData().length + " bytes."); - byte[] byteData = dataBank.getByteData(); - ps.print("[ "); - for (int i = 0; i < byteData.length; i++) - { - ps.print(byteData[i] + " "); - } - ps.println("]"); - } - } - - /** - * An (inefficient) method for finding a relation by its 'from' object. - * @param relations The list of all relations to check. - * @param o1 The from object to find. - * @return The matching LCRelation or null if does not exist. - */ - private static LCRelation findRelation(List<LCRelation> relations, Object o1) - { - LCRelation fnd = null; - for (LCRelation rel : relations) - { - if (rel.getFrom() == o1) - { - fnd = rel; - break; - } - } - return fnd; - } - - /** - * Copied from conditions system to setup dummy detector. - * @param detectorName The name of the detector (shouldn't actually matter). - */ - private static void setDummyDetector(String detectorName) - { - ConditionsManager cond = ConditionsManager.defaultInstance(); - ConditionsReader dummyReader = ConditionsReader.createDummy(); - ((ConditionsManagerImplementation)cond).setConditionsReader(dummyReader, detectorName); - DummyDetector detector = new DummyDetector(detectorName); - cond.registerConditionsConverter(new DummyConditionsConverter(detector)); - } -}
\ No newline at end of file
Use REPLY-ALL to reply to list
To unsubscribe from the LCD-CVS list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCD-CVS&A=1