2 added + 5 modified, total 7 files
lcio/src/java/hep/lcio/util
diff -N Concatenate.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Concatenate.java 28 Apr 2006 23:38:49 -0000 1.1
@@ -0,0 +1,73 @@
+package hep.lcio.util;
+
+import hep.io.xdr.XDRInputStream;
+import hep.io.xdr.XDROutputStream;
+
+import java.io.EOFException;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Concatenation utility to combined many LCIO files into one.
+ * @author Jeremy McCormick
+ * @version $Id: Concatenate.java,v 1.1 2006/04/28 23:38:49 jeremy Exp $
+ */
+public class Concatenate
+{
+ /**
+ * Method to concatenate list of LCIO files to outfile.
+ * @param files A list of input files.
+ * @param outfile The output file.
+ * @throws Exception
+ */
+ public static void concatenate(List files, File outfile) throws Exception
+ {
+ if (files.size() == 0)
+ {
+ System.err.println("concatenate - Input file list is empty!");
+ return;
+ }
+
+ // Create the output stream.
+ XDROutputStream xo =
+ new XDROutputStream(new FileOutputStream(outfile.getAbsolutePath()));
+
+ // Loop over input files.
+ for (Iterator iter = files.iterator(); iter.hasNext();)
+ {
+ // Get the next file.
+ File f = (File)iter.next();
+
+ // Create the XDR input stream for this file.
+ XDRInputStream xi =
+ new XDRInputStream(new FileInputStream(f.getAbsolutePath()));
+
+ // Record loop.
+ for (;;)
+ {
+ WritableSIORecord rec = new WritableSIORecord();
+
+ // Read in the record.
+ try {
+ rec.read(xi);
+ }
+ catch (EOFException x)
+ {
+ break;
+ }
+
+ // Write out the record.
+ rec.write(xo);
+ }
+
+ // Close this input stream.
+ xi.close();
+ }
+
+ // Close the output stream.
+ xo.close();
+ }
+}
lcio/src/java/hep/lcio/util
diff -N ConcatenateCommandHandler.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ConcatenateCommandHandler.java 28 Apr 2006 23:38:49 -0000 1.1
@@ -0,0 +1,115 @@
+package hep.lcio.util;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+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 handling for concat command.
+ *
+ * @author jeremym
+ * @version $Id: ConcatenateCommandHandler.java,v 1.1 2006/04/28 23:38:49 jeremy Exp $
+ */
+public class ConcatenateCommandHandler extends CommandHandler
+{
+ List infiles = new ArrayList();
+ File outfile;
+ String fname = "concat_events.slcio";
+
+ ConcatenateCommandHandler()
+ {
+ super("concat","Concatenate LCIO files together.");
+
+ // Create concatenate options.
+ options = createConcatenateOptions();
+ }
+
+ /**
+ * Create Options for the concat command.
+ * @return An apache CLI Options object with concat options.
+ */
+ private static Options createConcatenateOptions()
+ {
+ Options options = new Options();
+
+ Option opt = new Option("i",false,"Add an input file.");
+ opt.setArgs(1);
+ options.addOption(opt);
+
+ opt = new Option("o",false,"Set the name of the output file.");
+ opt.setArgs(1);
+ options.addOption(opt);
+
+ opt = new Option("f", false,"List of input files, 1 per line.");
+ opt.setArgs(1);
+ options.addOption(opt);
+
+ return options;
+ }
+
+ /**
+ * Execute the concat command with current options.
+ */
+ public void execute() throws Exception
+ {
+ Concatenate.concatenate(infiles, outfile);
+ System.out.println("Concatenated " + infiles.size() + " files to " + outfile.getAbsolutePath());
+ }
+
+ /**
+ * Parse the arguments for concat options.
+ * @param argv The raw command-line options.
+ */
+ public void parse(String[] argv) throws Exception
+ {
+ Parser parser = new PosixParser();
+ CommandLine cl = parser.parse(options, argv);
+
+ // One of '-i' or '-f' is required.
+ if (!cl.hasOption("i") && !cl.hasOption("f"))
+ {
+ printUsage(true);
+ }
+
+ // Add individual files.
+ if (cl.hasOption("i"))
+ {
+ String[] infilepaths = cl.getOptionValues("i");
+ for (int i=0; i<infilepaths.length; i++)
+ {
+ infiles.add(new File(infilepaths[i]));
+ }
+ }
+
+ // Add list of files.
+ if (cl.hasOption("f"))
+ {
+ String[] infilelists = cl.getOptionValues("f");
+ for (int i=0; i<infilelists.length; i++)
+ {
+ List lines = FileUtil.loadFile(infilelists[i]);
+ for (Iterator iter = lines.iterator(); iter.hasNext();)
+ {
+ String line = (String)iter.next();
+ infiles.add(new File(line));
+ }
+ }
+ }
+
+ // Set output file name.
+ if (cl.hasOption("o"))
+ {
+ fname = cl.getOptionValue("o");
+ }
+
+ // Create new File for output.
+ outfile = new File(fname);
+ }
+}
\ No newline at end of file
lcio/src/java/hep/lcio/util
diff -u -r1.2 -r1.3
--- CommandHandler.java 28 Apr 2006 21:33:51 -0000 1.2
+++ CommandHandler.java 28 Apr 2006 23:38:48 -0000 1.3
@@ -9,13 +9,14 @@
* single command in the CommandLineTool.
*
* @author jeremym
- * @version $Id: CommandHandler.java,v 1.2 2006/04/28 21:33:51 jeremy Exp $
+ * @version $Id: CommandHandler.java,v 1.3 2006/04/28 23:38:48 jeremy Exp $
*/
public abstract class CommandHandler
{
String name;
String description;
+ Options options;
/**
* CommandHandler ctor.
@@ -57,14 +58,23 @@
}
/**
+ * Options for this command.
+ * @return Options object for this command.
+ */
+ public Options getOptions()
+ {
+ return options;
+ }
+
+ /**
* Print usage and (optionally) exit the program.
* @param doExit Whether or not to exit after printing usage.
*/
- public void printUsage(Options options, boolean doExit)
+ public void printUsage(boolean doExit)
{
HelpFormatter help = new HelpFormatter();
- help.printHelp(getName(), options);
+ help.printHelp(getName(), getOptions());
if (doExit)
{
lcio/src/java/hep/lcio/util
diff -u -r1.4 -r1.5
--- CommandLineTool.java 28 Apr 2006 21:33:51 -0000 1.4
+++ CommandLineTool.java 28 Apr 2006 23:38:48 -0000 1.5
@@ -29,7 +29,7 @@
* FIXME: Implement all of the above commands.
*
* @author jeremym
- * @version $Id: CommandLineTool.java,v 1.4 2006/04/28 21:33:51 jeremy Exp $
+ * @version $Id: CommandLineTool.java,v 1.5 2006/04/28 23:38:48 jeremy Exp $
*/
public class CommandLineTool
{
@@ -84,13 +84,15 @@
*/
private void registerHandlers()
{
+ addCommandHandler(new MergeCommandHandler());
+ addCommandHandler(new SplitCommandHandler());
+ addCommandHandler(new ConcatenateCommandHandler());
+
// addCommandHandler("compare", Compare)
- // addCommandHandler("concat", Concat);
- // addCommandHandler("printevent", PrintEvent);
+ // addCommandHandler("print", PrintEvent);
// addCommandHandler("header", HeaderScan);
- addCommandHandler(new MergeCommandHandler());
// addCommandHandler("siodump", SioDump);
- addCommandHandler(new SplitCommandHandler());
+ // addCommandHandler("random", RandomEvent);
}
/**
lcio/src/java/hep/lcio/util
diff -u -r1.4 -r1.5
--- MergeCommandHandler.java 28 Apr 2006 21:33:51 -0000 1.4
+++ MergeCommandHandler.java 28 Apr 2006 23:38:48 -0000 1.5
@@ -20,12 +20,11 @@
* passes the results to a method from MergeUtil.
*
* @author jeremym
- * @version $Id: MergeCommandHandler.java,v 1.4 2006/04/28 21:33:51 jeremy Exp $
+ * @version $Id: MergeCommandHandler.java,v 1.5 2006/04/28 23:38:48 jeremy Exp $
*/
public class MergeCommandHandler extends CommandHandler
{
Parser parser = new PosixParser();
- Options options;
File outfile;
File[] infiles;
int maxevents = Integer.MAX_VALUE;
lcio/src/java/hep/lcio/util
diff -u -r1.1 -r1.2
--- MergeTestEvents.java 24 Apr 2006 22:08:34 -0000 1.1
+++ MergeTestEvents.java 28 Apr 2006 23:38:49 -0000 1.2
@@ -26,7 +26,7 @@
LCWriter writer =
LCFactory.getInstance().createLCWriter();
- writer.open("mergetest1", LCIO.WRITE_NEW);
+ writer.open("test1", LCIO.WRITE_NEW);
ILCEvent event = new ILCEvent();
event.setDetectorName("TEST");
@@ -54,12 +54,12 @@
writer.writeEvent(event);
writer.close();
- writer.open("mergetest2", LCIO.WRITE_NEW);
+ writer.open("test2", LCIO.WRITE_NEW);
writer.writeEvent(event);
writer.close();
LCReader reader = LCFactory.getInstance().createLCReader();
- reader.open("mergetest1.slcio");
+ reader.open("test1.slcio");
LCEvent checkevent = reader.readNextEvent();
System.out.println("read back event: " + checkevent);
}
lcio/src/java/hep/lcio/util
diff -u -r1.1 -r1.2
--- SplitCommandHandler.java 28 Apr 2006 21:33:51 -0000 1.1
+++ SplitCommandHandler.java 28 Apr 2006 23:38:49 -0000 1.2
@@ -13,11 +13,10 @@
* 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 $
+ * @version $Id: SplitCommandHandler.java,v 1.2 2006/04/28 23:38:49 jeremy Exp $
*/
public class SplitCommandHandler extends CommandHandler
{
- Options options;
Parser parser = new PosixParser();
File infile;
int nevents;
@@ -60,7 +59,7 @@
if (!cl.hasOption("i") || !cl.hasOption("n"))
{
- printUsage(options, true);
+ printUsage(true);
}
String inputpath = null;
CVSspam 0.2.8