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 index 3d32f87807..40b6977ee6 100644 --- 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 @@ -217,30 +217,36 @@ public Bank getDataBankADC(String name, DetectorType type){ List adcDGTZ = this.getEntriesADC(type); if (adcDGTZ.isEmpty()) return null; - Bank adcBANK = new Bank(schemaFactory.getSchema(name), adcDGTZ.size()); - + Bank adcBANK = new Bank(schemaFactory.getSchema(name), + adcDGTZ.stream().mapToInt(b -> b.getADCSize()).sum()); + + int row = 0; 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()); + for (int j = 0; j transTableNames = new HashMap<>(); private final HashMap fitterTableNames = new HashMap<>(); - private final ExtendedFADCFitter extendedFitter = new ExtendedFADCFitter(); + private final ExtendedFADCMultiFitter extendedFitter = new ExtendedFADCMultiFitter(); private final MVTFitter mvtFitter = new MVTFitter(); private HashMap fitterTables = new HashMap<>(); @@ -219,17 +220,33 @@ private void fitPulses(DetectorDataDgtz data, IndexedTable cfg) { ADCData adc = data.getADCData(i); if(adc.getPulseSize()>0){ try { - extendedFitter.fit(nsa, nsb, tet, ped, adc.getPulseArray()); + Peak[] hits = extendedFitter.fit(nsa, nsb, tet, ped, adc.getPulseArray()); + if (hits.length > 0) { + adc.setIntegral(hits[0].adc + hits[0].ped*(nsa+nsb)); + adc.setHeight((short) hits[0].pulsePeakValue); + adc.setTimeWord(hits[0].t0); + adc.setPedestal((short) hits[0].ped); + adc.setADC(nsa, nsb); + // extra hits get a new object here: + for (int j = 1; j < hits.length; j++) { + ADCData a = new ADCData(); + a.setTimeStamp(adc.getTimeStamp()); + a.setOrder(adc.getOrder()); + a.setIntegral(hits[j].adc + hits[j].ped*(nsa+nsb)); + a.setHeight((short) hits[j].pulsePeakValue); + a.setTimeWord(hits[j].t0); + a.setPedestal((short) hits[j].ped); + a.setADC(nsa, nsb); + data.addADC(a); + } + } + } catch (Exception e) { + e.printStackTrace(); System.err.println(">>>> error : fitting pulse "+dd.getCrate()+ " / "+dd.getSlot()+" / "+dd.getChannel()); } - adc.setIntegral(extendedFitter.adc + extendedFitter.ped*(nsa+nsb)); - adc.setHeight((short) this.extendedFitter.pulsePeakValue); - adc.setTimeWord(this.extendedFitter.t0); - adc.setPedestal((short) this.extendedFitter.ped); } - data.getADCData(i).setADC(nsa, nsb); } } diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/ExtendedFADCFitter.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/ExtendedFADCFitter.java index 245c5c84d8..7fea41bf18 100644 --- a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/ExtendedFADCFitter.java +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/ExtendedFADCFitter.java @@ -1,51 +1,22 @@ package org.jlab.detector.decode; +import org.jlab.detector.decode.FADCData.Peak; + /** * * @author gavalian */ -public class ExtendedFADCFitter implements IFADCFitter { - - private int pedistalMinBin = 1; - private int pedistalMaxBin = 5; - - int p1=1,p2=15; - int mmsum,summing_in_progress; +public class ExtendedFADCFitter extends Peak implements IFADCFitter { - public int t0,adc,ped,pedsum; - public int thresholdCrossing,pulsePeakValue, pulsePeakPosition, pulseWidth; - public double baseline, rms; - private int tcourse, tfine; + private final int pedistalMaxBin = 5; + private final int p1 = 1; + private final int p2 = 15; - public ExtendedFADCFitter(){ - - } + public ExtendedFADCFitter(){} - public void fitCourseTime(int nsa, int nsb, int tet, int pedr, short[] pulse) { - pedsum=0;adc=0;mmsum=0;summing_in_progress=0; - for (int mm=0; mmp1 && mm<=p2) pedsum+=pulse[mm]; - if(mm==p2) pedsum=pedsum/(p2-p1); - if(mm>p2) { - if (pedr==0) ped=pedsum; - if (pedr!=0) ped=pedr; - if ((summing_in_progress==0) && pulse[mm]>ped+tet) { - summing_in_progress=1; - tcourse = mm; - t0 = (tcourse << 6); - for (int ii=1; ii0 && mmsum>(nsa+nsb)) summing_in_progress=-1; - if(summing_in_progress>0) {adc+=(pulse[mm]-ped); mmsum++;} - } - } - } - - public void fit(int nsa, int nsb, int tet, int pedr, short[] pulse) { - t0=0; adc=0; ped=0; pedsum=0; baseline=0; rms=0; - thresholdCrossing=0; pulsePeakValue=0; pulsePeakPosition=0; pulseWidth=0; - tcourse=0; tfine=0; + @Override + public Peak[] fit(int nsa, int nsb, int tet, int pedr, short[] pulse) { + zero(); double noise = 0; int tstart = pedistalMaxBin+1; int tcross = 0; @@ -58,7 +29,7 @@ public void fit(int nsa, int nsb, int tet, int pedr, short[] pulse) { pedsum += pulse[bin]; } ped=pedsum/pulse.length; - return; + return new Peak[]{this}; } if (pedr==0) { tstart = p2+1; @@ -109,57 +80,16 @@ public void fit(int nsa, int nsb, int tet, int pedr, short[] pulse) { int a0 = pulse[s0]; int a1 = pulse[s0+1]; // set course time to be the sample before the 50% crossing - tcourse = s0; + tcoarse = s0; // set the fine time from interpolation between the two samples before and after the 50% crossing (6 bits resolution) tfine = ((int) ((halfMax - a0)/(a1-a0) * 64)); - t0 = (tcourse << 6) + tfine; + t0 = (tcoarse << 6) + tfine; } if(s1>-1 && s0>-1) { - int a0 = pulse[s1]; - int a1 = pulse[s1+1]; pulseWidth = s1 - s0; } } - } - - private double findPedestal(DetectorDataDgtz.ADCData data){ - int ped = 0; - for(int i = this.pedistalMinBin; i < this.pedistalMaxBin; i++){ - ped += data.getPulseValue(i); - } - double ped_norm = ((double) ped) / (Math.abs(this.pedistalMaxBin-this.pedistalMinBin)); - return ped_norm; - } - - private int findMaximumADC(DetectorDataDgtz.ADCData data){ - int bin = this.pedistalMaxBin ; - int max = data.getPulseValue(pedistalMaxBin); - - for(int i = this.pedistalMaxBin; i < data.getPulseSize(); i++){ - if(data.getPulseValue(i)>max){ - max = data.getPulseValue(i); - bin = i; - } - } - return bin; - } - - private int findPulseIntegral(DetectorDataDgtz.ADCData data, int maxValueBin, int nsa, int nsb){ - int min_bin = maxValueBin - nsb; - int max_bin = maxValueBin + nsa; - if(min_bin>=0&&max_binthis.pedistalMaxBin){ - double ped = this.findPedestal(data); - } + return new Peak[]{this}; } } diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/ExtendedFADCMultiFitter.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/ExtendedFADCMultiFitter.java new file mode 100644 index 0000000000..2a4d9dd8ec --- /dev/null +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/ExtendedFADCMultiFitter.java @@ -0,0 +1,101 @@ +package org.jlab.detector.decode; + +import java.util.ArrayList; +import java.util.List; +import org.jlab.detector.decode.FADCData.Peak; + +public class ExtendedFADCMultiFitter extends Peak implements IFADCFitter { + + static final int PED_BIN1=1; + static final int PED_BIN2=15; + + public ExtendedFADCMultiFitter(){} + + @Override + public Peak[] fit(int nsa, int nsb, int tet, int pedr, short[] pulse) { + + zero(); + + if (pulse.length < PED_BIN2+1) return new Peak[]{this}; + + if (pedr != 0) { + // user-defined pedestal: + baseline = ped = pedr; + } + else { + // calculate pedestal: + for (int bin = PED_BIN1+1; bin < PED_BIN2+1; bin++) { + pedsum += pulse[bin]; + rms += pulse[bin] * pulse[bin]; + } + baseline = ((double) pedsum) / (PED_BIN2 - PED_BIN1); + ped = (int)baseline; + rms = Math.sqrt(rms / (PED_BIN2 - PED_BIN1) - baseline * baseline); + } + + boolean belowThreshold = false; + List hits = new ArrayList<>(3); + + for (int crossBin = PED_BIN2+1; crossBin halfMax) { + + // found half-height, now define timing: + int a0 = pulse[halfBinUp]; + int a1 = pulse[halfBinUp+1]; + f.tcoarse = halfBinUp; + f.tfine = ((int) ((halfMax - a0)/(a1-a0) * 64)); + f.t0 = (f.tcoarse << 6) + f.tfine; + + // find the half-max crossing downwards: + for (int halfBinDown = halfBinUp; halfBinDown halfMax && pulse[halfBinDown+1] <= halfMax) { + f.pulseWidth = halfBinDown - halfBinUp; + break; + } + } + break; + } + } + + // add the new hit we just made: + + // move search bin forward: + crossBin = f.pulsePeakPosition + 1; + break; + } + } + } + } + return hits.stream().toArray(Peak[]::new); + } + +} diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/FADCData.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/FADCData.java index fb3670a76a..cb17705f0a 100644 --- a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/FADCData.java +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/FADCData.java @@ -12,6 +12,16 @@ */ public class FADCData { + public static class Peak { + public int t0, adc, ped, pedsum, tcoarse, tfine; + public int thresholdCrossing, pulsePeakValue, pulsePeakPosition, pulseWidth; + public double baseline, rms; + public void zero() { + pedsum=0; t0=0; adc=0; ped=0; baseline=0; rms=0; tcoarse=0; tfine=0; + thresholdCrossing=0; pulsePeakValue=0; pulsePeakPosition=0; pulseWidth=0; + } + } + private final DetectorDescriptor desc = new DetectorDescriptor(); private short[] adcBuffer = null; @@ -130,103 +140,6 @@ public List getDecoded(){ return result; } - public void decode(){ - - List result = new ArrayList<>(); - - short[] bucket = new short[16]; - - //int channel; - //int minimum; - int nwords; - int nskip; - int position = 0; - int headerWord; - int pedestal ; - int compressedWord; - byte[] array = new byte[4]; - - System.out.println(desc.toString()); - System.out.println("------------------------------------------------------"); - for(int i = 0 ; i < adcBuffer.length/2; i++){ - System.out.print(String.format("%04X ", adcBuffer[i*2+1])); - System.out.print(String.format("%04X ", adcBuffer[i*2])); - if((i+1)%8==0) System.out.println(); - } - System.out.println(); - System.out.println("------------------------------------------------------"); - ByteBuffer buffer = ByteBuffer.wrap(array); - buffer.order(ByteOrder.LITTLE_ENDIAN); - - while(position> 23) & 0x0F; - nwords = (headerWord&0x07); - nskip = (dataH>>4) &0x0F; - pedestal = (headerWord>>8)&0x0FFF; - compressedWord = (headerWord>>27)&0x0F; - - position+=2; - if(compressedWord==5){ - - short value; - result.clear(); - for(int i = 0; i < 4; i++){ - value = (short) (adcBuffer[position+i]&0x000F); - - bucket[i*4] = (short) (value+pedestal); - result.add( (short) (value+pedestal)); - - value = (short) ((adcBuffer[position+i] >> 4)&0x000F); - - bucket[i*4+1] = (short) (value+pedestal); - result.add( (short) (value+pedestal)); - - value = (short) ((adcBuffer[position+i] >> 8)&0x000F); - bucket[i*4+2] = (short) (value+pedestal); - result.add( (short) (value+pedestal)); - - value = (short) ((adcBuffer[position+i] >> 12)&0x000F); - bucket[i*4+3] = (short) (value+pedestal); - result.add( (short) (value+pedestal)); - } - - position += 4; - - if(nwords>0){ - - for(int i = 0; i < nwords*2; i++){ - short first = (short) ( (adcBuffer[i+position]&0x00FF) << 4); - short second = (short) ( ((adcBuffer[i+position] >>8)&0x00FF)<<4); - if( (nskip+i*2)<=15) - bucket[nskip+i*2] += first; - if( (nskip+i*2+1)<=15) - bucket[nskip+i*2+1] += second; - } - - position += nwords*2; - } - result.clear(); - for(int k = 0 ; k < bucket.length; k++){ - result.add(bucket[k]); - } - for(int i = 0; i < result.size(); i++){ - System.out.print(String.format("%6d", result.get(i))); - } - System.out.println(); - } - } - } - public void show(){ int length = 0; if(adcBuffer!=null) length = adcBuffer.length; diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/IFADCFitter.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/IFADCFitter.java index 851ba29575..1bbe5a69f7 100644 --- a/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/IFADCFitter.java +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/decode/IFADCFitter.java @@ -1,11 +1,9 @@ package org.jlab.detector.decode; -import org.jlab.detector.decode.DetectorDataDgtz.ADCData; +import org.jlab.detector.decode.FADCData.Peak; -/** - * - * @author gavalian - */ public interface IFADCFitter { - void fit(ADCData data); + + public Peak[] fit(int nsa, int nsb, int tet, int pedr, short[] pulse); + }