Belle II Software  release-06-02-00
ECLCompress.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #pragma once
10 
11 #include <algorithm>
12 #include <vector>
13 #include <framework/utilities/Utils.h>
14 
15 namespace Belle2 {
20  namespace ECL {
22  unsigned int ilog2(unsigned int v);
23 
28  class BitStream {
29  public:
33  BitStream(): m_pos(0), m_store(1, 0) {}
34 
40  explicit BitStream(int n): m_pos(0), m_store(n, 0) {}
41 
46  void putNBits(unsigned int value, unsigned int n)
47  {
48  unsigned int bpos = m_pos % 32, wpos = m_pos / 32;
49  value &= 0xffffffffu >> (32 - n);
50  // check if we have enough space and double in size if necessary.
51  if (branch_unlikely(m_pos + n > m_store.size() * 32)) m_store.resize(2 * m_store.size(), 0);
52  m_store[wpos] |= value << bpos;
53  if (bpos + n > 32) m_store[wpos + 1] = value >> (32 - bpos);
54  m_pos += n;
55  }
56 
61  unsigned int getNBits(unsigned int n)
62  {
63  unsigned int bpos = m_pos % 32, wpos = m_pos / 32;
64  // make sure we don't access memory we don't own
65  if (branch_unlikely(m_pos + n > m_store.size() * 32)) throw std::range_error("Not enough bits in stream");
66  unsigned int res = m_store[wpos] >> bpos;
67  if (bpos + n > 32) res |= m_store[wpos + 1] << (32 - bpos);
68  m_pos += n;
69  return res & (0xffffffffu >> (32 - n));
70  }
71 
73  size_t getPos() const {return m_pos;}
74 
76  void setPos(size_t pos) {m_pos = pos;}
77 
79  std::vector<unsigned int>& getStore() {return m_store;}
80 
82  void resize()
83  {
84  m_store.resize((m_pos + 31) / 32);
85  }
86  protected:
87  size_t m_pos;
88  std::vector<unsigned int> m_store;
89  };
90 
97  struct width_t {
98  unsigned char w0,
99  w1,
100  w2,
101  w3;
102  };
103 
107  class ECLCompress {
108  public:
110  virtual ~ECLCompress() {};
111 
116  virtual void compress(BitStream& out, const int* adc) = 0;
117 
122  virtual void uncompress(BitStream& in, int* adc) = 0;
123  };
124 
128  class ECLBaseCompress: public ECLCompress {
129  public:
130  void compress(BitStream& out, const int* adc) override;
131  void uncompress(BitStream& out, int* adc) override;
132  };
133 
138  public:
139  void compress(BitStream& out, const int* adc) override;
140  void uncompress(BitStream& out, int* adc) override;
141  };
142 
146  class ECLDCTCompress: public ECLCompress {
147  public:
153  ECLDCTCompress(double scale, double c0, width_t* w);
154  void compress(BitStream& out, const int* adc) override;
155  void uncompress(BitStream& out, int* adc) override;
156  protected:
157  const double m_scale;
158  const double m_c0;
159  const width_t* m_widths;
160  };
161 
166  ECLCompress* selectAlgo(int compAlgo);
167  }
169 }
Bit stream struct.
Definition: ECLCompress.h:28
size_t getPos() const
Get current position in the stream.
Definition: ECLCompress.h:73
std::vector< unsigned int > m_store
The bit storage.
Definition: ECLCompress.h:88
void resize()
Resize data vector to the current position.
Definition: ECLCompress.h:82
void setPos(size_t pos)
Set position in the stream.
Definition: ECLCompress.h:76
BitStream(int n)
Constructor with the reserved and cleared storage prepared for incoming bits.
Definition: ECLCompress.h:40
BitStream()
Default constructor for ROOT.
Definition: ECLCompress.h:33
size_t m_pos
Current position in the storage.
Definition: ECLCompress.h:87
unsigned int getNBits(unsigned int n)
Fetch n bits.
Definition: ECLCompress.h:61
void putNBits(unsigned int value, unsigned int n)
Push n least significant bits of "value" to the stream.
Definition: ECLCompress.h:46
std::vector< unsigned int > & getStore()
Get data vector.
Definition: ECLCompress.h:79
ECL waveform compression/decompression to/from the BitStream storage with the BASE algorithm.
Definition: ECLCompress.h:128
void compress(BitStream &out, const int *adc) override
Compress the ECL waveform.
Definition: ECLCompress.cc:32
void uncompress(BitStream &out, int *adc) override
Decompress the ECL waveform.
Definition: ECLCompress.cc:52
Abstract class (interface) for ECL waveform compression/decompression to/from the BitStream storage.
Definition: ECLCompress.h:107
virtual void uncompress(BitStream &in, int *adc)=0
Decompress the ECL waveform.
virtual void compress(BitStream &out, const int *adc)=0
Compress the ECL waveform.
virtual ~ECLCompress()
virtual destructure
Definition: ECLCompress.h:110
ECL waveform compression/decompression to/from the BitStream storage based on the Discrete Cosine Tra...
Definition: ECLCompress.h:146
const width_t * m_widths
Bit widths for the DCT coefficients for prefix encoding.
Definition: ECLCompress.h:159
ECLDCTCompress(double scale, double c0, width_t *w)
Constructor for DCT based compression algorithm.
Definition: ECLCompress.cc:238
void compress(BitStream &out, const int *adc) override
Compress the ECL waveform.
Definition: ECLCompress.cc:240
const double m_scale
Scale factor for quantization.
Definition: ECLCompress.h:157
const double m_c0
Average waveform amplitude.
Definition: ECLCompress.h:158
void uncompress(BitStream &out, int *adc) override
Decompress the ECL waveform.
Definition: ECLCompress.cc:258
ECL waveform compression/decompression to/from the BitStream storage with the DELTA algorithm.
Definition: ECLCompress.h:137
void compress(BitStream &out, const int *adc) override
Compress the ECL waveform.
Definition: ECLCompress.cc:59
void uncompress(BitStream &out, int *adc) override
Decompress the ECL waveform.
Definition: ECLCompress.cc:84
#define branch_unlikely(x)
A macro to tell the compiler that the argument x will be very likely be false.
Definition: Utils.h:134
Abstract base class for different kinds of events.
Bit widths for the prefix coding to encode integers which are mainly concentrated around zero and pro...
Definition: ECLCompress.h:97
unsigned char w0
Progressive bit width to encode an integer value.
Definition: ECLCompress.h:98
unsigned char w2
Progressive bit width to encode an integer value.
Definition: ECLCompress.h:100
unsigned char w1
Progressive bit width to encode an integer value.
Definition: ECLCompress.h:99
unsigned char w3
Progressive bit width to encode an integer value.
Definition: ECLCompress.h:101