Commit in lcio/src/java/hep/lcio on MAIN
test/RandomEvent.java+210-1851.8 -> 1.9
util/RandomEventCommandHandler.java+106added 1.1
    /CommandLineTool.java+25-201.8 -> 1.9
+341-205
1 added + 2 modified, total 3 files
JM: Add a random command to CommandLineTool for generating N number of random events.  Uses Tony's existing hep.lcio.test.RandomEvent class.

lcio/src/java/hep/lcio/test
RandomEvent.java 1.8 -> 1.9
diff -u -r1.8 -r1.9
--- RandomEvent.java	1 Jun 2005 15:14:07 -0000	1.8
+++ RandomEvent.java	7 Jun 2006 22:32:33 -0000	1.9
@@ -26,193 +26,218 @@
 import java.util.Random;
 
 /**
- *
+ * 
  * @author tonyj
  */
 public class RandomEvent extends ILCEvent
 {
-   private static Random r = new Random();
-   /** Creates a new instance of RandomEvent */
-   public RandomEvent()
-   {
-      randomize(this);
-      addCollection(LCIO.TRACK,ITrack.class,1<<LCIO.TRBIT_HITS);
-      addCollection(LCIO.CLUSTER,ICluster.class,1<<LCIO.CLBIT_HITS);
-      addCollection(LCIO.RECONSTRUCTEDPARTICLE,IReconstructedParticle.class);
-      addCollection(LCIO.CALORIMETERHIT,ICalorimeterHit.class,1<<LCIO.RCHBIT_ID1 | 1<<LCIO.RCHBIT_LONG | 1<<LCIO.RCHBIT_TIME);
-      addCollection(LCIO.LCFLOATVEC,ILCFloatVec.class);
-      addCollection(LCIO.LCINTVEC,ILCIntVec.class);
-      //addCollection(LCIO.LCRELATION,ILCRelation.class);
-      //addColleciton(LCIO.LCGENERICOBJEC,ILCGenericObject.class);
-      addCollection(LCIO.LCSTRVEC,ILCStrVec.class);
-      addCollection(LCIO.MCPARTICLE,IMCParticle.class);
-      addCollection(LCIO.RAWCALORIMETERHIT,IRawCalorimeterHit.class,1<<LCIO.RCHBIT_ID1 | 1<<LCIO.RCHBIT_TIME);
-      addCollection(LCIO.SIMCALORIMETERHIT,ISimCalorimeterHit.class,1<<LCIO.CHBIT_ID1 | 1<<LCIO.CHBIT_LONG);
-      addCollection(LCIO.SIMTRACKERHIT,ISimTrackerHit.class);
-      addCollection(LCIO.TRACKERHIT,ITrackerHit.class);
-      addCollection(LCIO.TPCHIT,ITPCHit.class);
-      addCollection(LCIO.TRACKERRAWDATA,ITrackerRawData.class, 1<<LCIO.TRAWBIT_ID1);
-      addCollection(LCIO.TRACKERDATA,ITrackerData.class, 1<<LCIO.TRAWBIT_ID1);
-      addCollection(LCIO.TRACKERPULSE,ITrackerPulse.class, 1<<LCIO.TRAWBIT_ID1);
-
-      // Put in some links between objects, has to be done by hand for now
-      LCCollection collection = this.getCollection(LCIO.TRACKERHIT);
-      LCCollection target = this.getCollection(LCIO.TPCHIT);
-      for (Iterator i = collection.iterator(); i.hasNext(); )
-      {
-         int size = r.nextInt(10);
-         List l = new ArrayList(size);
-         for (int ii=0; ii<size; ii++)
-         {
-            l.add(target.get(r.nextInt(target.size())));
-         }
-         ITrackerHit hit = (ITrackerHit) i.next();
-         hit.setRawHits(l);
-      }
-
-      collection = this.getCollection(LCIO.CALORIMETERHIT);
-      target = this.getCollection(LCIO.RAWCALORIMETERHIT);
-      for (Iterator i = collection.iterator(); i.hasNext(); )
-      {
-         ICalorimeterHit hit = (ICalorimeterHit) i.next();
-         hit.setRawHit((LCObject) target.get(r.nextInt(target.size())));
-      }
-
-      collection = this.getCollection(LCIO.TRACKERPULSE);
-      target = this.getCollection(LCIO.TRACKERDATA);
-      for (Iterator i = collection.iterator(); i.hasNext(); )
-      {
-         ITrackerPulse hit = (ITrackerPulse) i.next();
-         hit.setTrackerData((TrackerData) target.get(r.nextInt(target.size())));
-      }
-
-      collection = this.getCollection(LCIO.TRACK);
-      target = this.getCollection(LCIO.TRACKERHIT);
-      for (Iterator i = collection.iterator(); i.hasNext(); )
-      {
-         int size = r.nextInt(10);
-         List l = new ArrayList(size);
-         for (int ii=0; ii<size; ii++)
-         {
-            l.add(target.get(r.nextInt(target.size())));
-         }
-         ITrack track = (ITrack) i.next();
-         track.setTrackerHits(l);
-      }
-
-      collection = this.getCollection(LCIO.CLUSTER);
-      target = this.getCollection(LCIO.CALORIMETERHIT);
-      for (Iterator i = collection.iterator(); i.hasNext(); )
-      {
-         int size = r.nextInt(10);
-         List l = new ArrayList(size);
-         float[] weights = new float[size];
-         for (int ii=0; ii<size; ii++)
-         {
-            l.add(target.get(r.nextInt(target.size())));
-            weights[ii] = r.nextFloat();
-         }
-         ICluster cluster = (ICluster) i.next();
-         cluster.setCalorimeterHits(l,weights);
-      }
-      collection = this.getCollection(LCIO.RECONSTRUCTEDPARTICLE);
-      target = this.getCollection(LCIO.CLUSTER);
-      for (Iterator i = collection.iterator(); i.hasNext(); )
-      {
-         int size = r.nextInt(10);
-         List l = new ArrayList(size);
-         for (int ii=0; ii<size; ii++)
-         {
-            l.add(target.get(r.nextInt(target.size())));
-         }
-         IReconstructedParticle particle = (IReconstructedParticle) i.next();
-         particle.setClusters(l);
-      }
-      collection = this.getCollection(LCIO.RECONSTRUCTEDPARTICLE);
-      target = this.getCollection(LCIO.TRACK);
-      for (Iterator i = collection.iterator(); i.hasNext(); )
-      {
-         int size = r.nextInt(10);
-         List l = new ArrayList(size);
-         for (int ii=0; ii<size; ii++)
-         {
-            l.add(target.get(r.nextInt(target.size())));
-         }
-         IReconstructedParticle particle = (IReconstructedParticle) i.next();
-         particle.setTracks(l);
-      }
-      collection = this.getCollection(LCIO.RECONSTRUCTEDPARTICLE);
-      for (Iterator i = collection.iterator(); i.hasNext(); )
-      {
-         int size = r.nextInt(10);
-         List l = new ArrayList(size);
-         for (int ii=0; ii<size; ii++)
-         {
-            IParticleID pid = new IParticleID();
-            randomize(pid);
-            l.add(pid);
-         }
-         IReconstructedParticle particle = (IReconstructedParticle) i.next();
-         particle.setParticleIDs(l);
-      }
-   }
-   private void addCollection(String type, Class klass)
-   {
-      addCollection(type,klass,0);
-   }
-   private void addCollection(String type, Class klass, int flag)
-   {
-      int n = r.nextInt(100);
-      LCCollection collection = new ILCCollection(type);
-      collection.setFlag(flag);
-      for (int i=0; i<n; i++)
-      {
-         try
-         {
-            Object obj = klass.newInstance();
-            randomize(obj);
-            collection.add(obj);
-         }
-         catch (Throwable t)
-         {
-            throw new RuntimeException("Could not create new instance of "+klass,t);
-         }
-      }
-      this.addCollection(collection, type);
-   }
-   private static void randomize(Object obj)
-   {
-      try
-      {
-         BeanInfo info = Introspector.getBeanInfo(obj.getClass());
-         PropertyDescriptor[] desc = info.getPropertyDescriptors();
-         for (int i=0; i<desc.length; i++)
-         {
-            Method m = desc[i].getWriteMethod();
-            Class type = desc[i].getPropertyType();
-            Object[] args = new Object[1];
-            if (m != null && type != null)
-            {
-               if (type == int.class)     args[0] = new Integer(r.nextInt());
-               if (type == float.class)   args[0] = new Float(r.nextFloat());
-               if (type == double.class)  args[0] = new Double(r.nextDouble());
-               if (type == long.class)    args[0] = new Long(r.nextLong());
-               if (type == boolean.class) args[0] = r.nextDouble() >= .5 ? Boolean.TRUE : Boolean.FALSE;
-               if (type == String.class)  args[0] = gobbledygook();
-            }
-            if (args[0] != null) m.invoke(obj,args);
-         }
-      }
-      catch (Throwable t)
-      {
-         throw new RuntimeException("Could not randomize "+obj.getClass(),t);
-      }
-   }
-
-   private static String gobbledygook()
-   {
-      StringBuffer buffer = new StringBuffer(10);
-      for (int i=0; i<10; i++) buffer.append((char) ('a'+r.nextInt(26)));
-      return buffer.toString();
-   }
+	private static Random r = new Random();
+	private int maxobj = 100;
+
+	public RandomEvent(int maxobj)
+	{
+		this.maxobj = maxobj;
+		create();
+	}
+	
+	public RandomEvent()
+	{
+		create();
+	}
+
+	private void create()
+	{
+		randomize(this);
+		addCollection(LCIO.TRACK, ITrack.class, 1 << LCIO.TRBIT_HITS);
+		addCollection(LCIO.CLUSTER, ICluster.class, 1 << LCIO.CLBIT_HITS);
+		addCollection(LCIO.RECONSTRUCTEDPARTICLE, IReconstructedParticle.class);
+		addCollection(LCIO.CALORIMETERHIT, ICalorimeterHit.class, 1 << LCIO.RCHBIT_ID1 | 1 << LCIO.RCHBIT_LONG | 1 << LCIO.RCHBIT_TIME);
+		addCollection(LCIO.LCFLOATVEC, ILCFloatVec.class);
+		addCollection(LCIO.LCINTVEC, ILCIntVec.class);
+		// FIXME: Put in ILCRelation.
+		//addCollection(LCIO.LCRELATION,ILCRelation.class);
+		addCollection(LCIO.LCGENERICOBJECT, ILCGenericObject.class);
+		addCollection(LCIO.LCSTRVEC, ILCStrVec.class);
+		addCollection(LCIO.MCPARTICLE, IMCParticle.class);
+		addCollection(LCIO.RAWCALORIMETERHIT, IRawCalorimeterHit.class, 1 << LCIO.RCHBIT_ID1 | 1 << LCIO.RCHBIT_TIME);
+		addCollection(LCIO.SIMCALORIMETERHIT, ISimCalorimeterHit.class, 1 << LCIO.CHBIT_ID1 | 1 << LCIO.CHBIT_LONG);
+		addCollection(LCIO.SIMTRACKERHIT, ISimTrackerHit.class);
+		addCollection(LCIO.TRACKERHIT, ITrackerHit.class);
+		addCollection(LCIO.TPCHIT, ITPCHit.class);
+		addCollection(LCIO.TRACKERRAWDATA, ITrackerRawData.class, 1 << LCIO.TRAWBIT_ID1);
+		addCollection(LCIO.TRACKERDATA, ITrackerData.class, 1 << LCIO.TRAWBIT_ID1);
+		addCollection(LCIO.TRACKERPULSE, ITrackerPulse.class, 1 << LCIO.TRAWBIT_ID1);
+
+		// Put in some links between objects, has to be done by hand for now
+		LCCollection collection = this.getCollection(LCIO.TRACKERHIT);
+		LCCollection target = this.getCollection(LCIO.TPCHIT);
+		for (Iterator i = collection.iterator(); i.hasNext();)
+		{
+			int size = r.nextInt(10);
+			List l = new ArrayList(size);
+			for (int ii = 0; ii < size; ii++)
+			{
+				l.add(target.get(r.nextInt(target.size())));
+			}
+			ITrackerHit hit = (ITrackerHit) i.next();
+			hit.setRawHits(l);
+		}
+
+		collection = this.getCollection(LCIO.CALORIMETERHIT);
+		target = this.getCollection(LCIO.RAWCALORIMETERHIT);
+		for (Iterator i = collection.iterator(); i.hasNext();)
+		{
+			ICalorimeterHit hit = (ICalorimeterHit) i.next();
+			hit.setRawHit((LCObject) target.get(r.nextInt(target.size())));
+		}
+
+		collection = this.getCollection(LCIO.TRACKERPULSE);
+		target = this.getCollection(LCIO.TRACKERDATA);
+		for (Iterator i = collection.iterator(); i.hasNext();)
+		{
+			ITrackerPulse hit = (ITrackerPulse) i.next();
+			hit.setTrackerData((TrackerData) target.get(r.nextInt(target.size())));
+		}
+
+		collection = this.getCollection(LCIO.TRACK);
+		target = this.getCollection(LCIO.TRACKERHIT);
+		for (Iterator i = collection.iterator(); i.hasNext();)
+		{
+			int size = r.nextInt(10);
+			List l = new ArrayList(size);
+			for (int ii = 0; ii < size; ii++)
+			{
+				l.add(target.get(r.nextInt(target.size())));
+			}
+			ITrack track = (ITrack) i.next();
+			track.setTrackerHits(l);
+		}
+
+		collection = this.getCollection(LCIO.CLUSTER);
+		target = this.getCollection(LCIO.CALORIMETERHIT);
+		for (Iterator i = collection.iterator(); i.hasNext();)
+		{
+			int size = r.nextInt(10);
+			List l = new ArrayList(size);
+			float[] weights = new float[size];
+			for (int ii = 0; ii < size; ii++)
+			{
+				l.add(target.get(r.nextInt(target.size())));
+				weights[ii] = r.nextFloat();
+			}
+			ICluster cluster = (ICluster) i.next();
+			cluster.setCalorimeterHits(l, weights);
+		}
+		collection = this.getCollection(LCIO.RECONSTRUCTEDPARTICLE);
+		target = this.getCollection(LCIO.CLUSTER);
+		for (Iterator i = collection.iterator(); i.hasNext();)
+		{
+			int size = r.nextInt(10);
+			List l = new ArrayList(size);
+			for (int ii = 0; ii < size; ii++)
+			{
+				l.add(target.get(r.nextInt(target.size())));
+			}
+			IReconstructedParticle particle = (IReconstructedParticle) i.next();
+			particle.setClusters(l);
+		}
+		collection = this.getCollection(LCIO.RECONSTRUCTEDPARTICLE);
+		target = this.getCollection(LCIO.TRACK);
+		for (Iterator i = collection.iterator(); i.hasNext();)
+		{
+			int size = r.nextInt(10);
+			List l = new ArrayList(size);
+			for (int ii = 0; ii < size; ii++)
+			{
+				l.add(target.get(r.nextInt(target.size())));
+			}
+			IReconstructedParticle particle = (IReconstructedParticle) i.next();
+			particle.setTracks(l);
+		}
+		collection = this.getCollection(LCIO.RECONSTRUCTEDPARTICLE);
+		for (Iterator i = collection.iterator(); i.hasNext();)
+		{
+			int size = r.nextInt(10);
+			List l = new ArrayList(size);
+			for (int ii = 0; ii < size; ii++)
+			{
+				IParticleID pid = new IParticleID();
+				randomize(pid);
+				l.add(pid);
+			}
+			IReconstructedParticle particle = (IReconstructedParticle) i.next();
+			particle.setParticleIDs(l);
+		}
+	}
+
+	private void addCollection(String type, Class klass)
+	{
+		addCollection(type, klass, 0);
+	}
+
+	private void addCollection(String type, Class klass, int flag)
+	{
+		int n = r.nextInt(maxobj);
+		if (n==0) n=1;
+		LCCollection collection = new ILCCollection(type);
+		collection.setFlag(flag);
+		for (int i = 0; i < n; i++)
+		{
+			try
+			{
+				Object obj = klass.newInstance();
+				randomize(obj);
+				collection.add(obj);
+			}
+			catch (Throwable t)
+			{
+				throw new RuntimeException("Could not create new instance of " + klass, t);
+			}
+		}
+		this.addCollection(collection, type);
+	}
+
+	private static void randomize(Object obj)
+	{
+		try
+		{
+			BeanInfo info = Introspector.getBeanInfo(obj.getClass());
+			PropertyDescriptor[] desc = info.getPropertyDescriptors();
+			for (int i = 0; i < desc.length; i++)
+			{
+				Method m = desc[i].getWriteMethod();
+				Class type = desc[i].getPropertyType();
+				Object[] args = new Object[1];
+				if (m != null && type != null)
+				{
+					if (type == int.class)
+						args[0] = new Integer(r.nextInt());
+					if (type == float.class)
+						args[0] = new Float(r.nextFloat());
+					if (type == double.class)
+						args[0] = new Double(r.nextDouble());
+					if (type == long.class)
+						args[0] = new Long(r.nextLong());
+					if (type == boolean.class)
+						args[0] = r.nextDouble() >= .5 ? Boolean.TRUE : Boolean.FALSE;
+					if (type == String.class)
+						args[0] = gobbledygook();
+				}
+				if (args[0] != null)
+					m.invoke(obj, args);
+			}
+		}
+		catch (Throwable t)
+		{
+			throw new RuntimeException("Could not randomize " + obj.getClass(), t);
+		}
+	}
+
+	private static String gobbledygook()
+	{
+		StringBuffer buffer = new StringBuffer(10);
+		for (int i = 0; i < 10; i++)
+			buffer.append((char) ('a' + r.nextInt(26)));
+		return buffer.toString();
+	}
 }

lcio/src/java/hep/lcio/util
RandomEventCommandHandler.java added at 1.1
diff -N RandomEventCommandHandler.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ RandomEventCommandHandler.java	7 Jun 2006 22:32:34 -0000	1.1
@@ -0,0 +1,106 @@
+package hep.lcio.util;
+
+import hep.lcio.implementation.io.LCFactory;
+import hep.lcio.io.LCWriter;
+import hep.lcio.test.RandomEvent;
+
+import java.io.File;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+
+/**
+ * CommandHandler for the random command.
+ * 
+ * @author jeremym
+ * @version $Id: RandomEventCommandHandler.java,v 1.1 2006/06/07 22:32:34 jeremy Exp $
+ */
+public class RandomEventCommandHandler extends CommandHandler
+{
+	File outfile;
+	int nevt = 1;
+	int maxobj = 100;
+	
+	public RandomEventCommandHandler()
+	{
+		super("random","Create LCEvents filled with randomly generated data.");
+		
+		options = createRandomEventOptions();
+	}
+	
+	/** 
+	 * Create options for the random command.
+	 */
+	private static Options createRandomEventOptions()
+	{
+		Options options = new Options();
+		
+		Option opt = new Option("o", true, "Set the LCIO output file name.");
+		opt.setArgs(1);
+		options.addOption(opt);
+		
+		opt = new Option("n", true, "Set the number of random events to generate.");
+		opt.setArgs(1);
+		options.addOption(opt);
+		
+		opt = new Option("m", true, "Set the maximum number of objects in a collection.");
+		opt.setArgs(1);
+		options.addOption(opt);
+		
+		return options;
+	}
+	
+	/**
+	 * Execute the random command with current options.
+	 */
+	public void execute() throws Exception
+	{
+		LCWriter writer = LCFactory.getInstance().createLCWriter();
+		writer.open(outfile.getAbsolutePath());
+		
+		for (int i=0; i<nevt; i++)
+		{
+			writer.writeEvent(new RandomEvent(maxobj));
+		}
+		
+		writer.close();
+	}
+
+	/**
+	 * Parse the raw options for the random command.
+	 */
+	public void parse(String[] argv) throws Exception
+	{
+		CommandLine cl = parser.parse(options, argv);
+		
+		String fname = "randomevents";
+		if (cl.hasOption("o"))
+		{
+			fname = cl.getOptionValue("o");
+		}
+		outfile = new File(fname);
+		
+		if (cl.hasOption("n"))
+		{
+			nevt = Integer.parseInt(cl.getOptionValue("n"));
+			
+			if (nevt < 1)
+			{
+				System.err.println("WARNING: Ignoring invalid value " + nevt + " for the -m switch.");
+				nevt = 1;
+			}
+		}
+		
+		if (cl.hasOption("m"))
+		{
+			maxobj = Integer.parseInt(cl.getOptionValue("m"));
+			
+			if (maxobj < 1)
+			{
+				System.err.println("WARNING: Ignoring invalid value " + maxobj + " for the -m switch.");
+				maxobj = 1;
+			}
+		}
+	}
+}

lcio/src/java/hep/lcio/util
CommandLineTool.java 1.8 -> 1.9
diff -u -r1.8 -r1.9
--- CommandLineTool.java	6 Jun 2006 21:27:03 -0000	1.8
+++ CommandLineTool.java	7 Jun 2006 22:32:34 -0000	1.9
@@ -33,13 +33,16 @@
  * @see hep.lcio.util.Compare compare [X]
  * -compare series of LCIO files
  * 
- * @see hep.lcio.util.Headers count
+ * @see hep.lcio.util.Headers count [X]
  * -print header info
  * -number of LCRunHeader
  * -number of LCEventHeader
  * -number of LCEvent
  * 
- * @see hep.lcio.util.PrintEvent dump
+ * @see hep.lcio.util.RandomEvent random [X]
+ * -generate X random events
+ * 
+ * @see hep.lcio.util.PrintEvent print
  * -dump lcio file (similar to C++ dump cmd)
  * 
  * @see hep.lcio.util.Filter filter
@@ -47,7 +50,7 @@
  * -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
@@ -55,7 +58,7 @@
  * FIXME: Implement all of the above commands.
  * 
  * @author jeremym
- * @version $Id: CommandLineTool.java,v 1.8 2006/06/06 21:27:03 jeremy Exp $
+ * @version $Id: CommandLineTool.java,v 1.9 2006/06/07 22:32:34 jeremy Exp $
  */
 public class CommandLineTool
 {
@@ -85,6 +88,24 @@
 	}	
 	
 	/**
+	 * Register default command handlers with the CommandLineTool.
+	 */
+	private void registerHandlers()
+	{
+		addCommandHandler(new MergeCommandHandler());
+		addCommandHandler(new SplitCommandHandler());
+		addCommandHandler(new ConcatenateCommandHandler());
+		addCommandHandler(new SIODumpCommandHandler());
+		addCommandHandler(new CompareCommandHandler());
+		addCommandHandler(new HeaderCountCommandHandler());
+		addCommandHandler(new RandomEventCommandHandler());
+		
+		// addCommandHandler(new PrintEventCommandHandler());
+		// addCommandHandler(new ValidateEventCommandHandler());
+		// addCommandHandler(new FilterEventCommandHandler());
+	}
+	
+	/**
 	 * Get the global options.
 	 * @return A CommandLine object representing the global options,
 	 * e.g. the options in front of the command.
@@ -106,22 +127,6 @@
 	}
 
 	/**
-	 * Register default command handlers with the CommandLineTool.
-	 */
-	private void registerHandlers()
-	{
-		addCommandHandler(new MergeCommandHandler());
-		addCommandHandler(new SplitCommandHandler());
-		addCommandHandler(new ConcatenateCommandHandler());
-		addCommandHandler(new SIODumpCommandHandler());
-		addCommandHandler(new CompareCommandHandler());
-		addCommandHandler(new HeaderCountCommandHandler());
-
-		// addCommandHandler("print", PrintEventCommandHandler());				
-		// addCommandHandler("random", RandomEventCommandHandler());
-	}
-
-	/**
 	 * Register the CommandLineTool's global options.
 	 */
 	private void registerOptions()
CVSspam 0.2.8