forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCTFCoder.h
More file actions
315 lines (284 loc) · 11 KB
/
CTFCoder.h
File metadata and controls
315 lines (284 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file CTFCoder.h
/// \author ruben.shahoyan@cern.ch
/// \brief class for entropy encoding/decoding of FDD digits data
#ifndef O2_FDD_CTFCODER_H
#define O2_FDD_CTFCODER_H
#include <algorithm>
#include <iterator>
#include <string>
#include "FDDBase/Geometry.h"
#include "DataFormatsFDD/CTF.h"
#include "DataFormatsFDD/Digit.h"
#include "DataFormatsFDD/ChannelData.h"
#include "DetectorsCommonDataFormats/DetID.h"
#include "DetectorsBase/CTFCoderBase.h"
class TTree;
namespace o2
{
namespace fdd
{
class CTFCoder final : public o2::ctf::CTFCoderBase
{
public:
CTFCoder(o2::ctf::CTFCoderBase::OpType op) : o2::ctf::CTFCoderBase(op, CTF::getNBlocks(), o2::detectors::DetID::FDD) {}
~CTFCoder() final = default;
/// entropy-encode digits to buffer with CTF
template <typename VEC>
o2::ctf::CTFIOSize encode(VEC& buff, const gsl::span<const Digit>& digitVec, const gsl::span<const ChannelData>& channelVec);
/// entropy decode clusters from buffer with CTF
template <typename VDIG, typename VCHAN>
o2::ctf::CTFIOSize decode(const CTF::base& ec, VDIG& digitVec, VCHAN& channelVec);
void createCoders(const std::vector<char>& bufVec, o2::ctf::CTFCoderBase::OpType op) final;
private:
/// compres digits clusters to CompressedDigits
template <int MAJOR_VERSION, int MINOR_VERSION>
void compress(CompressedDigits& cd, const gsl::span<const Digit>& digitVec, const gsl::span<const ChannelData>& channelVec);
size_t estimateCompressedSize(const CompressedDigits& cc);
/// decompress CompressedDigits to digits
template <int MAJOR_VERSION, int MINOR_VERSION, typename VDIG, typename VCHAN>
void decompress(const CompressedDigits& cd, VDIG& digitVec, VCHAN& channelVec);
void appendToTree(TTree& tree, CTF& ec);
void readFromTree(TTree& tree, int entry, std::vector<Digit>& digitVec, std::vector<ChannelData>& channelVec);
void assignDictVersion(o2::ctf::CTFDictHeader& h) const final;
};
/// entropy-encode clusters to buffer with CTF
template <typename VEC>
o2::ctf::CTFIOSize CTFCoder::encode(VEC& buff, const gsl::span<const Digit>& digitVec, const gsl::span<const ChannelData>& channelVec)
{
using MD = o2::ctf::Metadata::OptStore;
// what to do which each field: see o2::ctd::Metadata explanation
constexpr MD optField[CTF::getNBlocks()] = {
MD::EENCODE_OR_PACK, // BLC_trigger
MD::EENCODE_OR_PACK, // BLC_bcInc
MD::EENCODE_OR_PACK, // BLC_orbitInc
MD::EENCODE_OR_PACK, // BLC_nChan
MD::EENCODE_OR_PACK, // BLC_idChan
MD::EENCODE_OR_PACK, // BLC_time
MD::EENCODE_OR_PACK, // BLC_charge
MD::EENCODE_OR_PACK // BLC_feeBits
};
CompressedDigits cd;
if (mExtHeader.isValidDictTimeStamp()) {
if (mExtHeader.minorVersion == 0 && mExtHeader.majorVersion == 1) {
compress<1, 0>(cd, digitVec, channelVec);
} else {
compress<1, 1>(cd, digitVec, channelVec);
}
} else {
compress<1, 1>(cd, digitVec, channelVec);
}
// book output size with some margin
auto szIni = estimateCompressedSize(cd);
buff.resize(szIni);
auto ec = CTF::create(buff);
using ECB = CTF::base;
ec->setHeader(cd.header);
assignDictVersion(static_cast<o2::ctf::CTFDictHeader&>(ec->getHeader()));
ec->setANSHeader(mANSVersion);
// at every encoding the buffer might be autoexpanded, so we don't work with fixed pointer ec
o2::ctf::CTFIOSize iosize;
#define ENCODEFDD(part, slot, bits) CTF::get(buff.data())->encode(part, int(slot), bits, optField[int(slot)], &buff, mCoders[int(slot)], getMemMarginFactor());
// clang-format off
iosize += ENCODEFDD(cd.trigger, CTF::BLC_trigger, 0);
iosize += ENCODEFDD(cd.bcInc, CTF::BLC_bcInc, 0);
iosize += ENCODEFDD(cd.orbitInc, CTF::BLC_orbitInc, 0);
iosize += ENCODEFDD(cd.nChan, CTF::BLC_nChan, 0);
iosize += ENCODEFDD(cd.idChan , CTF::BLC_idChan, 0);
iosize += ENCODEFDD(cd.time, CTF::BLC_time, 0);
iosize += ENCODEFDD(cd.charge, CTF::BLC_charge, 0);
iosize += ENCODEFDD(cd.feeBits, CTF::BLC_feeBits, 0);
// clang-format on
CTF::get(buff.data())->print(getPrefix(), mVerbosity);
finaliseCTFOutput<CTF>(buff);
iosize.rawIn = sizeof(Digit) * digitVec.size() + sizeof(ChannelData) * channelVec.size();
return iosize;
}
/// decode entropy-encoded clusters to standard compact clusters
template <typename VDIG, typename VCHAN>
o2::ctf::CTFIOSize CTFCoder::decode(const CTF::base& ec, VDIG& digitVec, VCHAN& channelVec)
{
CompressedDigits cd;
cd.header = ec.getHeader();
const auto& hd = static_cast<const o2::ctf::CTFDictHeader&>(cd.header);
checkDictVersion(hd);
ec.print(getPrefix(), mVerbosity);
o2::ctf::CTFIOSize iosize;
#define DECODEFDD(part, slot) ec.decode(part, int(slot), mCoders[int(slot)])
// clang-format off
iosize += DECODEFDD(cd.trigger, CTF::BLC_trigger);
iosize += DECODEFDD(cd.bcInc, CTF::BLC_bcInc);
iosize += DECODEFDD(cd.orbitInc, CTF::BLC_orbitInc);
iosize += DECODEFDD(cd.nChan, CTF::BLC_nChan);
iosize += DECODEFDD(cd.idChan, CTF::BLC_idChan);
iosize += DECODEFDD(cd.time, CTF::BLC_time);
iosize += DECODEFDD(cd.charge, CTF::BLC_charge);
iosize += DECODEFDD(cd.feeBits, CTF::BLC_feeBits);
// clang-format on
//
if (hd.minorVersion == 0 && hd.majorVersion == 1) {
decompress<1, 0>(cd, digitVec, channelVec);
} else {
decompress<1, 1>(cd, digitVec, channelVec);
}
iosize.rawIn = sizeof(Digit) * digitVec.size() + sizeof(ChannelData) * channelVec.size();
return iosize;
}
/// decompress compressed digits to standard digits
template <int MAJOR_VERSION, int MINOR_VERSION, typename VDIG, typename VCHAN>
void CTFCoder::decompress(const CompressedDigits& cd, VDIG& digitVec, VCHAN& channelVec)
{
digitVec.clear();
channelVec.clear();
digitVec.reserve(cd.header.nTriggers);
channelVec.reserve(cd.idChan.size());
uint32_t firstEntry = 0, clCount = 0, chipCount = 0;
o2::InteractionRecord ir(cd.header.firstBC, cd.header.firstOrbit);
for (uint32_t idig = 0; idig < cd.header.nTriggers; idig++) {
// restore ROFRecord
if (cd.orbitInc[idig]) { // non-0 increment => new orbit
ir.bc = cd.bcInc[idig]; // bcInc has absolute meaning
ir.orbit += cd.orbitInc[idig];
} else {
ir.bc += cd.bcInc[idig];
}
firstEntry = channelVec.size();
uint8_t chID = 0;
int8_t nChanA = 0, nChanC = 0;
int32_t amplA = 0, amplC = 0;
int16_t timeA = 0, timeC = 0;
for (uint8_t ic = 0; ic < cd.nChan[idig]; ic++) {
auto icc = channelVec.size();
if constexpr (MINOR_VERSION == 0 && MAJOR_VERSION == 1) {
// Old decoding procedure, mostly for Pilot Beam in October 2021
chID += cd.idChan[icc];
} else {
// New decoding procedure, w/o sorted ChID requriment
chID = cd.idChan[icc];
}
const auto& chan = channelVec.emplace_back(chID, cd.time[icc], cd.charge[icc], cd.feeBits[icc]);
// rebuild digit
if (chan.mPMNumber > 7) { // A side
amplA += chan.mChargeADC;
timeA += chan.mTime;
nChanA++;
} else {
amplC += chan.mChargeADC;
timeC += chan.mTime;
nChanC++;
}
}
if (nChanA) {
timeA /= nChanA;
amplA *= 0.125;
} else {
timeA = Triggers::DEFAULT_TIME;
amplA = Triggers::DEFAULT_AMP;
}
if (nChanC) {
timeC /= nChanC;
amplC *= 0.125;
} else {
timeC = Triggers::DEFAULT_TIME;
amplC = Triggers::DEFAULT_AMP;
}
Triggers trig;
trig.setTriggers(cd.trigger[idig], nChanA, nChanC, amplA, amplC, timeA, timeC);
digitVec.emplace_back(firstEntry, cd.nChan[idig], ir, trig);
}
}
///________________________________
template <int MAJOR_VERSION, int MINOR_VERSION>
void CTFCoder::compress(CompressedDigits& cd, const gsl::span<const Digit>& digitVec, const gsl::span<const ChannelData>& channelVec)
{
// convert digits/channel to their compressed version
cd.clear();
cd.header.det = mDet;
if (!digitVec.size()) {
return;
}
uint32_t firstDig = digitVec.size(), nDigSel = digitVec.size(), nChanSel = channelVec.size();
std::vector<bool> reject(digitVec.size());
if (mIRFrameSelector.isSet()) {
for (size_t id = 0; id < digitVec.size(); id++) {
if (mIRFrameSelector.check(digitVec[id].mIntRecord) < 0) {
reject[id] = true;
nDigSel--;
nChanSel -= digitVec[id].ref.getEntries();
} else if (firstDig == digitVec.size()) {
firstDig = id;
}
}
} else {
firstDig = 0;
}
if (nDigSel == 0) { // nothing is selected
return;
}
const auto& dig0 = digitVec[firstDig];
cd.header.nTriggers = nDigSel;
cd.header.firstOrbit = dig0.mIntRecord.orbit;
cd.header.firstBC = dig0.mIntRecord.bc;
cd.trigger.resize(cd.header.nTriggers);
cd.bcInc.resize(cd.header.nTriggers);
cd.orbitInc.resize(cd.header.nTriggers);
cd.nChan.resize(cd.header.nTriggers);
cd.idChan.resize(nChanSel);
cd.time.resize(nChanSel);
cd.charge.resize(nChanSel);
cd.feeBits.resize(nChanSel);
uint16_t prevBC = cd.header.firstBC;
uint32_t prevOrbit = cd.header.firstOrbit;
uint32_t ccount = 0, dcount = 0;
for (uint32_t idig = 0; idig < digitVec.size(); idig++) {
if (reject[idig]) {
continue;
}
const auto& digit = digitVec[idig];
const auto chanels = digit.getBunchChannelData(channelVec); // we assume the channels are sorted
// fill trigger info
cd.trigger[dcount] = digit.mTriggers.getTriggersignals();
if (prevOrbit == digit.mIntRecord.orbit) {
cd.bcInc[dcount] = digit.mIntRecord.bc - prevBC;
cd.orbitInc[dcount] = 0;
} else {
cd.bcInc[dcount] = digit.mIntRecord.bc;
cd.orbitInc[dcount] = digit.mIntRecord.orbit - prevOrbit;
}
prevBC = digit.mIntRecord.bc;
prevOrbit = digit.mIntRecord.orbit;
// fill channels info
cd.nChan[dcount] = chanels.size();
if (!cd.nChan[dcount]) {
LOG(debug) << "Digits with no channels";
dcount++;
continue;
}
uint8_t prevChan = 0;
for (uint8_t ic = 0; ic < cd.nChan[dcount]; ic++) {
if constexpr (MINOR_VERSION == 0 && MAJOR_VERSION == 1) {
cd.idChan[ccount] = chanels[ic].mPMNumber - prevChan; // Old method, lets keep it for a while
} else {
cd.idChan[ccount] = chanels[ic].mPMNumber;
}
cd.time[ccount] = chanels[ic].mTime; // make sure it fits to short!!!
cd.charge[ccount] = chanels[ic].mChargeADC; // make sure we really need short!!!
cd.feeBits[ccount] = chanels[ic].mFEEBits;
prevChan = chanels[ic].mPMNumber;
ccount++;
}
dcount++;
}
}
} // namespace fdd
} // namespace o2
#endif // O2_FDD_CTFCODER_H