Belle II Software  release-05-01-25
ECLDspData.cc
1 #include <ecl/dbobjects/ECLDspData.h>
2 
3 using namespace Belle2;
4 
5 void ECLDspData::packCoefVector(const std::vector<short int>& src, std::vector<short int>& dst)
6 {
7  const int N_CHANNELS = 16;
8  int size = src.size();
9 
10  if (getPackerVersion() == 0) {
11  dst = src;
12  } else if (getPackerVersion() >= 1) {
13  // Apply reversible transformation to shrink the range
14  // of saved coefficients into much smaller interval.
15  // This improves the compression by a factor of ~2.
16 
17  dst.resize(size);
18  for (int i = size - 1; i >= 0; i--) {
19  if (i >= 2 * N_CHANNELS)
20  dst[i] = src[i] - 2 * src[i - N_CHANNELS] + src[i - 2 * N_CHANNELS];
21  else if (i >= N_CHANNELS)
22  dst[i] = src[i] - 2 * src[i - N_CHANNELS];
23  else
24  dst[i] = src[i];
25  }
26  }
27 
28  if (getPackerVersion() == 2) {
29  // Rearrange the data in such a way that each coeff
30  // only takes up 4 bits (special value 0xF indicates
31  // coefficients that are too big to be packed)
32  //
33  // Vector structure after packing:
34  // [big_coefs | packed_coefs | packed_coefs.size()]
35 
36  // Bits allocated for each value
37  const int value_bits = 4;
38  const long value_max = 0xF;
39  const int values_packed = sizeof(short) * 8 / value_bits;
40 
41  int shift = -6;
42 
43  std::vector<short> packed(size / values_packed);
44  int len = -1;
45  for (int i = 0; i < size; i++) {
46  if (i % values_packed == 0) packed[i / values_packed] = 0;
47 
48  short val = dst[i] - shift;
49  if (val >= 0 && val < value_max) {
50  if (len < 0) len = i;
51  packed[i / values_packed] |= val << (value_bits * (i % values_packed));
52  } else {
53  if (len >= 0 && len < i) {
54  dst[len++] = dst[i];
55  }
56  packed[i / values_packed] |= short(value_max) << (value_bits * (i % values_packed));
57  }
58  }
59 
60  int packed_size = 0;
61 
62  if (len >= 0) {
63  dst.resize(len + packed.size());
64  packed_size = packed.size();
65  for (int i = 0; i < packed_size; i++) {
66  dst[len + i] = packed[i];
67  }
68  }
69  dst.push_back(packed_size);
70  }
71 }
72 void ECLDspData::unpackCoefVector(const std::vector<short int>& src, std::vector<short int>& dst) const
73 {
74  const int N_CHANNELS = 16;
75 
76  int packer_version = m_extraData[1];
77  int dst_size = 0;
78 
79  if (packer_version == 1) {
80  dst_size = src.size();
81  dst.resize(dst_size);
82  }
83  if (packer_version >= 2) {
84  // Number of bits allocated for each value
85  const int value_bits = 4;
86  const long value_max = 0xF;
87  const int values_packed = sizeof(short) * 8 / value_bits;
88 
89  int size = src.size();
90  int packed_size = src[--size];
91 
92  int packed_start = size - packed_size;
93  dst_size = packed_size * values_packed;
94 
95  dst.resize(dst_size);
96 
97  if (packed_size > 0) {
98  int unpacked_index = 0;
99  int dst_index = 0;
100  const int shift = -6;
101 
102  for (int i = packed_start; i < size; i++) {
103  auto package = src[i];
104  for (int k = 0; k < values_packed; k++) {
105  short val = package & value_max;
106  if (val != value_max) {
107  dst[dst_index++] = val + shift;
108  } else {
109  dst[dst_index++] = src[unpacked_index++];
110  }
111  package >>= value_bits;
112  }
113  }
114  } else {
115  dst = src;
116  dst.resize(size);
117  }
118  }
119 
120 
121  if (packer_version == 0) {
122  dst = src;
123  } else if (packer_version >= 1) {
124  const std::vector<short int>& new_src = packer_version >= 2 ? dst : src;
125 
126  for (int i = 0; i < dst_size; i++) {
127  if (i >= 2 * N_CHANNELS)
128  dst[i] = new_src[i] + 2 * dst[i - N_CHANNELS] - dst[i - 2 * N_CHANNELS];
129  else if (i >= N_CHANNELS)
130  dst[i] = new_src[i] + 2 * dst[i - N_CHANNELS];
131  else
132  dst[i] = new_src[i];
133  }
134  }
135 }
136 
Belle2::ECLDspData::m_extraData
std::vector< short int > m_extraData
This vector contains all parameters that didn't exist in the initial version of ECL DSP file format.
Definition: ECLDspData.h:102
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ECLDspData::packCoefVector
void packCoefVector(const std::vector< short int > &src, std::vector< short int > &dst)
Convert vector of DSP coefficients (src) to ECLDspData internal format (dst).
Definition: ECLDspData.cc:5
Belle2::ECLDspData::getPackerVersion
static short int getPackerVersion()
GETTERS.
Definition: ECLDspData.h:146
Belle2::ECLDspData::unpackCoefVector
void unpackCoefVector(const std::vector< short int > &src, std::vector< short int > &dst) const
Convert vector of DSP coefficients (src) to ECLDspData internal format (dst).
Definition: ECLDspData.cc:72