Commit in lcsim/src/org/lcsim/contrib/Cassell on MAIN
DriverLoop.java+124added 1.1
Convenience loop for running a Driver over lcio files outside of JAS

lcsim/src/org/lcsim/contrib/Cassell
DriverLoop.java added at 1.1
diff -N DriverLoop.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ DriverLoop.java	13 May 2008 19:11:07 -0000	1.1
@@ -0,0 +1,124 @@
+// Based on the class written by Jan Strube, adapted by Mat Charles.
+//
+// NOT a general purpose loop. Specific to lcio files, and to processing a Driver.
+// Arguments are: 0 - #events
+//                1 - name of output aida file
+//                2 - name of Driver to run
+//            3...n - names of lcio files to process
+//
+// Ron Cassell
+//
+
+package org.lcsim.contrib.Cassell;
+
+import java.io.File;
+import org.lcsim.mc.fast.MCFast;
+import org.lcsim.util.Driver;
+import org.lcsim.util.aida.AIDA;
+import org.lcsim.util.loop.LCIODriver;
+import org.lcsim.util.loop.LCSimLoop;
+import org.freehep.record.loop.LoopSourceExhaustedException;
+
+public class DriverLoop
+{
+    public DriverLoop()
+    {
+    }
+    public static void main(String[] args) throws Exception
+    {
+        if (args.length<4 || args[0].equals("h"))
+        {
+            //usage();
+            System.exit(1);
+        }
+        else
+        {
+            // First arg: Number of events to analyze
+            long numToProcess = Integer.parseInt(args[0]);
+            // Second arg: Name of default AIDA output file
+            String aidaout = args[1];
+            // Third arg: name of Driver to run
+            LCSimLoop loop = new LCSimLoop();
+            Driver myDriver = DriverLoop.makeDriverFromString(args[2]);
+            loop.add(myDriver);
+            long eventsProcessed = 0;
+            // Fourth and subsequent args: names of SIO files to analyze
+            for (int i=3; i<args.length && numToProcess>0; i++)
+            {
+                String filename = args[i];
+                File input = new File(filename);
+                loop.setLCIORecordSource(input);
+                long eventsCountedFromThisFile = 0;
+                long eventsProcessedAtStartOfThisFile = loop.getTotalCountableSupplied();
+                while (numToProcess>0)
+                {
+                    try
+                    {
+                        long eventsProcessedThisIteration = loop.loop(numToProcess);
+                        eventsCountedFromThisFile += eventsProcessedThisIteration;
+                        numToProcess -= eventsProcessedThisIteration;
+                    }
+                    catch (LoopSourceExhaustedException x)
+                    {
+                        long eventsProcessedAtEndOfThisFile = loop.getTotalCountableSupplied();
+                        System.out.println("File "+filename+" exhausted after "+eventsProcessedAtEndOfThisFile+" records total. ["+x+"]");
+                        // Adjust for events processed this loop
+                        long extraProcessed = (eventsProcessedAtEndOfThisFile-eventsProcessedAtStartOfThisFile) - eventsCountedFromThisFile;
+                        if (extraProcessed>0)
+                        {
+                            numToProcess -= extraProcessed;
+                        }
+                        break;
+                    }
+                }
+                
+                eventsProcessed = loop.getTotalCountableSupplied();
+                long eventsProcessedFromThisFile = eventsProcessed - eventsProcessedAtStartOfThisFile;
+                System.out.println("Processed "+eventsProcessedFromThisFile+" events from file "+filename+",: Total is "+eventsProcessed+", with "+numToProcess+" to go.");
+                
+            }
+            loop.dispose();
+            AIDA.defaultInstance().saveAs(aidaout);
+            System.out.println("Processed "+eventsProcessed+" events total.");
+        }
+    }
+    
+    public static Driver makeDriverFromString(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)
+        {
+            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;
+    }
+}
CVSspam 0.2.8