1 added + 3 modified, total 4 files
lcio/src/java/hep/lcio/util
diff -N CompareCommandHandler.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CompareCommandHandler.java 2 Jun 2006 23:48:34 -0000 1.1
@@ -0,0 +1,155 @@
+package hep.lcio.util;
+
+import hep.lcio.event.LCEvent;
+import hep.lcio.implementation.io.LCFactory;
+import hep.lcio.io.ILCFactory;
+import hep.lcio.io.LCReader;
+
+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;
+
+/**
+ * CommandHandler for the compare command.
+ *
+ * @author jeremym
+ * @version $Id: CompareCommandHandler.java,v 1.1 2006/06/02 23:48:34 jeremy Exp $
+ */
+public class CompareCommandHandler extends CommandHandler
+{
+ List files = new ArrayList();
+ List readers = new ArrayList();
+ int verbosity = 1;
+ int ntoskip = 0;
+ int ntocompare = 1;
+
+ public CompareCommandHandler()
+ {
+ super("compare", "Compare events from two LCIO files.");
+
+ options = createCompareOptions();
+ }
+
+ /**
+ * Creates the CLI options for the compare command.
+ * @return Options for the compare command.
+ */
+ private static Options createCompareOptions()
+ {
+ Options options = new Options();
+
+ Option opt = new Option("f", false, "Add a file to compare (Must have at least 2).");
+ opt.setArgs(1);
+ options.addOption(opt);
+
+ opt = new Option("n", true, "Set number of events to compare.");
+ opt.setArgs(1);
+ options.addOption(opt);
+
+ opt = new Option("s", true, "Set number of events to skip.");
+ opt.setArgs(1);
+ options.addOption(opt);
+
+ opt = new Option("v", true, "Set verbosity level (must be between 1 and 4).");
+ opt.setArgs(1);
+ options.addOption(opt);
+
+ return options;
+ }
+
+ /**
+ * Parse the command line options for the compare command using apache CLI.
+ * @param argv The input arguments for the compare command.
+ */
+ public void parse(String[] argv) throws Exception
+ {
+ CommandLine cl = parser.parse(options, argv);
+
+ String f[] = cl.getOptionValues("f");
+ if (f != null && f.length >= 2)
+ {
+ for (int i = 0; i < f.length; i++)
+ {
+ files.add(new File(f[i]));
+ }
+ }
+ else
+ {
+ printUsage(true);
+ }
+
+ if (cl.hasOption("n"))
+ {
+ ntocompare = Integer.parseInt(cl.getOptionValue("n"));
+ }
+
+ if (cl.hasOption("s"))
+ {
+ ntoskip = Integer.parseInt(cl.getOptionValue("s"));
+ }
+
+ if (cl.hasOption("v"))
+ {
+ verbosity = Integer.parseInt(cl.getOptionValue("v"));
+ }
+ }
+
+ /**
+ * Execute the merge compare with current arguments.
+ */
+ public void execute() throws Exception
+ {
+ ILCFactory factory = LCFactory.getInstance();
+
+ // Setup all the LCReaders.
+ for (int i = 0; i < files.size(); i++)
+ {
+ LCReader rdr = factory.createLCReader();
+ rdr.open(((File) files.get(i)).getAbsolutePath());
+
+ if (ntoskip > 0)
+ {
+ System.out.println("Skipping " + ntoskip + " events in file " + ((File)files.get(i)).getAbsolutePath());
+ rdr.skipNEvents(ntoskip);
+ }
+
+ readers.add(rdr);
+ }
+
+ // Compare each event to base event.
+ for (int i = 0; i < ntocompare; i++)
+ {
+ LCEvent baseevent = ((LCReader) readers.get(0)).readNextEvent();
+
+ if (baseevent == null) break;
+
+ LCEvent nextevent = null;
+ for (int j = 1; j < readers.size(); j++)
+ {
+ ObjectComparator cmp = new ObjectComparator();
+ cmp.setVerbosity(verbosity);
+
+ nextevent = ((LCReader) readers.get(j)).readNextEvent();
+
+ if (nextevent == null) break;
+
+ cmp.compare(baseevent, nextevent);
+
+ // Print result.
+ System.out.println("Compare event " + baseevent.getEventNumber() + " from " + ((File)files.get(0)).getName() + " with event " + nextevent.getEventNumber() + " from " + ((File)files.get(j)).getName() + " : " + cmp.getResultString());
+ }
+
+ if (nextevent == null) break;
+ }
+
+ // Close the LCReaders.
+ for (int i = 0; i < readers.size(); i++)
+ {
+ ((LCReader) readers.get(i)).close();
+ }
+ }
+}
\ No newline at end of file
lcio/src/java/hep/lcio/util
diff -u -r1.4 -r1.5
--- CommandHandler.java 2 Jun 2006 00:22:57 -0000 1.4
+++ CommandHandler.java 2 Jun 2006 23:48:34 -0000 1.5
@@ -11,14 +11,14 @@
* single command in the CommandLineTool.
*
* @author jeremym
- * @version $Id: CommandHandler.java,v 1.4 2006/06/02 00:22:57 jeremy Exp $
+ * @version $Id: CommandHandler.java,v 1.5 2006/06/02 23:48:34 jeremy Exp $
*/
public abstract class CommandHandler
{
String name;
String description;
- Options options;
+ Options options = new Options();
protected static Parser parser = new PosixParser();
/**
lcio/src/java/hep/lcio/util
diff -u -r1.6 -r1.7
--- CommandLineTool.java 2 Jun 2006 00:22:57 -0000 1.6
+++ CommandLineTool.java 2 Jun 2006 23:48:34 -0000 1.7
@@ -18,26 +18,42 @@
*
* lcio [global_options] [command] [command_options]
*
- *
- *
* @see hep.lcio.util.Concat concat [X]
+ * -concatenate LCIO files together
*
* @see hep.lcio.util.MergeCommandHandler merge [X]
+ * -overlay events
*
* @see hep.lcio.util.Split split [X]
+ * -split LCIO file into multiple output files
*
* @see hep.lcio.util.SioDump siodump [X]
+ * -dump low-level SIOinfo
+ *
+ * @see hep.lcio.util.Compare compare [X]
+ * -compare series of LCIO files
*
* @see hep.lcio.util.Headers head
+ * -print header info
+ * -number of LCRunHeader
+ * -number of LCEventHeader
+ * -number of LCEvent
+ *
+ * @see hep.lcio.util.PrintEvent dump
+ * -dump lcio file (similar to C++ dump cmd)
*
- * @see hep.lcio.util.Compare compare
+ * @see hep.lcio.util.Filter filter
+ * -filter out/in by coll type
+ * -filter out/in by coll name
*
- * @see hep.lcio.util.PrintEvent print
+ * @see hep.lcio.util.Validate validate
+ * -Is an LCIO file?
+ * -version check
*
* FIXME: Implement all of the above commands.
*
* @author jeremym
- * @version $Id: CommandLineTool.java,v 1.6 2006/06/02 00:22:57 jeremy Exp $
+ * @version $Id: CommandLineTool.java,v 1.7 2006/06/02 23:48:34 jeremy Exp $
*/
public class CommandLineTool
{
@@ -96,8 +112,8 @@
addCommandHandler(new SplitCommandHandler());
addCommandHandler(new ConcatenateCommandHandler());
addCommandHandler(new SIODumpCommandHandler());
+ addCommandHandler(new CompareCommandHandler());
- // addCommandHandler("compare", CompareCommandHandler())
// addCommandHandler("print", PrintEventCommandHandler());
// addCommandHandler("header", HeaderScanCommandHandler());
// addCommandHandler("random", RandomEventCommandHandler());
@@ -144,6 +160,7 @@
*/
// FIXME: Method needs to handle quoting properly.
// FreeHep's argv package can handle this.
+ // FIXME: Actually parse and set global options.
public void parse(String[] argv) throws Exception
{
// Get the index of a command.
lcio/src/java/hep/lcio/util
diff -u -r1.4 -r1.5
--- ObjectComparator.java 3 Jun 2005 23:56:18 -0000 1.4
+++ ObjectComparator.java 2 Jun 2006 23:48:34 -0000 1.5
@@ -20,266 +20,358 @@
* Object comparison code refactored out of tonyj's old hep.lcio.test.RandomEvent .
*
*/
-public class ObjectComparator {
+public class ObjectComparator
+{
- // comparison result
- public static final int NOT_EQUAL = 1;
- public static final int EQUAL = 0;
- public static final int NO_COMPARISON = -1;
-
- // verbosity
- public static final int SILENT = 0;
- public static final int INFO = 1;
- public static final int DEBUG = 2;
- public static final int ALL = 3;
-
- // current result
- int m_result;
-
- // current method
- Method m_method ;
-
- // dup map
- Map m_alreadyChecked;
-
- public ObjectComparator() {
- m_result = EQUAL;
- m_alreadyChecked = new HashMap();
- }
-
- private void setResultCode(int cr) throws IllegalArgumentException {
- if ( cr < -1 || cr > 1 ) {
- throw new IllegalArgumentException("Not a valid result: " + cr);
- }
-
- m_result = cr;
- }
-
- public String getResultString() {
- String rs = "";
- if ( m_result == NOT_EQUAL ) {
- rs = "Not Equal";
- } else if ( m_result == EQUAL ) {
- rs = "Equal";
- } else if ( m_result == NO_COMPARISON ) {
- rs = "No Comparison";
- }
- return rs;
- }
-
- public void reset() {
-//fg: should be same as c'tor
-// m_result = NO_COMPARISON;
- m_result = EQUAL ;
- m_alreadyChecked.clear();
- }
-
- public int getResultCode() {
- return m_result;
- }
-
- public void compare(Object o1, Object o2) throws IllegalArgumentException {
- //System.out.println("class: " + o1.getClass().getCanonicalName() );
-
- // did this object already?
- if (m_alreadyChecked.get(o1) == o2) {
- return;
- }
-
- // add to checked map
- m_alreadyChecked.put(o1,o2);
-
- try {
- //System.out.println("comparisons...");
-
- // basic object comparison
- try {
- if (o1 instanceof Comparable) {
- //System.out.println("object");
- compareObject(o1, o2);
- }
- } catch (Throwable t) {
- System.out.println("error comparing object" );
- }
-
- // array
- try {
- if (o1.getClass().isArray()) {
- //System.out.println("array");
- compareArray(o1, o2);
- }
- } catch (Throwable t) {
- System.out.println("error comparing array");
- }
-
- // collection
- try {
- if (o1 instanceof Collection) {
- //System.out.println("collection");
- compareCollection(o1, o2);
- }
- } catch (Throwable t) {
- System.out.println("error comparing collection");
- }
-
-
- // bean
- try {
- compareBeanProperties(o1, o2);
- } catch (Throwable t) {
- System.out.println("error comparing bean properties");
- }
-
- // LCEvent
- try {
- if (o1 instanceof LCEvent) {
- //System.out.println("LCEvent");
- compareLCEvent(o1, o2);
- }
- } catch (Throwable t) {
- System.out.println("error comparing LCEvent");
- t.printStackTrace();
- }
-
- } catch (Throwable t) {
- throw new IllegalArgumentException("FATAL ERROR: Could not compare " + o1 + " " + o2);
- }
- }
-
- public void compareLCEvent(Object o1, Object o2) {
- String[] names = ((LCEvent) o1).getCollectionNames();
- for (int i=0; i<names.length; i++) {
-
- System.out.println("object compare: " + names[i]);
-
- Collection c1 = ((LCEvent) o1).getCollection(names[i]);
- Collection c2 = ((LCEvent) o2).getCollection(names[i]);
-
- // missing coll; don't bother comparing the rest of them
- if (c1 == null || c2 == null) {
- System.out.println("one of the events is missing coll: " + names[i]);
- setResultCode(NOT_EQUAL);
- return;
- }
-
- // different size colls treated like array with different # elements
- if ( c1.size() != c2.size() ) {
- System.out.println(names[i] + " size is different: " + c1.size() + " " + c2.size() );
- setResultCode(NOT_EQUAL);
- // same size coll
- } else {
-
- // iterate over coll, comparing the objects
- Iterator i1 = c1.iterator();
- Iterator i2 = c2.iterator();
- while ( i1.hasNext() ) {
- Object v1 = i1.next();
- Object v2 = null;
- if (i2.hasNext() ) {
- v2 = i2.next();
- // fewer elements in 2nd event
- } else {
- System.out.println("2nd event has fewer objects in coll: " + names[i]);
- setResultCode(NOT_EQUAL);
- break;
- }
-
- compare(v1,v2);
- }
-
- // more elements left in 2nd event
- if (i2.hasNext() ) {
- System.out.println("2nd event has more objects in coll: " + names[i]);
- setResultCode(NOT_EQUAL);
- }
-
- }
- }
- }
-
- public void compareObject(Object o1, Object o2) {
-
- // both null
- if (o1 == null && o2 == null) return;
-
- // one null
- if (o1 == null || o2 == null) {
- System.out.println("one object is null");
- setResultCode(NOT_EQUAL);
- }
-
- // comparison
- if (o1 instanceof Comparable) {
- int rc = ((Comparable) o1).compareTo(o2);
- if (rc != 0) {
- System.out.println( "objects not equal: " + o1.getClass().getName() + " ( last method: " + m_method + " )" );
- System.out.println(o1 + " != " + o2);
- setResultCode(NOT_EQUAL);
- }
- }
- }
-
- private void compareArray(Object o1, Object o2) {
- if (Array.getLength(o1) != Array.getLength(o2)) {
- System.out.println("arr len not equal");
- setResultCode(NOT_EQUAL);
- } else {
- for (int i=0; i<Array.getLength(o1);i++) {
- Object v1 = Array.get(o1, i);
- Object v2 = Array.get(o2, i);
- compare(v1, v2);
- }
- }
- }
-
- private void compareCollection(Object o1, Object o2) {
- Collection c1 = (Collection) o1;
- Collection c2 = (Collection) o2;
- if (c1.size() != c2.size()) {
- System.out.println("coll size !=");
- setResultCode(NOT_EQUAL);
- } else {
- Iterator i1 = c1.iterator();
- Iterator i2 = c2.iterator();
- while ( i1.hasNext() ) {
- Object v1 = i1.next();
- Object v2 = i2.next();
- compare(v1,v2);
- }
- }
- }
-
- private void compareBeanProperties(Object o1, Object o2) {
- //System.out.println("bean");
-
- try {
- BeanInfo info = Introspector.getBeanInfo(o1.getClass(),Object.class);
- PropertyDescriptor[] desc = info.getPropertyDescriptors();
-
- //if ( desc.length == 0 ) {
- // System.out.println("WARNING: no bean properties for " + o1.getClass().getCanonicalName() );
- //}
-
- if ( desc.length != 0 ) {
- for (int i=0; i<desc.length; i++) {
- Method m = desc[i].getReadMethod();
- m_method = m ;
- if (m != null) {
- try {
- Object v1 = m.invoke(o1,(Object[])null);
- Object v2 = m.invoke(o2,(Object[])null);
- compare(v1,v2);
- } catch (Throwable t) {
- /* just eat it for now */
- //System.out.println("skipping bean compare w/ error: " + desc[i].getDisplayName() );
- //t.printStackTrace();
- }
- }
- }
- }
- } catch (Throwable t) {
- System.out.println("error comparing bean properties");
- t.printStackTrace();
- }
- }
+ // comparison result
+ public static final int NOT_EQUAL = 1;
+ public static final int EQUAL = 0;
+ public static final int NO_COMPARISON = -1;
+
+ // verbosity
+ public static final int SILENT = 0;
+ public static final int INFO = 1;
+ public static final int DEBUG = 2;
+ public static final int ALL = 3;
+
+ // current result
+ int m_result;
+
+ // current method
+ Method m_method;
+
+ // dup map
+ Map m_alreadyChecked;
+
+ int verbosity = 1;
+
+ public ObjectComparator()
+ {
+ m_result = EQUAL;
+ m_alreadyChecked = new HashMap();
+ }
+
+ public void setVerbosity(int verbosity)
+ {
+ this.verbosity = verbosity;
+ }
+
+ private void setResultCode(int cr) throws IllegalArgumentException
+ {
+ if (cr < -1 || cr > 1)
+ {
+ throw new IllegalArgumentException("Not a valid result: " + cr);
+ }
+
+ m_result = cr;
+ }
+
+ public String getResultString()
+ {
+ String rs = "";
+ if (m_result == NOT_EQUAL)
+ {
+ rs = "Not Equal";
+ }
+ else if (m_result == EQUAL)
+ {
+ rs = "Equal";
+ }
+ else if (m_result == NO_COMPARISON)
+ {
+ rs = "No Comparison";
+ }
+ return rs;
+ }
+
+ public void reset()
+ {
+ m_result = EQUAL;
+ m_alreadyChecked.clear();
+ }
+
+ public int getResultCode()
+ {
+ return m_result;
+ }
+
+ public void compare(Object o1, Object o2) throws IllegalArgumentException
+ {
+ //System.out.println("class: " + o1.getClass().getCanonicalName() );
+
+ // did this object already?
+ if (m_alreadyChecked.get(o1) == o2)
+ {
+ return;
+ }
+
+ // add to checked map
+ m_alreadyChecked.put(o1, o2);
+
+ try
+ {
+ //System.out.println("comparisons...");
+
+ // basic object comparison
+ try
+ {
+ if (o1 instanceof Comparable)
+ {
+ //System.out.println("object");
+ compareObject(o1, o2);
+ }
+ }
+ catch (Throwable t)
+ {
+ System.out.println("error comparing object");
+ }
+
+ // array
+ try
+ {
+ if (o1.getClass().isArray())
+ {
+ //System.out.println("array");
+ compareArray(o1, o2);
+ }
+ }
+ catch (Throwable t)
+ {
+ System.out.println("error comparing array");
+ }
+
+ // collection
+ try
+ {
+ if (o1 instanceof Collection)
+ {
+ //System.out.println("collection");
+ compareCollection(o1, o2);
+ }
+ }
+ catch (Throwable t)
+ {
+ System.out.println("error comparing collection");
+ }
+
+ // bean
+ try
+ {
+ compareBeanProperties(o1, o2);
+ }
+ catch (Throwable t)
+ {
+ System.out.println("error comparing bean properties");
+ }
+
+ // LCEvent
+ try
+ {
+ if (o1 instanceof LCEvent)
+ {
+ //System.out.println("LCEvent");
+ compareLCEvent(o1, o2);
+ }
+ }
+ catch (Throwable t)
+ {
+ System.out.println("error comparing LCEvent");
+ t.printStackTrace();
+ }
+
+ }
+ catch (Throwable t)
+ {
+ throw new IllegalArgumentException("FATAL ERROR: Could not compare " + o1 + " " + o2);
+ }
+ }
+
+ public void compareLCEvent(Object o1, Object o2)
+ {
+ String[] names = ((LCEvent) o1).getCollectionNames();
+ for (int i = 0; i < names.length; i++)
+ {
+
+ if (verbosity > 1)
+ {
+ System.out.println("object compare: " + names[i]);
+ }
+
+ Collection c1 = ((LCEvent) o1).getCollection(names[i]);
+ Collection c2 = ((LCEvent) o2).getCollection(names[i]);
+
+ // missing coll; don't bother comparing the rest of them
+ if (c1 == null || c2 == null)
+ {
+
+ if (verbosity > 1)
+ {
+ System.out.println("one of the events is missing coll: " + names[i]);
+ }
+ setResultCode(NOT_EQUAL);
+ return;
+ }
+
+ // different size colls treated like array with different # elements
+ if (c1.size() != c2.size())
+ {
+ if (verbosity > 1)
+ {
+ System.out.println(names[i] + " size is different: " + c1.size() + " " + c2.size());
+ }
+ setResultCode(NOT_EQUAL);
+ // same size coll
+ }
+ else
+ {
+
+ // iterate over coll, comparing the objects
+ Iterator i1 = c1.iterator();
+ Iterator i2 = c2.iterator();
+ while (i1.hasNext())
+ {
+ Object v1 = i1.next();
+ Object v2 = null;
+ if (i2.hasNext())
+ {
+ v2 = i2.next();
+ // fewer elements in 2nd event
+ }
+ else
+ {
+
+ if (verbosity > 1)
+ {
+ System.out.println("2nd event has fewer objects in coll: " + names[i]);
+ }
+ setResultCode(NOT_EQUAL);
+ break;
+ }
+
+ compare(v1, v2);
+ }
+
+ // more elements left in 2nd event
+ if (i2.hasNext())
+ {
+ if (verbosity > 1)
+ {
+ System.out.println("2nd event has more objects in coll: " + names[i]);
+ }
+ setResultCode(NOT_EQUAL);
+ }
+
+ }
+ }
+ }
+
+ public void compareObject(Object o1, Object o2)
+ {
+ // both null
+ if (o1 == null && o2 == null)
+ return;
+
+ // one null
+ if (o1 == null || o2 == null)
+ {
+ System.out.println("one object is null");
+ setResultCode(NOT_EQUAL);
+ }
+
+ // comparison
+ if (o1 instanceof Comparable)
+ {
+ int rc = ((Comparable) o1).compareTo(o2);
+ if (rc != 0)
+ {
+ System.out.println("objects not equal: " + o1.getClass().getName() + " ( last method: " + m_method + " )");
+ System.out.println(o1 + " != " + o2);
+ setResultCode(NOT_EQUAL);
+ }
+ }
+ }
+
+ private void compareArray(Object o1, Object o2)
+ {
+ if (Array.getLength(o1) != Array.getLength(o2))
+ {
+ System.out.println("arr len not equal");
+ setResultCode(NOT_EQUAL);
+ }
+ else
+ {
+ for (int i = 0; i < Array.getLength(o1); i++)
+ {
+ Object v1 = Array.get(o1, i);
+ Object v2 = Array.get(o2, i);
+ compare(v1, v2);
+ }
+ }
+ }
+
+ private void compareCollection(Object o1, Object o2)
+ {
+ Collection c1 = (Collection) o1;
+ Collection c2 = (Collection) o2;
+ if (c1.size() != c2.size())
+ {
+ System.out.println("coll size !=");
+ setResultCode(NOT_EQUAL);
+ }
+ else
+ {
+ Iterator i1 = c1.iterator();
+ Iterator i2 = c2.iterator();
+ while (i1.hasNext())
+ {
+ Object v1 = i1.next();
+ Object v2 = i2.next();
+ compare(v1, v2);
+ }
+ }
+ }
+
+ private void compareBeanProperties(Object o1, Object o2)
+ {
+ //System.out.println("bean");
+
+ try
+ {
+ BeanInfo info = Introspector.getBeanInfo(o1.getClass(), Object.class);
+ PropertyDescriptor[] desc = info.getPropertyDescriptors();
+
+ //if ( desc.length == 0 ) {
+ // System.out.println("WARNING: no bean properties for " + o1.getClass().getCanonicalName() );
+ //}
+
+ if (desc.length != 0)
+ {
+ for (int i = 0; i < desc.length; i++)
+ {
+ Method m = desc[i].getReadMethod();
+ m_method = m;
+ if (m != null)
+ {
+ try
+ {
+ Object v1 = m.invoke(o1, (Object[]) null);
+ Object v2 = m.invoke(o2, (Object[]) null);
+ compare(v1, v2);
+ }
+ catch (Throwable t)
+ {
+ /* just eat it for now */
+ //System.out.println("skipping bean compare w/ error: " + desc[i].getDisplayName() );
+ //t.printStackTrace();
+ }
+ }
+ }
+ }
+ }
+ catch (Throwable t)
+ {
+ System.out.println("error comparing bean properties");
+ t.printStackTrace();
+ }
+ }
}
CVSspam 0.2.8