Skip to content

Commit d8bf8ee

Browse files
wiechuladavidrohr
authored andcommitted
Speed up SACs, improved error messages, fix stream alignment
* Speed up using custom hex string to int convertion, some orders of magnitude * Add packet position information in error messages * Fix false error message in initial string alignment * Fix wrong error message in packet count check overflowing 16bit boundary
1 parent 7d75107 commit d8bf8ee

2 files changed

Lines changed: 56 additions & 17 deletions

File tree

Detectors/TPC/calibration/include/TPCCalibration/SACDecoder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ class Decoder
233233

234234
uint32_t mDebugLevel{0}; ///< Amount of debug information to print
235235

236+
uint32_t decodeTimeStamp(const char* data);
237+
236238
/// \return status message: 1 = good, 0 = data length too short, -1 = decoding error
237239
int decodeChannels(DecodedDataFE& sacs, size_t& carry, int feid);
238240
void decode(int feid);

Detectors/TPC/calibration/src/SACDecoder.cxx

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ bool Decoder::process(const char* data, size_t size)
6060

6161
// check packet counters are increasing by one
6262
//
63-
if (lastPacketInstance && (packetInstance != (lastPacketInstance + 1))) {
63+
if (lastPacketInstance && (uint16_t(packetInstance) != uint16_t(lastPacketInstance + 1))) { // convert to uint16_t to respect size of sac.header.pktCount
6464
LOGP(error, "Packet for instance {} missing, last packet {}, this packet {}", instance, lastPacketInstance, packetInstance);
6565
}
6666

@@ -137,6 +137,29 @@ int Decoder::decodeChannels(DecodedDataFE& sacs, size_t& carry, int feid)
137137
return 1;
138138
}
139139

140+
uint32_t Decoder::decodeTimeStamp(const char* data)
141+
{
142+
int carry = 0;
143+
uint32_t value = 0;
144+
for (int i = 0; i < 8; ++i) {
145+
const auto c = data[carry];
146+
uint32_t nibble = 0;
147+
if ((c >= '0') && (c <= '9')) {
148+
nibble = c - '0';
149+
} else if ((c >= 'A') && (c <= 'F')) {
150+
nibble = c - 'A' + 10;
151+
} else {
152+
LOGP(error, "unexpected '{}' in time stamp", data[carry]);
153+
break;
154+
}
155+
value <<= 4;
156+
value |= (nibble & 0xF);
157+
++carry;
158+
}
159+
160+
return value;
161+
}
162+
140163
void Decoder::decode(int feid)
141164
{
142165
const auto startTime = HighResClock::now();
@@ -190,11 +213,7 @@ void Decoder::decode(int feid)
190213
const auto streamStart = carry;
191214
// time stamp comes after channel data
192215
++carry;
193-
std::string_view vd(&data[carry], 8);
194-
std::stringstream str(vd.data());
195-
str.flags(std::ios_base::hex);
196-
uint32_t timeStamp;
197-
str >> timeStamp;
216+
uint32_t timeStamp = decodeTimeStamp(&data[carry]);
198217
decdata.timeStamp = timeStamp;
199218
decAdditional.timeStamp = timeStamp;
200219

@@ -226,7 +245,8 @@ void Decoder::decode(int feid)
226245
auto& refCount = mTSCountFEs[feid].second;
227246
// NOTE: use (refTime > 1) instead of (refTime > 0), since in some cases the packet with TS 0 is missing
228247
if ((refCount == 0) && (refTime > 1)) {
229-
LOGP(info, "Skipping initial data packet {} with time stamp {} for FE {}", mTSCountFEs[feid].first, timeStamp, feid);
248+
LOGP(detail, "Skipping initial data packet {} with time stamp {} for FE {}",
249+
mTSCountFEs[feid].first, timeStamp, feid);
230250
} else {
231251
if (refTime < firstRefTime) {
232252
// LOGP(info, "FE {}: {} < {}, adding {} DataPoint(s)", feid, refTime, firstRefTime, firstRefTime - refTime);
@@ -240,12 +260,18 @@ void Decoder::decode(int feid)
240260
mDecodedData.setData(refTime - firstRefTime, refTime, decdata, feid);
241261

242262
if (refCount != refTime) {
243-
LOGP(warning, "Unexpected time stamp in FE {}. Count {} != TS {} ({}), dump: {}", feid, refCount, refTime, timeStamp, std::string_view(&data[streamStart], std::min(size_t(20), dataSize - streamStart - carry)));
244263
// NOTE: be graceful in case TS 0 is missing and avoid furhter warnings
264+
if (!((refCount == 0) && (refTime == 1))) {
265+
LOGP(warning, "Unexpected time stamp in FE {}. Count {} != TS {} ({}), dump: {}", feid, refCount, refTime, timeStamp, std::string_view(&data[streamStart], std::min(size_t(20), dataSize - streamStart - carry)));
266+
}
245267
if (((refCount == 0) && (refTime == 1)) || ((mReAlignType == ReAlignType::AlignAndFillMissing) && syncLost)) {
246268
while (refCount < refTime) {
247269
mDecodedData.setData(refCount - firstRefTime, refCount, DecodedDataFE(), feid);
248-
LOGP(warning, "Adding dummy data for FE {}, TS {}", feid, refCount);
270+
if ((refCount == 0) && (refTime == 1)) {
271+
LOGP(info, "Adding dummy data for FE {}, TS {}", feid, refCount);
272+
} else {
273+
LOGP(warning, "Adding dummy data for FE {}, TS {}", feid, refCount);
274+
}
249275
++refCount;
250276
}
251277
syncLost = false;
@@ -293,22 +319,33 @@ void Decoder::decode(int feid)
293319
}
294320
++carry;
295321
} else if (data[carry] >= 'a' && data[carry] <= 'z') {
296-
if (mReAlignType != ReAlignType::None) {
297-
LOGP(error, "Skipping {} for FE {}, trying to re-align data stream", data[carry], feid);
298-
aligned = false;
299-
syncLost = true;
300-
} else {
301-
LOGP(error, "Skipping {} for FE {}, might lead to decoding problems", data[carry], feid);
322+
if (mTSCountFEs[feid].first == 0) {
323+
// in case we still haven't processed the first packet we simply skip end sequence termination and continue
324+
LOGP(info, "Skipping {} for FE {}, time stamp packet count {}, ref count {}",
325+
data[carry], feid, mTSCountFEs[feid].first, mTSCountFEs[feid].second);
302326
++carry;
327+
} else {
328+
if (mReAlignType != ReAlignType::None) {
329+
LOGP(warning, "Skipping {} for FE {}, trying to re-align data stream, time stamp packet count {}, ref count {}",
330+
data[carry], feid, mTSCountFEs[feid].first, mTSCountFEs[feid].second);
331+
aligned = false;
332+
syncLost = true;
333+
} else {
334+
LOGP(error, "Skipping {} for FE {}, might lead to decoding problems, time stamp packet count {}, ref count {}",
335+
data[carry], feid, mTSCountFEs[feid].first, mTSCountFEs[feid].second);
336+
++carry;
337+
}
303338
}
304339
decdata.reset();
305340
} else {
306341
if (mReAlignType != ReAlignType::None) {
307-
LOGP(warn, "Can't interpret position for FE {}, {} / {}, {}, trying to re-align data stream\n", feid, carry, dataSize, std::string_view(&data[carry - 8], std::min(size_t(20), dataSize - 8 - carry)));
342+
LOGP(warn, "Can't interpret position for FE {}, {} / {}, {}, trying to re-align data stream, time stamp packet count {}, ref count {}",
343+
feid, carry, dataSize, std::string_view(&data[carry - 8], std::min(size_t(20), dataSize - 8 - carry)), mTSCountFEs[feid].first, mTSCountFEs[feid].second);
308344
aligned = false;
309345
syncLost = true;
310346
} else {
311-
LOGP(error, "Can't interpret position for FE {}, {} / {}, {}, stopping decoding\n", feid, carry, dataSize, std::string_view(&data[carry - 8], std::min(size_t(20), dataSize - 8 - carry)));
347+
LOGP(error, "Can't interpret position for FE {}, {} / {}, {}, stopping decoding, time stamp packet count {}, ref count {}",
348+
feid, carry, dataSize, std::string_view(&data[carry - 8], std::min(size_t(20), dataSize - 8 - carry)), mTSCountFEs[feid].first, mTSCountFEs[feid].second);
312349
break;
313350
}
314351
}

0 commit comments

Comments
 (0)