Print

Print


Commit in lcio/src/java/hep/lcio/util on MAIN
HeaderCount.java+131added 1.1
HeaderCountCommandHandler.java+101added 1.1
CommandLineTool.java+16-151.7 -> 1.8
+248-15
2 added + 1 modified, total 3 files
JM: Add a count command to CommandLineTool for printing number of runs and events in an LCIO file.

lcio/src/java/hep/lcio/util
HeaderCount.java added at 1.1
diff -N HeaderCount.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ HeaderCount.java	6 Jun 2006 21:27:03 -0000	1.1
@@ -0,0 +1,131 @@
+package hep.lcio.util;
+
+import hep.lcd.io.sio.SIOReader;
+import hep.lcd.io.sio.SIORecord;
+import hep.lcio.event.LCIO;
+
+import java.io.BufferedInputStream;
+import java.io.EOFException;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Utility method for counting the number of runs and events in an LCIO file.
+ * 
+ * @author jeremym
+ * @version $Id: HeaderCount.java,v 1.1 2006/06/06 21:27:03 jeremy Exp $
+ */
+public class HeaderCount
+{
+	/**
+	 * Count number of events and runs.
+	 * 
+	 * @param pstr The PrintStream where output will go.
+	 * @param files A list of LCIO input files.
+	 * @param printEvent Print number of events.
+	 * @param printRun Print number of runs.
+	 * @param printTot Print totals.
+	 */
+	public static void countHeaders(PrintStream pstr, List files, boolean printEvent, boolean printRun, boolean printTot)
+	{		
+		int evttot = 0;
+		int runtot = 0;
+		StringBuffer strbuff = new StringBuffer();
+		for (Iterator iter = files.iterator(); iter.hasNext(); ) 
+		{
+			int evt = 0;
+			int run = 0;
+			
+			File file = (File)iter.next();
+
+			strbuff.append(file.getAbsolutePath());
+			
+			SIOReader reader;
+			try {
+				reader = new SIOReader(new BufferedInputStream(new FileInputStream(file.getAbsolutePath())));
+			}
+			catch (Exception e)
+			{
+				throw new RuntimeException(e);
+			}
+			
+			for (;;)
+			{
+				SIORecord record;
+				
+				try {
+					record = reader.readRecord();
+				}
+				catch (EOFException x)
+				{
+					break;
+				}
+				catch (IOException x)
+				{
+					throw new RuntimeException(x);
+				}
+				
+				String name;
+				try {
+					name = record.getRecordName();
+				}
+				catch (IOException x)
+				{
+					throw new RuntimeException(x);
+				}
+				
+				if (name.compareTo(LCIO.LCEVENT) == 0)
+				{
+					evt++;
+				}
+				else if (name.compareTo(LCIO.LCRUNHEADER) == 0)
+				{
+					run++;
+				}				
+			}
+			
+			if (printRun)
+			{
+				strbuff.append('\t');
+				strbuff.append(run);
+			}
+			
+			if (printEvent)
+			{
+				strbuff.append('\t');
+				strbuff.append(evt);
+			}
+			
+			if (printRun || printEvent)
+			{
+				strbuff.append('\n');
+			}
+			
+			evttot += evt;
+			runtot += run;
+			
+			try {
+				reader.close();
+			}
+			catch (IOException x)
+			{}
+			
+			reader = null;
+		}
+		
+		if (printTot)
+		{
+			strbuff.append("TOTAL");
+			strbuff.append('\t');
+			strbuff.append(runtot);
+			strbuff.append('\t');
+			strbuff.append(evttot);
+		}
+		
+		pstr.print(strbuff.toString());
+	}
+}
\ No newline at end of file

lcio/src/java/hep/lcio/util
HeaderCountCommandHandler.java added at 1.1
diff -N HeaderCountCommandHandler.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ HeaderCountCommandHandler.java	6 Jun 2006 21:27:03 -0000	1.1
@@ -0,0 +1,101 @@
+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 handling for the count command.
+ * 
+ * @author jeremym
+ * @version $Id: HeaderCountCommandHandler.java,v 1.1 2006/06/06 21:27:03 jeremy Exp $
+ */
+public class HeaderCountCommandHandler extends CommandHandler
+{
+	boolean printRun = false;
+	boolean printEvent = false;
+	boolean printTot = false;
+	List files = new ArrayList();
+	
+	HeaderCountCommandHandler()
+	{
+		super("count","Count number of run and event headers.");
+		
+		options = createHeaderCountOptions();
+	}
+	
+	/**
+	 * Create the options for the count command. 
+	 * @return Options object with HeaderCount options.
+	 */
+	private static Options createHeaderCountOptions()
+	{
+		Options options = new Options();
+		
+		Option opt = new Option("r", false, "Print number of run headers.");
+		options.addOption(opt);
+		
+		opt = new Option("e", false, "Print number of event headers.");
+		options.addOption(opt);
+		
+		opt = new Option("t", false, "Print totals of all files processed.");
+		options.addOption(opt);
+		
+		opt = new Option("f", true, "Add an input LCIO file.");
+		opt.setArgs(1);
+		options.addOption(opt);
+		
+		return options;
+	}
+
+	/**
+	 * Parse the command line for the count command.
+	 */
+	public void parse(String[] argv) throws Exception
+	{
+		CommandLine cl = parser.parse(options, argv);
+		
+		if (cl.hasOption("r"))
+		{
+			printRun = true;
+		}
+		
+		if (cl.hasOption("e"))
+		{
+			printEvent = true;
+		}
+		
+		if (cl.hasOption("t")) {
+			printTot = true;
+		}
+		
+		if (!printRun && !printEvent && !printTot)
+		{
+			printEvent = true;
+		}
+		
+		if (cl.hasOption("f"))
+		{
+			String[] f = cl.getOptionValues("f");
+			for (int i=0; i<f.length; i++)
+			{
+				files.add(new File(f[i]));
+			}
+		}
+		else {
+			printUsage(true);
+		}
+	}
+
+	/**
+	 * Execute the count command with current arguments.
+	 */
+	public void execute() throws Exception
+	{
+		HeaderCount.countHeaders(System.out, files, printEvent, printRun, printTot);
+	}
+}

lcio/src/java/hep/lcio/util
CommandLineTool.java 1.7 -> 1.8
diff -u -r1.7 -r1.8
--- CommandLineTool.java	2 Jun 2006 23:48:34 -0000	1.7
+++ CommandLineTool.java	6 Jun 2006 21:27:03 -0000	1.8
@@ -33,7 +33,7 @@
  * @see hep.lcio.util.Compare compare [X]
  * -compare series of LCIO files
  * 
- * @see hep.lcio.util.Headers head
+ * @see hep.lcio.util.Headers count
  * -print header info
  * -number of LCRunHeader
  * -number of LCEventHeader
@@ -45,6 +45,8 @@
  * @see hep.lcio.util.Filter filter
  * -filter out/in by coll type
  * -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?
@@ -53,7 +55,7 @@
  * FIXME: Implement all of the above commands.
  * 
  * @author jeremym
- * @version $Id: CommandLineTool.java,v 1.7 2006/06/02 23:48:34 jeremy Exp $
+ * @version $Id: CommandLineTool.java,v 1.8 2006/06/06 21:27:03 jeremy Exp $
  */
 public class CommandLineTool
 {
@@ -113,9 +115,9 @@
 		addCommandHandler(new ConcatenateCommandHandler());
 		addCommandHandler(new SIODumpCommandHandler());
 		addCommandHandler(new CompareCommandHandler());
-		
-		// addCommandHandler("print", PrintEventCommandHandler());
-		// addCommandHandler("header", HeaderScanCommandHandler());		
+		addCommandHandler(new HeaderCountCommandHandler());
+
+		// addCommandHandler("print", PrintEventCommandHandler());				
 		// addCommandHandler("random", RandomEventCommandHandler());
 	}
 
@@ -160,7 +162,6 @@
 	 */
 	// 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.
@@ -175,7 +176,7 @@
 		// Set the command to execute.
 		setCommand(argv[icmd]);
 
-		System.out.println("Executing command: " + getCommand());
+		//System.out.println("Executing command: " + getCommand());
 
 		// Get a command handler for this command string.
 		CommandHandler handler = getCommandHandler(getCommand());
@@ -187,20 +188,20 @@
 		{
 			globargv = new String[nglob];
 			System.arraycopy(argv, 0, globargv, 0, nglob);
-			for (int i = 0; i < globargv.length; i++)
-			{
-				System.out.println("globargv[" + i + "]=" + globargv[i]);
-			}
+			//for (int i = 0; i < globargv.length; i++)
+			//{
+			//	System.out.println("globargv[" + i + "]=" + globargv[i]);
+			//}
 		}
 
 		// Arguments are passed verbatim to the command.
 		int ncmd = argv.length - (icmd + 1);
 		String[] cmdargv = new String[ncmd];
 		System.arraycopy(argv, icmd + 1, cmdargv, 0, ncmd);
-		for (int i = 0; i < cmdargv.length; i++)
-		{
-			System.out.println("cmdargv[" + i + "]=" + cmdargv[i]);
-		}
+		//for (int i = 0; i < cmdargv.length; i++)
+		//{
+		//	System.out.println("cmdargv[" + i + "]=" + cmdargv[i]);
+		//}
 
 		// Parse global options.
 		cl = parser.parse(options, globargv);
CVSspam 0.2.8