Belle II Software  release-05-01-25
ECLCompress.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Alexei Sibidanov *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #ifndef ECLCOMPRESS_H
12 #define ECLCOMPRESS_H
13 
14 #include <algorithm>
15 #include <vector>
16 #include <string.h>
17 
18 namespace Belle2 {
23  namespace ECL {
25  unsigned int ilog2(unsigned int v);
26 
31  class BitStream {
32  public:
36  BitStream(): m_pos(0) {}
37 
43  explicit BitStream(int n): m_pos(0)
44  {
45  m_store.resize(n);
46  memset(m_store.data(), 0, m_store.size()*sizeof(unsigned int));
47  }
48 
53  void putNBits(unsigned int value, unsigned int n)
54  {
55  unsigned int bpos = m_pos % 32, wpos = m_pos / 32;
56  value &= 0xffffffffu >> (32 - n);
57  m_store[wpos] |= value << bpos;
58  if (bpos + n > 32) m_store[wpos + 1] = value >> (32 - bpos);
59  m_pos += n;
60  }
61 
66  unsigned int getNBits(unsigned int n)
67  {
68  unsigned int bpos = m_pos % 32, wpos = m_pos / 32;
69  unsigned int res = m_store[wpos] >> bpos;
70  if (bpos + n > 32) res |= m_store[wpos + 1] << (32 - bpos);
71  m_pos += n;
72  return res & (0xffffffffu >> (32 - n));
73  }
74 
76  size_t getPos() const {return m_pos;}
77 
79  void setPos(size_t pos) {m_pos = pos;}
80 
82  std::vector<unsigned int>& getStore() {return m_store;}
83 
85  void resize()
86  {
87  m_store.resize((m_pos + 31) / 32);
88  }
89  protected:
90  size_t m_pos;
91  std::vector<unsigned int> m_store;
92  };
93 
100  struct width_t {
101  unsigned char w0,
102  w1,
103  w2,
104  w3;
105  };
106 
110  class ECLCompress {
111  public:
113  virtual ~ECLCompress() {};
114 
119  virtual void compress(BitStream& out, const int* adc) = 0;
120 
125  virtual void uncompress(BitStream& in, int* adc) = 0;
126  };
127 
131  class ECLBaseCompress: public ECLCompress {
132  public:
133  void compress(BitStream& out, const int* adc) override;
134  void uncompress(BitStream& out, int* adc) override;
135  };
136 
140  class ECLDeltaCompress: public ECLCompress {
141  public:
142  void compress(BitStream& out, const int* adc) override;
143  void uncompress(BitStream& out, int* adc) override;
144  };
145 
149  class ECLDCTCompress: public ECLCompress {
150  public:
156  ECLDCTCompress(double scale, double c0, width_t* w);
157  void compress(BitStream& out, const int* adc) override;
158  void uncompress(BitStream& out, int* adc) override;
159  protected:
160  const double m_scale;
161  const double m_c0;
162  const width_t* m_widths;
163  };
164 
169  ECLCompress* selectAlgo(int compAlgo);
170  }
172 }
173 #endif
Belle2::ECL::ECLDCTCompress::m_scale
const double m_scale
Scale factor for quantization.
Definition: ECLCompress.h:168
Belle2::ECL::ECLDCTCompress::m_c0
const double m_c0
Average waveform amplitude.
Definition: ECLCompress.h:169
Belle2::ECL::ECLBaseCompress
ECL waveform compression/decompression to/from the BitStream storage with the BASE algorithm.
Definition: ECLCompress.h:139
Belle2::ECL::BitStream::resize
void resize()
Resize data vector to the current position.
Definition: ECLCompress.h:93
Belle2::ECL::width_t::w0
unsigned char w0
Progressive bit width to encode an integer value.
Definition: ECLCompress.h:109
Belle2::ECL::BitStream
Bit stream struct.
Definition: ECLCompress.h:39
Belle2::ECL::width_t::w1
unsigned char w1
Progressive bit width to encode an integer value.
Definition: ECLCompress.h:110
Belle2::ECL::BitStream::setPos
void setPos(size_t pos)
Set position in the stream.
Definition: ECLCompress.h:87
Belle2::ECL::BitStream::m_store
std::vector< unsigned int > m_store
The bit storage.
Definition: ECLCompress.h:99
Belle2::ECL::ECLDeltaCompress::compress
void compress(BitStream &out, const int *adc) override
Compress the ECL waveform.
Definition: ECLCompress.cc:61
Belle2::ECL::width_t::w2
unsigned char w2
Progressive bit width to encode an integer value.
Definition: ECLCompress.h:111
Belle2::ECL::ECLCompress::uncompress
virtual void uncompress(BitStream &in, int *adc)=0
Decompress the ECL waveform.
Belle2::ECL::BitStream::BitStream
BitStream()
Default constructor for ROOT.
Definition: ECLCompress.h:44
Belle2::ECL::width_t
Bit widths for the prefix coding to encode integers which are mainly concentrated around zero and pro...
Definition: ECLCompress.h:108
Belle2::ECL::BitStream::m_pos
size_t m_pos
Current position in the storage.
Definition: ECLCompress.h:98
Belle2::ECL::ECLDCTCompress::m_widths
const width_t * m_widths
Bit widths for the DCT coefficients for prefix encoding.
Definition: ECLCompress.h:170
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ECL::ECLCompress
Abstract class (interface) for ECL waveform compression/decompression to/from the BitStream storage.
Definition: ECLCompress.h:118
Belle2::ECL::ECLCompress::~ECLCompress
virtual ~ECLCompress()
virtual destructure
Definition: ECLCompress.h:121
Belle2::ECL::BitStream::getNBits
unsigned int getNBits(unsigned int n)
Fetch n bits.
Definition: ECLCompress.h:74
Belle2::ECL::ECLDCTCompress::ECLDCTCompress
ECLDCTCompress(double scale, double c0, width_t *w)
Constructor for DCT based compression algorithm.
Definition: ECLCompress.cc:240
Belle2::ECL::ECLBaseCompress::uncompress
void uncompress(BitStream &out, int *adc) override
Decompress the ECL waveform.
Definition: ECLCompress.cc:54
Belle2::ECL::BitStream::getStore
std::vector< unsigned int > & getStore()
Get data vector.
Definition: ECLCompress.h:90
Belle2::ECL::BitStream::putNBits
void putNBits(unsigned int value, unsigned int n)
Push n least significant bits of "value" to the stream.
Definition: ECLCompress.h:61
Belle2::ECL::width_t::w3
unsigned char w3
Progressive bit width to encode an integer value.
Definition: ECLCompress.h:112
Belle2::ECL::ECLDCTCompress::uncompress
void uncompress(BitStream &out, int *adc) override
Decompress the ECL waveform.
Definition: ECLCompress.cc:260
Belle2::ECL::ECLDeltaCompress::uncompress
void uncompress(BitStream &out, int *adc) override
Decompress the ECL waveform.
Definition: ECLCompress.cc:86
Belle2::ECL::ECLCompress::compress
virtual void compress(BitStream &out, const int *adc)=0
Compress the ECL waveform.
Belle2::ECL::BitStream::getPos
size_t getPos() const
Get current position in the stream.
Definition: ECLCompress.h:84
Belle2::ECL::ECLBaseCompress::compress
void compress(BitStream &out, const int *adc) override
Compress the ECL waveform.
Definition: ECLCompress.cc:34
Belle2::ECL::ECLDCTCompress::compress
void compress(BitStream &out, const int *adc) override
Compress the ECL waveform.
Definition: ECLCompress.cc:242