View Javadoc

1   /*
2    * Calibrate.java
3    *
4    * Created on May 19, 2008, 11:50 AM
5    *
6    * $Id: Calibrate.java,v 1.8 2008/05/21 20:30:56 ngraf Exp $
7    */
8   
9   package org.lcsim.cal.calib;
10  
11  import java.io.BufferedReader;
12  import java.io.File;
13  import java.io.FileInputStream;
14  import java.io.FileNotFoundException;
15  import java.io.IOException;
16  import java.io.InputStreamReader;
17  import java.text.DecimalFormat;
18  import java.util.ArrayList;
19  import java.util.Calendar;
20  import java.util.Date;
21  import java.util.GregorianCalendar;
22  import java.util.List;
23  import org.lcsim.util.Driver;
24  import org.lcsim.util.aida.AIDA;
25  import org.lcsim.util.loop.LCIOEventSource;
26  import org.lcsim.util.loop.LCSimLoop;
27  
28  /**
29   *
30   * @author Norman Graf
31   */
32  public class Calibrate
33  {
34      
35      /** Creates a new instance of Calibrate */
36      public Calibrate()
37      {
38      }
39      
40      public static void main(String[] args) throws Exception
41      {
42          // remind users of correct calling sequence
43          if(args.length<2)
44          {
45              usage();
46              return ;
47          }
48          
49          // which Driver should we run?
50          String driverName = args[0];
51          Driver driver = Calibrate.createDriver(driverName);
52          if(driver == null)
53          {
54              return;
55          }
56          
57          // which files to process?
58          String listOfFiles = args[1];
59          List<File> filesToProcess = null;
60          LCIOEventSource src = null;
61          try
62          {
63              filesToProcess = filesToProcess(listOfFiles);
64          }
65          catch (FileNotFoundException x)
66          {
67              System.out.println("File "+filesToProcess+ " not found!");
68              return;
69          }
70          try
71          {
72              src = new LCIOEventSource("Calibrate", filesToProcess);
73              
74              int numToProcess=-1;
75              if(args.length>2) numToProcess=Integer.parseInt(args[2]);
76              
77              System.out.println("Processing "+(numToProcess<0 ? "all": numToProcess) +" events from "+listOfFiles+" using "+ driverName );
78              LCSimLoop loop = new LCSimLoop();
79              loop.setLCIORecordSource(src);
80              System.out.println("adding the driver");
81              loop.add(driver);
82              System.out.println("looping");
83              try
84              {
85                  loop.loop(numToProcess);
86              }
87              catch(Exception e)
88              {
89                  System.out.println("Error during event processing loop");
90                  e.printStackTrace();
91                  return;
92              }
93              System.out.println("done looping");
94              loop.dispose();
95              
96              //remove suffix from list of filenames
97              int truncate = listOfFiles.lastIndexOf(".");
98              String listOfFilesName = listOfFiles.substring(0,truncate);
99  
100             String defaultAidaFileName = listOfFilesName+"_"+driver.getClass().getSimpleName()+"_"+date()+".aida";
101             System.out.println("aida file written to "+defaultAidaFileName);
102             AIDA.defaultInstance().saveAs(defaultAidaFileName);
103         }
104         catch (IOException x)
105         {
106             System.out.println("Experienced an IOException during");
107             x.printStackTrace();
108             return;
109         }
110     }
111     
112     public static void usage()
113     {
114         System.out.println("This is Calibrate");
115         System.out.println("usage:");
116         System.out.println("java Calibrate fullyQualifiedDriverName listOfInputFiles [number of events to process]");
117     }
118     
119     public static Driver createDriver(String name)
120     {
121         // We're given the (string) name of a driver class.
122         // First, make a Class object for that class.
123         // If the named class doesn't exist, this will throw
124         // a ClassNotFoundException:
125         Class newClassObject = null;
126         try
127         {
128             newClassObject = Class.forName(name);
129         }
130         catch (java.lang.ClassNotFoundException x)
131         {
132             System.out.println("\nYour Driver -- "+name+" -- is not recognized as a valid class \n");
133             System.out.println("Is it fully qualified? Please check the full package name.");
134             System.out.println("Is it in your classpath?\n");
135             return null;
136             // throw new AssertionError(x);
137         }
138         
139         // Next, create an instance of the class:
140         // This can throw InstantiationException, IllegalAccessException
141         Object newObject = null;
142         try
143         {
144             newObject = newClassObject.newInstance();
145         }
146         catch (java.lang.InstantiationException x)
147         {
148             throw new AssertionError(x);
149         }
150         catch (java.lang.IllegalAccessException x )
151         {
152             throw new AssertionError(x);
153         }
154         
155         // This better be a driver. Cast it:
156         Driver newDriver = (Driver) newObject;
157         
158         // OK. Now return it.
159         return newDriver;
160     }
161     
162     
163     public static List<File> filesToProcess(String listOfFiles) throws Exception
164     {
165         List<File> filesToProcess = new ArrayList<File>();
166         FileInputStream fin =  new FileInputStream(listOfFiles);
167         BufferedReader br =  new BufferedReader(new InputStreamReader(fin));
168         String line;
169         
170         while ( (line = br.readLine()) != null)
171         {
172             File f = new File(line.trim());
173             if(!f.exists()) throw new RuntimeException("Input file "+f+ " does not exist!");
174             filesToProcess.add(f);
175         }
176         return filesToProcess;
177     }
178     private static String date()
179     {
180         Calendar cal = new GregorianCalendar();
181         Date date = new Date();
182         cal.setTime(date);
183         DecimalFormat formatter = new DecimalFormat("00");
184         String day = formatter.format(cal.get(Calendar.DAY_OF_MONTH));
185         String month =  formatter.format(cal.get(Calendar.MONTH)+1);
186         return cal.get(Calendar.YEAR)+month+day;
187     }
188     
189 }