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
278 lines (253 loc) · 11.7 KB
/
CTFCoder.h
File metadata and controls
278 lines (253 loc) · 11.7 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
// 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 ZDC data
#ifndef O2_ZDC_CTFCODER_H
#define O2_ZDC_CTFCODER_H
#include <algorithm>
#include <iterator>
#include <string>
#include <array>
#include "DataFormatsZDC/CTF.h"
#include "DetectorsCommonDataFormats/DetID.h"
#include "DetectorsBase/CTFCoderBase.h"
#include "ZDCReconstruction/CTFHelper.h"
class TTree;
namespace o2
{
namespace zdc
{
class CTFCoder final : public o2::ctf::CTFCoderBase
{
public:
CTFCoder(o2::ctf::CTFCoderBase::OpType op) : o2::ctf::CTFCoderBase(op, CTF::getNBlocks(), o2::detectors::DetID::ZDC) {}
~CTFCoder() final = default;
/// entropy-encode data to buffer with CTF
template <typename VEC>
o2::ctf::CTFIOSize encode(VEC& buff, const gsl::span<const BCData>& trgData, const gsl::span<const ChannelData>& chanData, const gsl::span<const OrbitData>& pedData);
/// entropy decode data from buffer with CTF
template <typename VTRG, typename VCHAN, typename VPED>
o2::ctf::CTFIOSize decode(const CTF::base& ec, VTRG& trigVec, VCHAN& chanVec, VPED& pedVec);
void createCoders(const std::vector<char>& bufVec, o2::ctf::CTFCoderBase::OpType op) final;
void setBCShiftOrbits(long v) { mBCShiftOrbits = v; }
auto getBCShiftOrbits() const { return mBCShiftOrbits; }
private:
template <typename VEC>
o2::ctf::CTFIOSize encode_impl(VEC& buff, const gsl::span<const BCData>& trgData, const gsl::span<const ChannelData>& chanData, const gsl::span<const OrbitData>& pedData);
void appendToTree(TTree& tree, CTF& ec);
void readFromTree(TTree& tree, int entry, std::vector<BCData>& trigVec, std::vector<ChannelData>& chanVec, std::vector<OrbitData>& pedVec);
std::vector<BCData> mTrgDataFilt;
std::vector<ChannelData> mChanDataFilt;
std::vector<OrbitData> mPedDataFilt;
int64_t mBCShiftOrbits = 0; // integer orbit shift (in BCs) corresponding to mBCShift. Since BCs 0 and 3563 should not be changed, this value will be used for them
};
/// entropy-encode clusters to buffer with CTF
template <typename VEC>
o2::ctf::CTFIOSize CTFCoder::encode(VEC& buff, const gsl::span<const BCData>& trigData, const gsl::span<const ChannelData>& chanData, const gsl::span<const OrbitData>& pedData)
{
if (mIRFrameSelector.isSet()) { // preselect data
std::unordered_map<uint32_t, int> orbitSaved;
mTrgDataFilt.clear();
mChanDataFilt.clear();
mPedDataFilt.clear();
for (const auto& trig : trigData) {
if (mIRFrameSelector.check(trig.ir) >= 0) {
mTrgDataFilt.push_back(trig);
auto chanIt = chanData.begin() + trig.ref.getFirstEntry();
auto& trigC = mTrgDataFilt.back();
trigC.ref.set((int)mChanDataFilt.size(), trig.ref.getEntries());
std::copy(chanIt, chanIt + trig.ref.getEntries(), std::back_inserter(mChanDataFilt));
orbitSaved[trig.ir.orbit]++;
}
}
// collect saved orbits data
for (const auto& ped : pedData) {
if (orbitSaved.find(ped.ir.orbit) != orbitSaved.end()) {
mPedDataFilt.push_back(ped);
}
}
return encode_impl(buff, mTrgDataFilt, mChanDataFilt, mPedDataFilt);
}
return encode_impl(buff, trigData, chanData, pedData);
}
template <typename VEC>
o2::ctf::CTFIOSize CTFCoder::encode_impl(VEC& buff, const gsl::span<const BCData>& trigData, const gsl::span<const ChannelData>& chanData, const gsl::span<const OrbitData>& pedData)
{
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, // _bcIncTrig
MD::EENCODE_OR_PACK, // _orbitIncTrig,
MD::EENCODE_OR_PACK, // _moduleTrig,
MD::EENCODE_OR_PACK, // _channelsHL,
MD::EENCODE_OR_PACK, // _triggersHL,
MD::EENCODE_OR_PACK, // _extTriggers,
MD::EENCODE_OR_PACK, // _nchanTrig,
//
MD::EENCODE_OR_PACK, // _chanID,
MD::EENCODE_OR_PACK, // _chanData,
//
MD::EENCODE_OR_PACK, // _orbitIncEOD,
MD::EENCODE_OR_PACK, // _pedData
MD::EENCODE_OR_PACK, // _sclInc
};
CTFHelper helper(trigData, chanData, pedData);
// book output size with some margin
auto szIni = sizeof(CTFHeader) + helper.getSize() * 2. / 3; // will be autoexpanded if needed
buff.resize(szIni);
auto ec = CTF::create(buff);
using ECB = CTF::base;
ec->setHeader(helper.createHeader());
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 ENCODEZDC(beg, end, slot, bits) CTF::get(buff.data())->encode(beg, end, int(slot), bits, optField[int(slot)], &buff, mCoders[int(slot)], getMemMarginFactor());
// clang-format off
iosize += ENCODEZDC(helper.begin_bcIncTrig(), helper.end_bcIncTrig(), CTF::BLC_bcIncTrig, 0);
iosize += ENCODEZDC(helper.begin_orbitIncTrig(), helper.end_orbitIncTrig(), CTF::BLC_orbitIncTrig, 0);
iosize += ENCODEZDC(helper.begin_moduleTrig(), helper.end_moduleTrig(), CTF::BLC_moduleTrig, 0);
iosize += ENCODEZDC(helper.begin_channelsHL(), helper.end_channelsHL(), CTF::BLC_channelsHL, 0);
iosize += ENCODEZDC(helper.begin_triggersHL(), helper.end_triggersHL(), CTF::BLC_triggersHL, 0);
iosize += ENCODEZDC(helper.begin_extTriggers(), helper.end_extTriggers(), CTF::BLC_extTriggers, 0);
iosize += ENCODEZDC(helper.begin_nchanTrig(), helper.end_nchanTrig(), CTF::BLC_nchanTrig, 0);
//
iosize += ENCODEZDC(helper.begin_chanID(), helper.end_chanID(), CTF::BLC_chanID, 0);
iosize += ENCODEZDC(helper.begin_chanData(), helper.end_chanData(), CTF::BLC_chanData, 0);
//
iosize += ENCODEZDC(helper.begin_orbitIncEOD(), helper.end_orbitIncEOD(), CTF::BLC_orbitIncEOD, 0);
iosize += ENCODEZDC(helper.begin_pedData(), helper.end_pedData(), CTF::BLC_pedData, 0);
iosize += ENCODEZDC(helper.begin_sclInc(), helper.end_sclInc(), CTF::BLC_sclInc, 0);
// clang-format on
CTF::get(buff.data())->print(getPrefix(), mVerbosity);
finaliseCTFOutput<CTF>(buff);
iosize.rawIn = sizeof(BCData) * trigData.size() + sizeof(ChannelData) * chanData.size() + sizeof(OrbitData) * pedData.size();
return iosize;
}
/// decode entropy-encoded clusters to standard compact clusters
template <typename VTRG, typename VCHAN, typename VPED>
o2::ctf::CTFIOSize CTFCoder::decode(const CTF::base& ec, VTRG& trigVec, VCHAN& chanVec, VPED& pedVec)
{
auto header = ec.getHeader();
checkDictVersion(static_cast<const o2::ctf::CTFDictHeader&>(header));
ec.print(getPrefix(), mVerbosity);
std::vector<int16_t> bcIncTrig, scalerInc;
std::vector<int32_t> orbitIncTrig, orbitIncEOD;
std::vector<uint16_t> moduleTrig, nchanTrig, chanData, pedData, triggersHL, channelsHL;
std::vector<uint8_t> extTriggers, chanID;
o2::ctf::CTFIOSize iosize;
#define DECODEZDC(part, slot) ec.decode(part, int(slot), mCoders[int(slot)])
// clang-format off
iosize += DECODEZDC(bcIncTrig, CTF::BLC_bcIncTrig);
iosize += DECODEZDC(orbitIncTrig, CTF::BLC_orbitIncTrig);
iosize += DECODEZDC(moduleTrig, CTF::BLC_moduleTrig);
iosize += DECODEZDC(channelsHL, CTF::BLC_channelsHL);
iosize += DECODEZDC(triggersHL, CTF::BLC_triggersHL);
iosize += DECODEZDC(extTriggers, CTF::BLC_extTriggers);
iosize += DECODEZDC(nchanTrig, CTF::BLC_nchanTrig);
//
iosize += DECODEZDC(chanID, CTF::BLC_chanID);
iosize += DECODEZDC(chanData, CTF::BLC_chanData);
//
iosize += DECODEZDC(orbitIncEOD, CTF::BLC_orbitIncEOD);
iosize += DECODEZDC(pedData, CTF::BLC_pedData);
iosize += DECODEZDC(scalerInc, CTF::BLC_sclInc);
// clang-format on
//
trigVec.clear();
chanVec.clear();
pedVec.clear();
trigVec.reserve(header.nTriggers);
chanVec.reserve(header.nChannels);
pedVec.reserve(header.nEOData);
// triggers and channels
uint32_t firstEntry = 0;
o2::InteractionRecord ir(header.firstBC, header.firstOrbit);
auto chanDataIt = chanData.begin();
auto chanIdIt = chanID.begin();
auto modTrigIt = moduleTrig.begin();
auto pedValIt = pedData.begin();
auto sclIncIt = scalerInc.begin();
auto channelsHLIt = channelsHL.begin();
auto triggersHLIt = triggersHL.begin();
auto scalers = header.firstScaler;
bool checkIROK = (mBCShift == 0); // need to check if CTP offset correction does not make the local time negative ?
for (uint32_t itrig = 0; itrig < header.nTriggers; itrig++) {
// restore TrigRecord
if (orbitIncTrig[itrig]) { // non-0 increment => new orbit
ir.bc = bcIncTrig[itrig]; // bcInc has absolute meaning
ir.orbit += orbitIncTrig[itrig];
} else {
ir.bc += bcIncTrig[itrig];
}
long bcshift = 0;
if (mBCShift) {
bcshift = (ir.bc == 0 || ir.bc == o2::constants::lhc::LHCMaxBunches - 1) ? mBCShiftOrbits : mBCShift; // we should never touch the BC of BC=0 or 3563
}
if (checkIROK || canApplyBCShift(ir, bcshift)) { // correction will be ok
checkIROK = true;
} else { // correction would make IR prior to mFirstTFOrbit, skip
chanDataIt += NTimeBinsPerBC * nchanTrig[itrig];
chanIdIt += nchanTrig[itrig];
channelsHLIt += 2;
triggersHLIt += 2;
continue;
}
auto firstChanEntry = chanVec.size();
for (uint16_t ic = 0; ic < nchanTrig[itrig]; ic++) {
auto& chan = chanVec.emplace_back();
chan.id = *chanIdIt++;
std::copy_n(chanDataIt, NTimeBinsPerBC, chan.data.begin());
chanDataIt += NTimeBinsPerBC;
}
uint32_t chHL = (uint32_t(*channelsHLIt++) << 16) + *channelsHLIt++;
uint32_t trHL = (uint32_t(*triggersHLIt++) << 16) + *triggersHLIt++;
auto& bcTrig = trigVec.emplace_back(firstChanEntry, chanVec.size() - firstChanEntry, ir - bcshift, chHL, trHL, extTriggers[itrig]);
std::copy_n(modTrigIt, NModules, bcTrig.moduleTriggers.begin());
modTrigIt += NModules;
}
// pedestal data
ir = {o2::constants::lhc::LHCMaxBunches - 1, header.firstOrbitEOData};
for (uint32_t ip = 0; ip < header.nEOData; ip++) {
ir.orbit += orbitIncEOD[ip];
long bcshift = 0;
if (mBCShift) {
bcshift = (ir.bc == 0 || ir.bc == o2::constants::lhc::LHCMaxBunches - 1) ? mBCShiftOrbits : mBCShift; // we should never touch the BC of BC=0 or 3563
}
if (checkIROK || canApplyBCShift(ir, bcshift)) { // correction will be ok
checkIROK = true;
} else { // correction would make IR prior to mFirstTFOrbit, skip
sclIncIt += NChannels;
pedValIt += NChannels;
continue;
}
for (uint32_t ic = 0; ic < NChannels; ic++) {
scalers[ic] += *sclIncIt++; // increment scaler
}
auto& ped = pedVec.emplace_back(OrbitData{ir - bcshift, {}, scalers});
std::copy_n(pedValIt, NChannels, ped.data.begin());
pedValIt += NChannels;
}
// make sure whole data was used
assert(chanDataIt == chanData.end());
assert(chanIdIt == chanID.end());
assert(modTrigIt == moduleTrig.end());
assert(pedValIt == pedData.end());
assert(channelsHLIt == channelsHL.end());
assert(triggersHLIt == triggersHL.end());
assert(sclIncIt == scalerInc.end());
iosize.rawIn = sizeof(BCData) * trigVec.size() + sizeof(ChannelData) * chanVec.size() + sizeof(OrbitData) * pedVec.size();
return iosize;
}
} // namespace zdc
} // namespace o2
#endif // O2_ZDC_CTFCODER_H