|
| 1 | +package org.jlab.clas.reco; |
| 2 | + |
| 3 | +import java.util.Set; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.nio.ByteBuffer; |
| 6 | +import java.nio.ByteOrder; |
| 7 | +import java.util.concurrent.ArrayBlockingQueue; |
| 8 | +import java.util.concurrent.BlockingQueue; |
| 9 | +import org.jlab.clara.base.ClaraUtil; |
| 10 | +import org.jlab.clara.engine.Engine; |
| 11 | +import org.jlab.clara.engine.EngineData; |
| 12 | +import org.jlab.clara.engine.EngineDataType; |
| 13 | +import org.jlab.clara.engine.EngineStatus; |
| 14 | +import org.jlab.detector.decode.CLASDecoder; |
| 15 | +import org.jlab.io.evio.EvioDataEvent; |
| 16 | +import org.jlab.io.hipo.HipoDataEvent; |
| 17 | +import org.jlab.jnp.hipo4.data.SchemaFactory; |
| 18 | +import org.json.JSONObject; |
| 19 | + |
| 20 | +/** |
| 21 | + * |
| 22 | + * @author baltzell |
| 23 | + */ |
| 24 | +public class DecoderEngine implements Engine { |
| 25 | + |
| 26 | + static final int POOL_SIZE = 64; |
| 27 | + static final Set<EngineDataType> ED_TYPES = ClaraUtil.buildDataTypes( |
| 28 | + Clas12Types.EVIO,Clas12Types.HIPO,EngineDataType.JSON,EngineDataType.STRING); |
| 29 | + |
| 30 | + SchemaFactory schema; |
| 31 | + BlockingQueue<CLASDecoder> pool; |
| 32 | + int constantsShared = 64; |
| 33 | + |
| 34 | + public DecoderEngine() { |
| 35 | + schema = new SchemaFactory(); |
| 36 | + schema.initFromDirectory(System.getenv("CLAS12DIR") + "/etc/bankdefs/hipo4"); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Set<EngineDataType> getInputDataTypes() { return ED_TYPES; } |
| 41 | + @Override |
| 42 | + public Set<EngineDataType> getOutputDataTypes() { return ED_TYPES; } |
| 43 | + @Override |
| 44 | + public EngineData executeGroup(Set<EngineData> set) { return null; } |
| 45 | + @Override |
| 46 | + public Set<String> getStates() { return new HashSet<>(); } |
| 47 | + @Override |
| 48 | + public String getDescription() { return "decoder engine"; } |
| 49 | + @Override |
| 50 | + public String getVersion() { return "1.0"; } |
| 51 | + @Override |
| 52 | + public String getAuthor() { return "baltzell"; } |
| 53 | + @Override |
| 54 | + public void reset() {} |
| 55 | + @Override |
| 56 | + public void destroy() {} |
| 57 | + |
| 58 | + @Override |
| 59 | + public EngineData configure(EngineData ed) { |
| 60 | + JSONObject json = new JSONObject(ed.getData()); |
| 61 | + pool = new ArrayBlockingQueue<>(POOL_SIZE); |
| 62 | + CLASDecoder d0 = null; |
| 63 | + for (int i=0; i<POOL_SIZE; i++) { |
| 64 | + CLASDecoder d; |
| 65 | + if (i % constantsShared == 0) { |
| 66 | + d0 = new CLASDecoder(); |
| 67 | + if (json.has("variation")) d0.setVariation(json.getString("variation")); |
| 68 | + if (json.has("timestamp")) d0.setVariation(json.getString("timestamp")); |
| 69 | + d = d0; |
| 70 | + } |
| 71 | + else { |
| 72 | + d = new CLASDecoder(d0); |
| 73 | + } |
| 74 | + pool.add(d); |
| 75 | + } |
| 76 | + return ed; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public EngineData execute(EngineData input) { |
| 81 | + |
| 82 | + EngineData output = input; |
| 83 | + |
| 84 | + // if it's EVIO, decode it, otherwise just pass it along |
| 85 | + if (input.getMimeType().equals("binary/data-evio")) { |
| 86 | + EvioDataEvent evio; |
| 87 | + try { |
| 88 | + ByteBuffer bb = (ByteBuffer) input.getData(); |
| 89 | + //evio = new EvioDataEvent(bb.array(), bb.order()); |
| 90 | + evio = new EvioDataEvent(bb.array(), ByteOrder.LITTLE_ENDIAN); |
| 91 | + } catch (Exception e) { |
| 92 | + String msg = String.format("Error reading input event%n%n%s", ClaraUtil.reportException(e)); |
| 93 | + output.setStatus(EngineStatus.ERROR); |
| 94 | + output.setDescription(msg); |
| 95 | + return output; |
| 96 | + } |
| 97 | + HipoDataEvent hipo; |
| 98 | + try { |
| 99 | + CLASDecoder d = pool.take(); |
| 100 | + hipo = new HipoDataEvent(d.getDecodedEvent(evio),schema); |
| 101 | + pool.put(d); |
| 102 | + output.setData("binary/data-hipo", hipo.getHipoEvent()); |
| 103 | + } catch (Exception e) { |
| 104 | + String msg = String.format("Error processing input event%n%n%s", ClaraUtil.reportException(e)); |
| 105 | + output.setStatus(EngineStatus.ERROR); |
| 106 | + output.setDescription(msg); |
| 107 | + return output; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return output; |
| 112 | + } |
| 113 | +} |
0 commit comments