Commit in hps-java/src/main/java/org/lcsim/hps/evio on MAIN | |||
EvioFileProducer.java | +50 | -140 | 1.1 -> 1.2 |
EVIO producer that puts events onto ET Ring
diff -u -r1.1 -r1.2 --- EvioFileProducer.java 24 Feb 2012 06:38:24 -0000 1.1 +++ EvioFileProducer.java 9 Mar 2012 02:50:16 -0000 1.2 @@ -1,7 +1,6 @@
package org.lcsim.hps.evio; import java.io.File;
-import java.io.IOException;
import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List;
@@ -15,57 +14,36 @@
import org.jlab.coda.et.enums.Mode; import org.jlab.coda.jevio.EvioEvent; import org.jlab.coda.jevio.EvioException;
-import org.jlab.coda.jevio.EvioFile;
+import org.jlab.coda.jevio.EvioReader;
// This is copied and modified from Carl Timmer's EvioProducer class in et 12 org.jlab.coda.et.apps package. public class EvioFileProducer {
- //File file = null; - EvioFile evioFile = null; - ByteBuffer byteBuffer = null; - - // These are all used by the wrapper method doMain. - List<String> evioFilePaths = new ArrayList<String>(); - String etName = null, host = null;
+ EvioReader reader; + ByteBuffer byteBuffer; + String evioFileName; + String etName, host; +
int port = EtConstants.serverPort; int group = 1;
- int delay = 0; - int size = 32;
+ int size = 1000; // Default event size.
EvioFileProducer() {}
- - /* - EvioFileProducer(File file) { - this.file = file; - try { - this.evioFile = new EvioFile(file); - } catch (IOException x) { - throw new RuntimeException(x); - } - } - - public void setFile(File file) - { - this.file = file; - try { - this.evioFile = new EvioFile(file); - } catch (IOException x) { - throw new RuntimeException(x); - } - } - */ -
+
private EvioEvent nextEvent() { try {
- return evioFile.parseNextEvent();
+ return reader.parseNextEvent();
} catch (EvioException x) { throw new RuntimeException(x); } }
+ // Get the next byte buffer and also set the class variable byteBuffer to the same.
private ByteBuffer nextByteBuffer() { EvioEvent nextEvent = nextEvent();
+ if (nextEvent == null) + return null;
byteBuffer = ByteBuffer.allocate(nextEvent.getTotalBytes()); return byteBuffer; }
@@ -75,13 +53,11 @@
" [-d <delay in millisec>] [-g <group #>]\n\n" + " -f ET system's name\n" + " -s size in bytes for requested events\n" +
- " -p port number for a udp broadcast\n" + - " -d delay in millisec between getting and putting events\n" +
+ " -p port number for a udp broadcast\n" +
" -g group number of new events to get\n" + " -host host the ET system resides on (defaults to anywhere)\n\n" + " This consumer works by making a connection to the\n" + " ET system's tcp server port.\n");
- // Kill the program after this is called.
System.exit(1); }
@@ -90,14 +66,14 @@
} public static void main(String[] args) {
- (new EvioFileProducer()).doMain(args); // call wrapper method
+ (new EvioFileProducer()).doMain(args); // call wrapper method
} public void doMain(String[] args) { try { for (int i = 0; i < args.length; i++) { if (args[i].equalsIgnoreCase("-e")) {
- evioFilePaths.add(new String(args[++i]));
+ evioFileName = new String(args[++i]);
} else if (args[i].equalsIgnoreCase("-f")) { etName = args[++i];
@@ -149,62 +125,15 @@
usage(); return; }
- } - else if (args[i].equalsIgnoreCase("-d")) { - try { - delay = Integer.parseInt(args[++i]); - if (delay < 1) { - System.out.println("delay must be > 0."); - usage(); - return; - } - } - catch (NumberFormatException ex) { - System.out.println("Did not specify a proper delay."); - usage(); - return; - } - }
+ }
else { usage(); return; } }
- - List<File> evioFiles = new ArrayList<File>(); - System.out.println("Got EVIO input files ..."); - for (String evioFilePath : evioFilePaths) { - System.out.println(evioFilePath); - File evioFile = new File(evioFilePath); - if (!evioFile.exists()) { - System.out.println("The EVIO input file " + evioFilePath + " does not exist!"); - throw new RuntimeException("EVIO file " + evioFilePath + " was not found."); - } - else { - evioFiles.add(evioFile); - } - } - // TODO Handle more than one file. - if (evioFiles.size() > 1) { - System.out.println("Can't handle more than one input EVIO file right now!"); - throw new RuntimeException(); - } - - // Set EVIO input file. - this.evioFile = new EvioFile(evioFiles.get(0)); -
+
if (host == null) { host = EtConstants.hostAnywhere;
- /* - try { - host = InetAddress.getLocalHost().getHostName(); - } - catch (UnknownHostException ex) { - System.out.println("Host not specified and cannot find local host name."); - usage(); - return; - } - */
} if (etName == null) {
@@ -212,71 +141,52 @@
return; }
- // make a direct connection to ET system's tcp server
+ // Setup ET system with the command line config.
EtSystemOpenConfig config = new EtSystemOpenConfig(etName, host, port);
- - // create ET system object with verbose debugging output
EtSystem sys = new EtSystem(config, EtConstants.debugInfo); sys.open();
- - // get GRAND_CENTRAL station object
EtStation gc = sys.stationNameToObject("GRAND_CENTRAL");
- - // attach to grandcentral
EtAttachment att = sys.attach(gc); // array of events EtEvent[] mevs;
- int chunk = 1, count = 0, startingVal = 0; - long t1, t2, totalT = 0, totalCount = 0; - double rate, avgRate;
+ // Buffer for writing
ByteBuffer buf;
- - // keep track of time for event rate calculations - t1 = System.currentTimeMillis(); - - for (int i = 0; i < 50; i++) { - while (count < 30000L) { - // get array of new events - mevs = sys.newEvents(att, Mode.SLEEP, false, 0, chunk, size, group); - - if (delay > 0) Thread.sleep(delay); - - for (int j = 0; j < mevs.length; j++) { - buf = this.nextByteBuffer(); - //mevs[j].getDataBuffer().put(buf); - this.copyToEtEvent(mevs[j]); - int len = buf.position(); - mevs[j].setLength(len); - } - startingVal++; - - // put events back into ET system - sys.putEvents(att, mevs); - count += mevs.length; - - } - - // calculate the event rate - t2 = System.currentTimeMillis(); - rate = 1000.0 * ((double) count) / ((double) (t2 - t1)); - totalCount += count; - totalT += t2 - t1; - avgRate = 1000.0 * ((double) totalCount) / totalT; - System.out.println("rate = " + String.format("%.3g", rate) + - " Hz, avg = " + String.format("%.3g", avgRate)); - count = 0; - t1 = System.currentTimeMillis();
+ + // Open EVIO reader. + reader = new EvioReader(evioFileName); + + // Loop until event source is exhausted. + int eventCount = 0; + while (true) { + buf = nextByteBuffer(); + if (buf == null) { // Break if no more events. + System.out.println("end of events"); + break; + } + mevs = sys.newEvents( + att, // attachment + Mode.SLEEP, // wait mode + false, // create a buffer + 0, // delay + 1, // number of events + size, // size of event but overwritten later + group); // group number; default value is arbitrary + mevs[0].getDataBuffer().put(byteBuffer); // Set raw bytes of EtEvent. + mevs[0].setLength(buf.position()); // Set actual length from buffer position. + sys.putEvents(att, mevs); // Put events back into ET system. + System.out.println("put event of size " + mevs[0].getLength()); + ++eventCount;
}
- System.out.println("End of producing events, now close");
+ System.out.println("put " + eventCount + " events onto ET server"); + + // Cleanup.
sys.close();
- System.out.println("Closing EVIO file ..."); - evioFile.close();
+ reader.close();
}
- catch (Exception ex) { - System.out.println("Error using ET system as producer"); - ex.printStackTrace();
+ catch (Exception e) { + throw new RuntimeException(e);
} } }
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