|
| 1 | +package org.jlab.io.clara; |
| 2 | + |
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.nio.ByteOrder; |
| 5 | +import java.nio.file.Path; |
| 6 | +import org.jlab.clara.engine.EngineDataType; |
| 7 | +import org.jlab.clara.std.services.AbstractEventReaderService; |
| 8 | +import org.jlab.clara.std.services.EventReaderException; |
| 9 | +import org.jlab.coda.jevio.EvioException; |
| 10 | +import org.jlab.detector.decode.CLASDecoder4; |
| 11 | +import org.jlab.io.evio.EvioDataEvent; |
| 12 | +import org.jlab.io.evio.EvioSource; |
| 13 | +import org.jlab.jnp.hipo4.data.Event; |
| 14 | +import org.jlab.jnp.hipo4.io.HipoReader; |
| 15 | +import org.json.JSONObject; |
| 16 | + |
| 17 | +/** |
| 18 | + * |
| 19 | + * @author baltzell |
| 20 | + */ |
| 21 | +public class Clas12Reader extends AbstractEventReaderService<Object> { |
| 22 | + |
| 23 | + boolean evio; |
| 24 | + CLASDecoder4 decoder; |
| 25 | + private long maxEvents; |
| 26 | + private Double torus; |
| 27 | + private Double solenoid; |
| 28 | + |
| 29 | + @Override |
| 30 | + protected Object createReader(Path path, JSONObject opts) throws EventReaderException { |
| 31 | + if (path.toString().endsWith(".hipo")) { |
| 32 | + evio = false; |
| 33 | + HipoReader r = new HipoReader(); |
| 34 | + r.open(path.toString()); |
| 35 | + return r; |
| 36 | + } |
| 37 | + else { |
| 38 | + evio = true; |
| 39 | + EvioSource r = new EvioSource(); |
| 40 | + r.open(path.toString()); |
| 41 | + maxEvents = r.getEventCount(); |
| 42 | + decoder = new CLASDecoder4(); |
| 43 | + torus = opts.has("torus") ? opts.getDouble("torus") : null; |
| 44 | + solenoid = opts.has("solenoid") ? opts.getDouble("solenoid") : null; |
| 45 | + if (opts.has("variation")) decoder.setVariation(opts.getString("variation")); |
| 46 | + if (opts.has("timestamp")) decoder.setTimestamp(opts.getString("timestamp")); |
| 47 | + return r; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected void closeReader() { |
| 53 | + if (evio) ((EvioSource)reader).close(); |
| 54 | + else ((HipoReader)reader).close(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected int readEventCount() throws EventReaderException { |
| 59 | + if (evio) return ((EvioSource)reader).getEventCount(); |
| 60 | + else return ((HipoReader)reader).getEventCount(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected Object readEvent(int eventNumber) throws EventReaderException { |
| 65 | + try { |
| 66 | + if (evio) { |
| 67 | + if (eventNumber++ >= maxEvents) return null; |
| 68 | + ByteBuffer b = ((EvioSource)reader).getEventBuffer(eventNumber, true); |
| 69 | + EvioDataEvent e = new EvioDataEvent(b.array(), readByteOrder()); |
| 70 | + return decoder.getDecodedEvent(e, -1, eventNumber, torus, solenoid); |
| 71 | + } |
| 72 | + else { |
| 73 | + return ((HipoReader)reader).getEvent(new Event(),eventNumber); |
| 74 | + } |
| 75 | + } catch (EvioException e) { |
| 76 | + throw new EventReaderException(e); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public ByteOrder readByteOrder() throws EventReaderException { |
| 82 | + return ByteOrder.LITTLE_ENDIAN; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected EngineDataType getDataType() { |
| 87 | + return Clas12Types.HIPO; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments