View Javadoc

1   package org.lcsim.util.loop;
2   
3   import java.io.EOFException;
4   import java.io.File;
5   import java.io.IOException;
6   import java.util.Collections;
7   import java.util.List;
8   
9   import hep.io.stdhep.StdhepEvent;
10  import hep.io.stdhep.StdhepReader;
11  import hep.io.stdhep.StdhepRecord;
12  import hep.physics.particle.properties.ParticlePropertyManager;
13  import org.freehep.record.source.AbstractRecordSource;
14  import org.freehep.record.source.NoSuchRecordException;
15  import org.freehep.record.source.RecordSource;
16  import org.lcsim.event.EventHeader;
17  import org.lcsim.event.util.LCSimFactory;
18  
19  
20  /**
21   * {@link RecordSource} implementation that wraps {@link StdhepReader}.
22   * 
23   * @author tonyj
24   * @version $Id: StdhepEventSource.java,v 1.10 2012/07/02 21:50:17 jeremy Exp $
25   */
26  public class StdhepEventSource extends AbstractRecordSource {
27  
28    private List<File> files;
29    private boolean atEnd;
30    private StdhepReader reader;
31    private final StdhepConverter converter;
32    private EventHeader currentEvent;
33    private long currentEventNumber = -1L;
34    private int currentFile = 0;
35  
36    /**
37     * Create an event source from a single stdhep file
38     */
39    public StdhepEventSource(File file, String detectorName) throws IOException {
40      super(file.getName());
41      reader = new StdhepReader(file.getAbsolutePath());
42      converter = new StdhepConverter(ParticlePropertyManager.getParticlePropertyProvider(), new LCSimFactory(detectorName));
43      this.files = Collections.singletonList(file);
44    }
45  
46    /**
47     * Create an event source from a list of stdhep files
48     */
49    public StdhepEventSource(FileList list, String detectorName) throws IOException {
50      super(list.getTitle());
51      this.files = list.getFileList();
52      if (files.isEmpty()) throw new IOException("File list is empty");
53      reader = new StdhepReader(files.get(0).getAbsolutePath());
54      converter = new StdhepConverter(ParticlePropertyManager.getParticlePropertyProvider(), new LCSimFactory(detectorName));
55    }
56  
57    public Object getCurrentRecord() throws IOException {
58      if (currentEvent == null) throw new IllegalStateException();
59      return currentEvent;
60    }
61  
62    public long size() {
63      if (files.size() == 1) return reader.getNumberOfEvents();
64      throw new UnsupportedOperationException();
65    }
66  
67    public Class<?> getRecordClass() {
68      return EventHeader.class;
69    }
70  
71    public boolean supportsNext() {
72      return true;
73    }
74  
75    public boolean hasNext() {
76      return !atEnd;
77    }
78  
79    public void next() throws IOException, NoSuchRecordException {
80      for (;;) {
81        try {
82          for (;;) {
83            StdhepRecord record = reader.nextRecord();
84            if (record instanceof StdhepEvent) {
85              currentEvent = (EventHeader) converter.convert((StdhepEvent) record);
86              //currentEvent.put("INPUT_FILE", files.get(currentFile));
87              currentEventNumber++;
88              break;
89            }
90          }
91        } catch (EOFException x) {
92          currentFile++;
93          if (currentFile >= files.size()) {
94            atEnd = true;
95            throw new NoSuchRecordException();
96          } else {
97            reader.close();
98            reader = new StdhepReader(files.get(currentFile).getAbsolutePath());
99            continue;
100         }
101       }
102       return;
103     }
104   }
105 
106   public void releaseRecord(Object obj) {
107     currentEvent = null;
108   }
109 
110   public boolean supportsRewind() {
111     return true;
112   }
113 
114   public boolean hasRewind() {
115     return currentEventNumber != -1L;
116   }
117 
118   public void rewind() throws IOException {
119     if (currentFile == 0) {
120       reader.rewind();
121     } else {
122       currentFile = 0;
123       reader.close();
124       reader = new StdhepReader(files.get(0).getAbsolutePath());
125     }
126     atEnd = false;
127     currentEventNumber = -1L;
128     currentEvent = null;
129   }
130 
131   public void close() throws IOException {
132     if (reader != null) {
133       reader.close();
134       reader = null;
135     }
136     currentEvent = null;
137   }
138 
139   public void finalize() {
140     try {
141       close();
142     } catch (IOException x) {
143     }
144   }
145 
146   public boolean supportsIndex() {
147     return true;
148   }
149 
150   public boolean hasIndex(long index) {
151     try {
152       return index < size();
153     } catch (UnsupportedOperationException x) {
154       return index >= 0L;
155     }
156   }
157 
158   public void jump(long index) throws IOException, NoSuchRecordException {
159     if (index < 0L) throw new NoSuchRecordException();
160     if (index == currentEventNumber) return;
161     if (index < currentEventNumber) rewind();
162     try {
163       for (;;) {
164         try {
165           for (;;) {
166             StdhepRecord record = reader.nextRecord();
167             if (record instanceof StdhepEvent) {
168               currentEventNumber++;
169               if (currentEventNumber == index) {
170                 currentEvent = (EventHeader) converter.convert((StdhepEvent) record);
171                 //currentEvent.put("INPUT_FILE", files.get(currentFile));
172                 return;
173               }
174             }
175           }
176         } catch (EOFException x) {
177           currentFile++;
178           if (currentFile >= files.size()) {
179             atEnd = true;
180             throw new NoSuchRecordException(x);
181           } else {
182             reader.close();
183             reader = new StdhepReader(files.get(currentFile).getAbsolutePath());
184             continue;
185           }
186         }
187       }
188     } catch (IOException x) {
189       throw new NoSuchRecordException(x);
190     }
191   }
192 
193   public long getCurrentIndex() {
194     return currentEventNumber;
195   }
196   
197 }