Skip to content

Commit 7278b4e

Browse files
matthias-kleinerwiechula
authored andcommitted
TPC: sort buffer of pressure in case it is not sorted
- allocate enough memory to prevent reallocation
1 parent 5042672 commit 7278b4e

File tree

1 file changed

+21
-6
lines changed
  • DataFormats/Detectors/TPC/src

1 file changed

+21
-6
lines changed

DataFormats/Detectors/TPC/src/DCS.cxx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,27 @@ void fillBuffer(std::pair<std::vector<float>, std::vector<TimeStampType>>& buffe
329329
}
330330
}
331331

332-
std::pair<std::vector<float>, std::vector<TimeStampType>> buffTmp{
333-
std::vector<float>(buffer.first.begin() + idxStartBuffer, buffer.first.end()),
334-
std::vector<TimeStampType>(buffer.second.begin() + idxStartBuffer, buffer.second.end())};
335-
336-
buffTmp.first.insert(buffTmp.first.end(), values.first.begin(), values.first.end());
337-
buffTmp.second.insert(buffTmp.second.end(), values.second.begin(), values.second.end());
332+
std::pair<std::vector<float>, std::vector<TimeStampType>> buffTmp;
333+
auto& [buffVals, buffTimes] = buffTmp;
334+
335+
// Preallocate enough capacity to avoid reallocations
336+
buffVals.reserve(buffer.first.size() - idxStartBuffer + values.first.size());
337+
buffTimes.reserve(buffer.second.size() - idxStartBuffer + values.second.size());
338+
// Insert the kept part of the old buffer
339+
buffVals.insert(buffVals.end(), buffer.first.begin() + idxStartBuffer, buffer.first.end());
340+
buffTimes.insert(buffTimes.end(), buffer.second.begin() + idxStartBuffer, buffer.second.end());
341+
// Insert the new values
342+
buffVals.insert(buffVals.end(), values.first.begin(), values.first.end());
343+
buffTimes.insert(buffTimes.end(), values.second.begin(), values.second.end());
344+
345+
// this should not happen
346+
if (!std::is_sorted(buffTimes.begin(), buffTimes.end())) {
347+
LOGP(info, "Pressure buffer not sorted after filling - sorting it");
348+
std::vector<size_t> idx(buffTimes.size());
349+
o2::math_utils::SortData(buffTimes, idx);
350+
o2::math_utils::Reorder(buffVals, idx);
351+
o2::math_utils::Reorder(buffTimes, idx);
352+
}
338353

339354
buffer = std::move(buffTmp);
340355
}

0 commit comments

Comments
 (0)