hps-java/src/test/java/org/lcsim/hps/evio
diff -u -r1.3 -r1.4
--- RawTrackerHitToEvio4Converter.java 1 Mar 2012 22:49:05 -0000 1.3
+++ RawTrackerHitToEvio4Converter.java 9 Mar 2012 02:50:32 -0000 1.4
@@ -3,13 +3,15 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
-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.DataType;
+import org.jlab.coda.jevio.EventWriter;
+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.event.EventHeader;
import org.lcsim.event.RawTrackerHit;
import org.lcsim.util.Driver;
@@ -21,113 +23,106 @@
* and compares the two to make sure they match.
*
* @author Jeremy McCormick <[log in to unmask]>
- * @version $Id: RawTrackerHitToEvio4Converter.java,v 1.3 2012/03/01 22:49:05 jeremy Exp $
+ * @version $Id: RawTrackerHitToEvio4Converter.java,v 1.4 2012/03/09 02:50:32 jeremy Exp $
*/
public class RawTrackerHitToEvio4Converter extends Driver {
-
+
private final String format = "N(L,I,I)";
private String rawHitCollectionName = "RawTrackerHitMaker_RawTrackerHits";
private static String lcioFileName = "ap2.2gev050mevsel_SLIC-v2r11p1_geant4-v9r3p2_QGSP_BERT_HPS-Test-JLAB-v4pt0_trackRecon.slcio";
private static String lcioFileUrl = "http://www.lcsim.org/test/hps/" + lcioFileName;
-
- public static void testMe() throws IOException, LoopException {
+ private static String evioFileName = "compositeDataTest.evio";
+ private EventWriter writer;
+
+ public RawTrackerHitToEvio4Converter() {}
+
+ public static void testMe() throws Exception {
+
+ // Write EVIO v4 test events.
FileCache cache = new FileCache();
cache.setCacheDirectory(new File("."));
File file = cache.getCachedFile(new URL(lcioFileUrl));
LCSimLoop loop = new LCSimLoop();
loop.setLCIORecordSource(file);
loop.add(new RawTrackerHitToEvio4Converter());
- loop.loop(1);
+ loop.loop(-1);
+ System.out.println("processed " + loop.getTotalCountableSupplied() + " events");
+ loop.dispose();
+
+ // Read back events.
+ EvioReader reader = new EvioReader(evioFileName);
+ EvioEvent event = reader.parseNextEvent();
+ while (true) {
+ CompositeData cdata = event.getCompositeData();
+ List<Object> items = cdata.getItems();
+ int n = items.size();
+ for (int i=0; i<n; i+=3) {
+ Long id = (Long)items.get(i);
+ int time = (Integer)items.get(i+1);
+ int adcValue = (Integer)items.get(i+2);
+ System.out.println("id=0x"+Long.toHexString(id)+"; time="+time+"; adcValue="+adcValue);
+ }
+ if (reader.getNumEventsRemaining() == 0) {
+ break;
+ }
+ event = reader.parseNextEvent();
+ }
+ }
+
+ private CompositeData convertToCompositeData(List<RawTrackerHit> hits) {
+ int dataSize = hits.size();
+ CompositeData.Data data = new CompositeData.Data();
+ data.addN(dataSize);
+ for (RawTrackerHit hit : hits) {
+ data.addLong(hit.getCellID());
+ data.addInt(hit.getTime());
+ data.addInt(hit.getADCValues()[0]);
+ }
+ CompositeData cdata = null;
+ try {
+ cdata = new CompositeData(format, 1, data, 0 ,0);
+ }
+ catch (EvioException e) {
+ throw new RuntimeException(e);
+ }
+
+ return cdata;
}
-
- public RawTrackerHitToEvio4Converter() {}
- private static final int BANK_SIZE = 11;
+ public void setRawHitCollectionName(String rawHitCollectionName) {
+ this.rawHitCollectionName = rawHitCollectionName;
+ }
- // Convert a single RawTrackerHit into a CompositeData object. The real converter would put N hits into each object
- // but this method just puts one hit in each 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) = (6, 16, composite_flag, 1)
- bank[4] = 6; // This is the length of the data segment. This has to be 6 to include the N for repeating data structures.
- bank[5] = 6 << 16 | 0xF << 8 | 1;
-
- // N to repeat. This uses 1 just 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);
+ public void startOfData() {
try {
- return new CompositeData(byteArray, ByteOrder.BIG_ENDIAN);
- } catch (EvioException x) {
- throw new RuntimeException(x);
+ writer = new EventWriter(evioFileName);
+ } catch (EvioException e) {
+ throw new RuntimeException(e);
}
}
-
-
- public void setRawHitCollectionName(String rawHitCollectionName) {
- this.rawHitCollectionName = rawHitCollectionName;
+
+ public void endOfData() {
+ try {
+ writer.close();
+ } catch (EvioException e) {
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
- public void process(EventHeader event) {
+ public void process(EventHeader event) {
List<RawTrackerHit> hits = event.get(RawTrackerHit.class, rawHitCollectionName);
- CompositeData cData = null;
-
- for (RawTrackerHit hit : hits) {
-
- // Print out RawTrackerHit.
- System.out.println("Converting RawTrackerHit: ");
- System.out.println(" cellID = " + Long.toHexString(hit.getCellID()));
- System.out.println(" time = " + hit.getTime());
- System.out.println(" adc = " + hit.getADCValues()[0]);
-
- // Make composite data.
- cData = this.convertToCompositeData(hit);
-
- // Print out EVIO.
- System.out.println("Converted to EVIO: ");
- long cellID = cData.getLong();
- System.out.println(" cellID = " + Long.toHexString(cellID));
- int time = cData.getInt();
- System.out.println(" time = " + time);
- int adc = cData.getInt();
- System.out.println(" adc = " + adc);
-
- // Throw exceptions if data doesn't match...
- 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("----------------------------");
+ System.out.println("event " + event.getEventNumber() + " has " + hits.size() + " RawTrackerHits");
+ CompositeData cdata = this.convertToCompositeData(hits);
+ CompositeTester.printCompositeDataObject(cdata);
+ EvioEvent ev = new EvioEvent(0, DataType.COMPOSITE, 0);
+ try {
+ ev.appendCompositeData(cdata);
+ writer.writeEvent(ev);
}
- }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
}
\ No newline at end of file