Print

Print


Commit in lcsim-cal-calib/src/org/lcsim/cal/calib on MAIN
Calibrate.java+124-301.4 -> 1.5
Generalize main class to be able to specify driver at runtime.

lcsim-cal-calib/src/org/lcsim/cal/calib
Calibrate.java 1.4 -> 1.5
diff -u -r1.4 -r1.5
--- Calibrate.java	20 May 2008 17:43:42 -0000	1.4
+++ Calibrate.java	21 May 2008 17:34:39 -0000	1.5
@@ -3,7 +3,7 @@
  *
  * Created on May 19, 2008, 11:50 AM
  *
- * $Id: Calibrate.java,v 1.4 2008/05/20 17:43:42 ngraf Exp $
+ * $Id: Calibrate.java,v 1.5 2008/05/21 17:34:39 ngraf Exp $
  */
 
 package org.lcsim.cal.calib;
@@ -11,9 +11,16 @@
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStreamReader;
+import java.text.DecimalFormat;
 import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
 import java.util.List;
+import org.lcsim.util.Driver;
 import org.lcsim.util.aida.AIDA;
 import org.lcsim.util.loop.LCIOEventSource;
 import org.lcsim.util.loop.LCSimLoop;
@@ -32,51 +39,129 @@
     
     public static void main(String[] args) throws Exception
     {
-        
-        if(args.length<1)
+        // remind users of correct calling sequence
+        if(args.length<2)
         {
             usage();
             return ;
         }
-
-        String listOfFiles = args[0];
- 
-        List<File> filesToProcess = filesToProcess(listOfFiles);
-        LCIOEventSource src = new LCIOEventSource("SamplingFractionAnalysis", filesToProcess);
-        
-        int numToProcess=-1;
-        if(args.length>1) numToProcess=Integer.parseInt(args[1]);
-        
-        System.out.println("Processing "+numToProcess+" events from "+listOfFiles);
-        LCSimLoop loop = new LCSimLoop();
-        loop.setLCIORecordSource(src);
-        System.out.println("adding the driver");
-        loop.add(new SamplingFractionAnalysisDriver());
-        System.out.println("looping");
+        
+        // which Driver should we run?
+        String driverName = args[0];
+        Driver driver = Calibrate.createDriver(driverName);
+        if(driver == null)
+        {
+            return;
+        }
+        
+        // which files to process?
+        String listOfFiles = args[1];
+        List<File> filesToProcess = null;
+        LCIOEventSource src = null;
         try
         {
-            loop.loop(numToProcess);
+            filesToProcess = filesToProcess(listOfFiles);
         }
-        catch(Exception e)
+        catch (FileNotFoundException x)
         {
-            e.printStackTrace();
+            System.out.println("File "+filesToProcess+ " not found!");
+            return;
+        }
+        try
+        {
+            src = new LCIOEventSource("Calibrate", filesToProcess);
+            
+            int numToProcess=-1;
+            if(args.length>2) numToProcess=Integer.parseInt(args[2]);
+            
+            System.out.println("Processing "+(numToProcess<0 ? "all": numToProcess) +" events from "+listOfFiles+" using "+ driverName );
+            LCSimLoop loop = new LCSimLoop();
+            loop.setLCIORecordSource(src);
+            System.out.println("adding the driver");
+            loop.add(driver);
+            System.out.println("looping");
+            try
+            {
+                loop.loop(numToProcess);
+            }
+            catch(Exception e)
+            {
+                System.out.println("Error during event processing loop");
+                e.printStackTrace();
+                return;
+            }
+            System.out.println("done looping");
+            loop.dispose();
+            
+            //remove suffix from list of filenames
+            int truncate = listOfFiles.lastIndexOf(".");
+            String listOfFilesName = listOfFiles.substring(0,truncate);
+            //remove any directories from the filename
+            truncate = listOfFiles.lastIndexOf("/");
+            listOfFilesName = listOfFilesName.substring(truncate,listOfFilesName.length());
+            String defaultAidaFileName = driverName+"_"+listOfFilesName+"_"+date()+".aida";
+            System.out.println("aida file written to "+defaultAidaFileName);
+            AIDA.defaultInstance().saveAs(defaultAidaFileName);
+        }
+        catch (IOException x)
+        {
+            System.out.println("Experienced an IOException during");
+            x.printStackTrace();
+            return;
         }
-        System.out.println("done looping");
-        loop.dispose();
-        
-        int truncate = listOfFiles.lastIndexOf(".");
-        String listOfFilesName = listOfFiles.substring(0,truncate);
-        System.out.println(listOfFilesName);
-        AIDA.defaultInstance().saveAs(listOfFilesName+"_SamplingFractionAnalysis.aida");
     }
     
     public static void usage()
     {
         System.out.println("This is Calibrate");
         System.out.println("usage:");
-        System.out.println("java Calibrate listOfInputFiles [number of events to process]");
+        System.out.println("java Calibrate fullyQualifiedDriverName listOfInputFiles [number of events to process]");
+    }
+    
+    public static Driver createDriver(String name)
+    {
+        // We're given the (string) name of a driver class.
+        // First, make a Class object for that class.
+        // If the named class doesn't exist, this will throw
+        // a ClassNotFoundException:
+        Class newClassObject = null;
+        try
+        {
+            newClassObject = Class.forName(name);
+        }
+        catch (java.lang.ClassNotFoundException x)
+        {
+            System.out.println("\nYour Driver -- "+name+" -- is not recognized as a valid class \n");
+            System.out.println("Is it fully qualified? Please check the full package name.");
+            System.out.println("Is it in your classpath?\n");
+            return null;
+            // throw new AssertionError(x);
+        }
+        
+        // Next, create an instance of the class:
+        // This can throw InstantiationException, IllegalAccessException
+        Object newObject = null;
+        try
+        {
+            newObject = newClassObject.newInstance();
+        }
+        catch (java.lang.InstantiationException x)
+        {
+            throw new AssertionError(x);
+        }
+        catch (java.lang.IllegalAccessException x )
+        {
+            throw new AssertionError(x);
+        }
+        
+        // This better be a driver. Cast it:
+        Driver newDriver = (Driver) newObject;
+        
+        // OK. Now return it.
+        return newDriver;
     }
     
+    
     public static List<File> filesToProcess(String listOfFiles) throws Exception
     {
         List<File> filesToProcess = new ArrayList<File>();
@@ -90,8 +175,17 @@
             if(!f.exists()) throw new RuntimeException("Input file "+f+ " does not exist!");
             filesToProcess.add(f);
         }
-        
         return filesToProcess;
     }
+    private static String date()
+    {
+        Calendar cal = new GregorianCalendar();
+        Date date = new Date();
+        cal.setTime(date);
+        DecimalFormat formatter = new DecimalFormat("00");
+        String day = formatter.format(cal.get(Calendar.DAY_OF_MONTH));
+        String month =  formatter.format(cal.get(Calendar.MONTH)+1);
+        return cal.get(Calendar.YEAR)+month+day;
+    }
     
 }
CVSspam 0.2.8