HuffDataCompressor.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 /////////////////////////////////////////////////////////////////////////////
3 //
4 // Class for compressing ADC data internally represented
5 // as 16 bit unsigned int
6 // Max ADC resolution, however, cannot be greater than 15bits as
7 // 1bit is reserved to signal whether the packet is compressed or not
8 //
9 // The Huffman encoding scheme used here is the one developed by uBooNE
10 //
11 // The basic idea is to handle binnary codes as strings --> maybe not
12 // the most performant, but hopefully easier to understand ;-)
13 //
14 /////////////////////////////////////////////////////////////////////////////
15 /////////////////////////////////////////////////////////////////////////////
16 
17 #ifndef __HUFFDATACOMPRESSOR_H__
18 #define __HUFFDATACOMPRESSOR_H__
19 
20 #include <map>
21 #include <vector>
22 #include <deque>
23 #include <bitset>
24 #include <fstream>
25 #include <utility>
26 
27 #include "dlardaq.h"
28 
29 namespace dlardaq
30 {
31  //
32  // compress/decompress raw ADC data using Huffman encoding
33  //
35  {
36  public:
38  {
39  static HuffDataCompressor inst;
40  return inst;
41  }
42 
43  // compress single sequence
44  // need to provide number of bits of the ADC
45  // and the raw data vector
46  // outputs compressed stream packed into 8 bit vector
47  //
48  void CompressChData( short nbadc, std::vector<adc16_t> &raw_in,
49  std::vector<BYTE> &bin_out );
50 
51  // the raw adc should just be adc values without event header
52  // these data are in 1D array: [tdc + ch x ntdc]
53  void CompressEventData( short nbadc, size_t nch, size_t seqlen,
54  std::vector<adc16_t> &raw_in,
55  std::vector<BYTE> &bin_out );
56 
57  // the raw adc should just be adc values without event header
58  // these data are in 2D array: [ch, tdc]
59  void CompressEventData( short nbadc, size_t nch, size_t seqlen,
60  std::vector< std::vector<adc16_t> > &raw_in,
61  std::vector<BYTE> &bin_out );
62 
63 
64  // decompress event from binary sequence in memory
65  //
66  void DecompressEventData( short nbadc,
67  size_t nch,
68  size_t seqlen,
69  const char *buf, size_t bufsize, size_t &byteidx,
70  std::vector<adc16_t> &adc );
71 
72  // NOTE: Before calling this function
73  // one must ensure that the position of the input file
74  // is always set at the begining of the event data
75  // This function reads the file byte by byte so it could
76  // be rather slow
77  void DecompressEventData( std::ifstream &fin,
78  short nbadc,
79  size_t nch,
80  size_t seqlen,
81  std::vector< adc16_t > &adc);
82 
83 
84  //
85  void PrintEncoding();
86 
87  void SetVerbosity(int val){ m_Verbosity = val; }
88 
89  private:
90 
91  // ctor
95  // Prevent unwanted destruction
97 
98  // define encoding scheme
99  void SetEncoding();
100 
101  // structure to collect Huffman codes
102  struct HuffAccum_t
103  {
104  std::string codestr; // binary code; leave empty for raw data
105  short value; // value: diff / or raw adc
106  short nrep; // number of repetitions of this code
107  };
108 
109  //
110  std::vector<HuffAccum_t> HuffBuffer; // buffer for Huffman codes
111 
112  // add to buffer
113  void AddToHuffBuffer(short val, bool rawadc);
114 
115 
116  //
117  bool SetNbitsAdc( short nbadc );
118 
119  //
120  void AddWordsToByteBuffer(std::string words,
121  std::vector<BYTE> &buf,
122  std::string &partbyte);
123 
124  //
125  void ReadNextByte( size_t &byteidx, const char *buf,
126  std::deque< std::bitset<1> > &bits );
127 
128  // read next byte from file stream
129  void ReadNextByte( std::ifstream &fin, std::deque< std::bitset<1> > &bits, bool &status );
130 
131 
132  // get code from value
134 
135  // get value from code
136  //short GetValueFromCode(std::string code, bool &ok);
137 
138  std::string GetBinaryString(short val, size_t strsize);
139 
140  //
141  //
142  std::map<short, std::string> m_CmMap; // map to compresss
143  //std::map<std::string, short> m_UCmMap; // map to decompress
144 
145  // map to decompress is in the sorted vector of codes
146  std::vector< std::pair<std::string, short> > m_UCmMap;
147 
148  int m_Verbosity; //
149 
150  short m_MaxAdcBits; // max ADC bits
151  short m_MaxDiff; // max difference
152  size_t m_PacketSize; // size of packet in bits
153  size_t m_MinCodeSize; // shortest code length
154  size_t m_MaxCodeSize; // longest code length
155 
156  size_t m_NbitsByte; // size in bits of buffer
157  size_t m_NbitsHead; // number of header bits
158  size_t m_NbitsHC; // number of bits for huffman codes
159 
160  short m_NSeqRep; // encode sequence repetition
161  short m_NSeqRepVal; // dummy value
162  bool m_SeqEnable; // flag to encode repetitions
163  };
164 }
165 
166 
167 #endif
void CompressChData(short nbadc, std::vector< adc16_t > &raw_in, std::vector< BYTE > &bin_out)
std::string string
Definition: nybbler.cc:12
void CompressEventData(short nbadc, size_t nch, size_t seqlen, std::vector< adc16_t > &raw_in, std::vector< BYTE > &bin_out)
std::map< short, std::string > m_CmMap
struct vector vector
void AddToHuffBuffer(short val, bool rawadc)
int16_t adc
Definition: CRTFragment.hh:202
HuffDataCompressor & operator=(const HuffDataCompressor &)
std::vector< HuffAccum_t > HuffBuffer
std::vector< std::pair< std::string, short > > m_UCmMap
static HuffDataCompressor & Instance()
void AddWordsToByteBuffer(std::string words, std::vector< BYTE > &buf, std::string &partbyte)
void DecompressEventData(short nbadc, size_t nch, size_t seqlen, const char *buf, size_t bufsize, size_t &byteidx, std::vector< adc16_t > &adc)
std::string GetBinaryString(short val, size_t strsize)
void ReadNextByte(size_t &byteidx, const char *buf, std::deque< std::bitset< 1 > > &bits)