-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathCTFCoder.cxx
More file actions
223 lines (198 loc) · 8.28 KB
/
CTFCoder.cxx
File metadata and controls
223 lines (198 loc) · 8.28 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
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// 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.cxx
/// \author fnoferin@cern.ch
/// \brief class for entropy encoding/decoding of TOF compressed digits data
#include "TOFReconstruction/CTFCoder.h"
#include "CommonUtils/StringUtils.h"
#include <TTree.h>
using namespace o2::tof;
///___________________________________________________________________________________
// Register encoded data in the tree (Fill is not called, will be done by caller)
void CTFCoder::appendToTree(TTree& tree, CTF& ec)
{
ec.appendToTree(tree, mDet.getName());
}
///___________________________________________________________________________________
// extract and decode data from the tree
void CTFCoder::readFromTree(TTree& tree, int entry, std::vector<ReadoutWindowData>& rofRecVec, std::vector<Digit>& cdigVec, std::vector<uint8_t>& pattVec)
{
assert(entry >= 0 && entry < tree.GetEntries());
CTF ec;
ec.readFromTree(tree, mDet.getName(), entry);
decode(ec, rofRecVec, cdigVec, pattVec);
}
///________________________________
void CTFCoder::compress(CompressedInfos& cc,
const gsl::span<const ReadoutWindowData>& rofRecVec,
const gsl::span<const Digit>& cdigVec,
const gsl::span<const uint8_t>& pattVec)
{
// store in the header the orbit of 1st ROF
cc.clear();
if (!rofRecVec.size()) {
return;
}
const auto& rofRec0 = rofRecVec[0];
int nrof = rofRecVec.size();
LOGF(DEBUG, "TOF compress %d ReadoutWindow with %ld digits", nrof, cdigVec.size());
cc.header.nROFs = nrof;
cc.header.firstOrbit = rofRec0.getBCData().orbit;
cc.header.firstBC = rofRec0.getBCData().bc;
cc.header.nPatternBytes = pattVec.size();
cc.header.nDigits = cdigVec.size();
cc.bcIncROF.resize(cc.header.nROFs);
cc.orbitIncROF.resize(cc.header.nROFs);
cc.ndigROF.resize(cc.header.nROFs);
cc.ndiaROF.resize(cc.header.nROFs);
cc.ndiaCrate.resize(cc.header.nROFs * 72);
//
cc.timeFrameInc.resize(cc.header.nDigits);
cc.timeTDCInc.resize(cc.header.nDigits);
cc.stripID.resize(cc.header.nDigits);
cc.chanInStrip.resize(cc.header.nDigits);
cc.tot.resize(cc.header.nDigits);
cc.pattMap.resize(pattVec.size());
uint16_t prevBC = cc.header.firstBC;
uint32_t prevOrbit = cc.header.firstOrbit;
std::vector<Digit> digCopy;
for (uint32_t irof = 0; irof < rofRecVec.size(); irof++) {
const auto& rofRec = rofRecVec[irof];
const auto& intRec = rofRec.getBCData();
int64_t rofInBC = intRec.toLong();
// define interaction record
if (intRec.orbit == prevOrbit) {
cc.orbitIncROF[irof] = 0;
cc.bcIncROF[irof] = intRec.bc - prevBC; // store increment of BC if in the same orbit
} else {
cc.orbitIncROF[irof] = intRec.orbit - prevOrbit;
cc.bcIncROF[irof] = intRec.bc; // otherwise, store absolute bc
prevOrbit = intRec.orbit;
}
prevBC = intRec.bc;
auto ndig = rofRec.size();
cc.ndigROF[irof] = ndig;
cc.ndiaROF[irof] = rofRec.sizeDia();
for (int icrate = 0; icrate < 72; icrate++) {
if (rofRec.isEmptyCrate(icrate)) {
cc.ndiaCrate[irof * 72 + icrate] = 0;
} else {
cc.ndiaCrate[irof * 72 + icrate] = rofRec.getDiagnosticInCrate(icrate) + 1; // shifted by one since -1 means crate not available (then to get unsigned int)
}
}
if (!ndig) { // no hits data for this ROF --> not fill
continue;
}
int idigMin = rofRec.first(), idigMax = idigMin + ndig;
int idig = idigMin;
// make a copy of digits
digCopy.clear();
for (; idig < idigMax; idig++) {
digCopy.emplace_back(cdigVec[idig]);
}
// sort digits according to time (ascending order)
std::sort(digCopy.begin(), digCopy.end(),
[](o2::tof::Digit a, o2::tof::Digit b) {
if (a.getBC() == b.getBC()) {
return a.getTDC() < b.getTDC();
} else {
return a.getBC() < b.getBC();
}
});
int timeframe = 0;
int tdc = 0;
idig = idigMin;
for (; idig < idigMax; idig++) {
const auto& dig = digCopy[idig - idigMin];
int deltaBC = dig.getBC() - rofInBC;
int ctimeframe = deltaBC / 64;
int cTDC = (deltaBC % 64) * 1024 + dig.getTDC();
if (ctimeframe == timeframe) {
cc.timeFrameInc[idig] = 0;
cc.timeTDCInc[idig] = cTDC - tdc;
} else {
cc.timeFrameInc[idig] = ctimeframe - timeframe;
cc.timeTDCInc[idig] = cTDC;
timeframe = ctimeframe;
}
tdc = cTDC;
int chan = dig.getChannel();
cc.stripID[idig] = chan / Geo::NPADS;
cc.chanInStrip[idig] = chan % Geo::NPADS;
cc.tot[idig] = dig.getTOT();
LOGF(DEBUG, "%d) TOFBC = %d, deltaBC = %d, TDC = %d, CH=%d", irof, rofInBC, deltaBC, cTDC, chan);
LOGF(DEBUG, "%d) TF=%d, TDC=%d, STRIP=%d, CH=%d, TOT=%d", idig, cc.timeFrameInc[idig], cc.timeTDCInc[idig], cc.stripID[idig], cc.chanInStrip[idig], cc.tot[idig]);
}
}
memcpy(cc.pattMap.data(), pattVec.data(), cc.header.nPatternBytes); // RSTODO: do we need this?
}
///________________________________
void CTFCoder::createCoders(const std::string& dictPath, o2::ctf::CTFCoderBase::OpType op)
{
bool mayFail = true; // RS FIXME if the dictionary file is not there, do not produce exception
auto buff = readDictionaryFromFile<CTF>(dictPath, mayFail);
if (!buff.size()) {
if (mayFail) {
return;
}
throw std::runtime_error("Failed to create CTF dictionaty");
}
const auto* ctf = CTF::get(buff.data());
auto getFreq = [ctf](CTF::Slots slot) -> o2::rans::FrequencyTable {
o2::rans::FrequencyTable ft;
auto bl = ctf->getBlock(slot);
auto md = ctf->getMetadata(slot);
ft.addFrequencies(bl.getDict(), bl.getDict() + bl.getNDict(), md.min, md.max);
return std::move(ft);
};
auto getProbBits = [ctf](CTF::Slots slot) -> int {
return ctf->getMetadata(slot).probabilityBits;
};
CompressedInfos cc; // just to get member types
#define MAKECODER(part, slot) createCoder<decltype(part)::value_type>(op, getFreq(slot), getProbBits(slot), int(slot))
// clang-format off
MAKECODER(cc.bcIncROF, CTF::BLCbcIncROF);
MAKECODER(cc.orbitIncROF, CTF::BLCorbitIncROF);
MAKECODER(cc.ndigROF, CTF::BLCndigROF);
MAKECODER(cc.ndiaROF, CTF::BLCndiaROF);
MAKECODER(cc.ndiaCrate, CTF::BLCndiaCrate);
MAKECODER(cc.timeFrameInc, CTF::BLCtimeFrameInc);
MAKECODER(cc.timeTDCInc, CTF::BLCtimeTDCInc);
MAKECODER(cc.stripID, CTF::BLCstripID);
MAKECODER(cc.chanInStrip, CTF::BLCchanInStrip);
MAKECODER(cc.tot, CTF::BLCtot);
MAKECODER(cc.pattMap, CTF::BLCpattMap);
// clang-format on
}
///________________________________
size_t CTFCoder::estimateCompressedSize(const CompressedInfos& cc)
{
size_t sz = 0;
// clang-format off
// RS FIXME this is very crude estimate, instead, an empirical values should be used
#define VTP(vec) typename std::remove_reference<decltype(vec)>::type::value_type
#define ESTSIZE(vec, slot) mCoders[int(slot)] ? \
rans::calculateMaxBufferSize(vec.size(), reinterpret_cast<const o2::rans::LiteralEncoder64<VTP(vec)>*>(mCoders[int(slot)].get())->getAlphabetRangeBits(), sizeof(VTP(vec)) ) : vec.size()*sizeof(VTP(vec))
sz += ESTSIZE(cc.bcIncROF, CTF::BLCbcIncROF);
sz += ESTSIZE(cc.orbitIncROF, CTF::BLCorbitIncROF);
sz += ESTSIZE(cc.ndigROF, CTF::BLCndigROF);
sz += ESTSIZE(cc.ndiaROF, CTF::BLCndiaROF);
sz += ESTSIZE(cc.ndiaCrate, CTF::BLCndiaCrate);
sz += ESTSIZE(cc.timeFrameInc, CTF::BLCtimeFrameInc);
sz += ESTSIZE(cc.timeTDCInc, CTF::BLCtimeTDCInc);
sz += ESTSIZE(cc.stripID, CTF::BLCstripID);
sz += ESTSIZE(cc.chanInStrip, CTF::BLCchanInStrip);
sz += ESTSIZE(cc.tot, CTF::BLCtot);
sz += ESTSIZE(cc.pattMap, CTF::BLCpattMap);
// clang-format on
sz *= 2. / 3; // if needed, will be autoexpanded
LOG(DEBUG) << "Estimated output size is " << sz << " bytes";
return sz;
}