Print

Print


Commit in lcio/src/java/hep/lcio/util on MAIN
Validate.java+113added 1.1
ValidateCommandHandler.java+105added 1.1
CommandLineTool.java+11-101.10 -> 1.11
ObjectComparator.java+1-21.5 -> 1.6
+230-12
2 added + 2 modified, total 4 files
JM: Add validate command to CommandLineTool.

lcio/src/java/hep/lcio/util
Validate.java added at 1.1
diff -N Validate.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Validate.java	28 Jun 2006 18:31:55 -0000	1.1
@@ -0,0 +1,113 @@
+package hep.lcio.util;
+
+import hep.lcd.io.sio.SIOBlock;
+import hep.lcd.io.sio.SIOReader;
+import hep.lcd.io.sio.SIORecord;
+import hep.lcio.event.LCEvent;
+import hep.lcio.implementation.io.LCFactory;
+import hep.lcio.io.LCReader;
+
+import java.io.BufferedInputStream;
+import java.io.EOFException;
+import java.io.File;
+import java.io.FileInputStream;
+
+/**
+ * 
+ * Several static utility methods for simple validation of LCIO/SIO files. 
+ * 
+ * @author jeremym
+ * @version $Id: Validate.java,v 1.1 2006/06/28 18:31:55 jeremy Exp $
+ */
+public class Validate
+{	
+	/** 
+	 * Confirm major and minor versions of an LCIO file.
+	 * @param major The major version to match.
+	 * @param minor The minor version to match.
+	 * @throws If versions do not match, or if there is an IO error.
+	 */
+	public static void validateVersion(File f, int major, int minor) throws Exception
+	{
+		if (major == -1 && minor == -1)
+		{
+			return;
+		}
+		
+		SIOReader rdr = 
+			new SIOReader(new BufferedInputStream(new FileInputStream(f.getAbsolutePath())));
+		
+		for (;;)
+		{
+			SIORecord rec;
+			try {
+				rec = rdr.readRecord();
+			}
+			catch (EOFException e)
+			{
+				break;
+			}
+			
+			SIOBlock block = rec.getBlock();
+			
+			if (block == null)
+			{
+				System.err.println("null block");
+				break;
+			}
+			
+			int bmajor = block.getMajorVersion();
+			
+			if (major != -1 && bmajor != major)
+			{
+				throw new Exception(block.getBlockName() + " : major version mismatch : " + bmajor + " != " + major);
+			}
+			
+			int bminor = block.getMinorVersion();
+			
+			if (minor != -1 && bminor != minor)
+			{
+				throw new Exception(block.getBlockName() + " : minor version mismatch : " + bminor + " != " + minor);
+			}
+		}
+
+		try { rdr.close(); } catch (Exception e) {}
+	}
+	
+	/**
+	 * Perform test read of an LCIO file.
+	 * @param f The file to read.
+	 * @param nevents The number of events to read.
+	 * @throws Exception If file does not exist or does not have nevents.
+	 */
+	public static void testRead(File f, int nevents) throws Exception
+	{
+		LCReader rdr = LCFactory.getInstance().createLCReader();
+		
+		if (!f.exists())
+		{
+			throw new Exception(f.getAbsolutePath() + " does not exist.");
+		}
+		
+		rdr.open(f.getAbsolutePath());
+	
+		int nread = 0;
+				
+		for (int i=0; i<nevents; i++)
+		{
+			LCEvent event = rdr.readNextEvent();
+			if (event == null)
+			{
+				break;
+			}
+			++nread;
+		}
+		
+		if (nread < nevents)
+		{
+			throw new Exception(f.getAbsolutePath() + " does not have " + nevents + " events.");
+		}
+		
+		rdr.close();
+	}
+}
\ No newline at end of file

lcio/src/java/hep/lcio/util
ValidateCommandHandler.java added at 1.1
diff -N ValidateCommandHandler.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ValidateCommandHandler.java	28 Jun 2006 18:31:55 -0000	1.1
@@ -0,0 +1,105 @@
+package hep.lcio.util;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+
+/**
+ * Command-line handler for the validate utility.
+ * 
+ * @author Jeremy McCormick
+ * @version $Id: ValidateCommandHandler.java,v 1.1 2006/06/28 18:31:55 jeremy Exp $
+ */
+public class ValidateCommandHandler extends CommandHandler
+{
+	List infiles = new ArrayList();
+	int ntoread = 1;
+	int major = -1;
+	int minor = -1;
+	
+	public ValidateCommandHandler()
+	{
+		super("validate","Perform basic validation on an LCIO file.");
+		options = createValidateOptions();
+	}
+	
+	/**
+	 * Creates commandline options for the validate command.
+	 * @return Options object for validate command.
+	 */
+	public static Options createValidateOptions()
+	{
+		Options options = new Options();
+		
+		Option opt = new Option("f", true, "Add an LCIO file to validate.");
+		opt.setArgs(1);
+		options.addOption(opt);
+		
+		opt = new Option("M", true, "Set major version.");
+		opt.setArgs(1);
+		options.addOption(opt);
+		
+		opt = new Option("m", true, "Set minor version.");
+		opt.setArgs(1);
+		options.addOption(opt);
+		
+		opt = new Option("n", true, "Set number of events to read.");
+		opt.setArgs(1);
+		options.addOption(opt);
+		
+		return options;
+	}
+	
+	/**
+	 * Parse the commandline options for the validate command.
+	 */
+	public void parse(String[] argv) throws Exception
+	{
+		CommandLine cl = parser.parse(options, argv);
+		
+		if (cl.hasOption("f"))
+		{
+			String files[] = cl.getOptionValues("f");
+			for (int i=0; i<files.length; i++)
+			{
+				infiles.add(new File(files[i]));
+			}
+		}
+		else {
+			printUsage(true);
+		}
+		
+		if (cl.hasOption("M"))
+		{
+			major = Integer.parseInt(cl.getOptionValue("M"));
+		}
+		
+		if (cl.hasOption("m"))
+		{
+			minor = Integer.parseInt(cl.getOptionValue("m"));
+		}
+		
+		if (cl.hasOption("n"))
+		{
+			ntoread = Integer.parseInt(cl.getOptionValue("n"));
+		}
+	}
+
+	/**
+	 * Execute the validate command with current options.
+	 */
+	public void execute() throws Exception
+	{
+		for (int i=0; i<infiles.size(); i++)
+		{
+			File f = (File)infiles.get(i);
+			
+			Validate.testRead(f, ntoread);			
+			Validate.validateVersion(f, major, minor);
+		}
+	}
+}
\ No newline at end of file

lcio/src/java/hep/lcio/util
CommandLineTool.java 1.10 -> 1.11
diff -u -r1.10 -r1.11
--- CommandLineTool.java	27 Jun 2006 21:56:46 -0000	1.10
+++ CommandLineTool.java	28 Jun 2006 18:31:55 -0000	1.11
@@ -41,6 +41,13 @@
  * 
  * @see hep.lcio.util.RandomEvent random [X]
  * -generate X random events
+ *  
+ * @see hep.lcio.util.Validate validate [x]
+ * -Is an LCIO file?
+ * -version check
+ * 
+ * @see hep.lcio.util.StdHepConverter stdhep
+ * -convert from stdhep file to lcio MCParticles
  * 
  * @see hep.lcio.util.PrintEvent print
  * -dump lcio file (similar to C++ dump cmd)
@@ -50,18 +57,11 @@
  * -filter out/in by coll name
  * -filter out/in by event number
  * -filter out/in by run number
- *  
- * @see hep.lcio.util.Validate validate
- * -Is an LCIO file?
- * -version check
- * 
- * @see hep.lcio.util.StdHepConverter stdhep
- * -convert from stdhep file to lcio MCParticles
- * 
+ *
  * FIXME: Implement all of the above commands.
  * 
  * @author jeremym
- * @version $Id: CommandLineTool.java,v 1.10 2006/06/27 21:56:46 jeremy Exp $
+ * @version $Id: CommandLineTool.java,v 1.11 2006/06/28 18:31:55 jeremy Exp $
  */
 public class CommandLineTool
 {
@@ -102,10 +102,11 @@
 		addCommandHandler(new CompareCommandHandler());
 		addCommandHandler(new HeaderCountCommandHandler());
 		addCommandHandler(new RandomEventCommandHandler());
+		addCommandHandler(new ValidateCommandHandler());
 		
 		// addCommandHandler(new PrintEventCommandHandler());
-		// addCommandHandler(new ValidateEventCommandHandler());
 		// addCommandHandler(new FilterEventCommandHandler());
+		// addCommandHandler(new StdHepCommandHandler());
 	}
 	
 	/**

lcio/src/java/hep/lcio/util
ObjectComparator.java 1.5 -> 1.6
diff -u -r1.5 -r1.6
--- ObjectComparator.java	2 Jun 2006 23:48:34 -0000	1.5
+++ ObjectComparator.java	28 Jun 2006 18:31:55 -0000	1.6
@@ -15,10 +15,9 @@
 
 /**
  *
- * @author jeremym
- *
  * Object comparison code refactored out of tonyj's old hep.lcio.test.RandomEvent .
  *
+ * @author jeremym
  */
 public class ObjectComparator
 {
CVSspam 0.2.8