Author: [log in to unmask] Date: Thu Jul 23 15:33:07 2015 New Revision: 3276 Log: add some options Modified: java/trunk/evio/src/main/java/org/hps/evio/BasicEvioFileReader.java Modified: java/trunk/evio/src/main/java/org/hps/evio/BasicEvioFileReader.java ============================================================================= --- java/trunk/evio/src/main/java/org/hps/evio/BasicEvioFileReader.java (original) +++ java/trunk/evio/src/main/java/org/hps/evio/BasicEvioFileReader.java Thu Jul 23 15:33:07 2015 @@ -1,7 +1,15 @@ package org.hps.evio; import java.io.File; - +import java.util.Date; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; +import org.hps.record.evio.EvioEventUtilities; import org.jlab.coda.jevio.BaseStructure; import org.jlab.coda.jevio.CompositeData; import org.jlab.coda.jevio.DataType; @@ -11,43 +19,93 @@ public class BasicEvioFileReader { static public void main(String[] args) { - if (args.length < 1) { + + Options options = new Options(); + options.addOption(new Option("q", false, "quiet - don't print event contents")); + options.addOption(new Option("c", false, "print control events")); + + // Parse the command line options. + if (args.length == 0) { + printUsage(options); + } + final CommandLineParser parser = new PosixParser(); + CommandLine cl = null; + try { + cl = parser.parse(options, args); + } catch (final ParseException e) { + throw new RuntimeException("Problem parsing command line options.", e); + } + + if (cl.getArgs().length < 1) { throw new RuntimeException("Missing EVIO file name."); } - String evioFileName = args[0]; - File evioFile = new File(evioFileName); - if (!evioFile.exists()) { - throw new RuntimeException("File " + evioFileName + " does not exist."); + + boolean quiet = cl.hasOption("q"); + boolean printControlEvents = cl.hasOption("c"); + +// String evioFileName = args[0]; + for (String evioFileName : cl.getArgs()) { + File evioFile = new File(evioFileName); + if (!evioFile.exists()) { + throw new RuntimeException("File " + evioFileName + " does not exist."); + } + System.out.println("Opened file " + evioFileName); + try { + org.jlab.coda.jevio.EvioReader reader = new org.jlab.coda.jevio.EvioReader(evioFile, true, false); + int eventN = 1; + int badEvents = 0; + fileLoop: + while (true) { + if (!quiet) { + System.out.println("Reading event " + eventN); + } + try { + EvioEvent event = reader.nextEvent(); + if (event == null) { + break fileLoop; + } + reader.parseEvent(event); + //printBytes(event.getRawBytes()); // DEBUG + if (!quiet) { + System.out.println("Successfully read event " + eventN);// + " which contains " + event.getTotalBytes() + " bytes."); + printBank(event, ""); + } + if (printControlEvents && EvioEventUtilities.isControlEvent(event) && !EvioEventUtilities.isEpicsEvent(event)) { + int[] controlEventData = EvioEventUtilities.getControlEventData(event); + if (controlEventData == null) { + printBank(event, ""); + } + System.out.print(event.getHeader().getTag() + "\t"); + + for (int i = 0; i < controlEventData.length; i++) { + System.out.print(controlEventData[i] + "\t"); + } + Date timestamp = new Date(controlEventData[0] * 1000L); + System.out.println(timestamp); + } + } catch (Exception e) { + System.out.println("Caught Exception processing event " + eventN + " which was..."); + e.printStackTrace(); + ++badEvents; + } + ++eventN; + if (!quiet) { + System.out.println("-------"); + } + } + System.out.println("There were " + badEvents + " bad events out of " + eventN + " total."); + reader.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } } - try { - org.jlab.coda.jevio.EvioReader reader = new org.jlab.coda.jevio.EvioReader(evioFile,true,false); - int eventN = 1; - int badEvents = 0; - fileLoop: - while (true) { - System.out.println("Reading event " + eventN); - try { - EvioEvent event = reader.nextEvent(); - if (event == null) { - break fileLoop; - } - reader.parseEvent(event); - //printBytes(event.getRawBytes()); // DEBUG - System.out.println("Successfully read event " + eventN);// + " which contains " + event.getTotalBytes() + " bytes."); - printBank(event, ""); - } catch (Exception e) { - System.out.println("Caught Exception processing event " + eventN + " which was..."); - e.printStackTrace(); - ++badEvents; - } - ++eventN; - System.out.println("-------"); - } - System.out.println("There were " + badEvents + " bad events out of " + eventN + " total."); - reader.close(); - } catch (Exception e) { - throw new RuntimeException(e); - } + } + + private static void printUsage(Options options) { + System.out.println("BasicEvioFileReader [options] [evioFiles]"); + final HelpFormatter help = new HelpFormatter(); + help.printHelp(" ", options); + System.exit(1); } private static void printBank(BaseStructure bank, String indent) throws EvioException {