1
2
3
4
5
6
7
8
9
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
20
21 public class Splitter
22 {
23
24
25
26 public static void main(String args[]) throws Exception
27 {
28
29 if(args.length<1) usage();
30 if(args.length==1 && args[0].equals("-h")) usage();
31
32
33 String inputFile = args[0];
34
35
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
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
55
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 }