lcio/src/java/hep/lcio/util
diff -u -r1.2 -r1.3
--- Split.java 15 Jun 2007 23:14:57 -0000 1.2
+++ Split.java 13 Mar 2009 18:40:04 -0000 1.3
@@ -21,7 +21,7 @@
* chunks of given size.
*
* @author Jeremy McCormick
- * @version $Id: Split.java,v 1.2 2007/06/15 23:14:57 jeremy Exp $
+ * @version $Id: Split.java,v 1.3 2009/03/13 18:40:04 jeremy Exp $
*/
public class Split
{
@@ -31,35 +31,64 @@
* @param nevents The number of events in each new output file.
* @throws Exception
*/
- public static List split(File infile, int nevents, int maxevents) throws Exception
- {
+ public static List split(File infile, File outdir, int nevents, int maxevents) throws Exception
+ {
+ if (infile == null)
+ {
+ throw new IllegalArgumentException("The infile argument points to null.");
+ }
+
+ if (!infile.exists())
+ {
+ throw new IllegalArgumentException("Input file " + infile.toString() + " does not exist.");
+ }
+
+ // Decide where the files will go. Uses either the -d argument from
+ // the command line, if it was set. Or by default the files will
+ // be created in the current working directory.
+ String outpath = null;
+ if (outdir != null)
+ {
+ if (outdir.exists() && outdir.isDirectory() && outdir.canWrite())
+ {
+ outpath = outdir.getCanonicalPath();
+ }
+ else
+ {
+ throw new IllegalArgumentException("Directory " + outdir.toString() + " is not valid.");
+ }
+ }
+ else
+ {
+ outpath = System.getProperty("user.dir");
+ }
+
// List of files created.
List<File> outfilelist = new ArrayList<File>();
// Base name from input file name.
- String basename = infile.getAbsolutePath().replace(".slcio", "");
-
+ String basename = infile.getName().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()));
+ 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.
+ // Number of records read with name 'LCEvent'. This flag is reset when the next file is started.
int neventsread = 0;
// Flag to indicate processing should end.
@@ -72,7 +101,13 @@
{
// Next output file name.
outfile = basename + "-" + String.valueOf(filenum) + "-" + nevents + ".slcio";
-
+
+ // Add base directory for output if specified.
+ if (outpath != null)
+ {
+ outfile = outpath + File.separator + outfile;
+ }
+
// The output stream for this file.
XDROutputStream xo;
@@ -198,7 +233,7 @@
public static void printSplitSummary(List outfilelist, PrintStream ps) throws Exception
{
ps.println();
- ps.println("Split Summary");
+ ps.println("--- Split Summary ---");
ps.println();
// Read back events.
lcio/src/java/hep/lcio/util
diff -u -r1.5 -r1.6
--- SplitCommandHandler.java 15 Jun 2007 23:14:57 -0000 1.5
+++ SplitCommandHandler.java 13 Mar 2009 18:40:04 -0000 1.6
@@ -6,20 +6,19 @@
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.5 2007/06/15 23:14:57 jeremy Exp $
+ * @version $Id: SplitCommandHandler.java,v 1.6 2009/03/13 18:40:04 jeremy Exp $
*/
public class SplitCommandHandler extends CommandHandler
{
File infile;
int nevents;
int maxevents=-1;
+ File outdir=null;
SplitCommandHandler()
{
@@ -37,18 +36,22 @@
{
Options options = new Options();
- Option opt = new Option("h", false, "Print split usage.");
+ Option opt = new Option("h",false,"Print split usage.");
options.addOption(opt);
- opt = new Option("i", true, "The input LCIO file.");
+ 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 = new Option("n", true,"The number of events to split.");
opt.setArgs(1);
options.addOption(opt);
- opt = new Option("m", false, "The maximum number of events to read.");
+ opt = new Option("m", false,"The maximum number of events to read.");
+ opt.setArgs(1);
+ options.addOption(opt);
+
+ opt = new Option("d",false,"The output directory for split files; defaults to input file's directory.");
opt.setArgs(1);
options.addOption(opt);
@@ -90,6 +93,15 @@
{
maxevents = Integer.parseInt(cl.getOptionValue("m"));
}
+
+ if (cl.hasOption("d"))
+ {
+ outdir = new File(cl.getOptionValue("d"));
+ if (!outdir.exists())
+ {
+ throw new IllegalArgumentException("The directory " + outdir.toString() + " does not exist.");
+ }
+ }
}
/**
@@ -97,7 +109,7 @@
*/
public void execute() throws Exception
{
- List flist = Split.split(infile, nevents, maxevents);
+ List flist = Split.split(infile, outdir, nevents, maxevents);
Split.printSplitSummary(flist, System.out);
}
}