1 package org.lcsim.util.loop;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.PrintStream;
6
7 import org.freehep.record.loop.DefaultRecordLoop;
8 import org.freehep.record.source.NoSuchRecordException;
9 import org.freehep.record.source.RecordSource;
10 import org.lcsim.conditions.ConditionsManager;
11 import org.lcsim.conditions.ConditionsManagerImplementation;
12 import org.lcsim.conditions.ConditionsReader;
13 import org.lcsim.event.util.LCSimEventGenerator;
14 import org.lcsim.util.Driver;
15 import org.lcsim.util.DriverAdapter;
16
17 /**
18 * A main loop which can be used for standalone jobs (run outside of JAS).
19 * @author Tony Johnson
20 */
21 public class LCSimLoop extends DefaultRecordLoop {
22
23 private Driver top = new Driver();
24
25 /**
26 * Create a new LCSimLoop
27 */
28 public LCSimLoop() {
29 // Is there no global conditions manager installed yet?
30 if (!ConditionsManager.isSetup())
31 // Register a default conditions manager, which can still be overridden later if needed.
32 LCSimConditionsManagerImplementation.register();
33 DriverAdapter da = new DriverAdapter(top);
34 addRecordListener(da);
35 addLoopListener(da);
36 }
37
38 /**
39 * Add a driver to the loop. The Driver's will be called for each event, in the order in which
40 * they are added.
41 * @param driver The driver to add to the loop.
42 */
43 public void add(Driver driver) {
44 top.add(driver);
45 }
46
47 /**
48 * Remove a driver previously added to the loop.
49 * @param driver The driver to be removed.
50 */
51 public void remove(Driver driver) {
52 top.remove(driver);
53 }
54
55 /**
56 * Set the event source to be a give LCIO file.
57 * @param file The LCIO file from which to read.
58 * @throws java.io.IOException If the file cannot be opened.
59 */
60 public void setLCIORecordSource(File file) throws IOException {
61 super.setRecordSource(new LCIOEventSource(file));
62 }
63
64 /**
65 * Read events from the given LCIO event source
66 * @param src The source from which to read events.
67 * @throws java.io.IOException If an exception occurs while opening the file.
68 */
69 public void setLCIORecordSource(LCIOEventSource src) throws IOException {
70 super.setRecordSource(src);
71 }
72
73 /**
74 * Read events from the given stdhep file. Events will be converted to lcsim events as they are
75 * read.
76 * @param file The file to read
77 * @param detectorName The detector name to be added to the lcsim event.
78 * @throws java.io.IOException If an problem occurs when opening the file.
79 * @see org.lcsim.util.loop.StdhepEventSource
80 */
81 public void setStdhepRecordSource(File file, String detectorName) throws IOException {
82 super.setRecordSource(new StdhepEventSource(file, detectorName));
83 }
84
85 /**
86 * Set the event source to be an event generator.
87 * @param gen The event generator used to generate events.
88 */
89 public void setRecordSource(LCSimEventGenerator gen) {
90 super.setRecordSource(new EventGeneratorRecordSource(gen, "generator"));
91 }
92
93 /**
94 * Skip a given number of events.
95 * @param recordsToSkip The number of events to skip.
96 * @throws org.freehep.record.source.NoSuchRecordException If there are insufficient events
97 * available.
98 * @throws java.io.IOException If an IO exception occurs when reading an event
99 * @throws IllegalArgumentException if the argument is negative.
100 */
101 public void skip(long recordsToSkip) throws NoSuchRecordException, IOException {
102 if (recordsToSkip < 0L)
103 throw new IllegalArgumentException();
104 if (recordsToSkip == 0L)
105 return;
106 RecordSource rs = getRecordSource();
107 if (rs.supportsIndex()) {
108 try {
109 rs.jump(rs.getCurrentIndex() + recordsToSkip);
110 return;
111 } catch (IllegalStateException x) {
112 }
113 }
114 for (long i = 0; i < recordsToSkip; i++)
115 rs.next();
116 }
117
118 /**
119 * Loop over a given number of events, or until no more events are available. Statistics will
120 * be printed to standard output the end of the loop.
121 *
122 * @param number The number of events to loop over, or <CODE>-1</CODE> to loop until no more
123 * data is available.
124 *
125 * @throws IllegalStateException If there is a problem iterating over events (for example no
126 * event source specified).
127 * @throws IOException If there is an IO exception reading or writing events.
128 *
129 * @return The number of events actually iterated over.
130 */
131 public long loop(long number) throws IOException {
132 return loop(number, System.out);
133 }
134
135 /**
136 * Loop for the given number of events, optionally printing statistics to the given output
137 * stream
138 *
139 * @param number The number of events to loop over, or <CODE>-1</CODE> to loop until no more
140 * data is available.
141 *
142 * @throws IllegalStateException If there is a problem iterating over events (for example no
143 * event source specified).
144 * @throws IOException If there is an IO exception reading or writing events.
145 *
146 * @return The number of events actually iterated over.
147 */
148 public long loop(long number, PrintStream out) throws IOException {
149 top.clearStatistics();
150 if (number < 0L) {
151 execute(Command.GO, true);
152 } else {
153 execute(Command.GO_N, number, true);
154 execute(Command.STOP); // make sure endOfData() is called on drivers
155 }
156 Throwable t = getProgress().getException();
157 if (t != null && t instanceof IOException)
158 throw (IOException) t;
159 if (out != null)
160 printStatistics(out);
161 return getSupplied();
162 }
163
164 /**
165 * Print the statistics from the last loop call.
166 * @param out The PrintStream on which to print statistics.
167 */
168 public void printStatistics(PrintStream out) {
169 top.printStatistics(out);
170 }
171
172 /**
173 * Set a dummy detector.
174 * @param detectorName The name of the dummy detector.
175 */
176 public void setDummyDetector(String detectorName) {
177 ConditionsManager cond = ConditionsManager.defaultInstance();
178 ConditionsReader dummyReader = ConditionsReader.createDummy();
179 ((ConditionsManagerImplementation) cond).setConditionsReader(dummyReader, detectorName);
180 DummyDetector detector = new DummyDetector(detectorName);
181 cond.registerConditionsConverter(new DummyConditionsConverter(detector));
182 }
183
184 public void dispose() {
185 super.dispose();
186 }
187 }