14 #include "cetlib_except/exception.h" 25 unsigned int zerothreshold = 5;
30 unsigned int zerothreshold = 5;
44 unsigned int zerothreshold = 5;
49 unsigned int zerothreshold = 5;
60 unsigned int &zerothreshold)
75 unsigned int &zerothreshold,
91 void Compress(
const boost::circular_buffer<std::vector<short>> &adcvec_neighbors,
92 std::vector<short> &adc,
94 unsigned int &zerothreshold,
112 unsigned int &zerothreshold,
114 int &nearestneighbor,
115 bool fADCStickyCodeFeature)
120 ZeroSuppression(adc,zerothreshold, pedestal, nearestneighbor, fADCStickyCodeFeature);
122 ZeroSuppression(adc,zerothreshold, pedestal, nearestneighbor, fADCStickyCodeFeature);
130 void Compress(
const boost::circular_buffer<std::vector<short>> &adcvec_neighbors,
131 std::vector<short> &adc,
133 unsigned int &zerothreshold,
135 int &nearestneighbor,
136 bool fADCStickyCodeFeature)
141 ZeroSuppression(adcvec_neighbors,adc,zerothreshold, pedestal, nearestneighbor, fADCStickyCodeFeature);
143 ZeroSuppression(adcvec_neighbors,adc,zerothreshold, pedestal, nearestneighbor, fADCStickyCodeFeature);
154 unsigned int &zerothreshold)
156 const int adcsize = adc.size();
157 const int zerothresholdsigned = zerothreshold;
159 std::vector<short> zerosuppressed(adc.size());
160 int maxblocks = adcsize/2 + 1;
161 std::vector<short> blockbegin(maxblocks);
162 std::vector<short> blocksize(maxblocks);
164 unsigned int nblocks = 0;
165 unsigned int zerosuppressedsize = 0;
169 for(
int i = 0; i < adcsize; ++i){
170 int adc_current_value = std::abs(adc[i]);
172 if(adc_current_value > zerothresholdsigned){
176 blockbegin[nblocks] = i;
177 blocksize[nblocks] = 0;
181 zerosuppressed[zerosuppressedsize] = adc[i];
182 zerosuppressedsize++;
183 blocksize[nblocks]++;
185 if(i == adcsize-1) nblocks++;
188 if(adc_current_value <= zerothresholdsigned && blockcheck == 1){
189 zerosuppressed[zerosuppressedsize] = adc[i];
190 zerosuppressedsize++;
191 blocksize[nblocks]++;
199 adc.resize(2+nblocks+nblocks+zerosuppressedsize);
206 for(
unsigned int i = 0; i < nblocks; ++i)
207 adc[i+2] = blockbegin[i];
209 for(
unsigned int i = 0; i < nblocks; ++i)
210 adc[i+nblocks+2] = blocksize[i];
212 for(
unsigned int i = 0; i < zerosuppressedsize; ++i)
213 adc[i+nblocks+nblocks+2] = zerosuppressed[i];
224 unsigned int &zerothreshold,
225 int &nearestneighbor)
228 const int adcsize = adc.size();
229 const int zerothresholdsigned = zerothreshold;
231 std::vector<short> zerosuppressed(adcsize);
232 int maxblocks = adcsize/2 + 1;
233 std::vector<short> blockbegin(maxblocks);
234 std::vector<short> blocksize(maxblocks);
237 int zerosuppressedsize = 0;
239 int blockstartcheck = 0;
240 int endofblockcheck = 0;
242 for(
int i = 0; i < adcsize; ++i){
243 int adc_current_value = std::abs(adc[i]);
245 if(blockstartcheck==0){
246 if(adc_current_value>zerothresholdsigned){
248 if((i-nearestneighbor)<=(blockbegin[nblocks-1]+blocksize[nblocks-1]+1)){
251 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
255 blockbegin[nblocks] = (i - nearestneighbor > 0) ? i - nearestneighbor : 0;
256 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
261 blockbegin[nblocks] = (i - nearestneighbor > 0) ? i - nearestneighbor : 0;
262 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
267 else if(blockstartcheck==1){
268 if(adc_current_value>zerothresholdsigned){
269 blocksize[nblocks]++;
273 if(endofblockcheck<nearestneighbor){
275 blocksize[nblocks]++;
278 else if(i+2<adcsize){
279 if(std::abs(adc[i+1]) <= zerothresholdsigned && std::abs(adc[i+2]) <= zerothresholdsigned){
291 if(blockstartcheck==1){
295 for(
int i = 0; i < nblocks; ++i)
296 zerosuppressedsize += blocksize[i];
299 adc.resize(2+nblocks+nblocks+zerosuppressedsize);
300 zerosuppressed.resize(2+nblocks+nblocks+zerosuppressedsize);
303 int zerosuppressedcount = 0;
304 for(
int i = 0; i < nblocks; ++i){
306 for(
int j = 0; j < blocksize[i]; ++j){
307 zerosuppressed[zerosuppressedcount] = adc[blockbegin[i] + j];
308 zerosuppressedcount++;
314 for(
int i = 0; i < nblocks; ++i){
315 adc[i+2] = blockbegin[i];
316 adc[i+nblocks+2] = blocksize[i];
321 for(
int i = 0; i < zerosuppressedsize; ++i)
322 adc[i+nblocks+nblocks+2] = zerosuppressed[i];
335 unsigned int &zerothreshold,
337 int &nearestneighbor,
338 bool fADCStickyCodeFeature)
341 const int adcsize = adc.size();
342 const int zerothresholdsigned = zerothreshold;
344 std::vector<short> zerosuppressed(adcsize);
345 int maxblocks = adcsize/2 + 1;
346 std::vector<short> blockbegin(maxblocks);
347 std::vector<short> blocksize(maxblocks);
350 int zerosuppressedsize = 0;
352 int blockstartcheck = 0;
353 int endofblockcheck = 0;
355 for(
int i = 0; i < adcsize; ++i){
358 if(blockstartcheck==0){
359 if(adc_current_value>zerothresholdsigned){
361 if(i-nearestneighbor<=blockbegin[nblocks-1]+blocksize[nblocks-1]+1){
363 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
367 blockbegin[nblocks] = (i - nearestneighbor > 0) ? i - nearestneighbor : 0;
368 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
373 blockbegin[nblocks] = (i - nearestneighbor > 0) ? i - nearestneighbor : 0;
374 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
379 else if(blockstartcheck==1){
380 if(adc_current_value>zerothresholdsigned){
381 blocksize[nblocks]++;
385 if(endofblockcheck<nearestneighbor){
387 blocksize[nblocks]++;
390 else if(i+2<adcsize){
401 if(blockstartcheck==1){
406 for(
int i = 0; i < nblocks; ++i)
407 zerosuppressedsize += blocksize[i];
410 adc.resize(2+nblocks+nblocks+zerosuppressedsize);
411 zerosuppressed.resize(2+nblocks+nblocks+zerosuppressedsize);
414 int zerosuppressedcount = 0;
415 for(
int i = 0; i < nblocks; ++i){
417 for(
int j = 0; j < blocksize[i]; ++j){
418 zerosuppressed[zerosuppressedcount] = adc[blockbegin[i] + j];
419 zerosuppressedcount++;
425 for(
int i = 0; i < nblocks; ++i){
426 adc[i+2] = blockbegin[i];
427 adc[i+nblocks+2] = blocksize[i];
432 for(
int i = 0; i < zerosuppressedsize; ++i)
433 adc[i+nblocks+nblocks+2] = zerosuppressed[i];
445 void ZeroSuppression(
const boost::circular_buffer<std::vector<short>> &adcvec_neighbors,
446 std::vector<short> &adc,
447 unsigned int &zerothreshold,
448 int &nearestneighbor)
451 const int adcsize = adc.size();
452 const int zerothresholdsigned = zerothreshold;
454 std::vector<short> zerosuppressed(adcsize);
455 const int maxblocks = adcsize/2 + 1;
456 std::vector<short> blockbegin(maxblocks);
457 std::vector<short> blocksize(maxblocks);
460 int zerosuppressedsize = 0;
462 int blockstartcheck = 0;
463 int endofblockcheck = 0;
465 for(
int i = 0; i < adcsize; ++i){
469 int adc_current_value = 0;
471 for(boost::circular_buffer<std::vector<short>>::
const_iterator adcveciter = adcvec_neighbors.begin(); adcveciter!=adcvec_neighbors.end();++adcveciter){
472 const std::vector<short> &adcvec_current = *adcveciter;
473 const int adcvec_current_single = std::abs(adcvec_current[i]);
475 if(adc_current_value < adcvec_current_single){
476 adc_current_value = adcvec_current_single;
480 if(blockstartcheck==0){
481 if(adc_current_value>zerothresholdsigned){
483 if(i-nearestneighbor<=blockbegin[nblocks-1]+blocksize[nblocks-1]+1){
485 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
489 blockbegin[nblocks] = (i - nearestneighbor > 0) ? i - nearestneighbor : 0;
490 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
495 blockbegin[nblocks] = (i - nearestneighbor > 0) ? i - nearestneighbor : 0;
496 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
501 else if(blockstartcheck==1){
502 if(adc_current_value>zerothresholdsigned){
503 blocksize[nblocks]++;
507 if(endofblockcheck<nearestneighbor){
509 blocksize[nblocks]++;
512 else if(i+2<adcsize){
513 if(std::abs(adc[i+1]) <= zerothresholdsigned && std::abs(adc[i+2]) <= zerothresholdsigned){
524 if(blockstartcheck==1){
530 for(
int i = 0; i < nblocks; ++i)
531 zerosuppressedsize += blocksize[i];
534 adc.resize(2+nblocks+nblocks+zerosuppressedsize);
535 zerosuppressed.resize(2+nblocks+nblocks+zerosuppressedsize);
538 int zerosuppressedcount = 0;
539 for(
int i = 0; i < nblocks; ++i){
541 for(
int j = 0; j < blocksize[i]; ++j){
542 zerosuppressed[zerosuppressedcount] = adc[blockbegin[i] + j];
543 zerosuppressedcount++;
549 for(
int i = 0; i < nblocks; ++i){
550 adc[i+2] = blockbegin[i];
551 adc[i+nblocks+2] = blocksize[i];
556 for(
int i = 0; i < zerosuppressedsize; ++i)
557 adc[i+nblocks+nblocks+2] = zerosuppressed[i];
570 void ZeroSuppression(
const boost::circular_buffer<std::vector<short>> &adcvec_neighbors,
571 std::vector<short> &adc,
572 unsigned int &zerothreshold,
574 int &nearestneighbor,
575 bool fADCStickyCodeFeature)
578 const int adcsize = adc.size();
579 const int zerothresholdsigned = zerothreshold;
581 std::vector<short> zerosuppressed(adcsize);
582 const int maxblocks = adcsize/2 + 1;
583 std::vector<short> blockbegin(maxblocks);
584 std::vector<short> blocksize(maxblocks);
587 int zerosuppressedsize = 0;
589 int blockstartcheck = 0;
590 int endofblockcheck = 0;
592 for(
int i = 0; i < adcsize; ++i){
598 for(boost::circular_buffer<std::vector<short>>::
const_iterator adcveciter = adcvec_neighbors.begin(); adcveciter!=adcvec_neighbors.end();++adcveciter){
599 const std::vector<short> &adcvec_current = *adcveciter;
600 const int adcvec_current_single = std::abs(adcvec_current[i] - pedestal);
602 if(adc_current_value < adcvec_current_single){
603 adc_current_value = adcvec_current_single;
607 if(blockstartcheck==0){
608 if(adc_current_value>zerothresholdsigned){
610 if(i-nearestneighbor<=blockbegin[nblocks-1]+blocksize[nblocks-1]+1){
612 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
616 blockbegin[nblocks] = (i - nearestneighbor > 0) ? i - nearestneighbor : 0;
617 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
622 blockbegin[nblocks] = (i - nearestneighbor > 0) ? i - nearestneighbor : 0;
623 blocksize[nblocks] = i - blockbegin[nblocks] + 1;
628 else if(blockstartcheck==1){
629 if(adc_current_value>zerothresholdsigned){
630 blocksize[nblocks]++;
634 if(endofblockcheck<nearestneighbor){
636 blocksize[nblocks]++;
640 else if(i+2<adcsize){
652 if(blockstartcheck==1){
658 for(
int i = 0; i < nblocks; ++i)
659 zerosuppressedsize += blocksize[i];
662 adc.resize(2+nblocks+nblocks+zerosuppressedsize);
663 zerosuppressed.resize(2+nblocks+nblocks+zerosuppressedsize);
666 int zerosuppressedcount = 0;
667 for(
int i = 0; i < nblocks; ++i){
669 for(
int j = 0; j < blocksize[i]; ++j){
670 zerosuppressed[zerosuppressedcount] = adc[blockbegin[i] + j];
671 zerosuppressedcount++;
677 for(
int i = 0; i < nblocks; ++i){
678 adc[i+2] = blockbegin[i];
679 adc[i+nblocks+2] = blocksize[i];
684 for(
int i = 0; i < zerosuppressedsize; ++i)
685 adc[i+nblocks+nblocks+2] = zerosuppressed[i];
697 std::vector<short> &uncompressed)
699 const int lengthofadc = adc[0];
700 const int nblocks = adc[1];
702 uncompressed.resize(lengthofadc);
703 for (
int i = 0;i < lengthofadc; ++i){
707 int zerosuppressedindex = nblocks*2 + 2;
709 for(
int i = 0; i < nblocks; ++i){
711 for(
int j = 0; j < adc[2+nblocks+i]; ++j){
714 uncompressed[adc[2+i]+j] = adc[zerosuppressedindex];
715 zerosuppressedindex++;
726 std::vector<short> &uncompressed,
729 const int lengthofadc = adc[0];
730 const int nblocks = adc[1];
732 uncompressed.resize(lengthofadc);
733 for (
int i = 0;i < lengthofadc; ++i){
734 uncompressed[i] = pedestal;
737 int zerosuppressedindex = nblocks*2 + 2;
739 for(
int i = 0; i < nblocks; ++i){
741 for(
int j = 0; j < adc[2+nblocks+i]; ++j){
744 uncompressed[adc[2+i]+j] = adc[zerosuppressedindex];
745 zerosuppressedindex++;
757 std::vector<short> &uncompressed,
765 std::vector<short>
tmp(2*adc[0]);
770 for(
unsigned int i = 0; i < adc.size(); ++i) uncompressed[i] = adc[i];
774 <<
"raw::Uncompress() does not support compression #" 783 std::vector<short> &uncompressed,
792 std::vector<short>
tmp(2*adc[0]);
797 for(
unsigned int i = 0; i < adc.size(); ++i) uncompressed[i] = adc[i];
801 <<
"raw::Uncompress() does not support compression #" 825 std::vector<short>
const orig_adc(std::move(adc));
829 std::vector<short> diffs;
830 diffs.reserve(orig_adc.size());
831 std::adjacent_difference
832 (orig_adc.begin(), orig_adc.end(), std::back_inserter(diffs));
837 adc.reserve(orig_adc.size());
839 adc.push_back(orig_adc.front());
840 unsigned int curb = 15U;
842 std::bitset<16> bset;
845 for(
size_t i = 1U; i < diffs.size(); ++i){
850 if(i < diffs.size() - 3){
852 if(diffs[i+1] == 0 && diffs[i+2] == 0 && diffs[i+3] == 0){
860 adc.push_back(bset.to_ulong());
879 adc.push_back(bset.to_ulong());
897 adc.push_back(bset.to_ulong());
914 adc.push_back(bset.to_ulong());
929 adc.push_back(bset.to_ulong());
944 adc.push_back(bset.to_ulong());
959 adc.push_back(bset.to_ulong());
974 adc.push_back(bset.to_ulong());
989 adc.push_back(bset.to_ulong());
1003 adc.push_back(bset.to_ulong());
1011 if(orig_adc[i] > 0) adc.push_back(orig_adc[i]);
1013 std::bitset<16> tbit(-orig_adc[i]);
1015 adc.push_back(tbit.to_ulong());
1023 adc.push_back(bset.to_ulong());
1034 std::vector<short> &uncompressed)
1038 uncompressed[0] = adc[0];
1040 unsigned int curu = 1;
1041 short curADC = uncompressed[0];
1045 for(
unsigned int i = 1; i < adc.size() && curu < uncompressed.size(); ++i){
1047 std::bitset<16> bset(adc[i]);
1052 if( !bset.test(15) ){
1055 bset.set(14,
false);
1056 curADC = -1*bset.to_ulong();
1058 uncompressed[curu] = curADC;
1068 while( !bset.test(lowestb) && lowestb < 15) ++lowestb;
1073 << bset.to_string< char,std::char_traits<char>,std::allocator<char> >();
1077 while( b >= lowestb){
1081 while( !bset.test(b-zerocnt) && b-zerocnt > lowestb) ++zerocnt;
1086 for(
int s = 0;
s < 4; ++
s){
1087 uncompressed[curu] = curADC;
1090 if(curu > uncompressed.size()-1)
break;
1094 else if(zerocnt == 1){
1095 uncompressed[curu] = curADC;
1100 else if(zerocnt == 2){
1102 uncompressed[curu] = curADC;
1107 else if(zerocnt == 3){
1109 uncompressed[curu] = curADC;
1114 else if(zerocnt == 4){
1116 uncompressed[curu] = curADC;
1121 else if(zerocnt == 5){
1123 uncompressed[curu] = curADC;
1128 else if(zerocnt == 6){
1130 uncompressed[curu] = curADC;
1135 else if(zerocnt == 7){
1137 uncompressed[curu] = curADC;
1143 if(curu > uncompressed.size() - 1)
break;
1147 if(curu > uncompressed.size() - 1)
break;
1161 bool fADCStickyCodeFeature){
1163 int adc_return_value = std::abs(adc_value - pedestal);
1165 if(!fADCStickyCodeFeature){
1166 return adc_return_value;
1169 unsigned int sixlsbs = adc_value &
onemask;
1171 if((sixlsbs==onemask || sixlsbs==0) && std::abs(adc_value - pedestal) < 64){
1172 adc_return_value = 0;
1174 return adc_return_value;
void CompressHuffman(std::vector< short > &adc)
enum raw::_compress Compress_t
void UncompressHuffman(const std::vector< short > &adc, std::vector< short > &uncompressed)
const unsigned int onemask
Zero Suppression followed by Huffman Encoding.
Zero Suppression algorithm.
int ADCStickyCodeCheck(const short adc_value, const int pedestal, bool fADCStickyCodeFeature)
Collect all the RawData header files together.
void ZeroUnsuppression(const std::vector< short > &adc, std::vector< short > &uncompressed)
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
void Compress(std::vector< short > &adc, raw::Compress_t compress)
Compresses a raw data buffer.
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
void ZeroSuppression(std::vector< short > &adc, unsigned int &zerothreshold)
cet::coded_exception< error, detail::translate > exception