1 package org.lcsim.lcio;
2
3 import hep.io.sio.SIOBlock;
4 import hep.io.sio.SIOInputStream;
5 import hep.io.sio.SIOReader;
6 import hep.io.sio.SIORecord;
7 import java.io.EOFException;
8 import java.io.File;
9 import java.io.FileInputStream;
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.logging.Logger;
14 import org.lcsim.event.EventHeader;
15
16
17
18
19
20 public class LCIOReader
21 {
22 private SIOReader reader;
23 private Logger log = Logger.getLogger(LCIOWriter.class.getName());
24 private HandlerManager manager = HandlerManager.instance();
25
26 public LCIOReader(File file) throws IOException
27 {
28 reader = new SIOReader(new FileInputStream(file));
29 }
30 public void close() throws IOException
31 {
32 reader.close();
33 }
34 public void skipEvents(int numberToSkip) throws IOException
35 {
36 for (int i=0; i<numberToSkip; )
37 {
38 SIORecord record = reader.readRecord();
39 String name = record.getRecordName();
40 if (LCIOConstants.eventRecordName.equals(name)) i++;
41 }
42 }
43 public EventHeader read() throws IOException
44 {
45 for (;;)
46 {
47 SIORecord record = reader.readRecord();
48 String name = record.getRecordName();
49 if (!LCIOConstants.eventHeaderRecordName.equals(name)) continue;
50
51 SIOBlock block = record.getBlock();
52 int major = block.getMajorVersion() ;
53 int minor = block.getMinorVersion() ;
54 int version = major*1000 + minor;
55 if (version < 8)
56 throw new IOException("Sorry: files created with versions older than v00-08" + " are no longer supported !");
57
58 SIOInputStream in = block.getData();
59 LCIOEvent event = new LCIOEvent(in, version);
60
61 record = reader.readRecord();
62 name = record.getRecordName();
63 if (!LCIOConstants.eventRecordName.equals(name)) throw new IOException("LCIO record order problem");
64
65 List<LCIOCallback> callbacks = new ArrayList<LCIOCallback>();
66 for (;;)
67 {
68 block = record.getBlock();
69 if (block == null) break;
70 String blockName = block.getBlockName();
71 String type = event.getBlockType(blockName);
72 if (type != null)
73 {
74 LCIOBlockHandler handler = manager.handlerForType(type);
75 if (handler != null)
76 {
77 LCIOCallback callback = handler.readBlock(event,block);
78 if (callback != null) callbacks.add(callback);
79 }
80 else log.warning("No handler found for "+type);
81 }
82 }
83
84 for (LCIOCallback callback : callbacks) callback.callback();
85
86 return event;
87 }
88 }
89 public int skipEventsChecked(int numberToSkip) throws IOException
90 {
91 int i=0;
92 while(i < numberToSkip)
93 {
94 try {
95 SIORecord record = reader.readRecord();
96 String name = record.getRecordName();
97 if (LCIOConstants.eventRecordName.equals(name)) i++;
98 } catch (EOFException x) {
99 break;
100 }
101 }
102 return i;
103 }
104 }