Print

Print


Commit in SlicDiagnostics on MAIN
src/org/lcsim/slic/diagnostics/CommandLineProcessor.java+189added 1.1
                              /Main.java+18-141.9 -> 1.10
                              /Runner.java+41-951.5 -> 1.6
                              /SlicDiagnosticsDriver.java+28-31.15 -> 1.16
test/org/lcsim/slic/diagnostics/RunnerTest.java+9-51.2 -> 1.3
+285-117
1 added + 4 modified, total 5 files
JM: Add command line options to SlicDiagnostics

SlicDiagnostics/src/org/lcsim/slic/diagnostics
CommandLineProcessor.java added at 1.1
diff -N CommandLineProcessor.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ CommandLineProcessor.java	17 Feb 2006 01:26:34 -0000	1.1
@@ -0,0 +1,189 @@
+package org.lcsim.slic.diagnostics;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+
+/**
+ * CommandLineProcessor parses command line options and arguments specific to SlicDiagnostics.  
+ * The resulting parameters are passed to the Runner class in Main.
+ * 
+ * @author Jeremy McCormick
+ * @version $Id: CommandLineProcessor.java,v 1.1 2006/02/17 01:26:34 jeremy Exp $
+ */
+public class CommandLineProcessor
+{
+    private Options _options = new Options();
+    private CommandLineParser _parser = new PosixParser();
+        
+    private List<File> _infiles = new ArrayList<File>();
+    private File _outfile = null;
+    private int _maxevents = -1;
+    private int _verbosity = 1;
+    
+    /** Create a CommandLineProcessor where parse() will be called later. */
+    public CommandLineProcessor()
+    {               
+        defineOptions();
+    }    
+    
+    /** Create a CommandLineProcessor and call parse on input args. */
+    public CommandLineProcessor(String args[])
+    {               
+        defineOptions();
+        
+        try {
+            parse(args);
+        }
+        catch (ParseException pe)
+        {
+            throw new RuntimeException("Problem parsing args", pe);
+        }
+    }
+    
+    /** Define the command-line options for SlicDiagnostics. */
+    private void defineOptions()
+    {
+        Option opt = new Option("f",false,"file containing list of files to process"); 
+        opt.setArgs(1);                  
+        _options.addOption(opt); 
+        
+        opt = new Option("i",false,"input files or patterns"); 
+        _options.addOption(opt);
+        
+        opt = new Option("n",false,"max events to process");       
+        _options.addOption(opt); 
+        
+        opt = new Option("o",false,"output file name"); 
+        opt.setArgs(1);        
+        _options.addOption(opt); 
+        
+        opt = new Option("h", false,"print usage");               
+        _options.addOption(opt); 
+        
+        opt = new Option("v",false,"set verbosity"); opt.setArgs(1);
+        _options.addOption(opt);        
+    }
+    
+    /** Parse command-line options. */
+    public void parse(String args[]) throws ParseException
+    {
+        CommandLine cl = _parser.parse(_options, args);
+               
+        if (cl.hasOption("h") || args.length == 0)
+        {
+            HelpFormatter help = new HelpFormatter();
+            help.printHelp("SlicDiagnostics", _options);
+            System.exit(0);
+        }
+        
+        if (cl.hasOption("f") || args.length == 1)
+        {     
+            String infileList = null;
+            if (cl.hasOption("f"))
+            {
+                infileList = cl.getOptionValue("f");
+            }
+            else if (args.length == 1)
+            {
+                infileList = args[0];
+            }
+            readInputFileList(infileList);
+        }            
+        
+        if (cl.hasOption("i"))
+        {            
+            String vals[] = cl.getOptionValues("i");            
+            for (String f : vals)
+            {               
+                _infiles.add(new File(f));
+            }
+        }
+                
+        if (cl.hasOption("n"))
+        {
+            _maxevents = Integer.parseInt(cl.getOptionValue("n"));
+        }
+        
+        if (cl.hasOption("v"))
+        {
+            _verbosity = Integer.parseInt(cl.getOptionValue("v"));
+        }        
+        
+        if (_infiles.size() == 0)
+        {
+            throw new RuntimeException("Didn't find any input files to process.");
+        }
+        
+        if (cl.hasOption("o"))
+        {
+            _outfile = new File(cl.getOptionValue("o"));
+        }        
+        else {
+            String outfile = _infiles.get(0).toString().replace(".slcio","");
+            _outfile = new File(outfile);
+        }       
+    }
+    
+    /** Get the list of input files created by parse(). */
+    public List<File> getInputFileList()
+    {
+        return _infiles;
+    }
+    
+    /** Get the output file name. */
+    public File getOutputFile()
+    {
+        return _outfile;
+    }
+    
+    /** Get the verbosity. */
+    public int verbosity()
+    {
+        return _verbosity;
+    }
+    
+    /** Get the maximum number of events to process. */
+    public int maxEvents()
+    {
+        return _maxevents;
+    }
+    
+    /** Convert a line-delimited list of input files into a List of java File objects. */
+    private void readInputFileList(String filelist)
+    {
+        try
+        {
+            BufferedReader lines = getBufferedReader(filelist);
+            String line;
+            
+            while ( (line = lines.readLine()) != null)
+            {
+                _infiles.add(new File(line.trim()));
+            }
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("Problem running from file.", e);
+        }            
+    }
+           
+    /** Simple utility function to get a BufferedReader. */
+    static private BufferedReader getBufferedReader(String fileName) throws IOException
+    {
+        FileInputStream fin =  new FileInputStream(fileName);
+        return new BufferedReader(new InputStreamReader(fin));
+    }                      
+}

SlicDiagnostics/src/org/lcsim/slic/diagnostics
Main.java 1.9 -> 1.10
diff -u -r1.9 -r1.10
--- Main.java	9 Feb 2006 01:13:20 -0000	1.9
+++ Main.java	17 Feb 2006 01:26:34 -0000	1.10
@@ -1,13 +1,11 @@
 package org.lcsim.slic.diagnostics;
 
 /**
- * Main takes a single argument, which is a file containing
- * a list of files to process, one per line.  The real 
- * functionality comes from the Runner, so Main just 
- * creates one of these and passes control to it.
- *  
+ * Main uses the CommandLineProcessor and Runner to setup and run a 
+ * SlicDiagnostics command-line job.
+ * 
  * @author jeremym
- * @version $Id: Main.java,v 1.9 2006/02/09 01:13:20 jeremy Exp $
+ * @version $Id: Main.java,v 1.10 2006/02/17 01:26:34 jeremy Exp $
  */
 public class Main
 {
@@ -15,17 +13,23 @@
     {
         main(args);
     }
-    
+
     public static void main(String[] args)
     {
-        if (args.length == 0)
+        try
         {
-            System.err.println("Missing name of file containing list of files to process.");
-            System.exit(1);
+            CommandLineProcessor cl = new CommandLineProcessor(args);
+            Runner runner = new Runner();
+            runner.run(
+                    cl.getInputFileList(), 
+                    cl.getOutputFile(),
+                    cl.maxEvents(),
+                    cl.verbosity()
+                    );
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("Problem running SlicDiagnostics.", e);
         }
-        
-        Runner runner = new Runner();
-        runner.addFiles(args[0]);
-        runner.run();
     }
 }
\ No newline at end of file

SlicDiagnostics/src/org/lcsim/slic/diagnostics
Runner.java 1.5 -> 1.6
diff -u -r1.5 -r1.6
--- Runner.java	5 Jan 2006 03:23:05 -0000	1.5
+++ Runner.java	17 Feb 2006 01:26:34 -0000	1.6
@@ -1,121 +1,67 @@
 package org.lcsim.slic.diagnostics;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
 import java.util.List;
-import java.util.ArrayList;
 import org.lcsim.util.loop.LCIOEventSource;
 import org.lcsim.util.loop.LCSimLoop;
 
 /**
- * Helper class to run SlicDiagnosticsDriver on a file or list of files.
- * For multiple input files, it will automatically name the output AIDA file
- * after the first one in the list.
+ * A Helper class that runs SlicDiagnosticsDriver on a list of files
+ * by using LCIOEventSource and LCSimLoop.
  *
  * @author jeremym
- * @version $Id: Runner.java,v 1.5 2006/01/05 03:23:05 jeremy Exp $
+ * @version $Id: Runner.java,v 1.6 2006/02/17 01:26:34 jeremy Exp $
  */
 public class Runner
-{
-    SlicDiagnosticsDriver _driver = new SlicDiagnosticsDriver();
-    List<String> _fileNames = new ArrayList<String>();
-    List<File> _fileList = new ArrayList<File>();
-    
-    public Runner()
-    {}
-    
-    public void addFile(String filename)
-    {
-        this._fileNames.add(filename);
-    }
-    
-    public void addFiles(String filelist)
-    {
-        try
-        {
-            BufferedReader lines = getBufferedReader(filelist);
-            String line;
-            
-            while ( (line = lines.readLine()) != null)
-            {
-                addFile(line.trim());
-            }
-        }
-        catch (Exception e)
-        {
-            throw new RuntimeException("Problem running from file.", e);
-        }
-    }
-    
-    public void addFiles(List<String> fileNamesIn)
-    {
-        for ( String file : fileNamesIn )
-        {
-            addFile(file);
-        }
-    }
-    
-    public void addFiles(String[] fileNamesIn)
-    {
-        for ( String file : fileNamesIn)
-        {
-            addFile(file);
-        }
-    }
-    
-    public void run()
-    {
-        try
-        {
-            run(this._fileNames);
-        }
-        catch (Exception e)
-        {
-            throw new RuntimeException("Problem running files.", e);
-        }
-    }
-    
-    /** Run SlicAnalysisDriver on files from list in fileName. */
-    private void run(List<String> files) throws Exception
-    {
-        _driver.setAidaFileName(files.get(0).toString().replace(".slcio",""));
-        
-        for ( String f : files)
-        {
-            File fileIn = new File(f);
-            this._fileList.add(fileIn);
-        }
+{    
+    /** Run on list of files with defaults for other args. */
+    public static void run(List<File> files) throws Exception
+    {        
+        String outfile = files.get(0).toString().replace(".slcio","");
+        run(files, new File(outfile), -1, 1);
+    }
+    
+    /** Run on list files with all args. */
+    public static void run(List<File> files, File outfile, int maxevents, int verbosity) throws Exception
+    {           
+        SlicDiagnosticsDriver driver = new SlicDiagnosticsDriver();
+        
+        // Set name of output AIDA file
+        driver.setAidaFileName(outfile.toString());
+        
+        // Set verbosity of driver
+        driver.setVerbosity(verbosity);
+                
+        // Create a new record source with the list of files
+        LCIOEventSource src = new LCIOEventSource("SlicDiagnostics", files);               
         
-        LCIOEventSource src = new LCIOEventSource("SlicDiagnostics", _fileList);
+        // Create a new record loop
+        LCSimLoop loop = new LCSimLoop();  
         
-        LCSimLoop loop = new LCSimLoop();
+        // Setup the loop with the record source
         loop.setLCIORecordSource(src);
-        loop.add(_driver);
+        
+        // Add the SlicDiagnostics driver
+        loop.add(driver);
+        
+        if (maxevents != -1)
+        {
+            System.out.println("Maximum Events <" + maxevents + ">");
+        }
         
         try
         {
-            loop.loop(-1);
+            loop.loop(maxevents);     
         }
         catch (org.freehep.record.loop.NoLoopRecordException nlre)
         {
-            /* ignore this exception */
+            // Ignore this exception 
         }
         catch (Exception e)
         {
             System.out.println(e.getMessage());
         }
-        
-        loop.dispose();
-        
-        this._fileList.clear();
-    }
-    
-    static private BufferedReader getBufferedReader(String fileName) throws IOException
-    {
-        FileInputStream fin =  new FileInputStream(fileName);
-        return new BufferedReader(new InputStreamReader(fin));
-    }
-}
\ No newline at end of file
+                      
+        loop.dispose();        
+    }    
+}

SlicDiagnostics/src/org/lcsim/slic/diagnostics
SlicDiagnosticsDriver.java 1.15 -> 1.16
diff -u -r1.15 -r1.16
--- SlicDiagnosticsDriver.java	13 Feb 2006 22:05:52 -0000	1.15
+++ SlicDiagnosticsDriver.java	17 Feb 2006 01:26:34 -0000	1.16
@@ -18,7 +18,7 @@
  * A mother driver that loads all of the package's subdrivers.
  *
  * @author jeremym
- * @version $Id: SlicDiagnosticsDriver.java,v 1.15 2006/02/13 22:05:52 jeremy Exp $
+ * @version $Id: SlicDiagnosticsDriver.java,v 1.16 2006/02/17 01:26:34 jeremy Exp $
  */
 public class SlicDiagnosticsDriver extends Driver
 {
@@ -28,6 +28,7 @@
     private String _fileName = null;
     private static final String _defaultFileName = "SlicDiagnostics";            
     private EventHeader _currentEvent = null;
+    private int _verbosity = 1;
     
     private int _evtCnt = 0;
     
@@ -35,7 +36,11 @@
     {
         super();
         addDefaultSubdrivers();
-        message(this.getClass().getSimpleName() + " is loaded.");
+        
+        if (_verbosity > 0)
+        {
+            message(this.getClass().getSimpleName() + " is loaded.");
+        }
     }
     
     private void addDefaultSubdrivers()
@@ -54,6 +59,11 @@
         _currentEvent = event;
         super.process(event);
         ++_evtCnt;
+                
+        if (_verbosity > 1 && (_evtCnt % 100 == 0))
+        {
+            message("Processed events <" + _evtCnt + ">");
+        }
     }
     
     protected void startOfData()
@@ -67,7 +77,11 @@
     {
         super.endOfData();
         savePlots();
-        message("Processed " + "<" + _evtCnt + "> events.");
+        
+        if (_verbosity > 0)
+        {
+            message("Total events processed <" + _evtCnt + ">");
+        }
     }
     
     static AIDA aida()
@@ -149,4 +163,15 @@
     {
         System.out.println("SlicDiagnosticsDriver - " + m);
     }
+    
+    public void setVerbosity(int verbosity)
+    {
+        assert(verbosity >= 0 && verbosity <= 4);
+        _verbosity = verbosity;
+    }
+    
+    public int getVerbosity()
+    {
+        return _verbosity;
+    }
 }
\ No newline at end of file

SlicDiagnostics/test/org/lcsim/slic/diagnostics
RunnerTest.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- RunnerTest.java	4 Aug 2005 01:39:51 -0000	1.2
+++ RunnerTest.java	17 Feb 2006 01:26:34 -0000	1.3
@@ -9,11 +9,12 @@
 
 import java.io.File;
 import java.net.URL;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
+import java.util.ArrayList;
+
 import org.lcsim.util.cache.FileCache;
 
+import junit.framework.*;
+
 /**
  *
  * @author jeremym
@@ -38,9 +39,12 @@
                 new URL("http://www.lcsim.org/test/lcio/ZPoleUDS_50evt_SLIC_v1r9p1_sidmay05.slcio")
         );
         
+        ArrayList<File> files = new ArrayList<File>();
+        files.add(file);
+        
         Runner runner = new Runner();
-        runner.addFile(file.toString());
-        runner.run();
+        //runner.addFile(file.toString());
+        runner.run(files);  
         runner = null;
     }
 }
\ No newline at end of file
CVSspam 0.2.8