View Javadoc

1   /*
2    * Splitter.java
3    *
4    * Created on August 31, 2001, 5:47 PM
5    *
6    * Modified June 15, 2002 N.A. Graf
7    * Output file names are now derived from input file names.
8    * Trap normal EOF exceptions.
9    * Added usage hints.
10   *
11   */
12  package org.lcsim.util.stdhep;
13  
14  import java.io.*;
15  import java.util.StringTokenizer;
16  import hep.io.stdhep.*;
17  
18  /**
19   * @author tonyj
20   */
21  public class Splitter
22  {
23  	/**
24  	* @param args the command line arguments
25  	*/
26  	public static void main(String args[]) throws Exception
27  	{
28  		// remind user of correct usage
29  		if(args.length<1) usage();
30  		if(args.length==1 && args[0].equals("-h")) usage();
31  		
32  		// get input file
33  		String inputFile = args[0];                
34  
35  		// does it exist?
36  		File f = new File(inputFile);
37  		if (!f.exists())
38  		{
39  			System.out.println("\n\n  File "+f + " does not exist!");	
40  			System.exit(1);
41  		}
42  
43  		// set up the output file names
44  		String outputFile = null;
45  		StringTokenizer st = new StringTokenizer(inputFile, ".");
46  		if(st.hasMoreTokens()) outputFile = st.nextToken();
47  		if (outputFile==null)
48  		{
49  			System.out.println("\n\n  Problem parsing input file name");
50  			System.out.println(" \n\n File name should be in file.stdhep format");
51  			System.exit(1);
52  		}
53  
54  		// get number of events to put into each split output file
55  		// default is 100
56  		int nevts = 100;
57  		if ( args.length > 1 ) nevts = Integer.parseInt(args[1]);
58  		
59  		StdhepReader in = null;
60  		try
61  		{
62  		  in = new StdhepReader(inputFile);
63  		}
64  		catch(IOException ex)
65  		{
66  			System.out.println("Problem opening "+inputFile+" !");
67  			System.exit(1);
68  		}
69  		
70  		int readEvents = 0;
71  		int r=0;
72  		try
73  		{
74  			for (r = 0;;r++)
75  			{
76  				int n = 0;
77  				StdhepWriter out = null;
78                  try
79                  {
80                     for(;;)
81      					{
82  	    					StdhepRecord record = in.nextRecord();
83  	    					if (record instanceof StdhepEvent)
84  	    					{
85  	    						if (out == null)
86  	    						{
87      								String name = outputFile+"-"+r+"-"+nevts+".stdhep";
88  	    							System.out.println("Writing "+name);
89  	    							out = new StdhepWriter(name,"title","comment",nevts);
90  	    						}
91  	    						out.writeRecord(record);
92  	    						readEvents++;
93  	    						if (++n == nevts) break;
94      						}
95      					}
96      			}
97                  finally
98                  {
99                     if (out != null) out.close();
100 				}
101          	}
102 		}
103 		catch( EOFException ex)
104 		{
105 			System.out.println("\n\n Read "+readEvents+" events and created "+r +" output files.");
106 		}	  
107 		finally
108 		{
109 			in.close();
110 		}
111 	}
112 
113 	public static void usage()
114 	{
115 		System.out.println("Splitter: an application to split input stdhep files into smaller files.\n");
116 		System.out.println("Usage: \n\n >> java Splitter filename.stdhep [nevents] \n");
117 		System.out.println(" Where \n  filename is a file in stdhep format \n  nevents is the number of events in each output file [ default is 100 ]");
118 		System.out.println("  Output files will be named filename-nnn-nevents.stdhep");
119 		System.exit(0);
120 	}
121 
122 }