3 added + 3 modified, total 6 files
lcio/src/java/hep/lcio/util
diff -N Split.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Split.java 28 Apr 2006 21:33:51 -0000 1.1
@@ -0,0 +1,231 @@
+package hep.lcio.util;
+
+import hep.io.xdr.XDRInputStream;
+import hep.io.xdr.XDROutputStream;
+import hep.lcio.event.LCIO;
+
+import java.io.BufferedInputStream;
+import java.io.EOFException;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Split utility that takes an LCIO file and breaks it into
+ * chunks of given size.
+ *
+ * @author Jeremy McCormick
+ * @version $Id: Split.java,v 1.1 2006/04/28 21:33:51 jeremy Exp $
+ */
+public class Split
+{
+ /**
+ * Static utility function that splits infile into chunks with nevents.
+ * @param infile A File object pointing to an input LCIO file.
+ * @param nevents The number of events in each new output file.
+ * @throws Exception
+ */
+ public static List split(File infile, int nevents) throws Exception
+ {
+ // List of files created.
+ List outfilelist = new ArrayList();
+
+ // Base name from input file name.
+ String basename = infile.getAbsolutePath().replace(".slcio", "");
+
+ // XDR input stream from file that is being split up.
+ XDRInputStream xi = null;
+ try
+ {
+ // Create the XDR input stream.
+ xi = new XDRInputStream(new FileInputStream(infile.getAbsolutePath()));
+ }
+ catch (FileNotFoundException x)
+ {
+ // The input file does not exist.
+ throw new RuntimeException("File " + infile.getAbsolutePath() + " does not exist.", x);
+ }
+
+ // Output file sequence number.
+ int filenum = 0;
+
+ // Number of records processed.
+ int nrecs = 0;
+
+ // Number of records read with name 'LCEvent'.
+ // Reset when next file is started.
+ int neventsread = 0;
+
+ // Flag to indicate processing should end.
+ boolean done = false;
+
+ // file loop
+ String outfile;
+ for (;;)
+ {
+ // Next output file name.
+ outfile = basename + "-" + String.valueOf(filenum) + "-" + nevents + ".slcio";
+
+ // The output stream for this file.
+ XDROutputStream xo;
+
+ try
+ {
+ // FIXME: Using BufferedOutputStream seems to corrupt the output files.
+ //xo = new XDROutputStream(new BufferedOutputStream(new FileOutputStream(outfile)));
+ xo = new XDROutputStream(new FileOutputStream(outfile));
+ }
+ catch (FileNotFoundException x)
+ {
+ throw new RuntimeException("Error creating output file " + outfile);
+ }
+
+ // The record read loop.
+ for (;;)
+ {
+ // Create a new WritableSIORecord that can do stream read, write.
+ WritableSIORecord myrec = new WritableSIORecord();
+
+ // Read in a record.
+ try
+ {
+ myrec.read(xi);
+ }
+ catch (EOFException x)
+ {
+ done = true;
+ break;
+ }
+ catch (IOException x)
+ {
+ throw new RuntimeException(x);
+ }
+
+ // Write out the record.
+ if (!done)
+ {
+ try
+ {
+ myrec.write(xo);
+ xo.flush();
+
+ String recname = myrec.getName();
+ if (recname.compareTo(LCIO.LCEVENT) == 0)
+ {
+ // Increment number of events.
+ ++neventsread;
+ }
+
+ // Increment number of records.
+ ++nrecs;
+ }
+ catch (IOException x)
+ {
+ throw new RuntimeException("Error writing record " + (nevents - 1), x);
+ }
+ }
+
+ if (neventsread == nevents || done)
+ {
+ break;
+ }
+ }
+
+ // Add this file to the output file list.
+ outfilelist.add(new File(outfile));
+
+ // Close the output stream.
+ try
+ {
+ xo.flush();
+ xo.close();
+ xo = null;
+ }
+ catch (IOException x)
+ {
+ System.err.println("Error closing output stream.\n" + x.getMessage());
+ }
+
+ // If done flag is set, then stop processing.
+ if (done)
+ break;
+
+ // Increment file sequence number.
+ filenum += 1;
+
+ // Reset event counter.
+ neventsread = 0;
+
+ // Reset record counter.
+ nrecs = 0;
+ }
+
+ // Close the input stream.
+ try
+ {
+ xi.close();
+ xi = null;
+ }
+ catch (IOException x)
+ {
+ System.err.println("Error closing input stream.\n" + x.getMessage());
+ }
+
+ // Delete the last file as it doesn't have any events.
+ outfilelist.remove(outfilelist.size() - 1);
+ (new File(outfile)).delete();
+
+ // Return a list of files created.
+ return outfilelist;
+ }
+
+ public static void printSplitSummary(List outfilelist, PrintStream ps) throws Exception
+ {
+ ps.println();
+ ps.println("Split Summary");
+ ps.println();
+
+ // Read back events.
+ for (Iterator iter = outfilelist.iterator(); iter.hasNext();)
+ {
+ File f = (File) iter.next();
+
+ ps.println(f.getAbsoluteFile());
+
+ XDRInputStream xi = new XDRInputStream(new BufferedInputStream(new FileInputStream(f.getAbsolutePath())));
+
+ int nrec = 0;
+ int nevt = 0;
+
+ for (;;)
+ {
+ WritableSIORecord rec = new WritableSIORecord();
+
+ try
+ {
+ rec.read(xi);
+ ++nrec;
+ if (rec.getName().compareTo("LCEvent") == 0)
+ {
+ ++nevt;
+ }
+ }
+ catch (EOFException x)
+ {
+ ps.println(nrec + " records");
+ ps.println(nevt + " events");
+ xi.close();
+ break;
+ }
+ }
+
+ ps.println();
+ }
+ }
+}
lcio/src/java/hep/lcio/util
diff -N SplitCommandHandler.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SplitCommandHandler.java 28 Apr 2006 21:33:51 -0000 1.1
@@ -0,0 +1,87 @@
+package hep.lcio.util;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.Parser;
+import org.apache.commons.cli.PosixParser;
+
+/**
+ * Command-line handler for the split utility.
+ *
+ * @author Jeremy McCormick
+ * @version $Id: SplitCommandHandler.java,v 1.1 2006/04/28 21:33:51 jeremy Exp $
+ */
+public class SplitCommandHandler extends CommandHandler
+{
+ Options options;
+ Parser parser = new PosixParser();
+ File infile;
+ int nevents;
+
+ SplitCommandHandler()
+ {
+ super("split","Split a single LCIO file into multiple files.");
+
+ // Setup split command options.
+ options = createSplitOptions();
+ }
+
+ /**
+ * Create the options for the split command.
+ * @return An Apache CLI Options object with split options.
+ */
+ private static Options createSplitOptions()
+ {
+ Options options = new Options();
+
+ Option opt = new Option("i", true, "The input LCIO file.");
+ opt.setArgs(1);
+ options.addOption(opt);
+
+ opt = new Option("n", true, "The number of events to split.");
+ opt.setArgs(1);
+ options.addOption(opt);
+
+ return options;
+ }
+
+ /**
+ * Parse the argv for split command options.
+ * @param argv The raw command array from command line.
+ * @see hep.lcio.util.CommandHandler#parse(java.lang.String[])
+ */
+ public void parse(String[] argv) throws Exception
+ {
+ CommandLine cl = parser.parse(options, argv);
+
+ if (!cl.hasOption("i") || !cl.hasOption("n"))
+ {
+ printUsage(options, true);
+ }
+
+ String inputpath = null;
+ if (cl.hasOption("i"))
+ {
+ inputpath = cl.getOptionValue("i");
+ infile = new File(inputpath);
+ }
+
+ if (cl.hasOption("n"))
+ {
+ nevents = Integer.parseInt(cl.getOptionValue("n"));
+ }
+ }
+
+ /**
+ * Execute the split command with current options.
+ */
+ public void execute() throws Exception
+ {
+ List flist = Split.split(infile, nevents);
+ Split.printSplitSummary(flist, System.out);
+ }
+}
lcio/src/java/hep/lcio/util
diff -N WritableSIORecord.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WritableSIORecord.java 28 Apr 2006 21:33:51 -0000 1.1
@@ -0,0 +1,70 @@
+package hep.lcio.util;
+
+import hep.io.xdr.*;
+import java.io.*;
+
+class WritableSIORecord
+{
+ private int headerLength;
+ private int frame;
+ private int control;
+ private int compressedLength;
+ private int uncompressedLength;
+ private int nameLength;
+ private String name;
+ private byte[] data;
+
+ String getName()
+ {
+ return name;
+ }
+
+ void read(XDRInputStream xdr) throws IOException
+ {
+ // Now read the record header for the next record
+ long startPos = xdr.getBytesRead();
+ headerLength = xdr.readInt();
+ frame = xdr.readInt();
+ if (frame != 0xabadcafe)
+ throw new IOException("Framing error");
+ control = xdr.readInt();
+ if ((control & 0xfffe) != 0)
+ throw new IOException("Bad control word");
+
+ boolean compressed = (control & 1) != 0;
+ compressedLength = xdr.readInt();
+ uncompressedLength = xdr.readInt();
+
+ // Sanity check
+ if (!compressed && compressedLength != uncompressedLength)
+ throw new IOException("Data is insane");
+
+ nameLength = xdr.readInt();
+ if (nameLength > headerLength - xdr.getBytesRead() + startPos)
+ throw new IOException("Record name is insane");
+ name = xdr.readString(nameLength);
+
+ // read the (compressed) record
+
+ if (data == null || compressedLength > data.length)
+ data = new byte[compressedLength];
+ int rc = xdr.read(data, 0, compressedLength);
+
+ xdr.pad();
+ if (rc != compressedLength)
+ throw new IOException("Read error");
+ }
+
+ void write(XDROutputStream out) throws IOException
+ {
+ out.writeInt(headerLength);
+ out.writeInt(frame);
+ out.writeInt(control);
+ out.writeInt(compressedLength);
+ out.writeInt(uncompressedLength);
+ out.writeInt(nameLength);
+ out.writeStringChars(name);
+ out.write(data, 0, compressedLength);
+ out.pad();
+ }
+}
\ No newline at end of file
lcio/src/java/hep/lcio/util
diff -u -r1.1 -r1.2
--- CommandHandler.java 24 Apr 2006 22:08:34 -0000 1.1
+++ CommandHandler.java 28 Apr 2006 21:33:51 -0000 1.2
@@ -1,12 +1,15 @@
package hep.lcio.util;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+
/**
*
* A CommandHandler handles the command-line options for a
* single command in the CommandLineTool.
*
* @author jeremym
- * @version $Id: CommandHandler.java,v 1.1 2006/04/24 22:08:34 jeremy Exp $
+ * @version $Id: CommandHandler.java,v 1.2 2006/04/28 21:33:51 jeremy Exp $
*/
public abstract class CommandHandler
@@ -53,6 +56,22 @@
return this.description;
}
+ /**
+ * Print usage and (optionally) exit the program.
+ * @param doExit Whether or not to exit after printing usage.
+ */
+ public void printUsage(Options options, boolean doExit)
+ {
+ HelpFormatter help = new HelpFormatter();
+
+ help.printHelp(getName(), options);
+
+ if (doExit)
+ {
+ System.exit(0);
+ }
+ }
+
/**
* Parse this command's options from the command line.
* @param argv Raw options array following after the command string.
lcio/src/java/hep/lcio/util
diff -u -r1.3 -r1.4
--- CommandLineTool.java 26 Apr 2006 19:37:05 -0000 1.3
+++ CommandLineTool.java 28 Apr 2006 21:33:51 -0000 1.4
@@ -29,7 +29,7 @@
* FIXME: Implement all of the above commands.
*
* @author jeremym
- * @version $Id: CommandLineTool.java,v 1.3 2006/04/26 19:37:05 jeremy Exp $
+ * @version $Id: CommandLineTool.java,v 1.4 2006/04/28 21:33:51 jeremy Exp $
*/
public class CommandLineTool
{
@@ -90,7 +90,7 @@
// addCommandHandler("header", HeaderScan);
addCommandHandler(new MergeCommandHandler());
// addCommandHandler("siodump", SioDump);
- // addCommandHandler("split", Split);
+ addCommandHandler(new SplitCommandHandler());
}
/**
lcio/src/java/hep/lcio/util
diff -u -r1.3 -r1.4
--- MergeCommandHandler.java 26 Apr 2006 19:37:05 -0000 1.3
+++ MergeCommandHandler.java 28 Apr 2006 21:33:51 -0000 1.4
@@ -20,12 +20,12 @@
* passes the results to a method from MergeUtil.
*
* @author jeremym
- * @version $Id: MergeCommandHandler.java,v 1.3 2006/04/26 19:37:05 jeremy Exp $
+ * @version $Id: MergeCommandHandler.java,v 1.4 2006/04/28 21:33:51 jeremy Exp $
*/
public class MergeCommandHandler extends CommandHandler
{
Parser parser = new PosixParser();
- Options options = new Options();
+ Options options;
File outfile;
File[] infiles;
int maxevents = Integer.MAX_VALUE;
@@ -41,8 +41,8 @@
{
// Call CommandHandler ctor.
super("merge", "Merge LCIO events together.");
-
- // Setup options for the merge command.
+
+ // Setup merge command options.
options = createMergeOptions();
}
@@ -50,7 +50,7 @@
* Creates the CLI options for the merge command.
* @return Options for the merge command.
*/
- private Options createMergeOptions()
+ private static Options createMergeOptions()
{
Options options = new Options();
CVSspam 0.2.8