diff --git a/.containers/coatjava.Dockerfile b/.containers/coatjava.Dockerfile index 513e16c0a1..869c51c089 100644 --- a/.containers/coatjava.Dockerfile +++ b/.containers/coatjava.Dockerfile @@ -23,4 +23,4 @@ ARG REF_NAME=development RUN java --version && cd /opt && \ git clone https://code.jlab.org/hallb/alert/coatjava.git && cd coatjava && \ git fetch origin && git checkout ${REF_NAME} && ./build-coatjava.sh --quiet && \ - ./install-clara /opt/clara + ./bin/install-clara /opt/clara diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7e52f436a3..16c02be1e1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -88,7 +88,7 @@ jobs: - name: build run: | ./build-coatjava.sh --lfs --no-progress -T${{ env.nthreads }} - ./install-clara -b -c ./coatjava ./clara + ./bin/install-clara -b -c ./coatjava ./clara - name: tar # tarball to preserve permissions run: | tar czvf coatjava.tar.gz coatjava diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7e70b65987..8a1ff806ec 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -70,9 +70,8 @@ build: download: stage: build script: -# - xrdcp xroot://sci-xrootd.jlab.org///osgpool/hallb/clas12/validation/raw/rg-d/clas_018779.evio.00001 ./ - git lfs install - - git clone https://code.jlab.org/hallb/clas12/validation-data + - git clone --depth 1 https://code.jlab.org/hallb/clas12/validation-data - cp validation-data/raw/rg-d/clas_018779.evio.00001 . artifacts: when: always @@ -122,6 +121,19 @@ download: paths: - pages +kpp: + stage: test + needs: [build] + dependencies: [build] + script: + - tar -xzf coatjava.tar.gz + - tar -xzf clara.tar.gz + - git config --global --add safe.directory $CI_PROJECT_DIR + - git lfs install + - git submodule update --init validation/advanced-tests/data + - cd validation/advanced-tests + - ./run-advanced-tests.sh + eb: stage: test needs: [build] @@ -148,7 +160,7 @@ decoder: dependencies: [build,download] script: - tar -xzf clara.tar.gz - - decoder -n 1000 -o clas_018779_00001.hipo clas_018779.evio.00001 + - decoder4u -l FINE -n 10000 -o clas_018779_00001.hipo clas_018779.evio.00001 artifacts: when: always expire_in: 1 day diff --git a/bin/decoder4u b/bin/decoder4u new file mode 100755 index 0000000000..c0504c17bb --- /dev/null +++ b/bin/decoder4u @@ -0,0 +1,13 @@ +#!/bin/bash + +. `dirname $0`/../libexec/env.sh + +split_cli $@ + +export MALLOC_ARENA_MAX=1 + +java -Xmx2304m -Xms1280m -XX:+UseSerialGC ${jvm_options[@]} \ + -cp ${COATJAVA_CLASSPATH:-''} \ + org.jlab.detector.decode.CLASDecoder4U \ + ${class_options[@]} + diff --git a/install-clara b/bin/install-clara similarity index 100% rename from install-clara rename to bin/install-clara diff --git a/build-coatjava.sh b/build-coatjava.sh index 914a1fd58f..b25d3f0fba 100755 --- a/build-coatjava.sh +++ b/build-coatjava.sh @@ -367,7 +367,6 @@ for pom in $(find common-tools -name pom.xml); do done echo "installed coatjava to: $prefix_dir" -# install clara -if $installClara; then ./install-clara -c $prefix_dir $clara_home; fi +if $installClara; then ./bin/install-clara -c $prefix_dir $clara_home; fi echo "COATJAVA SUCCESSFULLY BUILT !" diff --git a/common-tools/clara-io/src/main/java/org/jlab/io/clara/Clas12Reader.java b/common-tools/clara-io/src/main/java/org/jlab/io/clara/Clas12Reader.java new file mode 100644 index 0000000000..7b10192c4b --- /dev/null +++ b/common-tools/clara-io/src/main/java/org/jlab/io/clara/Clas12Reader.java @@ -0,0 +1,90 @@ +package org.jlab.io.clara; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.file.Path; +import org.jlab.clara.engine.EngineDataType; +import org.jlab.clara.std.services.AbstractEventReaderService; +import org.jlab.clara.std.services.EventReaderException; +import org.jlab.coda.jevio.EvioException; +import org.jlab.detector.decode.CLASDecoder4; +import org.jlab.io.evio.EvioDataEvent; +import org.jlab.io.evio.EvioSource; +import org.jlab.jnp.hipo4.data.Event; +import org.jlab.jnp.hipo4.io.HipoReader; +import org.json.JSONObject; + +/** + * + * @author baltzell + */ +public class Clas12Reader extends AbstractEventReaderService { + + boolean evio; + CLASDecoder4 decoder; + private long maxEvents; + private Double torus; + private Double solenoid; + + @Override + protected Object createReader(Path path, JSONObject opts) throws EventReaderException { + if (path.toString().endsWith(".hipo")) { + evio = false; + HipoReader r = new HipoReader(); + r.open(path.toString()); + return r; + } + else { + evio = true; + EvioSource r = new EvioSource(); + r.open(path.toString()); + maxEvents = r.getEventCount(); + decoder = new CLASDecoder4(); + torus = opts.has("torus") ? opts.getDouble("torus") : null; + solenoid = opts.has("solenoid") ? opts.getDouble("solenoid") : null; + if (opts.has("variation")) decoder.setVariation(opts.getString("variation")); + if (opts.has("timestamp")) decoder.setTimestamp(opts.getString("timestamp")); + return r; + } + } + + @Override + protected void closeReader() { + if (evio) ((EvioSource)reader).close(); + else ((HipoReader)reader).close(); + } + + @Override + protected int readEventCount() throws EventReaderException { + if (evio) return ((EvioSource)reader).getEventCount(); + else return ((HipoReader)reader).getEventCount(); + } + + @Override + protected Object readEvent(int eventNumber) throws EventReaderException { + try { + if (evio) { + if (eventNumber++ >= maxEvents) return null; + ByteBuffer b = ((EvioSource)reader).getEventBuffer(eventNumber, true); + EvioDataEvent e = new EvioDataEvent(b.array(), readByteOrder()); + return decoder.getDecodedEvent(e, -1, eventNumber, torus, solenoid); + } + else { + return ((HipoReader)reader).getEvent(new Event(),eventNumber); + } + } catch (EvioException e) { + throw new EventReaderException(e); + } + } + + @Override + public ByteOrder readByteOrder() throws EventReaderException { + return ByteOrder.LITTLE_ENDIAN; + } + + @Override + protected EngineDataType getDataType() { + return Clas12Types.HIPO; + } + +} diff --git a/common-tools/clara-io/src/main/java/org/jlab/io/clara/DecoderWriter.java b/common-tools/clara-io/src/main/java/org/jlab/io/clara/DecoderWriter.java index 9be013bece..6881bf1928 100644 --- a/common-tools/clara-io/src/main/java/org/jlab/io/clara/DecoderWriter.java +++ b/common-tools/clara-io/src/main/java/org/jlab/io/clara/DecoderWriter.java @@ -66,7 +66,7 @@ protected HipoWriterSorted createWriter(Path file, JSONObject opts) throws Event init(opts); HipoWriterSorted w = new HipoWriterSorted(); super.configure(w, opts); - w.open(file.toString()); + w.open(file.toString().endsWith(".hipo") ? file.toString() : file.toString()+".hipo"); return w; } catch (Exception e) { throw new EventWriterException(e); diff --git a/common-tools/clara-io/src/main/resources/org/jlab/io/clara/Clas12Reader.yaml b/common-tools/clara-io/src/main/resources/org/jlab/io/clara/Clas12Reader.yaml new file mode 100644 index 0000000000..30c6e00495 --- /dev/null +++ b/common-tools/clara-io/src/main/resources/org/jlab/io/clara/Clas12Reader.yaml @@ -0,0 +1,11 @@ +--- +name: Clas12Reader +engine: org.jlab.io.clara.Clas12Reader +type: java + +author: Nathan Baltzell +email: baltzell@jlab.org + +version: 0.1 +description: + Reads EVIO or HIPO events from a file, converting to HIPO via "decoding" if EVIO. diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/base/DetectorDescriptor.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/base/DetectorDescriptor.java index 6cb753f016..930cabcfe1 100644 --- a/common-tools/clas-detector/src/main/java/org/jlab/detector/base/DetectorDescriptor.java +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/base/DetectorDescriptor.java @@ -88,6 +88,14 @@ public final void setSectorLayerComponent(int sector, int layer, int comp){ this.dt_COMPONENT = comp; } + public final void setSectorLayerComponentOrderType(int sector, int layer, int comp, int order, int type) { + this.dt_SECTOR = sector; + this.dt_LAYER = layer; + this.dt_COMPONENT = comp; + this.dt_ORDER = order; + this.detectorType = DetectorType.getType(type); + } + public static int generateHashCode(int s, int l, int c){ return ((s<<24)&0xFF000000)| ((l<<16)&0x00FF0000)|(c&0x0000FFFF); diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CLASDecoder.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CLASDecoder.java new file mode 100644 index 0000000000..b52c35a395 --- /dev/null +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CLASDecoder.java @@ -0,0 +1,809 @@ +package org.jlab.detector.decode; + +import java.util.List; +import java.util.Map; +import java.util.ArrayList; +import java.util.LinkedHashMap; + +import org.jlab.detector.scalers.DaqScalers; +import org.jlab.detector.base.DetectorDescriptor; +import org.jlab.detector.base.DetectorType; +import org.jlab.detector.calib.utils.RCDBProvider.RCDBManager; +import org.jlab.detector.decode.DetectorDataDgtz.HelicityDecoderData; +import org.jlab.detector.helicity.HelicityBit; +import org.jlab.detector.pulse.ModeAHDC; + +import org.jlab.io.base.DataEvent; +import org.jlab.io.evio.EvioDataEvent; +import org.jlab.io.hipo.HipoDataEvent; +import org.jlab.io.hipo.HipoDataSync; + +import org.jlab.jnp.hipo4.data.Bank; +import org.jlab.jnp.hipo4.data.Event; +import org.jlab.jnp.hipo4.data.SchemaFactory; +import org.jlab.utils.benchmark.Benchmark; + +import org.jlab.utils.groups.IndexedTable; +import org.jlab.utils.system.ClasUtilsFile; + +/** + * + * @author gavalian + */ +public class CLASDecoder { + + protected DetectorEventDecoder detectorDecoder = null; + protected SchemaFactory schemaFactory = new SchemaFactory(); + private CodaEventDecoder codaDecoder = null; + private List dataList = new ArrayList<>(); + private HipoDataSync writer = null; + private HipoDataEvent hipoEvent = null; + private boolean isRunNumberFixed = false; + private int decoderDebugMode = 0; + private ModeAHDC ahdcExtractor = new ModeAHDC(); + private RCDBManager rcdbManager = new RCDBManager(); + static protected boolean benchmark = true; + + public CLASDecoder(boolean development){ + codaDecoder = new CodaEventDecoder(); + detectorDecoder = new DetectorEventDecoder(development); + writer = new HipoDataSync(); + hipoEvent = (HipoDataEvent) writer.createEvent(); + String dir = ClasUtilsFile.getResourceDir("CLAS12DIR", "etc/bankdefs/hipo4"); + schemaFactory.initFromDirectory(dir); + } + + public CLASDecoder(){ + codaDecoder = new CodaEventDecoder(); + detectorDecoder = new DetectorEventDecoder(); + writer = new HipoDataSync(); + hipoEvent = (HipoDataEvent) writer.createEvent(); + String dir = ClasUtilsFile.getResourceDir("CLAS12DIR", "etc/bankdefs/hipo4"); + schemaFactory.initFromDirectory(dir); + } + + public SchemaFactory getSchemaFactory(){ + return schemaFactory; + } + + static void resume(String s) { if (benchmark) Benchmark.getInstance().resume(s); } + static void pause(String s) { if (benchmark) Benchmark.getInstance().pause(s); } + + public void setVariation(String variation) { + detectorDecoder.setVariation(variation); + } + + public void setTimestamp(String timestamp) { + detectorDecoder.setTimestamp(timestamp); + } + + public void setDebugMode(int mode){ + this.decoderDebugMode = mode; + } + + public void setRunNumber(int run){ + if(this.isRunNumberFixed==false){ + this.detectorDecoder.setRunNumber(run); + } + } + + public void setRunNumber(int run, boolean fixed){ + this.isRunNumberFixed = fixed; + this.detectorDecoder.setRunNumber(run); + System.out.println(" SETTING RUN NUMBER TO " + run + " FIXED = " + this.isRunNumberFixed); + } + + public CodaEventDecoder getCodaEventDecoder() { + return codaDecoder; + } + + /** + * return list of digitized ADC values from internal list + * @param type detector type + * @return + */ + public List getEntriesADC(DetectorType type){ + return this.getEntriesADC(type, dataList); + } + /** + * returns ADC entries from decoded data for given detector TYPE + * @param type detector type + * @param entries digitized data list + * @return list of ADC's for detector type + */ + public List getEntriesADC(DetectorType type, + List entries){ + List adc = new ArrayList<>(); + for(DetectorDataDgtz entry : entries){ + if(entry.getDescriptor().getType()==type){ + if(entry.getADCSize()>0&&entry.getTDCSize()==0){ + adc.add(entry); + } + } + } + + return adc; + } + + public List getEntriesTDC(DetectorType type){ + return getEntriesTDC(type,dataList); + } + + /** + * returns TDC entries from decoded data for given detector type + * @param type detector type + * @param entries digitized data list + * @return list of ADC's for detector type + */ + public List getEntriesTDC(DetectorType type, + List entries){ + List tdc = new ArrayList<>(); + for(DetectorDataDgtz entry : entries){ + if(entry.getDescriptor().getType()==type){ + if(entry.getTDCSize()>0&&entry.getADCSize()==0){ + tdc.add(entry); + } + } + } + return tdc; + } + + public List getEntriesVTP(DetectorType type){ + return getEntriesVTP(type,dataList); + } + /** + * returns VTP entries from decoded data for given detector type + * @param type detector type + * @param entries digitized data list + * @return list of VTP's for detector type + */ + public List getEntriesVTP(DetectorType type, + List entries){ + List vtp = new ArrayList<>(); + for(DetectorDataDgtz entry : entries){ + if(entry.getDescriptor().getType()==type){ + if(entry.getVTPSize()>0){ + vtp.add(entry); + } + } + } + return vtp; + } + + public List getEntriesSCALER(DetectorType type){ + return getEntriesSCALER(type,dataList); + } + /** + * returns VTP entries from decoded data for given detector type + * @param type detector type + * @param entries digitized data list + * @return list of VTP's for detector type + */ + public List getEntriesSCALER(DetectorType type, + List entries){ + List scaler = new ArrayList<>(); + for(DetectorDataDgtz entry : entries){ + if(entry.getDescriptor().getType()==type){ + if(entry.getSCALERSize()>0){ + scaler.add(entry); + } + } + } + return scaler; + } + + public void extractPulses(Event event) { + ahdcExtractor.update(30, null, event, schemaFactory, "AHDC::wf", "AHDC::adc"); + } + + public Bank getDataBankWF(String name, DetectorType type) { + List a = this.getEntriesADC(type); + if (a.isEmpty()) return null; + Bank b = new Bank(schemaFactory.getSchema(name), a.size()); + for (int i=0; i adcDGTZ = this.getEntriesADC(type); + if (adcDGTZ.isEmpty()) return null; + + Bank adcBANK = new Bank(schemaFactory.getSchema(name), adcDGTZ.size()); + + for(int i = 0; i < adcDGTZ.size(); i++){ + adcBANK.putByte( 0, i, (byte) adcDGTZ.get(i).getDescriptor().getSector()); + adcBANK.putByte( 1, i, (byte) adcDGTZ.get(i).getDescriptor().getLayer()); + adcBANK.putShort(2, i, (short) adcDGTZ.get(i).getDescriptor().getComponent()); + adcBANK.putByte( 3, i, (byte) adcDGTZ.get(i).getDescriptor().getOrder()); + adcBANK.putInt( 4, i, adcDGTZ.get(i).getADCData(0).getADC()); + if (type == DetectorType.BMT || type == DetectorType.FMT || type == DetectorType.FTTRK) { + adcBANK.putInt( 4, i, adcDGTZ.get(i).getADCData(0).getHeight()); + adcBANK.putInt( 7, i, adcDGTZ.get(i).getADCData(0).getIntegral()); + adcBANK.putLong(8, i, adcDGTZ.get(i).getADCData(0).getTimeStamp()); + } + if(type == DetectorType.BAND) { + adcBANK.putInt( 5, i, adcDGTZ.get(i).getADCData(0).getHeight()); + adcBANK.putFloat(6, i, (float) adcDGTZ.get(i).getADCData(0).getTime()); + adcBANK.putShort(7, i, (short) adcDGTZ.get(i).getADCData(0).getPedestal()); + } + else { + adcBANK.putFloat(5, i, (float) adcDGTZ.get(i).getADCData(0).getTime()); + adcBANK.putShort(6, i, (short) adcDGTZ.get(i).getADCData(0).getPedestal()); + } + if(type == DetectorType.BST) adcBANK.putLong(7, i, adcDGTZ.get(i).getADCData(0).getTimeStamp()); + } + return adcBANK; + } + + + public Bank getDataBankTDC(String name, DetectorType type){ + + List tdcDGTZ = this.getEntriesTDC(type); + if (tdcDGTZ.isEmpty()) return null; + Bank tdcBANK = new Bank(schemaFactory.getSchema(name), tdcDGTZ.size()); + + for(int i = 0; i < tdcDGTZ.size(); i++){ + tdcBANK.putByte( 0, i, (byte) tdcDGTZ.get(i).getDescriptor().getSector()); + tdcBANK.putByte( 1, i, (byte) tdcDGTZ.get(i).getDescriptor().getLayer()); + tdcBANK.putShort(2, i, (short) tdcDGTZ.get(i).getDescriptor().getComponent()); + tdcBANK.putByte( 3, i, (byte) (tdcDGTZ.get(i).getDescriptor().getOrder()+tdcDGTZ.get(i).getTDCData(0).getType().getTypeId())); + tdcBANK.putInt( 4, i, tdcDGTZ.get(i).getTDCData(0).getTime()); + if(type == DetectorType.DC) + tdcBANK.putShort(5, i, (short) tdcDGTZ.get(i).getTDCData(0).getToT()); + } + return tdcBANK; + } + + public Bank getDataBankTDCPetiroc(String name, DetectorType type){ + + List tdcDGTZ = this.getEntriesTDC(type); + if (tdcDGTZ.isEmpty()) return null; + Bank tdcBANK = new Bank(schemaFactory.getSchema(name), tdcDGTZ.size()); + + for(int i = 0; i < tdcDGTZ.size(); i++){ + tdcBANK.putByte( 0, i, (byte) tdcDGTZ.get(i).getDescriptor().getSector()); + tdcBANK.putByte( 1, i, (byte) tdcDGTZ.get(i).getDescriptor().getLayer()); + tdcBANK.putShort(2, i, (short) tdcDGTZ.get(i).getDescriptor().getComponent()); + tdcBANK.putByte( 3, i, (byte) tdcDGTZ.get(i).getDescriptor().getOrder()); + tdcBANK.putInt( 4, i, tdcDGTZ.get(i).getTDCData(0).getTime()); + tdcBANK.putInt( 5, i, tdcDGTZ.get(i).getTDCData(0).getToT()); + tdcBANK.putLong( 6, i, tdcDGTZ.get(i).getTDCData(0).getTimeStamp()); + tdcBANK.putInt( 7, i, tdcDGTZ.get(i).getTrigger()); + } + return tdcBANK; + } + + + public Bank getDataBankTimeStamp(String name, DetectorType type) { + + List tdcDGTZ = this.getEntriesTDC(type); + if (tdcDGTZ.isEmpty()) return null; + Map tsMap = new LinkedHashMap<>(); + for(DetectorDataDgtz tdc : tdcDGTZ) { + DetectorDescriptor desc = tdc.getDescriptor(); + int hash = ((desc.getCrate()<<8)&0xFF00) | (desc.getSlot()&0x00FF); + if(tsMap.containsKey(hash)) { + if(tsMap.get(hash).getTimeStamp() != tdc.getTimeStamp()) + System.out.println("WARNING: inconsistent timestamp for DCRB crate/slot " + + desc.getCrate() + "/" + desc.getSlot()); + } + else { + tsMap.put(hash, tdc); + } + } + + Bank tsBANK = new Bank(schemaFactory.getSchema(name), tsMap.size()); + + int i=0; + for(DetectorDataDgtz tdc : tsMap.values()) { + tsBANK.putByte(0, i, (byte) tdc.getDescriptor().getCrate()); + tsBANK.putByte(1, i, (byte) tdc.getDescriptor().getSlot()); + tsBANK.putLong(2, i, tdc.getTimeStamp()); + i++; + } + return tsBANK; + } + + public Bank getDataBankUndecodedADC(String name, DetectorType type){ + List adcDGTZ = this.getEntriesADC(type); + if (adcDGTZ.isEmpty()) return null; + Bank adcBANK = new Bank(schemaFactory.getSchema(name), adcDGTZ.size()); + + for(int i = 0; i < adcDGTZ.size(); i++){ + adcBANK.putByte( 0, i, (byte) adcDGTZ.get(i).getDescriptor().getCrate()); + adcBANK.putByte( 1, i, (byte) adcDGTZ.get(i).getDescriptor().getSlot()); + adcBANK.putShort(2, i, (short) adcDGTZ.get(i).getDescriptor().getChannel()); + adcBANK.putInt( 3, i, adcDGTZ.get(i).getADCData(0).getADC()); + adcBANK.putFloat(4, i, (float) adcDGTZ.get(i).getADCData(0).getTime()); + adcBANK.putShort(5, i, (short) adcDGTZ.get(i).getADCData(0).getPedestal()); + } + return adcBANK; + } + + public Bank getDataBankUndecodedTDC(String name, DetectorType type){ + + List tdcDGTZ = this.getEntriesTDC(type); + if (tdcDGTZ.isEmpty()) return null; + + Bank tdcBANK = new Bank(schemaFactory.getSchema(name), tdcDGTZ.size()); + + for(int i = 0; i < tdcDGTZ.size(); i++){ + tdcBANK.putByte( 0, i, (byte) tdcDGTZ.get(i).getDescriptor().getCrate()); + tdcBANK.putByte( 1, i, (byte) tdcDGTZ.get(i).getDescriptor().getSlot()); + tdcBANK.putShort(2, i, (short) tdcDGTZ.get(i).getDescriptor().getChannel()); + tdcBANK.putInt( 3, i, tdcDGTZ.get(i).getTDCData(0).getTime()); + } + return tdcBANK; + } + + public Bank getDataBankUndecodedVTP(String name, DetectorType type){ + + List vtpDGTZ = this.getEntriesVTP(type); + if (vtpDGTZ.isEmpty()) return null; + + Bank vtpBANK = new Bank(schemaFactory.getSchema(name), vtpDGTZ.size()); + + for(int i = 0; i < vtpDGTZ.size(); i++){ + vtpBANK.putByte(0, i, (byte) vtpDGTZ.get(i).getDescriptor().getCrate()); + vtpBANK.putInt( 1, i, vtpDGTZ.get(i).getVTPData(0).getWord()); + } + return vtpBANK; + } + + public Bank getDataBankUndecodedSCALER(String name, DetectorType type){ + + List scalerDGTZ = this.getEntriesSCALER(type); + if (scalerDGTZ.isEmpty()) return null; + + Bank scalerBANK = new Bank(schemaFactory.getSchema(name), scalerDGTZ.size()); + + for(int i = 0; i < scalerDGTZ.size(); i++){ + scalerBANK.putByte( 0, i, (byte) scalerDGTZ.get(i).getDescriptor().getCrate()); + scalerBANK.putByte( 1, i, (byte) scalerDGTZ.get(i).getDescriptor().getSlot()); + scalerBANK.putShort(2, i, (short) scalerDGTZ.get(i).getDescriptor().getChannel()); + scalerBANK.putByte( 3, i, (byte) scalerDGTZ.get(i).getSCALERData(0).getHelicity()); + scalerBANK.putByte( 4, i, (byte) scalerDGTZ.get(i).getSCALERData(0).getQuartet()); + scalerBANK.putLong( 5, i, scalerDGTZ.get(i).getSCALERData(0).getValue()); + } + return scalerBANK; + } + + + public long getTriggerPhase() { + long timestamp = this.codaDecoder.getTimeStamp(); + int phase_offset = 1; + return ((timestamp%6)+phase_offset)%6; // TI derived phase correction due to TDC and FADC clock differences + } + + public Bank createHeaderBank( int nrun, int nevent, Double torus, Double solenoid){ + + Bank bank = new Bank(schemaFactory.getSchema("RUN::config"), 1); + + int localRun = this.codaDecoder.getRunNumber(); + int localEvent = this.codaDecoder.getEventNumber(); + int localTime = this.codaDecoder.getUnixTime(); + long timeStamp = this.codaDecoder.getTimeStamp(); + long triggerBits = this.codaDecoder.getTriggerBits(); + + if(nrun>0){ + localRun = nrun; + localEvent = nevent; + } + + bank.putInt( 0, 0, localRun); + bank.putInt( 1, 0, localEvent); + bank.putInt( 2, 0, localTime); + bank.putLong(3, 0, triggerBits); + bank.putLong(4, 0, timeStamp); + + if (torus != null) { + bank.putFloat(7, 0, torus.floatValue()); + } + else if (rcdbManager.getTorusScale(localRun) == null) { + if (localRun > 100) throw new RuntimeException("Error retrieving torus scale from RCDB."); + } + else { + bank.putFloat(7, 0, rcdbManager.getTorusScale(localRun).floatValue()); + } + if (solenoid != null) { + bank.putFloat(8, 0, solenoid.floatValue()); + } + else if (rcdbManager.getSolenoidScale(localRun) == null) { + if (localRun > 100) throw new RuntimeException("Error retrieving solenoid scale from RCDB."); + } + else { + bank.putFloat(8, 0, rcdbManager.getSolenoidScale(localRun).floatValue()); + } + + return bank; + } + + public Bank createOnlineHelicityBank() { + if (this.codaDecoder.getHelicityLevel3()==HelicityBit.DNE.value()) return null; + Bank bank = new Bank(schemaFactory.getSchema("HEL::online"), 1); + byte helicityL3 = this.codaDecoder.getHelicityLevel3(); + IndexedTable hwpTable = this.detectorDecoder.scalerManager. + getConstants(this.detectorDecoder.getRunNumber(),"/runcontrol/hwp"); + bank.putByte(0,0, helicityL3); + bank.putByte(1,0,(byte)(helicityL3*hwpTable.getIntValue("hwp",0,0,0))); + return bank; + } + + public Bank createTriggerBank(){ + Bank bank = new Bank(schemaFactory.getSchema("RUN::trigger"), this.codaDecoder.getTriggerWords().size()); + for(int i=0; i createReconScalerBanks(Event event){ + return DaqScalers.createBanks(detectorDecoder.getRunNumber(), + schemaFactory, event, detectorDecoder.scalerManager); + } + + public Bank createBonusBank(){ + List bonusData = this.getEntriesADC(DetectorType.RTPC); + int totalSize = 0; + for(int i = 0; i < bonusData.size(); i++){ + short[] pulse = bonusData.get(i).getADCData(0).getPulseArray(); + totalSize += pulse.length; + } + + if (bonusData.isEmpty()) return null; + + Bank bonusBank = new Bank(schemaFactory.getSchema("RTPC::adc"), totalSize); + int currentRow = 0; + for(int i = 0; i < bonusData.size(); i++){ + + DetectorDataDgtz bonus = bonusData.get(i); + + short[] pulses = bonus.getADCData(0).getPulseArray(); + long timestamp = bonus.getADCData(0).getTimeStamp(); + double time = bonus.getADCData(0).getTime(); + double coeff = time*120.0; + + double offset1 = 0.0; + double offset2 = (double) (8*(timestamp%8)); + + for(int k = 0; k < pulses.length; k++){ + + double pulseTime = coeff + offset1 + offset2 + k*120.0; + + bonusBank.putByte( 0, currentRow, (byte) bonus.getDescriptor().getSector()); + bonusBank.putByte( 1, currentRow, (byte) bonus.getDescriptor().getLayer()); + bonusBank.putShort(2, currentRow, (short) bonus.getDescriptor().getComponent()); + bonusBank.putByte( 3, currentRow, (byte) bonus.getDescriptor().getOrder()); + bonusBank.putInt( 4, currentRow, pulses[k]); + bonusBank.putFloat(5, currentRow, (float) pulseTime); + bonusBank.putShort(6, currentRow, (short) 0); + currentRow++; + } + } + return bonusBank; + } + + public Bank createHelicityDecoderBank(EvioDataEvent event) { + HelicityDecoderData data = this.codaDecoder.getDataEntries_HelicityDecoder(event); + if(data!=null) { + Bank bank = new Bank(schemaFactory.getSchema("HEL::decoder"), 1); + int i=0; + bank.putByte(i++, 0, data.getHelicityState().getHelicity().value()); + bank.putByte(i++, 0, data.getHelicityState().getPairSync().value()); + bank.putByte(i++, 0, data.getHelicityState().getPatternSync().value()); + bank.putByte(i++, 0, data.getTSettle().value()); + bank.putByte(i++, 0, data.getHelicityPattern().value()); + bank.putByte(i++, 0, data.getPolarity()); + bank.putByte(i++, 0, data.getPatternPhaseCount()); + bank.putLong(i++, 0, data.getTimestamp()); + bank.putInt(i++, 0, data.getHelicitySeed()); + bank.putInt(i++, 0, data.getNTStableRisingEdge()); + bank.putInt(i++, 0, data.getNTStableFallingEdge()); + bank.putInt(i++, 0, data.getNPattern()); + bank.putInt(i++, 0, data.getNPair()); + bank.putInt(i++, 0, data.getTStableStart()); + bank.putInt(i++, 0, data.getTStableEnd()); + bank.putInt(i++, 0, data.getTStableTime()); + bank.putInt(i++, 0, data.getTSettleTime()); + bank.putInt(i++, 0, data.getPatternWindows()); + bank.putInt(i++, 0, data.getPairWindows()); + bank.putInt(i++, 0, data.getHelicityWindows()); + bank.putInt(i++, 0, data.getHelicityPatternWindows()); + return bank; + } + else + return null; + } + + public static Event createTaggedEvent(Event e, Bank runConfig, Bank... banks) { + Event t = new Event(); + for (Bank b : banks) { + e.read(b); + if (b.getRows() > 0) t.write(b); + } + if (!t.isEmpty()) { + e.read(runConfig); + t.write(runConfig); + } + return t; + } + + public static Event createTaggedEvent(SchemaFactory sf, Event e, String... banks) { + Bank[] b = new Bank[banks.length]; + for (int i=0; i0){ + event.write(adcBank); + } + } + } + + for(int i = 0; i < Config.wfBankTypes.length; i++){ + Bank wfBank = getDataBankWF(Config.wfBankNames[i],Config.wfBankTypes[i]); + if(wfBank!=null && wfBank.getRows()>0){ + event.write(wfBank); + } + } + + for(int i = 0; i < Config.tdcBankTypes.length; i++){ + Bank tdcBank = getDataBankTDC(Config.tdcBankNames[i],Config.tdcBankTypes[i]); + if(tdcBank!=null){ + if(tdcBank.getRows()>0){ + event.write(tdcBank); + } + } + } + + try { + Bank tdcBank = getDataBankTDCPetiroc("ATOF::tdc",DetectorType.ATOF); + if(tdcBank!=null){ + if(tdcBank.getRows()>0){ + event.write(tdcBank); + } + } + } catch(Exception e) { + e.printStackTrace(); + } + + try { + Bank tsBank = getDataBankTimeStamp("DC::jitter", DetectorType.DC); + if(tsBank != null) { + if(tsBank.getRows()>0) { + event.write(tsBank); + } + } + } catch(Exception e) { + e.printStackTrace(); + } + + /** + * Adding un-decoded banks to the event + */ + try { + Bank adcBankUD = this.getDataBankUndecodedADC("RAW::adc", DetectorType.UNDEFINED); + if(adcBankUD!=null){ + if(adcBankUD.getRows()>0){ + event.write(adcBankUD); + } + } + } catch(Exception e) { + e.printStackTrace(); + } + + try { + Bank tdcBankUD = this.getDataBankUndecodedTDC("RAW::tdc", DetectorType.UNDEFINED); + if(tdcBankUD!=null){ + if(tdcBankUD.getRows()>0){ + event.write(tdcBankUD); + } + } else { + + } + } catch(Exception e) { + e.printStackTrace(); + } + + try { + Bank vtpBankUD = this.getDataBankUndecodedVTP("RAW::vtp", DetectorType.UNDEFINED); + if(vtpBankUD!=null){ + if(vtpBankUD.getRows()>0){ + event.write(vtpBankUD); + } + } else { + + } + } catch(Exception e) { + e.printStackTrace(); + } + + try { + Bank scalerBankUD = this.getDataBankUndecodedSCALER("RAW::scaler", DetectorType.UNDEFINED); + if(scalerBankUD!=null){ + if(scalerBankUD.getRows()>0){ + event.write(scalerBankUD); + } + } + } catch(Exception e) { + e.printStackTrace(); + } + + try { + Bank bonusBank = this.createBonusBank(); + if(bonusBank!=null){ + if(bonusBank.getRows()>0){ + event.write(bonusBank); + } + } + } catch(Exception e) { + e.printStackTrace(); + } + + return event; + } + + public void initEvent(DataEvent event){ + + if(event instanceof EvioDataEvent){ + EvioDataEvent evioEvent = (EvioDataEvent) event; + if(evioEvent.getHandler().getStructure()!=null){ + try { + + codaDecoder.cacheBranches(evioEvent); + + resume("evio"); + dataList = codaDecoder.getDataEntries( (EvioDataEvent) event); + pause("evio"); + + resume("eviobp"); + List fadcPacked = codaDecoder.getADCEntries((EvioDataEvent) event); + pause("eviobp"); + + resume("ubitp"); + if (fadcPacked != null) { + //----------------------------------------------------------------------------- + // This part reads the BITPACKED FADC data from tag=57638 Format (cmcms) + // Then unpacks into Detector Digigitized data, and appends to existing buffer + // Modified on 9/5/2018 + //----------------------------------------------------------------------------- + List fadcUnpacked = FADCData.convert(fadcPacked); + dataList.addAll(fadcUnpacked); + } + pause("ubitp"); + + if(this.decoderDebugMode>0){ + System.out.println("\n>>>>>>>>> RAW decoded data"); + for(DetectorDataDgtz data : dataList){ + System.out.println(data); + } + } + + int runNumberCoda = codaDecoder.getRunNumber(); + this.setRunNumber(runNumberCoda); + + resume("translate"); + detectorDecoder.translate(dataList); + pause("translate"); + resume("fit"); + detectorDecoder.fitPulses(dataList); + pause("fit"); + detectorDecoder.filterTDCs(dataList); + + if(this.decoderDebugMode>0){ + System.out.println("\n>>>>>>>>> TRANSLATED data"); + for(DetectorDataDgtz data : dataList){ + System.out.println(data); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + + public Event getDecodedEvent(EvioDataEvent rawEvent, int run, int counter, Double torus, Double solenoid) { + + this.initEvent(rawEvent); + resume("banks"); + Event decodedEvent = this.getDataEvent(); + pause("banks"); + + Bank header = this.createHeaderBank(run, counter, torus, solenoid); + if(header!=null) decodedEvent.write(header); + + Bank trigger = this.createTriggerBank(); + if(trigger!=null) decodedEvent.write(trigger); + + Bank onlineHelicity = this.createOnlineHelicityBank(); + if(onlineHelicity!=null) decodedEvent.write(onlineHelicity); + + Bank decodedHelicity = this.createHelicityDecoderBank(rawEvent); + if (decodedHelicity!=null) decodedEvent.write(decodedHelicity); + + this.extractPulses(decodedEvent); + + Bank epics = createEpicsBank(); + if (epics != null) decodedEvent.write(epics); + + for (Bank b : createReconScalerBanks(decodedEvent)) + decodedEvent.write(b); + + return decodedEvent; + } +} diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CLASDecoder4.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CLASDecoder4.java index 799b260188..12aa6bf5d7 100644 --- a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CLASDecoder4.java +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CLASDecoder4.java @@ -1,813 +1,23 @@ package org.jlab.detector.decode; -import org.jlab.detector.scalers.DaqScalers; -import java.util.ArrayList; import java.util.List; - -import java.util.LinkedHashMap; -import java.util.Map; import java.util.TreeSet; -import org.jlab.detector.base.DetectorDescriptor; -import org.jlab.detector.base.DetectorType; -import org.jlab.detector.calib.utils.RCDBProvider.RCDBManager; -import org.jlab.detector.decode.DetectorDataDgtz.HelicityDecoderData; -import org.jlab.detector.helicity.HelicityBit; import org.jlab.detector.helicity.HelicitySequence; import org.jlab.detector.helicity.HelicityState; -import org.jlab.detector.pulse.ModeAHDC; -import org.jlab.io.base.DataEvent; import org.jlab.io.evio.EvioDataEvent; import org.jlab.io.evio.EvioSource; -import org.jlab.io.hipo.HipoDataEvent; -import org.jlab.io.hipo.HipoDataSync; import org.jlab.jnp.hipo4.data.Bank; import org.jlab.jnp.hipo4.data.Event; -import org.jlab.jnp.hipo4.data.SchemaFactory; import org.jlab.jnp.hipo4.io.HipoWriterSorted; import org.jlab.utils.benchmark.ProgressPrintout; -import org.jlab.utils.groups.IndexedTable; import org.jlab.utils.options.OptionParser; import org.jlab.utils.system.ClasUtilsFile; -/** - * - * @author gavalian - */ -public class CLASDecoder4 { - - protected DetectorEventDecoder detectorDecoder = null; - protected SchemaFactory schemaFactory = new SchemaFactory(); - private CodaEventDecoder codaDecoder = null; - private List dataList = new ArrayList<>(); - private HipoDataSync writer = null; - private HipoDataEvent hipoEvent = null; - private boolean isRunNumberFixed = false; - private int decoderDebugMode = 0; - private ModeAHDC ahdcExtractor = new ModeAHDC(); - private RCDBManager rcdbManager = new RCDBManager(); - - public CLASDecoder4(boolean development){ - codaDecoder = new CodaEventDecoder(); - detectorDecoder = new DetectorEventDecoder(development); - writer = new HipoDataSync(); - hipoEvent = (HipoDataEvent) writer.createEvent(); - String dir = ClasUtilsFile.getResourceDir("CLAS12DIR", "etc/bankdefs/hipo4"); - schemaFactory.initFromDirectory(dir); - } - - public CLASDecoder4(){ - codaDecoder = new CodaEventDecoder(); - detectorDecoder = new DetectorEventDecoder(); - writer = new HipoDataSync(); - hipoEvent = (HipoDataEvent) writer.createEvent(); - String dir = ClasUtilsFile.getResourceDir("CLAS12DIR", "etc/bankdefs/hipo4"); - schemaFactory.initFromDirectory(dir); - } - - public SchemaFactory getSchemaFactory(){ - return schemaFactory; - } - - public void setVariation(String variation) { - detectorDecoder.setVariation(variation); - } - - public void setTimestamp(String timestamp) { - detectorDecoder.setTimestamp(timestamp); - } - - public void setDebugMode(int mode){ - this.decoderDebugMode = mode; - } - - public void setRunNumber(int run){ - if(this.isRunNumberFixed==false){ - this.detectorDecoder.setRunNumber(run); - } - } - - public void setRunNumber(int run, boolean fixed){ - this.isRunNumberFixed = fixed; - this.detectorDecoder.setRunNumber(run); - System.out.println(" SETTING RUN NUMBER TO " + run + " FIXED = " + this.isRunNumberFixed); - } - - public CodaEventDecoder getCodaEventDecoder() { - return codaDecoder; - } - - public void initEvent(DataEvent event){ - - if(event instanceof EvioDataEvent){ - EvioDataEvent evioEvent = (EvioDataEvent) event; - if(evioEvent.getHandler().getStructure()!=null){ - try { - - dataList = codaDecoder.getDataEntries( (EvioDataEvent) event); - - //----------------------------------------------------------------------------- - // This part reads the BITPACKED FADC data from tag=57638 Format (cmcms) - // Then unpacks into Detector Digigitized data, and appends to existing buffer - // Modified on 9/5/2018 - //----------------------------------------------------------------------------- - - List fadcPacked = codaDecoder.getADCEntries((EvioDataEvent) event); - - if(fadcPacked!=null){ - List fadcUnpacked = FADCData.convert(fadcPacked); - dataList.addAll(fadcUnpacked); - } - // END of Bitpacked section - //----------------------------------------------------------------------------- - - if(this.decoderDebugMode>0){ - System.out.println("\n>>>>>>>>> RAW decoded data"); - for(DetectorDataDgtz data : dataList){ - System.out.println(data); - } - } - int runNumberCoda = codaDecoder.getRunNumber(); - this.setRunNumber(runNumberCoda); - - detectorDecoder.translate(dataList); - detectorDecoder.fitPulses(dataList); - detectorDecoder.filterTDCs(dataList); - if(this.decoderDebugMode>0){ - System.out.println("\n>>>>>>>>> TRANSLATED data"); - for(DetectorDataDgtz data : dataList){ - System.out.println(data); - } - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - } - /** - * return list of digitized ADC values from internal list - * @param type detector type - * @return - */ - public List getEntriesADC(DetectorType type){ - return this.getEntriesADC(type, dataList); - } - /** - * returns ADC entries from decoded data for given detector TYPE - * @param type detector type - * @param entries digitized data list - * @return list of ADC's for detector type - */ - public List getEntriesADC(DetectorType type, - List entries){ - List adc = new ArrayList<>(); - for(DetectorDataDgtz entry : entries){ - if(entry.getDescriptor().getType()==type){ - if(entry.getADCSize()>0&&entry.getTDCSize()==0){ - adc.add(entry); - } - } - } - - return adc; - } - - public List getEntriesTDC(DetectorType type){ - return getEntriesTDC(type,dataList); - } - - /** - * returns TDC entries from decoded data for given detector type - * @param type detector type - * @param entries digitized data list - * @return list of ADC's for detector type - */ - public List getEntriesTDC(DetectorType type, - List entries){ - List tdc = new ArrayList<>(); - for(DetectorDataDgtz entry : entries){ - if(entry.getDescriptor().getType()==type){ - if(entry.getTDCSize()>0&&entry.getADCSize()==0){ - tdc.add(entry); - } - } - } - return tdc; - } - - public List getEntriesVTP(DetectorType type){ - return getEntriesVTP(type,dataList); - } - /** - * returns VTP entries from decoded data for given detector type - * @param type detector type - * @param entries digitized data list - * @return list of VTP's for detector type - */ - public List getEntriesVTP(DetectorType type, - List entries){ - List vtp = new ArrayList<>(); - for(DetectorDataDgtz entry : entries){ - if(entry.getDescriptor().getType()==type){ - if(entry.getVTPSize()>0){ - vtp.add(entry); - } - } - } - return vtp; - } - - public List getEntriesSCALER(DetectorType type){ - return getEntriesSCALER(type,dataList); - } - /** - * returns VTP entries from decoded data for given detector type - * @param type detector type - * @param entries digitized data list - * @return list of VTP's for detector type - */ - public List getEntriesSCALER(DetectorType type, - List entries){ - List scaler = new ArrayList<>(); - for(DetectorDataDgtz entry : entries){ - if(entry.getDescriptor().getType()==type){ - if(entry.getSCALERSize()>0){ - scaler.add(entry); - } - } - } - return scaler; - } - - public void extractPulses(Event event) { - ahdcExtractor.update(30, null, event, schemaFactory, "AHDC::wf", "AHDC::adc"); - } - - public Bank getDataBankWF(String name, DetectorType type) { - List a = this.getEntriesADC(type); - Bank b = new Bank(schemaFactory.getSchema(name), a.size()); - for (int i=0; i adcDGTZ = this.getEntriesADC(type); - - if(schemaFactory.hasSchema(name)==false) return null; - - Bank adcBANK = new Bank(schemaFactory.getSchema(name), adcDGTZ.size()); - - for(int i = 0; i < adcDGTZ.size(); i++){ - adcBANK.putByte("sector", i, (byte) adcDGTZ.get(i).getDescriptor().getSector()); - adcBANK.putByte("layer", i, (byte) adcDGTZ.get(i).getDescriptor().getLayer()); - adcBANK.putShort("component", i, (short) adcDGTZ.get(i).getDescriptor().getComponent()); - adcBANK.putByte("order", i, (byte) adcDGTZ.get(i).getDescriptor().getOrder()); - adcBANK.putInt("ADC", i, adcDGTZ.get(i).getADCData(0).getADC()); - adcBANK.putFloat("time", i, (float) adcDGTZ.get(i).getADCData(0).getTime()); - adcBANK.putShort("ped", i, (short) adcDGTZ.get(i).getADCData(0).getPedestal()); - if(name == "BST::adc") adcBANK.putLong("timestamp", i, adcDGTZ.get(i).getADCData(0).getTimeStamp()); - if(name.equals("BMT::adc")||name.equals("FMT::adc")|| name.equals("FTTRK::adc")){ - adcBANK.putInt("ADC", i, adcDGTZ.get(i).getADCData(0).getHeight()); - adcBANK.putInt("integral", i, adcDGTZ.get(i).getADCData(0).getIntegral()); - adcBANK.putLong("timestamp", i, adcDGTZ.get(i).getADCData(0).getTimeStamp()); - } - if(name == "BAND::adc") adcBANK.putInt("amplitude", i, adcDGTZ.get(i).getADCData(0).getHeight()); - } - return adcBANK; - } - - - public Bank getDataBankTDC(String name, DetectorType type){ - - List tdcDGTZ = this.getEntriesTDC(type); - if(schemaFactory.hasSchema(name)==false) return null; - Bank tdcBANK = new Bank(schemaFactory.getSchema(name), tdcDGTZ.size()); - - if(tdcBANK==null) return null; - - for(int i = 0; i < tdcDGTZ.size(); i++){ - tdcBANK.putByte("sector", i, (byte) tdcDGTZ.get(i).getDescriptor().getSector()); - tdcBANK.putByte("layer", i, (byte) tdcDGTZ.get(i).getDescriptor().getLayer()); - tdcBANK.putShort("component", i, (short) tdcDGTZ.get(i).getDescriptor().getComponent()); - tdcBANK.putByte("order", i, (byte) (tdcDGTZ.get(i).getDescriptor().getOrder()+tdcDGTZ.get(i).getTDCData(0).getType().getTypeId())); - tdcBANK.putInt("TDC", i, tdcDGTZ.get(i).getTDCData(0).getTime()); - if(tdcBANK.getSchema().hasEntry("ToT")) - tdcBANK.putShort("ToT", i, (short) tdcDGTZ.get(i).getTDCData(0).getToT()); - } - return tdcBANK; - } - - public Bank getDataBankTDCPetiroc(String name, DetectorType type){ - - List tdcDGTZ = this.getEntriesTDC(type); - if(schemaFactory.hasSchema(name)==false){ - System.out.println("WARNING: No schema for TDC type : " + type); - return null; - } - Bank tdcBANK = new Bank(schemaFactory.getSchema(name), tdcDGTZ.size()); - - if(tdcBANK==null) return null; - - // Not sure why the schemea information isn't used here. - for(int i = 0; i < tdcDGTZ.size(); i++){ - tdcBANK.putByte("sector", i, (byte) tdcDGTZ.get(i).getDescriptor().getSector()); - tdcBANK.putByte("layer", i, (byte) tdcDGTZ.get(i).getDescriptor().getLayer()); - tdcBANK.putShort("component", i, (short) tdcDGTZ.get(i).getDescriptor().getComponent()); - tdcBANK.putByte("order", i, (byte) tdcDGTZ.get(i).getDescriptor().getOrder()); - tdcBANK.putInt("TDC", i, tdcDGTZ.get(i).getTDCData(0).getTime()); - tdcBANK.putInt("ToT", i, tdcDGTZ.get(i).getTDCData(0).getToT()); - tdcBANK.putLong("timestamp", i, tdcDGTZ.get(i).getTDCData(0).getTimeStamp()); - tdcBANK.putInt("trigger", i, tdcDGTZ.get(i).getTrigger()); - //System.err.println("event: " + tdcDGTZ.get(i).toString()); - } - return tdcBANK; - } - - - public Bank getDataBankTimeStamp(String name, DetectorType type) { - - List tdcDGTZ = this.getEntriesTDC(type); - if(schemaFactory.hasSchema(name)==false) return null; - Map tsMap = new LinkedHashMap<>(); - for(DetectorDataDgtz tdc : tdcDGTZ) { - DetectorDescriptor desc = tdc.getDescriptor(); - int hash = ((desc.getCrate()<<8)&0xFF00) | (desc.getSlot()&0x00FF); - if(tsMap.containsKey(hash)) { - if(tsMap.get(hash).getTimeStamp() != tdc.getTimeStamp()) - System.out.println("WARNING: inconsistent timestamp for DCRB crate/slot " - + desc.getCrate() + "/" + desc.getSlot()); - } - else { - tsMap.put(hash, tdc); - } - } - - Bank tsBANK = new Bank(schemaFactory.getSchema(name), tsMap.size()); - - if(tsBANK==null) return null; - - int i=0; - for(DetectorDataDgtz tdc : tsMap.values()) { - tsBANK.putByte("crate", i, (byte) tdc.getDescriptor().getCrate()); - tsBANK.putByte("slot", i, (byte) tdc.getDescriptor().getSlot()); - tsBANK.putLong("timestamp", i, tdc.getTimeStamp()); - i++; - } - return tsBANK; - } - - public Bank getDataBankUndecodedADC(String name, DetectorType type){ - List adcDGTZ = this.getEntriesADC(type); - Bank adcBANK = new Bank(schemaFactory.getSchema(name), adcDGTZ.size()); - - for(int i = 0; i < adcDGTZ.size(); i++){ - adcBANK.putByte("crate", i, (byte) adcDGTZ.get(i).getDescriptor().getCrate()); - adcBANK.putByte("slot", i, (byte) adcDGTZ.get(i).getDescriptor().getSlot()); - adcBANK.putShort("channel", i, (short) adcDGTZ.get(i).getDescriptor().getChannel()); - adcBANK.putInt("ADC", i, adcDGTZ.get(i).getADCData(0).getADC()); - adcBANK.putFloat("time", i, (float) adcDGTZ.get(i).getADCData(0).getTime()); - adcBANK.putShort("ped", i, (short) adcDGTZ.get(i).getADCData(0).getPedestal()); - } - return adcBANK; - } - - public Bank getDataBankUndecodedTDC(String name, DetectorType type){ - - List tdcDGTZ = this.getEntriesTDC(type); - - Bank tdcBANK = new Bank(schemaFactory.getSchema(name), tdcDGTZ.size()); - if(tdcBANK==null) return null; - - for(int i = 0; i < tdcDGTZ.size(); i++){ - tdcBANK.putByte("crate", i, (byte) tdcDGTZ.get(i).getDescriptor().getCrate()); - tdcBANK.putByte("slot", i, (byte) tdcDGTZ.get(i).getDescriptor().getSlot()); - tdcBANK.putShort("channel", i, (short) tdcDGTZ.get(i).getDescriptor().getChannel()); - tdcBANK.putInt("TDC", i, tdcDGTZ.get(i).getTDCData(0).getTime()); - } - return tdcBANK; - } - - public Bank getDataBankUndecodedVTP(String name, DetectorType type){ - - List vtpDGTZ = this.getEntriesVTP(type); - - Bank vtpBANK = new Bank(schemaFactory.getSchema(name), vtpDGTZ.size()); - if(vtpBANK==null) return null; - - for(int i = 0; i < vtpDGTZ.size(); i++){ - vtpBANK.putByte("crate", i, (byte) vtpDGTZ.get(i).getDescriptor().getCrate()); - vtpBANK.putInt("word", i, vtpDGTZ.get(i).getVTPData(0).getWord()); - } - return vtpBANK; - } - - public Bank getDataBankUndecodedSCALER(String name, DetectorType type){ - - List scalerDGTZ = this.getEntriesSCALER(type); - - Bank scalerBANK = new Bank(schemaFactory.getSchema(name), scalerDGTZ.size()); - if(scalerBANK==null) return null; - - for(int i = 0; i < scalerDGTZ.size(); i++){ - scalerBANK.putByte("crate", i, (byte) scalerDGTZ.get(i).getDescriptor().getCrate()); - scalerBANK.putByte("slot", i, (byte) scalerDGTZ.get(i).getDescriptor().getSlot()); - scalerBANK.putShort("channel", i, (short) scalerDGTZ.get(i).getDescriptor().getChannel()); - scalerBANK.putByte("helicity", i, (byte) scalerDGTZ.get(i).getSCALERData(0).getHelicity()); - scalerBANK.putByte("quartet", i, (byte) scalerDGTZ.get(i).getSCALERData(0).getQuartet()); - scalerBANK.putLong("value", i, scalerDGTZ.get(i).getSCALERData(0).getValue()); - } - return scalerBANK; - } - - public Event getDecodedEvent(EvioDataEvent rawEvent, int run, int counter, Double torus, Double solenoid) { - - Event decodedEvent = this.getDataEvent(rawEvent); - - Bank header = this.createHeaderBank(run, counter, torus, solenoid); - if(header!=null) decodedEvent.write(header); - - Bank trigger = this.createTriggerBank(); - if(trigger!=null) decodedEvent.write(trigger); - - Bank onlineHelicity = this.createOnlineHelicityBank(); - if(onlineHelicity!=null) decodedEvent.write(onlineHelicity); - - Bank decodedHelicity = this.createHelicityDecoderBank(rawEvent); - if (decodedHelicity!=null) decodedEvent.write(decodedHelicity); - - this.extractPulses(decodedEvent); - - Bank epics = createEpicsBank(); - if (epics != null) decodedEvent.write(epics); - - for (Bank b : createReconScalerBanks(decodedEvent)) - decodedEvent.write(b); - - return decodedEvent; - } - - public Event getDataEvent(DataEvent rawEvent){ - this.initEvent(rawEvent); - return getDataEvent(); - } - - public Event getDataEvent(){ - - Event event = new Event(); - - String[] wfBankNames = new String[]{"AHDC::wf"}; - DetectorType[] wfBankTypes = new DetectorType[]{DetectorType.AHDC}; - String[] adcBankNames = new String[]{"FTOF::adc","ECAL::adc","FTCAL::adc", - "FTHODO::adc", "FTTRK::adc", - "HTCC::adc","BST::adc","CTOF::adc", - "CND::adc","LTCC::adc","BMT::adc", - "FMT::adc","HEL::adc","RF::adc", - "BAND::adc","RASTER::adc"}; - DetectorType[] adcBankTypes = new DetectorType[]{DetectorType.FTOF,DetectorType.ECAL,DetectorType.FTCAL, - DetectorType.FTHODO,DetectorType.FTTRK, - DetectorType.HTCC,DetectorType.BST,DetectorType.CTOF, - DetectorType.CND,DetectorType.LTCC,DetectorType.BMT, - DetectorType.FMT,DetectorType.HEL,DetectorType.RF, - DetectorType.BAND, DetectorType.RASTER}; - - String[] tdcBankNames = new String[]{"FTOF::tdc","ECAL::tdc","DC::tot", - "HTCC::tdc","LTCC::tdc","CTOF::tdc", - "CND::tdc","RF::tdc","RICH::tdc", - "BAND::tdc"}; - DetectorType[] tdcBankTypes = new DetectorType[]{DetectorType.FTOF,DetectorType.ECAL, - DetectorType.DC,DetectorType.HTCC,DetectorType.LTCC, - DetectorType.CTOF,DetectorType.CND,DetectorType.RF, - DetectorType.RICH,DetectorType.BAND}; - - for(int i = 0; i < adcBankTypes.length; i++){ - Bank adcBank = getDataBankADC(adcBankNames[i],adcBankTypes[i]); - if(adcBank!=null){ - if(adcBank.getRows()>0){ - event.write(adcBank); - } - } - } - - for(int i = 0; i < wfBankTypes.length; i++){ - Bank wfBank = getDataBankWF(wfBankNames[i],wfBankTypes[i]); - if(wfBank!=null && wfBank.getRows()>0){ - event.write(wfBank); - } - } - - for(int i = 0; i < tdcBankTypes.length; i++){ - Bank tdcBank = getDataBankTDC(tdcBankNames[i],tdcBankTypes[i]); - if(tdcBank!=null){ - if(tdcBank.getRows()>0){ - event.write(tdcBank); - } - } - } - try { - // Do ATOF - Bank tdcBank = getDataBankTDCPetiroc("ATOF::tdc",DetectorType.ATOF); - if(tdcBank!=null){ - if(tdcBank.getRows()>0){ - event.write(tdcBank); - } - } - } catch(Exception e) { - e.printStackTrace(); - } - - - try { - Bank tsBank = getDataBankTimeStamp("DC::jitter", DetectorType.DC); - if(tsBank != null) { - if(tsBank.getRows()>0) { - event.write(tsBank); - } - } - } catch(Exception e) { - e.printStackTrace(); - } - - /** - * Adding un-decoded banks to the event - */ - try { - Bank adcBankUD = this.getDataBankUndecodedADC("RAW::adc", DetectorType.UNDEFINED); - if(adcBankUD!=null){ - if(adcBankUD.getRows()>0){ - event.write(adcBankUD); - } - } - } catch(Exception e) { - e.printStackTrace(); - } - - try { - Bank tdcBankUD = this.getDataBankUndecodedTDC("RAW::tdc", DetectorType.UNDEFINED); - if(tdcBankUD!=null){ - if(tdcBankUD.getRows()>0){ - event.write(tdcBankUD); - } - } else { - - } - } catch(Exception e) { - e.printStackTrace(); - } - - try { - Bank vtpBankUD = this.getDataBankUndecodedVTP("RAW::vtp", DetectorType.UNDEFINED); - if(vtpBankUD!=null){ - if(vtpBankUD.getRows()>0){ - event.write(vtpBankUD); - } - } else { - - } - } catch(Exception e) { - e.printStackTrace(); - } - - try { - Bank scalerBankUD = this.getDataBankUndecodedSCALER("RAW::scaler", DetectorType.UNDEFINED); - if(scalerBankUD!=null){ - if(scalerBankUD.getRows()>0){ - event.write(scalerBankUD); - } - } - } catch(Exception e) { - e.printStackTrace(); - } - //----------------------------------------------------- - // CREATING BONUS BANK -------------------------------- - //----------------------------------------------------- - try { - //System.out.println("creating bonus bank...."); - Bank bonusBank = this.createBonusBank(); - if(bonusBank!=null){ - if(bonusBank.getRows()>0){ - event.write(bonusBank); - } - } - } catch(Exception e) { - e.printStackTrace(); - } - return event; - } - - public long getTriggerPhase() { - long timestamp = this.codaDecoder.getTimeStamp(); - int phase_offset = 1; - return ((timestamp%6)+phase_offset)%6; // TI derived phase correction due to TDC and FADC clock differences - } - - public Bank createHeaderBank( int nrun, int nevent, Double torus, Double solenoid){ - - if(schemaFactory.hasSchema("RUN::config")==false) return null; - - Bank bank = new Bank(schemaFactory.getSchema("RUN::config"), 1); - - int localRun = this.codaDecoder.getRunNumber(); - int localEvent = this.codaDecoder.getEventNumber(); - int localTime = this.codaDecoder.getUnixTime(); - long timeStamp = this.codaDecoder.getTimeStamp(); - long triggerBits = this.codaDecoder.getTriggerBits(); - - if(nrun>0){ - localRun = nrun; - localEvent = nevent; - } - - bank.putInt("run", 0, localRun); - bank.putInt("event", 0, localEvent); - bank.putInt("unixtime", 0, localTime); - bank.putLong("trigger", 0, triggerBits); - bank.putLong("timestamp", 0, timeStamp); - - if (torus != null) { - bank.putFloat("torus", 0, torus.floatValue()); - } - else if (rcdbManager.getTorusScale(localRun) == null) { - if (localRun > 100) throw new RuntimeException("Error retrieving torus scale from RCDB."); - } - else { - bank.putFloat("torus", 0, rcdbManager.getTorusScale(localRun).floatValue()); - } - if (solenoid != null) { - bank.putFloat("solenoid", 0, solenoid.floatValue()); - } - else if (rcdbManager.getSolenoidScale(localRun) == null) { - if (localRun > 100) throw new RuntimeException("Error retrieving solenoid scale from RCDB."); - } - else { - bank.putFloat("solenoid", 0, rcdbManager.getSolenoidScale(localRun).floatValue()); - } - - return bank; - } - - public Bank createOnlineHelicityBank() { - if (schemaFactory.hasSchema("HEL::online")==false || - this.codaDecoder.getHelicityLevel3()==HelicityBit.DNE.value()) return null; - Bank bank = new Bank(schemaFactory.getSchema("HEL::online"), 1); - byte helicityL3 = this.codaDecoder.getHelicityLevel3(); - IndexedTable hwpTable = this.detectorDecoder.scalerManager. - getConstants(this.detectorDecoder.getRunNumber(),"/runcontrol/hwp"); - bank.putByte("helicityRaw",0, helicityL3); - bank.putByte("helicity",0,(byte)(helicityL3*hwpTable.getIntValue("hwp",0,0,0))); - return bank; - } - - public Bank createTriggerBank(){ - - if(schemaFactory.hasSchema("RUN::trigger")==false) return null; - - Bank bank = new Bank(schemaFactory.getSchema("RUN::trigger"), this.codaDecoder.getTriggerWords().size()); - - for(int i=0; i createReconScalerBanks(Event event){ - return DaqScalers.createBanks(detectorDecoder.getRunNumber(), - schemaFactory, event, detectorDecoder.scalerManager); - } - - public Bank createBonusBank(){ - if(schemaFactory.hasSchema("RTPC::adc")==false) return null; - List bonusData = this.getEntriesADC(DetectorType.RTPC); - int totalSize = 0; - for(int i = 0; i < bonusData.size(); i++){ - short[] pulse = bonusData.get(i).getADCData(0).getPulseArray(); - totalSize += pulse.length; - } - - Bank bonusBank = new Bank(schemaFactory.getSchema("RTPC::adc"), totalSize); - int currentRow = 0; - for(int i = 0; i < bonusData.size(); i++){ - - DetectorDataDgtz bonus = bonusData.get(i); - - short[] pulses = bonus.getADCData(0).getPulseArray(); - long timestamp = bonus.getADCData(0).getTimeStamp(); - double time = bonus.getADCData(0).getTime(); - double coeff = time*120.0; - - double offset1 = 0.0; - double offset2 = (double) (8*(timestamp%8)); - - for(int k = 0; k < pulses.length; k++){ - - double pulseTime = coeff + offset1 + offset2 + k*120.0; - - bonusBank.putByte("sector", currentRow, (byte) bonus.getDescriptor().getSector()); - bonusBank.putByte("layer" , currentRow, (byte) bonus.getDescriptor().getLayer()); - bonusBank.putShort("component", currentRow, (short) bonus.getDescriptor().getComponent()); - bonusBank.putByte("order", currentRow, (byte) bonus.getDescriptor().getOrder()); - bonusBank.putInt("ADC", currentRow, pulses[k]); - bonusBank.putFloat("time", currentRow, (float) pulseTime); - bonusBank.putShort("ped", currentRow, (short) 0); - currentRow++; - } - } - - return bonusBank; - } - - public Bank createHelicityDecoderBank(EvioDataEvent event) { - HelicityDecoderData data = this.codaDecoder.getDataEntries_HelicityDecoder(event); - if(data!=null) { - Bank bank = new Bank(schemaFactory.getSchema("HEL::decoder"), 1); - bank.putByte("helicity", 0, data.getHelicityState().getHelicity().value()); - bank.putByte("pair", 0, data.getHelicityState().getPairSync().value()); - bank.putByte("pattern", 0, data.getHelicityState().getPatternSync().value()); - bank.putByte("tSettle", 0, data.getTSettle().value()); - bank.putByte("helicityPattern", 0, data.getHelicityPattern().value()); - bank.putByte("polarity", 0, data.getPolarity()); - bank.putByte("phase", 0, data.getPatternPhaseCount()); - bank.putLong("timestamp", 0, data.getTimestamp()); - bank.putInt("helicitySeed", 0, data.getHelicitySeed()); - bank.putInt("nTStableRE", 0, data.getNTStableRisingEdge()); - bank.putInt("nTStableFE", 0, data.getNTStableFallingEdge()); - bank.putInt("nPattern", 0, data.getNPattern()); - bank.putInt("nPair", 0, data.getNPair()); - bank.putInt("tStableStart", 0, data.getTStableStart()); - bank.putInt("tStableEnd", 0, data.getTStableEnd()); - bank.putInt("tStableTime", 0, data.getTStableTime()); - bank.putInt("tSettleTime", 0, data.getTSettleTime()); - bank.putInt("patternArray", 0, data.getPatternWindows()); - bank.putInt("pairArray", 0, data.getPairWindows()); - bank.putInt("helicityArray", 0, data.getHelicityWindows()); - bank.putInt("helicityPArray", 0, data.getHelicityPatternWindows()); - return bank; - } - else - return null; - } - - public static Event createTaggedEvent(Event e, Bank runConfig, Bank... banks) { - Event t = new Event(); - for (Bank b : banks) { - e.read(b); - if (b.getRows() > 0) t.write(b); - } - if (!t.isEmpty()) { - e.read(runConfig); - t.write(runConfig); - } - return t; - } - - public static Event createTaggedEvent(SchemaFactory sf, Event e, String... banks) { - Bank[] b = new Bank[banks.length]; - for (int i=0; i 0) setRunNumber(runNumber, true); if (o.getOption("-x").getValue() != null) detectorDecoder.setTimestamp(o.getOption("-x").stringValue()); @@ -164,4 +144,31 @@ private void init(OptionParser o){ } } -} \ No newline at end of file + /** + * The command-line "decoder" program. + * @param args + */ + public static void main(String[] args) { + + // hijack arguments, when run from an IDE: + if (System.console() == null && args.length == 0) { + // delete output file, if necessary: + File f = new File("tmp.hipo"); + if (f.exists()) f.delete(); + // setup decoder command-line options: + args = new String[]{"-o","tmp.hipo",System.getenv("HOME")+"/data/clas_018779.evio.00001"}; + // try to find bankdefs: + System.setProperty("CLAS12DIR", System.getenv("HOME")+"/sw/coatjava/coatjava"); + } + + // parse command-line options: + OptionParser opts = getOptionParser(); + opts.parse(args); + + // run the decoder: + CLASDecoder4U decoder = new CLASDecoder4U(opts); + while (decoder.hasNext()) decoder.getNext(); + decoder.close(); + } + +} diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CodaEventDecoder.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CodaEventDecoder.java index 13d3c4fa8d..6caf767b8b 100644 --- a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CodaEventDecoder.java +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/CodaEventDecoder.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; import org.jlab.coda.jevio.ByteDataTransformer; @@ -30,6 +31,8 @@ */ public class CodaEventDecoder { + static final Logger LOGGER = Logger.getLogger("CodaEventDecoder"); + private int runNumber = 0; private int eventNumber = 0; private int unixTime = 0; @@ -39,15 +42,29 @@ public class CodaEventDecoder { private byte helicityLevel3 = HelicityBit.UDF.value(); private final List triggerWords = new ArrayList<>(); JsonObject epicsData = new JsonObject(); + List branchList = null; + TreeMap branchMap = null; private int tiMaster = -1; // FIXME: move this to CCDB, e.g., meanwhile cannot reuse ROC id private static final List PCIE_ROCS = Arrays.asList(new Integer[]{78}); - public CodaEventDecoder(){ - + public CodaEventDecoder() {} + + /** + * Loads map by crate(?). + * @param event + */ + void cacheBranches(EvioDataEvent event) { + branchList = getEventBranches(event); + branchMap = new TreeMap<>(); + for (EvioTreeBranch branch : branchList) { + if (!branchMap.containsKey(branch.getTag())) + branchMap.put(branch.getTag(), branch); + } } + /** * returns detector digitized data entries from the event. * all branches are analyzed and different types of digitized data @@ -69,9 +86,8 @@ public List getDataEntries(EvioDataEvent event){ // from the previous event, in the case where there's no HEAD bank: this.setTriggerBits(0); List rawEntries = new ArrayList<>(); - List branches = this.getEventBranches(event); this.setTimeStamp(event); - for(EvioTreeBranch branch : branches){ + for(EvioTreeBranch branch : branchList){ List list = this.getDataEntries(event,branch.getTag()); if(list != null){ rawEntries.addAll(list); @@ -186,8 +202,7 @@ public void setTriggerBits(long triggerBits) { public List getADCEntries(EvioDataEvent event){ List entries = new ArrayList<>(); - List branches = this.getEventBranches(event); - for(EvioTreeBranch branch : branches){ + for(EvioTreeBranch branch : branchList){ List list = this.getADCEntries(event,branch.getTag()); if(list != null){ entries.addAll(list); @@ -199,8 +214,7 @@ public List getADCEntries(EvioDataEvent event){ public List getADCEntries(EvioDataEvent event, int crate){ List entries = new ArrayList<>(); - List branches = this.getEventBranches(event); - EvioTreeBranch cbranch = this.getEventBranch(branches, crate); + EvioTreeBranch cbranch = this.getEventBranch(branchList, crate); if(cbranch == null ) return null; @@ -216,9 +230,8 @@ public List getADCEntries(EvioDataEvent event, int crate){ public List getADCEntries(EvioDataEvent event, int crate, int tagid){ List adc = new ArrayList<>(); - List branches = this.getEventBranches(event); - EvioTreeBranch cbranch = this.getEventBranch(branches, crate); + EvioTreeBranch cbranch = this.getEventBranch(branchList, crate); if(cbranch == null ) return null; for(EvioNode node : cbranch.getNodes()){ @@ -239,10 +252,9 @@ public List getADCEntries(EvioDataEvent event, int crate, int tagid){ */ public List getDataEntries(EvioDataEvent event, int crate){ - List branches = this.getEventBranches(event); List bankEntries = new ArrayList<>(); - EvioTreeBranch cbranch = this.getEventBranch(branches, crate); + EvioTreeBranch cbranch = this.getEventBranch(branchList, crate); if(cbranch == null ) return null; for (EvioNode node : cbranch.getNodes()) { @@ -309,7 +321,7 @@ else if(node.getTag()==57636){ * @param event * @return */ - public List getEventBranches(EvioDataEvent event){ + public static List getEventBranches(EvioDataEvent event){ ArrayList branches = new ArrayList<>(); try { @@ -330,7 +342,7 @@ public List getEventBranches(EvioDataEvent event){ } } catch (EvioException ex) { - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } return branches; } @@ -341,10 +353,13 @@ public List getEventBranches(EvioDataEvent event){ * @return */ public EvioTreeBranch getEventBranch(List branches, int tag){ + return branchMap.getOrDefault(tag, null); + /* for(EvioTreeBranch branch : branches){ if(branch.getTag()==tag) return branch; } return null; + */ } public void readHeaderBank(Integer crate, EvioNode node, EvioDataEvent event){ @@ -706,7 +721,7 @@ public List getDataEntries_57627(Integer crate, EvioNode node return entries; } catch (EvioException ex) { - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } return entries; @@ -776,7 +791,7 @@ public List getDataEntries_57640(Integer crate, EvioNode node return entries; } catch (EvioException ex) { - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } return entries; @@ -853,7 +868,7 @@ public List getDataEntries_57641(Integer crate, EvioNode node return entries; } catch (EvioException ex) { - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } return entries; @@ -915,7 +930,7 @@ public List getDataEntries_57602(Integer crate, EvioNode node } return entries; } catch (EvioException ex) { - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } return entries; @@ -987,7 +1002,7 @@ public List getDataEntries_57603(Integer crate, EvioNode node } return entries; } catch (EvioException ex) { - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } return entries; @@ -1030,9 +1045,9 @@ public List getDataEntries_57622(Integer crate, EvioNode node } } } catch (EvioException ex) { - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } catch (IndexOutOfBoundsException ex){ - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } @@ -1077,9 +1092,9 @@ public List getDataEntries_57648(Integer crate, EvioNode node } } } catch (EvioException ex) { - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } catch (IndexOutOfBoundsException ex){ - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } @@ -1140,7 +1155,7 @@ public List getDataEntries_57636(Integer crate, EvioNode node return entries; } catch (EvioException ex) { - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } return entries; @@ -1223,7 +1238,7 @@ public List getDataEntries_57657(Integer crate, EvioNode node return entries; } catch (EvioException ex) { - Logger.getLogger(CodaEventDecoder.class.getName()).log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } return entries; @@ -1233,8 +1248,7 @@ public List getDataEntries_57657(Integer crate, EvioNode node public void getDataEntries_EPICS(EvioDataEvent event){ epicsData = new JsonObject(); - List branches = this.getEventBranches(event); - for(EvioTreeBranch branch : branches){ + for(EvioTreeBranch branch : branchList){ for(EvioNode node : branch.getNodes()){ if(node.getTag()==57620) { byte[] stringData = ByteDataTransformer.toByteArray(node.getStructureBuffer(true)); @@ -1260,8 +1274,7 @@ public void getDataEntries_EPICS(EvioDataEvent event){ public HelicityDecoderData getDataEntries_HelicityDecoder(EvioDataEvent event){ HelicityDecoderData data = null; - List branches = this.getEventBranches(event); - for(EvioTreeBranch branch : branches){ + for(EvioTreeBranch branch : branchList){ for(EvioNode node : branch.getNodes()){ if(node.getTag()==57651) { @@ -1341,8 +1354,7 @@ public HelicityDecoderData getDataEntries_HelicityDecoder(EvioDataEvent event){ public List getDataEntries_Scalers(EvioDataEvent event){ List scalerEntries = new ArrayList<>(); - List branches = this.getEventBranches(event); - for(EvioTreeBranch branch : branches){ + for(EvioTreeBranch branch : branchList){ int crate = branch.getTag(); for(EvioNode node : branch.getNodes()){ if(node.getTag()==57637 || node.getTag()==57621){ @@ -1421,8 +1433,7 @@ else if(node.getTag()==57621 && loop>=5) { public List getDataEntries_VTP(EvioDataEvent event){ List vtpEntries = new ArrayList<>(); - List branches = this.getEventBranches(event); - for(EvioTreeBranch branch : branches){ + for(EvioTreeBranch branch : branchList){ int crate = branch.getTag(); for(EvioNode node : branch.getNodes()){ if(node.getTag()==57634){ @@ -1447,11 +1458,10 @@ public List getDataEntries_VTP(EvioDataEvent event){ public List getDataEntries_TDC(EvioDataEvent event){ List tdcEntries = new ArrayList<>(); - List branches = this.getEventBranches(event); - for(EvioTreeBranch branch : branches){ + for(EvioTreeBranch branch : branchList){ int crate = branch.getTag(); - EvioTreeBranch cbranch = this.getEventBranch(branches, branch.getTag()); + EvioTreeBranch cbranch = this.getEventBranch(branchList, branch.getTag()); for(EvioNode node : cbranch.getNodes()){ if(node.getTag()==57607){ int[] intData = ByteDataTransformer.toIntArray(node.getStructureBuffer(true)); @@ -1479,10 +1489,9 @@ public List getDataEntries_TDC(EvioDataEvent event){ public List getDataEntries_TI(EvioDataEvent event){ List tiEntries = new ArrayList<>(); - List branches = this.getEventBranches(event); - for(EvioTreeBranch branch : branches){ + for(EvioTreeBranch branch : branchList){ int crate = branch.getTag(); - EvioTreeBranch cbranch = this.getEventBranch(branches, branch.getTag()); + EvioTreeBranch cbranch = this.getEventBranch(branchList, branch.getTag()); for(EvioNode node : cbranch.getNodes()){ if(node.getTag()==57610){ long[] longData = ByteDataTransformer.toLongArray(node.getStructureBuffer(true)); diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/DetectorEventDecoder.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/DetectorEventDecoder.java index 4f726283a8..e8ae459a3f 100644 --- a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/DetectorEventDecoder.java +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/DetectorEventDecoder.java @@ -28,18 +28,27 @@ public class DetectorEventDecoder { List keysTrans = null; List keysFitter = null; List keysFilter = null; + List keysMicromega= null; private int runNumber = 10; private ExtendedFADCFitter extendedFitter = new ExtendedFADCFitter(); private MVTFitter mvtFitter = new MVTFitter(); + private TranslationTable translator = new TranslationTable(); + public DetectorEventDecoder(boolean development){ - if(development==true){ - this.initDecoderDev(); - } else { - this.initDecoder(); + if(development==true) this.initDecoderDev(); + else this.initDecoder(); + } + + public void setRunNumber(int run){ + if (run != this.runNumber) { + translator = new TranslationTable(); + for (int i=0; i detectorData){ + public void translate(List detectorData){ - // Preload CCDB tables: - ArrayList tables = new ArrayList<>(); - for (String name : tablesTrans) { - tables.add(translationManager.getConstants(runNumber, name)); - } + for (DetectorDataDgtz d : detectorData) { - for (DetectorDataDgtz data : detectorData) { + // Get the hardware indexing for this detector data object: + long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(d.getDescriptor().getCrate(), + d.getDescriptor().getSlot(), d.getDescriptor().getChannel()); - // Get the hardware indexing for this detector hit: - int crate = data.getDescriptor().getCrate(); - int slot = data.getDescriptor().getSlot(); - int channel = data.getDescriptor().getChannel(); - long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(crate,slot,channel); - - // Try to find it in the translation tables: - for (int j=0; j x = translator.getIntegersByHash(hash); - IndexedTable t = tables.get(j); - - // Found it; now set the detector indexing for this hit: - if (t.hasEntryByHash(hash)) { - - int sector = t.getIntValueByHash(0, hash); - int layer = t.getIntValueByHash(1, hash); - int component = t.getIntValueByHash(2, hash); - int order = t.getIntValueByHash(3, hash); - - data.getDescriptor().setSectorLayerComponent(sector, layer, component); - data.getDescriptor().setOrder(order); - data.getDescriptor().setType(keysTrans.get(j)); - - for(int i = 0; i < data.getADCSize(); i++) data.getADCData(i).setOrder(order); - for(int i = 0; i < data.getTDCSize(); i++) data.getTDCData(i).setOrder(order); - - // Assume there's only one instance of this crate/slot/channel - // in all translation tables, and we found it, so stop: - break; - } + // Set the translated detector indexing: + d.getDescriptor().setSectorLayerComponentOrderType(x.get(0),x.get(1),x.get(2),x.get(3),x.get(4)); + for (int i=0; i detectorData){ // preload CCDB tables once: ArrayList tables = new ArrayList<>(); - for (String name : tablesFitter) { - tables.add(fitterManager.getConstants(runNumber, name)); - } + for (String name : tablesFitter) tables.add(fitterManager.getConstants(runNumber, name)); + // loop over data: for(DetectorDataDgtz data : detectorData){ - int crate = data.getDescriptor().getCrate(); - int slot = data.getDescriptor().getSlot(); - int channel = data.getDescriptor().getChannel(); - long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(crate,slot,channel); - long hash0 = IndexedTable.DEFAULT_GENERATOR.hashCode(0,0,0); - for (int j=0; j 0) { - ADCData adc = data.getADCData(0); - mvtFitter.fit(adcOffset, fineTimeStampResolution, samplingTime, adc.getPulseArray(), adc.getTimeStamp(), sparseSample); - adc.setHeight((short) (mvtFitter.adcMax)); - adc.setTime((int) (mvtFitter.timeMax)); - adc.setIntegral((int) (mvtFitter.integral)); - adc.setTimeStamp(mvtFitter.timestamp); + + // custom MM fitter, uses only hash0: + if (keysMicromega.contains(data.getDescriptor().getType())) { + long hash0 = 0;//IndexedTable.DEFAULT_GENERATOR.hashCode(0,0,0); + for (int j=0; j 0) { + ADCData adc = data.getADCData(0); + mvtFitter.fit(adcOffset, fineTimeStampResolution, samplingTime, adc.getPulseArray(), adc.getTimeStamp(), sparseSample); + adc.setHeight((short) (mvtFitter.adcMax)); + adc.setTime((int) (mvtFitter.timeMax)); + adc.setIntegral((int) (mvtFitter.integral)); + adc.setTimeStamp(mvtFitter.timestamp); + } + break; } - } else { - if(daq.hasEntryByHash(hash)==true){ + } + } + + // standard fitter, uses full detector descriptor: + else { + int crate = data.getDescriptor().getCrate(); + int slot = data.getDescriptor().getSlot(); + int channel = data.getDescriptor().getChannel(); + long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(crate,slot,channel); + for (int j=0; j0){ - for(int i = 0; i < data.getADCSize(); i++){ + if (data.getADCSize() > 0) { + for (int i = 0; i < data.getADCSize(); i++) { ADCData adc = data.getADCData(i); - if(adc.getPulseSize()>0){ + if (adc.getPulseSize() > 0) { try { extendedFitter.fit(nsa, nsb, tet, ped, adc.getPulseArray()); } catch (Exception e) { System.out.println(">>>> error : fitting pulse " - + crate + " / " + slot + " / " + channel); + + crate + " / " + slot + " / " + channel); } - int adc_corrected = extendedFitter.adc + extendedFitter.ped*(nsa+nsb); + int adc_corrected = extendedFitter.adc + extendedFitter.ped * (nsa + nsb); adc.setHeight((short) this.extendedFitter.pulsePeakValue); adc.setIntegral(adc_corrected); adc.setTimeWord(this.extendedFitter.t0); adc.setPedestal((short) this.extendedFitter.ped); } + data.getADCData(i).setADC(nsa, nsb); } } - if(data.getADCSize()>0){ - for(int i = 0; i < data.getADCSize(); i++){ - data.getADCData(i).setADC(nsa, nsb); - } - } + break; } } } @@ -265,6 +252,7 @@ public void filterTDCs(List detectorData){ if(!filteredData.containsKey(key)) filteredData.put(key, new ArrayList<>()); filteredData.get(key).add(data); + break; } } for(int key : filteredData.keySet()) { @@ -277,8 +265,6 @@ public void filterTDCs(List detectorData){ } class TDCComparator implements Comparator { - - // override the compare() method @Override public int compare(DetectorDataDgtz s1, DetectorDataDgtz s2) { diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/TranslationTable.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/TranslationTable.java new file mode 100644 index 0000000000..de4a50faae --- /dev/null +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/TranslationTable.java @@ -0,0 +1,37 @@ +package org.jlab.detector.decode; + +import org.jlab.detector.base.DetectorType; +import org.jlab.utils.groups.IndexedTable; + +/** + * + * @author baltzell + */ +public class TranslationTable extends IndexedTable { + + public TranslationTable() { + super(3,new String[]{"sector/I","layer/I","component/I","order/I","type/I"}); + String[] x = new String[entryMap.keySet().size()]; + for (String name : entryMap.keySet()) + x[entryMap.get(name)] = name + "/" + entryTypes.get(name); + }; + + public void add(DetectorType dt, IndexedTable it) { + for (Object key : it.getList().getMap().keySet()) { + int crate = IndexedTable.DEFAULT_GENERATOR.getIndex((long) key, 0); + int slot = IndexedTable.DEFAULT_GENERATOR.getIndex((long) key, 1); + int channel = IndexedTable.DEFAULT_GENERATOR.getIndex((long) key, 2); + long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(crate, slot, channel); + // add row to the new table: + addEntry(crate, slot, channel); + // add all entries to the new row: + int column; + for (column=0; column benchmarks = new ArrayList<>(); + + public BenchmarkTimerTotal(String name) { + super(name); + } + + @Override + public String toString() { + double timePerCall = 0.0; + if (numberOfCalls != 0) timePerCall = getMiliseconds() / numberOfCalls * benchmarks.size(); + return String.format("TIMER (%-12s) : N Calls %12d, Total Time = %12.2f sec, Unit Time = %12.3f msec", + getName(), numberOfCalls, getSeconds(), timePerCall); + } + + public void add(BenchmarkTimer b) { + benchmarks.add(b); + totalTime += b.totalTime; + numberOfCalls += b.numberOfCalls; + } +} diff --git a/common-tools/clas-utils/src/main/java/org/jlab/utils/groups/IndexedTable.java b/common-tools/clas-utils/src/main/java/org/jlab/utils/groups/IndexedTable.java index e01daaef59..39ea81d197 100644 --- a/common-tools/clas-utils/src/main/java/org/jlab/utils/groups/IndexedTable.java +++ b/common-tools/clas-utils/src/main/java/org/jlab/utils/groups/IndexedTable.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; @@ -21,16 +22,15 @@ public class IndexedTable extends DefaultTableModel { public static final IndexGenerator DEFAULT_GENERATOR = new IndexGenerator(); + + protected Map entryMap = new LinkedHashMap<>(); + protected Map entryTypes = new LinkedHashMap<>(); private IndexedList entries = null; - private Map entryMap = new LinkedHashMap<>(); - private Map entryTypes = new LinkedHashMap<>(); private List entryNames = new ArrayList<>(); private List indexNames = new ArrayList<>(); private String precisionFormat = "%.6f"; - private Map> constrains = new HashMap<>(); - private int DEBUG_MODE = 0; public IndexedTable(int indexCount){ @@ -40,6 +40,18 @@ public IndexedTable(int indexCount){ } } + /** + * Clone the format of an existing IndexedTable. + * @param it + */ + public IndexedTable(IndexedTable it) { + entries = new IndexedList<>(it.indexNames.size()); + indexNames.addAll(it.indexNames); + entryMap = it.entryMap; + entryTypes = it.entryTypes; + entryNames = it.entryNames; + } + public IndexedTable(int indexCount,String format){ entries = new IndexedList<>(indexCount); for(int i = 0; i < indexCount; i++){ @@ -119,7 +131,11 @@ public void setIntValue(Integer value, String item, int... index){ } } } - + + public void setIntValueByHash(Integer value, int column, long hash) { + this.entries.getItemByHash(hash).setValue(column, value); + } + public void setDoubleValue(Double value, String item, int... index){ if(this.entries.hasItem(index)==false){ if(DEBUG_MODE>0) System.out.println( "[IndexedTable] ---> error.. entry does not exist"); @@ -134,35 +150,31 @@ public void setDoubleValue(Double value, String item, int... index){ } public int getIntValueByHash(int index, long hash) { - if (this.entries.hasItemByHash(hash)) - return this.entries.getItemByHash(hash).getValue(index).intValue(); - return 0; + return entries.getItemByHash(hash).getValue(index).intValue(); } - + public double getDoubleValueByHash(int index, long hash) { - if (this.entries.hasItemByHash(hash)) - return this.entries.getItemByHash(hash).getValue(index).doubleValue(); - return 0; + return entries.getItemByHash(hash).getValue(index).doubleValue(); } public int getIntValueByHash(String item, long hash) { - if (this.entries.hasItemByHash(hash)) { - if (this.entryMap.containsKey(item)) { - int index = this.entryMap.get(item); - return this.entries.getItemByHash(hash).getValue(index).intValue(); - } - } - return 0; + return entries.getItemByHash(hash).getValue(entryMap.get(item)).intValue(); } - + public double getDoubleValueByHash(String item, long hash) { - if (this.entries.hasItemByHash(hash)) { - if (this.entryMap.containsKey(item)) { - int index = this.entryMap.get(item); - return this.entries.getItemByHash(hash).getValue(index).doubleValue(); - } - } - return 0; + return entries.getItemByHash(hash).getValue(entryMap.get(item)).doubleValue(); + } + + public List getValuesByHash(long hash) { + return this.entries.getItemByHash(hash).entryValues; + } + + public List getIntegersByHash(long hash) { + return getValuesByHash(hash).stream().map(x -> x.intValue()).collect(Collectors.toList()); + } + + public List getDoublesByHash(long hash) { + return getValuesByHash(hash).stream().map(x -> x.doubleValue()).collect(Collectors.toList()); } public int getIntValue(String item, int... index){ @@ -201,6 +213,10 @@ public IndexedList getList(){ return this.entries; } + public Map getEntryMap(){ + return this.entryMap; + } + private void parseFormat(String format){ String[] tokens = format.split(":"); entryMap.clear(); @@ -250,7 +266,7 @@ public void show(){ @Override public String toString(){ StringBuilder str = new StringBuilder(); - str.append(String.format("IndexedList SIZE = %d\n", entryMap.size())); + str.append(String.format("IndexedList SIZE = %d/%d\n", entryMap.size(),entries.getMap().size())); for(Map.Entry entry : this.entryMap.entrySet()){ str.append(String.format("* %-24s * %3s * \n",entry.getKey(), this.entryTypes.get(entry.getKey()))); @@ -453,4 +469,31 @@ public boolean conflicts(IndexedTable it) { return !conflicts.isEmpty(); } + /** + * Make one big table. + * @param tables the tables to combine + * @return + */ + public static IndexedTable add(List tables) { + // create the new table: + IndexedTable ret = new IndexedTable(tables.get(0)); + for (IndexedTable table : tables) { + // loop over the input table rows: + for (Object key : table.getList().getMap().keySet()) { + // get the indexing for this row: + int crate = IndexedTable.DEFAULT_GENERATOR.getIndex((long)key, 0); + int slot = IndexedTable.DEFAULT_GENERATOR.getIndex((long)key, 1); + int channel = IndexedTable.DEFAULT_GENERATOR.getIndex((long)key, 2); + long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(crate, slot, channel); + // add row to the new table: + ret.addEntry(crate,slot,channel); + // set values for the new row: + for (int column : table.entryMap.values()) { + int value = table.getIntValueByHash(column, hash); + ret.setIntValueByHash(value, column, hash); + } + } + } + return ret; + } } diff --git a/common-tools/clas-utils/src/main/java/org/jlab/utils/options/OptionParser.java b/common-tools/clas-utils/src/main/java/org/jlab/utils/options/OptionParser.java index 5d8f04f81c..7a2df2cac4 100644 --- a/common-tools/clas-utils/src/main/java/org/jlab/utils/options/OptionParser.java +++ b/common-tools/clas-utils/src/main/java/org/jlab/utils/options/OptionParser.java @@ -179,6 +179,7 @@ else if(this.containsOptions(arguments,"-v","-version")==true){ } } if (this.requiresInputList && this.parsedInputList.isEmpty()) { + printUsage(); System.err.println(" \n*** ERROR *** Empty Input List."); System.exit(101); } diff --git a/etc/services/rgd-clarode.yml b/etc/services/rgd-clarode.yml index b3612af29d..0fef0e2016 100644 --- a/etc/services/rgd-clarode.yml +++ b/etc/services/rgd-clarode.yml @@ -19,8 +19,8 @@ configuration: outputBankPrefix: "HB" io-services: reader: - class: org.jlab.io.clara.DecoderReader - name: DecoderReader + class: org.jlab.io.clara.Clas12Reader + name: Clas12Reader writer: class: org.jlab.io.clara.DecoderWriter name: DecoderWriter diff --git a/validation/advanced-tests/run-eb-tests.sh b/validation/advanced-tests/run-eb-tests.sh index 43ca6e6ed4..9e64ae900b 100755 --- a/validation/advanced-tests/run-eb-tests.sh +++ b/validation/advanced-tests/run-eb-tests.sh @@ -118,7 +118,7 @@ then # install clara if ! [ -d clara_installation ] then - ../../install-clara clara_installation + ../../bin/install-clara clara_installation fi fi