-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathDecoderBase.cxx
More file actions
285 lines (243 loc) · 8.07 KB
/
DecoderBase.cxx
File metadata and controls
285 lines (243 loc) · 8.07 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
// 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 Decoder.cxx
/// @author Roberto Preghenella
/// @since 2020-02-24
/// @brief TOF compressed data decoder base class
#include "TOFReconstruction/DecoderBase.h"
#include "DetectorsRaw/HBFUtils.h"
#include "DetectorsRaw/RDHUtils.h"
#include <cstring>
#include <iostream>
// o2::ctf::CTFIOSize iosize;
#define ENCODER_PARANOID
// o2::ctf::CTFIOSize iosize;
#define ENCODER_VERBOSE
#ifdef DECODER_PARANOID
#warning "Building code with DecoderParanoid option. This may limit the speed."
#endif
#ifdef DECODER_VERBOSE
#warning "Building code with DecoderVerbose option. This may limit the speed."
#endif
#define colorReset "\033[0m"
#define colorRed "\033[1;31m"
#define colorGreen "\033[1;32m"
#define colorYellow "\033[1;33m"
#define colorBlue "\033[1;34m"
namespace o2
{
namespace tof
{
namespace compressed
{
using RDHUtils = o2::raw::RDHUtils;
template <typename RDH>
bool DecoderBaseT<RDH>::processHBF()
{
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
std::cout << colorBlue
<< "--- PROCESS HBF"
<< colorReset
<< std::endl;
}
#endif
if (mDecoderBufferSize <= 0) {
std::cout << colorRed
<< " got an empty buffer, do nothing"
<< colorReset
<< std::endl;
return true;
}
mDecoderRDH = reinterpret_cast<const RDH*>(mDecoderPointer);
auto rdh = mDecoderRDH;
int iterations = 0;
/** loop until RDH close **/
while (!RDHUtils::getStop(*rdh)) {
iterations++;
if (iterations > 5) {
return true;
}
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
std::cout << colorBlue
<< "--- RDH open/continue detected"
<< colorReset
<< std::endl;
o2::raw::RDHUtils::printRDH(*rdh);
}
#endif
/** rdh handler **/
rdhHandler(rdh);
auto headerSize = RDHUtils::getHeaderSize(*rdh);
auto memorySize = RDHUtils::getMemorySize(*rdh);
auto offsetToNext = RDHUtils::getOffsetToNext(*rdh);
auto drmPayload = memorySize - headerSize;
bool isValidRDH = RDHUtils::checkRDH(*rdh, false);
/** copy DRM payload to save buffer **/
if (isValidRDH && drmPayload > 0) {
std::memcpy(mDecoderSaveBuffer + mDecoderSaveBufferDataSize, reinterpret_cast<const char*>(rdh) + headerSize, drmPayload);
mDecoderSaveBufferDataSize += drmPayload;
}
#ifdef DECODER_VERBOSE
else {
if (mDecoderVerbose) {
RDHUtils::checkRDH(*rdh); // verbose
}
}
#endif
/** move to next RDH **/
rdh = reinterpret_cast<const RDH*>(reinterpret_cast<const char*>(rdh) + offsetToNext);
/** check next RDH is within buffer **/
if (reinterpret_cast<const char*>(rdh) < mDecoderBuffer + mDecoderBufferSize) {
continue;
}
/** otherwise return **/
return true;
}
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
std::cout << colorBlue
<< "--- RDH close detected"
<< colorReset
<< std::endl;
o2::raw::RDHUtils::printRDH(*rdh);
}
#endif
/** process DRM data **/
mDecoderPointer = reinterpret_cast<const uint32_t*>(mDecoderSaveBuffer);
mDecoderPointerMax = reinterpret_cast<const uint32_t*>(mDecoderSaveBuffer + mDecoderSaveBufferDataSize);
while (mDecoderPointer < mDecoderPointerMax) {
if (processDRM()) {
break;
}
}
mDecoderSaveBufferDataSize = 0;
/** rdh handler **/
rdhHandler(rdh);
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
std::cout << colorBlue
<< "--- END PROCESS HBF"
<< colorReset
<< std::endl;
}
#endif
/** move to next RDH **/
mDecoderPointer = reinterpret_cast<const uint32_t*>(reinterpret_cast<const char*>(rdh) + RDHUtils::getOffsetToNext(*rdh));
/** check next RDH is within buffer **/
if (reinterpret_cast<const char*>(mDecoderPointer) < mDecoderBuffer + mDecoderBufferSize) {
return false;
}
/** otherwise return **/
return true;
}
template <typename RDH>
bool DecoderBaseT<RDH>::processDRM()
{
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
std::cout << colorBlue << "--- PROCESS DRM"
<< colorReset
<< std::endl;
}
#endif
if ((*mDecoderPointer & 0x80000000) != 0x80000000) {
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
printf(" %08x [ERROR] \n ", *mDecoderPointer);
}
#endif
return true;
}
/** crate header detected **/
auto crateHeader = reinterpret_cast<const CrateHeader_t*>(mDecoderPointer);
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
printf(" %08x CrateHeader (drmID=%d) \n ", *mDecoderPointer, crateHeader->drmID);
}
#endif
mDecoderPointer++;
/** crate orbit expected **/
auto crateOrbit = reinterpret_cast<const CrateOrbit_t*>(mDecoderPointer);
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
printf(" %08x CrateOrbit (orbit=0x%08x) \n ", *mDecoderPointer, crateOrbit->orbitID);
}
#endif
mDecoderPointer++;
/** header handler **/
headerHandler(crateHeader, crateOrbit);
while (true) {
/** crate trailer detected **/
if (*mDecoderPointer & 0x80000000) {
auto crateTrailer = reinterpret_cast<const CrateTrailer_t*>(mDecoderPointer);
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
printf(" %08x CrateTrailer (numberOfDiagnostics=%d, numberOfErrors=%d) \n ", *mDecoderPointer, crateTrailer->numberOfDiagnostics, crateTrailer->numberOfErrors);
}
#endif
mDecoderPointer++;
auto diagnostics = reinterpret_cast<const Diagnostic_t*>(mDecoderPointer);
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
for (int i = 0; i < crateTrailer->numberOfDiagnostics; ++i) {
auto diagnostic = reinterpret_cast<const Diagnostic_t*>(mDecoderPointer + i);
printf(" %08x Diagnostic (slotId=%d) \n ", *(mDecoderPointer + i), diagnostic->slotID);
}
}
#endif
mDecoderPointer += crateTrailer->numberOfDiagnostics;
auto errors = reinterpret_cast<const Error_t*>(mDecoderPointer);
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
for (int i = 0; i < crateTrailer->numberOfErrors; ++i) {
auto error = reinterpret_cast<const Error_t*>(mDecoderPointer + i);
printf(" %08x Error (slotId=%d) \n ", *(mDecoderPointer + i), error->slotID);
}
}
#endif
mDecoderPointer += crateTrailer->numberOfErrors;
/** trailer handler **/
trailerHandler(crateHeader, crateOrbit, crateTrailer, diagnostics, errors);
return false;
}
/** frame header detected **/
auto frameHeader = reinterpret_cast<const FrameHeader_t*>(mDecoderPointer);
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
printf(" %08x FrameHeader (numberOfHits=%d) \n ", *mDecoderPointer, frameHeader->numberOfHits);
}
#endif
mDecoderPointer++;
auto packedHits = reinterpret_cast<const PackedHit_t*>(mDecoderPointer);
#ifdef DECODER_VERBOSE
if (mDecoderVerbose) {
for (int i = 0; i < frameHeader->numberOfHits; ++i) {
auto packedHit = reinterpret_cast<const PackedHit_t*>(mDecoderPointer + 1);
printf(" %08x PackedHit (tdcID=%d) \n ", *(mDecoderPointer + 1), packedHit->tdcID);
packedHits++;
}
}
#endif
mDecoderPointer += frameHeader->numberOfHits;
/** frame handler **/
frameHandler(crateHeader, crateOrbit, frameHeader, packedHits);
}
/** should never reach here **/
return false;
}
template class DecoderBaseT<o2::header::RAWDataHeaderV4>;
template class DecoderBaseT<o2::header::RAWDataHeaderV6>;
template class DecoderBaseT<o2::header::RAWDataHeaderV7>;
} // namespace compressed
} // namespace tof
} // namespace o2