1 package org.lcsim.mc.fast;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Random;
6
7 import org.apache.commons.cli.CommandLine;
8 import org.apache.commons.cli.HelpFormatter;
9 import org.apache.commons.cli.Options;
10 import org.apache.commons.cli.ParseException;
11 import org.apache.commons.cli.PosixParser;
12 import org.freehep.record.source.NoSuchRecordException;
13 import org.lcsim.geometry.compact.Detector;
14 import org.lcsim.geometry.util.DetectorLocator;
15 import org.lcsim.mc.fast.MCFast;
16 import org.lcsim.util.Driver;
17 import org.lcsim.util.loop.LCIODriver;
18 import org.lcsim.util.loop.LCSimLoop;
19 import org.lcsim.util.loop.LCSimConditionsManagerImplementation;
20 import org.lcsim.mc.fast.util.MonitorStdhep;
21
22
23
24
25
26
27
28
29 public class Main {
30 private static final String defaultDetector = "sidloi3";
31 private static final int defaultNumProcess = -1;
32 private static final int defaultNumSkip = -1;
33 private static final boolean defaultRefPoint000 = true;
34 private static final String defaultOutputFileName = "fastmc.slcio";
35 private static final boolean defaultBeamSpotConstraint = true;
36 private static final boolean defaultSimple = true;
37 private static final int defaultSeed = (new Random()).nextInt();
38 private static final boolean defaultDebug = false;
39
40 String inputFileName;
41 String outputFileName;
42 String detector;
43 int numToProcess;
44 int numToSkip;
45 int seed;
46 boolean refPoint000;
47 boolean beamSpotConstraint;
48 boolean simple;
49 boolean debug;
50
51 private static Options options = null;
52
53 private Main() {
54 createOptions();
55 }
56
57 Options getOptions() {
58 return options;
59 }
60
61 void parse(String[] args) {
62
63
64 PosixParser parser = new PosixParser();
65
66 CommandLine cmd = null;
67 try {
68 cmd = parser.parse(options, args);
69 } catch (ParseException x) {
70 System.out.println("Parsing failed: " + x.getMessage());
71 usage();
72 }
73
74
75 setParameters(cmd);
76
77
78 printParameters();
79 }
80
81 private void setParameters(CommandLine cmd) {
82 inputFileName = cmd.getOptionValue("i");
83 if (inputFileName == null)
84 usage();
85 outputFileName = cmd.getOptionValue("o", defaultOutputFileName);
86 detector = cmd.getOptionValue("d", defaultDetector);
87 numToProcess = Integer.valueOf(cmd.getOptionValue("r", String.valueOf(defaultNumProcess)));
88 numToSkip = Integer.valueOf(cmd.getOptionValue("s", String.valueOf(defaultNumSkip)));
89 seed = Integer.valueOf(cmd.getOptionValue("m", String.valueOf(defaultSeed)));
90 refPoint000 = Boolean.valueOf(cmd.getOptionValue("p", String.valueOf(defaultRefPoint000)));
91 beamSpotConstraint = Boolean.valueOf(cmd.getOptionValue("b", String.valueOf(defaultBeamSpotConstraint)));
92 simple = Boolean.valueOf(cmd.getOptionValue("S", String.valueOf(defaultSimple)));
93 debug = Boolean.valueOf(cmd.getOptionValue("v", String.valueOf(defaultDebug)));
94 }
95
96 private void printParameters() {
97 System.out.println("Received the following command line parameters:");
98 System.out.println('\t' + "inputFileName = " + inputFileName);
99 System.out.println('\t' + "outputFileName = " + outputFileName);
100 System.out.println('\t' + "detector = " + detector);
101 System.out.println('\t' + "events to process = " + numToProcess);
102 System.out.println('\t' + "events to skip = " + numToSkip);
103 System.out.println('\t' + "seed = " + seed);
104 System.out.println('\t' + "refPoint000 = " + refPoint000);
105 System.out.println('\t' + "beamSpotConstraint = " + beamSpotConstraint);
106 System.out.println('\t' + "simple mode = " + simple);
107 System.out.println('\t' + "debug mode = " + debug);
108 }
109
110 private void createOptions() {
111 options = new Options();
112 options.addOption("h", false, "print usage information");
113 options.addOption("i", true, "input file");
114 options.addOption("o", true, "output file name");
115 options.addOption("d", true, "detector name");
116 options.addOption("r", true, "number of events to process");
117 options.addOption("s", true, "number of events to skip");
118 options.addOption("m", true, "random seed");
119 options.addOption("p", false, "use default ref point");
120 options.addOption("b", false, "use beam spot constraint");
121 options.addOption("S", false, "use simple");
122 options.addOption("v", false, "print debug info");
123 }
124
125 private void usage() {
126 HelpFormatter formatter = new HelpFormatter();
127 formatter.printHelp(getClass().getCanonicalName(), options);
128 System.exit(1);
129 }
130
131 private void error(String message) {
132 System.out.println(message);
133 System.exit(1);
134 }
135
136 private void run() throws IOException, NoSuchRecordException {
137
138 Detector det = DetectorLocator.findDetector(detector);
139 if (det == null) {
140 error("Unknown detector: " + detector);
141 }
142
143
144 File input = new File(inputFileName);
145 if (!input.exists()) {
146 error("The input file " + input + " does not exist!");
147 }
148
149
150 LCIODriver writer = null;
151 File output = new File(outputFileName);
152 if (output.exists()) {
153 throw new RuntimeException("Output file already exists!");
154 }
155 writer = new LCIODriver(output);
156
157
158 Driver fast = new MCFast(beamSpotConstraint, simple, seed, debug, refPoint000);
159
160
161 LCSimConditionsManagerImplementation.register();
162 LCSimLoop loop = new LCSimLoop();
163 if (input.getName().contains(".stdhep")) {
164 loop.setStdhepRecordSource(input, detector);
165 } else {
166 loop.setLCIORecordSource(input);
167 }
168
169
170 if (debug) {
171 MonitorStdhep analysis = new MonitorStdhep();
172 loop.add(analysis);
173 }
174 loop.add(fast);
175 loop.add(writer);
176
177
178 if (numToSkip > 0) {
179 System.out.println("skipping " + numToSkip + " events");
180 loop.skip(numToSkip);
181 }
182 loop.loop(numToProcess);
183 loop.dispose();
184 System.out.println(getClass().getSimpleName() + ": processed " + loop.getTotalSupplied() + " events");
185 }
186
187 public static void main(String[] args) {
188 Main main = new Main();
189 main.parse(args);
190 try {
191 main.run();
192 } catch (Exception e) {
193 throw new RuntimeException(e);
194 }
195 }
196 }