hps-java/src/test/java/org/lcsim/hps/evio
diff -N HPSEvioReader.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ HPSEvioReader.java 31 Mar 2012 04:47:54 -0000 1.1
@@ -0,0 +1,423 @@
+package org.lcsim.hps.evio;
+
+import junit.framework.TestCase;
+
+import org.jlab.coda.jevio.EvioFile;
+import org.jlab.coda.jevio.IEvioListener;
+import org.jlab.coda.jevio.EvioEvent;
+import org.jlab.coda.jevio.IBlockHeader;
+import org.lcsim.event.RawTrackerHit;
+import org.lcsim.event.base.BaseRawTrackerHit;
+import org.lcsim.detector.IDetectorElement;
+import org.lcsim.event.SimTrackerHit;
+import java.nio.*;
+import org.jlab.coda.jevio.IEvioStructure;
+import org.jlab.coda.jevio.BaseStructureHeader;
+import org.jlab.coda.jevio.EvioException;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Iterator;
+import org.lcsim.util.lcio.LCIOWriter;
+import org.lcsim.event.base.BaseLCSimEvent;
+import org.lcsim.hps.recon.tracking.HPSMakeSVTCellID;
+
+import org.lcsim.hps.recon.tracking.HPSTrackerEvent;
+import org.lcsim.hps.recon.tracking.HPSTrackerSample;
+
+
+/**
+ * This is a test of using jevio routines to read EVIO files.
+ *
+ * @author homer
+ * @version $Id: HPSEvioReader.java,v 1.1 2012/03/31 04:47:54 homer Exp $
+ */
+public class HPSEvioReader extends TestCase {
+
+ private LCIOWriter writer;
+ BaseLCSimEvent newEvent;
+ int VERBOSE = 99;
+ /**
+ * The buffer representing a map of the input file.
+ */
+// private MappedByteBuffer trkByteBuffer;
+ int eventDataSizeBytes = 8000000;
+// byte rawdataInts[] = new byte[eventDataSizeBytes];
+ int rawdataInts[] = new int[eventDataSizeBytes];
+ int rawdataLength = 0;
+
+ public void testdoHPSEvioRead() throws IOException {
+ System.out.println("Test for EVIO.");
+
+ int UNKNOWN32 = (0x0);
+ // int UINT32 = (0x1);
+ int FLOAT32 = (0x2);
+ int CHARSTAR8 = (0x3);
+ int SHORT16 = (0x4);
+ int USHORT16 = (0x5);
+ int CHAR8 = (0x6);
+ int UCHAR8 = (0x7);
+ int DOUBLE64 = (0x8);
+ int LONG64 = (0x9);
+ int ULONG64 = (0xa);
+// final int INT32 = (0xb);
+ int TAGSEGMENT = (0xc);
+ int SEGMENT = (0xd);
+ int BANK = (0xe);
+ int ALSOBANK = (0x10);
+ int ALSOSEGMENT = (0x20);
+ int ALSOTAGSEGMENT = (0x40);
+ final int TRACKERTAG = (2);
+
+ // load the DAQ to LCSim cell maps
+//final MapDAQCellID mapcell = new MapDAQCellID();
+// mapcell.fillSVTCellMap();
+
+ // String fileName = "/mydat/neal/hps/cedExport/data/dvcs_5_500.ev";
+// String fileName = "/mydat/myhpstest/hps_test_data_2011_nov_18_1.evio";
+// String fileName = "/mydat/myhpstest/evio4_sample.dat";
+// String fileName = "/mydat/myhpstest/FADC_Data/hps_000244.dat";
+ String fileName = "/mydat/myhpstest/hps/evio/coda_test2.dat";
+
+ // Tracker data file name for testing
+
+//x String TrkFileName = "/mydat/myhpstest/00_baseline.bin";
+
+
+// ByteBuffer buf = ByteBuffer.allocate(1000 * 8192);
+
+ if (VERBOSE > 4) {
+ System.out.println("step1 - Trackertag = " + TRACKERTAG);
+ }
+ // parse bytes
+ class myListener implements IEvioListener {
+
+ public void startEventParse(EvioEvent evioEvent) {
+ if (VERBOSE > 4) {
+ System.out.println("step2");
+ }
+ }
+
+ public void endEventParse(EvioEvent evioEvent) {
+ if (VERBOSE > 4) {
+ System.out.println("step3 - endEventParse");
+ }
+ }
+
+ public void gotStructure(EvioEvent evioEvent, IEvioStructure structure) {
+
+ BaseStructureHeader header = structure.getHeader();
+
+// System.out.println("------------------");
+ if (VERBOSE > 0) {
+ System.out.println("" + structure);
+ }
+ System.out.println("Tag type = " + header.getTag());
+
+ switch (header.getTag()) {
+ case TRACKERTAG: //Tracker (will be TRACKERTAG)
+// IReadout ro;
+// if (header.getDataTypeName() != "INT32" ) {
+ if (header.getDataTypeName() != "UINT32") {
+ System.out.println("Non INT32 Tracker Data!!!");
+ break;
+ }
+// System.out.println("about to simulate having received tracker EVIO data");
+
+ List<RawTrackerHit> raw_hits = new ArrayList<RawTrackerHit>();
+ List<SimTrackerHit> sim_hits = new ArrayList<SimTrackerHit>();
+
+ HPSTrackerEvent trkev = new HPSTrackerEvent();
+ int trkdata[] = structure.getIntData();
+
+ int trklength = header.getLength();
+ IntBuffer trkIntBuffer = IntBuffer.wrap(trkdata);
+// for (int ii=0; ii<10; ii++) System.out.println("trkdata["+ii+"] = "+trkdata[ii]+"\t"+Integer.toString(trkdata[ii] & 0xffff, 16).toUpperCase());
+
+// ---- this is the code needed for real EVIO data (IF! IntBuffer is too slow.)
+// int dataStartMarker = 0;
+// while (dataStartMarker<trklength) {
+// int frameSize = trkdata[dataStartMarker];
+// for (int ii = 1; ii <= frameSize; ii++) {
+// rawdataInts[ii] = trkdata[dataStartMarker+ii];
+// }
+// dataStartMarker+=frameSize;
+// ----------------------------------------------------
+
+ while (trkIntBuffer.hasRemaining()) {
+ int frameSize = trkIntBuffer.get();
+ System.out.println("frameSize = " + frameSize + " remaining = " + trkIntBuffer.remaining());
+//x int frameSize = trkByteBuffer.getInt();
+//x while (trkByteBuffer.hasRemaining() && frameSize != 0) {
+ for (int ii = 0; ii < frameSize; ii++) {
+//x rawdataInts[ii] = trkByteBuffer.getInt();
+ rawdataInts[ii] = trkIntBuffer.get();
+ }
+ if (VERBOSE > 4) {
+ for (int ii = 0; ii < 20; ii++) {
+ // + rawdataInts[ii]
+ System.out.println("rawdataInts[" + ii + "] = " + "\t"
+ + Integer.toString((rawdataInts[ii] >> 16) & 0xffff, 16).toUpperCase() + ":"
+ + Integer.toString(rawdataInts[ii] & 0xffff, 16).toUpperCase());
+ }
+ }
+ trkev.setSize(frameSize);
+ trkev.setData(rawdataInts);
+ System.out.println("isTiFrame = " + trkev.isTiFrame());
+ if (trkev.isTiFrame()) {
+ System.out.println("Skipping because this is trigger data");
+ continue;
+ }
+ if (VERBOSE > 1) {
+ System.out.println("starting loop over samples");
+ }
+// for (int i = 0; i < header.getNumber(); i++) {
+ System.out.println("sample count = " + trkev.count());
+ for (int i = 0; i < trkev.count(); i++) {
+// if (VERBOSE > 4) {
+ System.out.println("sample# = " + i);
+// }
+ HPSTrackerSample trksamp = trkev.sample(i);
+// System.out.println("got trksamp, trksamp = " + trksamp);
+ long chanaddr = trksamp.channel();
+ if (VERBOSE > 3) {
+ System.out.println("got CellID");
+ }
+ int LCSimCellID_ = (int) chanaddr; // about to be replace with HPSMakeSVTCellID
+ if (VERBOSE > 3) {
+ System.out.println("got corresponding LCSim CellID = " + LCSimCellID_);
+ }
+
+ long chanapv = trksamp.apv();
+ if (VERBOSE > 3) {
+ System.out.println("got apv");
+ }
+ short chanadc[] = new short[6];
+ if (VERBOSE > 3) {
+ System.out.println("getting sample data");
+ }
+ HPSMakeSVTCellID tt = new HPSMakeSVTCellID();
+// long CellID = tt.HPSMakeSVTCellID(trksamp.hybrid(), trksamp.fpgaAddress(), trksamp.apv(), trksamp.channel());
+
+//old long CellID = HPSMakeSVTCellID.class.(trksamp.hybrid(), trksamp.fpgaAddress(), trksamp.apv(), trksamp.channel());
+ // int LCSimCellID_ = mapcell.SVTCellIDlookup(chanaddr);
+
+ for (int isamp = 0; isamp < 6; isamp++) {
+// System.out.println("sample = " + isamp);
+
+ chanadc[isamp] = (short) trksamp.value(isamp);
+ }
+ IDetectorElement detector_element = null;
+
+ if (VERBOSE > 2) {
+ System.out.println("Creating raw_hit with channel,apv = " + LCSimCellID_ + " , " + chanapv + " and adc = " + chanadc[0]);
+ }
+
+ int time = 0; // need to find source of this
+ RawTrackerHit raw_hit = new BaseRawTrackerHit(time, LCSimCellID_, chanadc, sim_hits, detector_element);
+ // Put hits onto readout and hit list
+// ro.addHit(raw_hit);
+ raw_hits.add(raw_hit);
+ }
+ }
+ if (VERBOSE > 1) {
+ System.out.println("Show the hits we created ...");
+ Iterator<RawTrackerHit> rhiter = raw_hits.iterator();
+
+ while (rhiter.hasNext()) {
+ RawTrackerHit rh = rhiter.next();
+ System.out.println("this hit was created: cellID=" + rh.getCellID() + " adc[0]=" + (rh.getADCValues())[0]);
+ }
+ }
+ switch (header.getDataType()) {
+ case FLOAT32:
+ if (VERBOSE > 4) {
+ System.out.println(" FLOAT VALS");
+ }
+ float floatdata[] = structure.getFloatData();
+ for (float f : floatdata) {
+ if (VERBOSE > 4) {
+ System.out.println(" " + f);
+ }
+ }
+ break;
+
+ case DOUBLE64:
+ if (VERBOSE > 4) {
+ System.out.println(" DOUBLE VALS");
+ }
+ double doubledata[] = structure.getDoubleData();
+ for (double d : doubledata) {
+ if (VERBOSE > 4) {
+ System.out.println(" " + d);
+ }
+ }
+ break;
+
+ case SHORT16:
+ if (VERBOSE > 4) {
+ System.out.println(" SHORT VALS");
+ }
+ short shortdata[] = structure.getShortData();
+ for (short i : shortdata) {
+ if (VERBOSE > 4) {
+ System.out.println(" 0x" + Integer.toHexString(i));
+ }
+ }
+ break;
+
+ case INT32:
+ case UINT32:
+ if (VERBOSE > 4) {
+ System.out.println(" INT VALS");
+ }
+ int intdata[] = structure.getIntData();
+ for (int i : intdata) {
+ if (VERBOSE > 4) {
+ System.out.println(" 0x" + Integer.toHexString(i));
+ }
+ }
+ break;
+
+ case LONG64:
+ if (VERBOSE > 4) {
+ System.out.println(" LONG VALS");
+ }
+ long longdata[] = structure.getLongData();
+ for (long i : longdata) {
+ if (VERBOSE > 4) {
+ System.out.println(" 0x" + Long.toHexString(i));
+ }
+ }
+ break;
+
+ case CHAR8:
+ case UCHAR8:
+ if (VERBOSE > 4) {
+ System.out.println(" BYTE VALS");
+ }
+ byte bytedata[] = structure.getByteData();
+ for (byte i : bytedata) {
+ if (VERBOSE > 4) {
+ System.out.println(" " + i);
+ }
+ }
+ break;
+
+ case CHARSTAR8:
+ System.out.println(" STRING VALS");
+ String stringdata[] = structure.getStringData();
+ for (String str : stringdata) {
+ System.out.println(" " + str);
+ }
+ break;
+ }
+ }
+ }
+
+ }
+
+ if (VERBOSE > 4) {
+ System.out.println("step7");
+ }
+
+ // jump past the block header
+// buf.position(4 * 8);
+// ByteBuffer buf2 = buf.slice();
+
+
+ if (VERBOSE > 4) {
+ System.out.println("step8");
+ }
+// ByteParser parser = new ByteParser();
+// buf2.rewind();
+ EvioEvent event = null;
+
+ if (VERBOSE > 4) {
+ System.out.println("step8.1");
+ }
+ /*
+ * try {
+ System.out.println("step8.2");
+ event = parser.parseEvent(buf2);
+ System.out.println("step8.3");
+ System.out.println("Event = " + event.toString());
+ } catch (EvioException e) {
+ e.printStackTrace();
+ }
+ */
+
+// System.out.println("step9");
+ // open the EVIO file
+ File fileIn = new File(fileName);
+//x File trkIn = new File(TrkFileName);
+
+ System.out.println(
+ "read ev file: " + fileName + " size: " + fileIn.length());
+//x System.out.println(
+//x "read trk file: " + TrkFileName + " size: " + trkIn.length());
+// FileInputStream fileInputStream = new FileInputStream(trkIn);
+// FileChannel inputChannel = fileInputStream.getChannel();
+
+// long sz = inputChannel.size();
+// trkByteBuffer = inputChannel.map(FileChannel.MapMode.READ_ONLY, 0L, sz);
+
+// inputChannel.close(); // this object is no longer needed
+ // set buffer to beginning
+// trkByteBuffer.position(0);
+
+ // int offset = 0;
+// rawdataLength = trkByteBuffer.remaining() / 4;
+// if (VERBOSE > 4) {
+// System.out.println("Number of Ints to read is " + rawdataLength);
+// }
+// ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN;
+// trkByteBuffer.order(byteOrder);
+
+// int[] header = new int[8000000];
+// for (int i = 0; i < 8000000; i++) {
+// header[i] = trkByteBuffer.getInt();
+// }
+
+
+
+ try {
+ EvioFile evioFile = new EvioFile(fileName);
+
+// EventParser parser = new EventParser();
+// parser.addEvioListener(new myListener());
+// EventParser ep = new EventParser();
+ // ep.addEvioListener(new myListener());
+ evioFile.getParser().addEvioListener(new myListener());
+ int stat = 1;
+ while (stat != 0) {
+ try {
+ EvioEvent ev = evioFile.nextEvent();
+ if (ev == null) {
+ stat = 0;
+ }
+ evioFile.parseEvent(ev);
+ System.out.println("reading event");
+// int[] buftt = ev.getEvtArray();
+ System.out.println("events remaining =" + evioFile.getNumEventsRemaining());
+ IBlockHeader bh = evioFile.getCurrentBlockHeader();
+ System.out.println("nextEvent: BLOCK HEADER :\n" + bh.toString());
+ } catch (EvioException e) {
+ System.out.println(e);
+ }
+
+ // MappedByteBuffer buffer = evioFile.getMappedByteBuffer();
+ if (VERBOSE > 4) {
+ System.out.println("step10");
+ }
+ }
+ } catch (IOException e) {
+ System.out.println(e);
+ }
+ // read events until eof reached
+//x inputChannel.close();
+
+ }
+}