LarsoftHuffmanCompressService_service.cc
Go to the documentation of this file.
1 // LarsoftHuffmanCompressService.cxx
2 
8 
9 using std::string;
10 using std::ostream;
11 using std::cout;
12 using std::endl;
13 
14 namespace {
15 
16 // Display signal array.
17 // logLevel >= 3: Show # entries
18 // 4: Show at least 50 entries
19 // alllevel: Show all entries
20 // All elements if logLevel >= alllevel
21 template<class C>
22 void logsigs(const C& sigs, int logLevel, int alllevel, string myname) {
23  if ( logLevel > 2 ) cout << myname << " size: " << sigs.size() << endl;
24  if ( logLevel > 2 ) {
25  for ( unsigned int isig=0; isig<sigs.size(); ++isig ) {
26  if ( logLevel<alllevel && isig > 50 ) {
27  cout << myname << " ..." << endl;
28  break;
29  }
30  cout << myname << " sigs[" << isig << "]: " << sigs[isig] << endl;
31  }
32  }
33 }
34 
35 } // end unnamed namespace
36 
37 //**********************************************************************
38 
40 LarsoftHuffmanCompressService(bool useBlock, bool useHuffman, int logLevel)
41 : m_UseBlock(useBlock), m_UseHuffman(useHuffman), m_LogLevel(logLevel) { }
42 
43 //**********************************************************************
44 
47 : m_LogLevel(1) {
48  const string myname = "LarsoftHuffmanCompressService::ctor: ";
49  m_UseBlock = pset.get<bool>("UseBlock");
50  m_UseHuffman = pset.get<bool>("UseHuffman");
51  pset.get_if_present<int>("LogLevel", m_LogLevel);
52  if ( m_LogLevel > 0 ) {
53  cout << myname << " UseBlock: " << m_UseBlock << endl;
54  cout << myname << " UseHuffman: " << m_UseHuffman << endl;
55  cout << myname << " LogLevel: " << m_LogLevel << endl;
56  }
57 }
58 
59 //**********************************************************************
60 
62 compress(AdcCountVector& sigs, const AdcFilterVector& keep, AdcCount offset,
63  raw::Compress_t& comp) const {
64  const string myname = "LarsoftHuffmanCompressService::compress: ";
65  if ( keep.size() != sigs.size() ) {
66  cout << "ERROR: Filter and ADC have different sizes: " << keep.size()
67  << " != " << sigs.size() << endl;
68  return 1;
69  }
70  AdcCountVector newsigs;
71  comp = raw::kNone;
72  logsigs(sigs, m_LogLevel, 6, myname + "Before compression");
73  logsigs(keep, m_LogLevel, 6, myname + "Filter");
74  if ( m_LogLevel > 1 ) {
75  int nkeep = 0;
76  for ( bool val : keep ) if ( val ) ++nkeep;
77  cout << myname << "Keeping " << nkeep << "/" << keep.size() << " ticks" << endl;
78  }
79  if ( m_UseBlock ) {
80  block(sigs, keep, newsigs);
81  sigs = newsigs;
82  comp = raw::kZeroSuppression;
83  } else {
85  repsvc.compress(sigs, keep, offset, comp);
86  }
87  if ( m_UseHuffman ) {
89  if ( m_UseBlock ) comp = raw::kZeroHuffman;
90  else comp = raw::kHuffman;
91  }
92  logsigs(sigs, m_LogLevel, 5, myname + "After compression");
93  return 0;
94 }
95 
96 //**********************************************************************
97 
98 ostream& LarsoftHuffmanCompressService::print(ostream& out, string prefix) const {
99  out << prefix << "LarsoftHuffmanCompressService";
100  return out;
101 }
102 
103 //**********************************************************************
104 
105 // Copied from larsoft/lardata/RawData/raw.cxx
106 // void ZeroSuppression(std::vector<short> &adc, unsigned int &zerothreshold)
107 // and modified to suppress using keep instead of appying a threshold.
108 // Also, the old algorithm kept an extra tck at the end of each block.
109 // This is not done here.
110 
112 block(const AdcCountVector& sigsin, const AdcFilterVector& keep, AdcCountVector& sigsout) const {
113  const unsigned int adcsize = sigsin.size();
114  std::vector<short> zerosuppressed(sigsin.size());
115  unsigned int maxblocks = adcsize/2 + 1;
116  std::vector<short> blockbegin(maxblocks);
117  std::vector<short> blocksize(maxblocks);
118  unsigned int nblocks = 0;
119  unsigned int zerosuppressedsize = 0;
120  bool inblock = false;
121  for ( unsigned int i=0; i<adcsize; ++i ) {
122  if ( keep[i] ) {
123  if ( ! inblock ) {
124  blockbegin[nblocks] = i;
125  blocksize[nblocks] = 0;
126  inblock = true;
127  }
128  zerosuppressed[zerosuppressedsize] = sigsin[i];
129  ++zerosuppressedsize;
130  ++blocksize[nblocks];
131  if ( i == adcsize-1 ) ++nblocks;
132  } else if ( inblock ) {
133  //zerosuppressed[zerosuppressedsize] = sigsin[i];
134  //++zerosuppressedsize;
135  //++blocksize[nblocks];
136  ++nblocks;
137  inblock = false;
138  }
139  }
140  sigsout.resize(2+nblocks+nblocks+zerosuppressedsize);
141  sigsout[0] = adcsize; //fill first entry in adc with length of uncompressed vector
142  sigsout[1] = nblocks;
143  for ( unsigned int i=0; i<nblocks; ++i ) sigsout[i+2] = blockbegin[i];
144  for ( unsigned int i=0; i<nblocks; ++i ) sigsout[i+nblocks+2] = blocksize[i];
145  for ( unsigned int i=0; i<zerosuppressedsize; ++i ) sigsout[i+nblocks+nblocks+2] = zerosuppressed[i];
146 }
147 
148 //**********************************************************************
149 
151 
152 //**********************************************************************
void CompressHuffman(std::vector< short > &adc)
Definition: raw.cxx:849
Huffman Encoding.
Definition: RawTypes.h:10
LarsoftHuffmanCompressService(bool useBlock, bool useHuffman, int logLevel)
std::vector< AdcCount > AdcCountVector
Definition: AdcTypes.h:19
enum raw::_compress Compress_t
std::string string
Definition: nybbler.cc:12
void block(const AdcCountVector &oldsigs, const AdcFilterVector &keep, AdcCountVector &newsigs) const
Zero Suppression followed by Huffman Encoding.
Definition: RawTypes.h:12
no compression
Definition: RawTypes.h:9
Zero Suppression algorithm.
Definition: RawTypes.h:11
int compress(AdcCountVector &sigs, const AdcFilterVector &keep, AdcCount offset, raw::Compress_t &comp) const
T get(std::string const &key) const
Definition: ParameterSet.h:271
std::ostream & print(std::ostream &out=std::cout, std::string prefix=" ") const
std::vector< bool > AdcFilterVector
Definition: AdcTypes.h:27
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
short AdcCount
Definition: AdcTypes.h:18
int compress(AdcCountVector &sigs, const AdcFilterVector &keep, AdcCount offset, raw::Compress_t &comp) const
QTextStream & endl(QTextStream &s)
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)