20#include <fmt/format.h>
36 return (raw >> 15) ? mag : -mag;
39uint16_t CMVPerTF::quantizeBelowThreshold(
uint16_t raw,
float quantizationMean,
float quantizationSigma)
45 if (quantizationSigma <= 0.f) {
49 const float adc = (
raw & 0x7FFFu) / 128.f;
50 const float distance = (
adc - quantizationMean) / quantizationSigma;
54 float quantizedADC =
adc;
55 if (lossStrength > 0.85f) {
56 quantizedADC = std::round(
adc * 10.f) / 10.f;
57 }
else if (lossStrength > 0.60f) {
58 quantizedADC = std::round(
adc * 100.f) / 100.f;
59 }
else if (lossStrength > 0.30f) {
60 quantizedADC = std::round(
adc * 1000.f) / 1000.f;
61 }
else if (lossStrength > 0.12f) {
62 quantizedADC = std::round(
adc * 10000.f) / 10000.f;
63 }
else if (lossStrength > 0.03f) {
64 quantizedADC = std::round(
adc * 1000000.f) / 1000000.f;
68 const uint16_t quantizedMagnitude =
static_cast<uint16_t>(std::clamp(std::lround(quantizedADC * 128.f), 0l, 0x7FFFl));
69 return static_cast<uint16_t>((
raw & 0x8000u) | quantizedMagnitude);
77void CMVPerTF::encodeVarintInto(
uint32_t value, std::vector<uint8_t>& out)
79 while (
value > 0x7F) {
80 out.push_back(
static_cast<uint8_t>((
value & 0x7F) | 0x80));
99 return static_cast<uint16_t>((
val >= 0 ? 0x8000u : 0u) | mag);
112 throw std::runtime_error(
"decodeVarintLocal: unexpected end of varint data");
125void huffmanEncode(
const std::vector<uint32_t>& symbols, std::vector<uint8_t>&
buf)
127 if (symbols.empty()) {
130 for (
int i = 0;
i < 12; ++
i) {
137 std::map<uint32_t, uint64_t> freq;
149 std::vector<HNode>
nodes;
150 nodes.reserve(freq.size() * 2);
151 for (
const auto& [sym,
f] : freq) {
152 nodes.push_back({
f, sym, -1, -1,
true});
155 auto cmp = [&](
int a,
int b) {
158 std::vector<int> heap;
159 heap.reserve(
nodes.size());
160 for (
int i = 0; i < static_cast<int>(
nodes.size()); ++
i) {
163 std::make_heap(heap.begin(), heap.end(),
cmp);
165 while (heap.size() > 1) {
166 std::pop_heap(heap.begin(), heap.end(),
cmp);
167 const int a = heap.back();
169 std::pop_heap(heap.begin(), heap.end(),
cmp);
170 const int b = heap.back();
173 heap.push_back(
static_cast<int>(
nodes.size()) - 1);
174 std::push_heap(heap.begin(), heap.end(),
cmp);
178 std::map<uint32_t, uint8_t> codeLens;
180 const int root = heap[0];
181 std::vector<std::pair<int, int>>
stack;
182 stack.push_back({root, 0});
183 while (!
stack.empty()) {
186 if (
nodes[idx].isLeaf) {
200 std::vector<SymLen> symLens;
201 symLens.reserve(codeLens.size());
202 for (
const auto& [sym,
len] : codeLens) {
203 symLens.push_back({sym,
len});
205 std::sort(symLens.begin(), symLens.end(), [](
const SymLen&
a,
const SymLen&
b) {
206 return a.len != b.len ? a.len < b.len : a.sym < b.sym;
210 std::map<uint32_t, std::pair<uint32_t, uint8_t>> codeTable;
214 for (
const auto& sl : symLens) {
216 code = (code + 1) << (sl.len - prevLen);
218 codeTable[sl.sym] = {code, sl.len};
224 buf.reserve(
buf.size() + 4 + symLens.size() * 5 + 8 + (symbols.size() / 8 + 1));
226 for (
int i = 0;
i < 4; ++
i) {
227 buf.push_back(
static_cast<uint8_t>((numSym >> (8 *
i)) & 0xFF));
229 for (
const auto& sl : symLens) {
230 for (
int i = 0;
i < 4; ++
i) {
231 buf.push_back(
static_cast<uint8_t>((sl.sym >> (8 *
i)) & 0xFF));
233 buf.push_back(sl.len);
237 const size_t totalBitsOffset =
buf.size();
238 for (
int i = 0;
i < 8; ++
i) {
247 const auto& [code,
len] = codeTable.at(
z);
248 for (
int b =
static_cast<int>(
len) - 1;
b >= 0; --
b) {
249 curByte =
static_cast<uint8_t>(curByte | (((code >>
b) & 1u) << (7 - bitsInByte)));
252 if (bitsInByte == 8) {
253 buf.push_back(curByte);
259 if (bitsInByte > 0) {
260 buf.push_back(curByte);
264 for (
int i = 0;
i < 8; ++
i) {
265 buf[totalBitsOffset +
i] =
static_cast<uint8_t>((totalBits >> (8 *
i)) & 0xFF);
276 throw std::runtime_error(
"huffmanDecode: unexpected end reading uint32");
289 std::vector<SymLen> symLens(numSym);
291 symLens[
i].sym = readU32();
293 throw std::runtime_error(
"huffmanDecode: unexpected end reading code length");
295 symLens[
i].len = *
ptr++;
298 std::map<uint8_t, uint32_t> firstCode;
299 std::map<uint8_t, std::vector<uint32_t>> symsByLen;
303 for (
const auto& sl : symLens) {
305 code = (code + 1) << (sl.len - prevLen);
307 if (!firstCode.count(sl.len)) {
308 firstCode[sl.len] = code;
310 symsByLen[sl.len].push_back(sl.sym);
316 throw std::runtime_error(
"huffmanDecode: unexpected end reading totalBits");
319 for (
int i = 0;
i < 8; ++
i) {
324 const uint8_t minLen = symLens.empty() ? 1 : symLens.front().len;
325 const uint8_t maxLen = symLens.empty() ? 1 : symLens.back().len;
330 auto nextBit = [&]() ->
int {
333 throw std::runtime_error(
"huffmanDecode: unexpected end of bitstream");
338 const int bit = (curByte >> bitPos) & 1;
343 std::vector<uint32_t> out;
345 while (out.size() < N) {
348 for (
uint8_t curLen = 1; curLen <= maxLen; ++curLen) {
349 if (bitsRead >= totalBits) {
350 throw std::runtime_error(
"huffmanDecode: bitstream exhausted before all symbols decoded");
352 accum = (accum << 1) | static_cast<uint32_t>(nextBit());
354 if (curLen < minLen) {
357 const auto fcIt = firstCode.find(curLen);
358 if (fcIt == firstCode.end()) {
361 if (accum >= fcIt->second) {
363 const auto& sv = symsByLen.at(curLen);
364 if (idx < sv.size()) {
365 out.push_back(sv[idx]);
372 throw std::runtime_error(
"huffmanDecode: invalid Huffman code in bitstream");
384 if (cru < 0 || cru >=
static_cast<int>(
CRU::MaxCRU)) {
385 throw std::out_of_range(fmt::format(
"CMVPerTF::getCMV: cru {} out of range [0, {})", cru,
static_cast<int>(
CRU::MaxCRU)));
387 if (timeBin < 0 ||
static_cast<uint32_t>(timeBin) >= cmv::NTimeBinsPerTF) {
388 throw std::out_of_range(fmt::format(
"CMVPerTF::getCMV: timeBin {} out of range [0, {})", timeBin,
static_cast<int>(cmv::NTimeBinsPerTF)));
390 return mDataPerTF[cru * cmv::NTimeBinsPerTF + timeBin];
400 const bool positive = (
raw >> 15) & 1;
401 return positive ? mag / 128.f : -mag / 128.f;
406 if (threshold <= 0.f) {
410 const float mag = (
mDataPerTF[
i] & 0x7FFF) / 128.f;
411 if (mag < threshold) {
419 if (threshold == 0) {
428 if (rounded > threshold) {
457 std::vector<uint8_t> posStream;
458 std::vector<uint16_t> rawValues;
460 for (
int cru = 0; cru < static_cast<int>(
CRU::MaxCRU); ++cru) {
466 for (
uint32_t tb = 0; tb < cmv::NTimeBinsPerTF; ++tb) {
477 encodeVarintInto(
first ?
e.tb : (
e.tb - prevTB), posStream);
478 rawValues.push_back(
e.val);
485 std::vector<uint8_t> valStream;
487 std::vector<uint32_t> zigzags;
488 zigzags.reserve(rawValues.size());
490 zigzags.push_back(zigzagEncode(cmvToSigned(
v)));
493 huffmanEncode(zigzags, valStream);
496 encodeVarintInto(
z, valStream);
502 valStream.push_back(
static_cast<uint8_t>(
v & 0xFF));
503 valStream.push_back(
static_cast<uint8_t>(
v >> 8));
509 out.
mData.reserve(4 + posStream.size() + valStream.size());
510 for (
int i = 0;
i < 4; ++
i) {
511 out.
mData.push_back(
static_cast<uint8_t>((posStreamSize >> (8 *
i)) & 0xFF));
513 out.
mData.insert(out.
mData.end(), posStream.begin(), posStream.end());
514 out.
mData.insert(out.
mData.end(), valStream.begin(), valStream.end());
522 out.
mData.reserve(total * 2);
530 std::vector<uint32_t> zigzags;
531 zigzags.reserve(total);
532 for (
int cru = 0; cru < static_cast<int>(
CRU::MaxCRU); ++cru) {
534 for (
uint32_t tb = 0; tb < cmv::NTimeBinsPerTF; ++tb) {
540 zigzags.push_back(zigzagEncode(encoded));
545 huffmanEncode(zigzags, out.
mData);
548 encodeVarintInto(
z, out.
mData);
559std::vector<std::pair<int, uint32_t>> CMVPerTFCompressed::decodeSparsePositions(
const uint8_t*&
ptr,
const uint8_t*
end)
563 throw std::runtime_error(
"CMVPerTFCompressed::decompress: truncated position header");
571 throw std::runtime_error(
"CMVPerTFCompressed::decompress: posStream overflows payload");
575 std::vector<std::pair<int, uint32_t>> positions;
577 for (
int cru = 0; cru < static_cast<int>(
CRU::MaxCRU); ++cru) {
582 const uint32_t delta = decodeVarintLocal(p, posEnd);
583 tb =
first ? delta : (tb + delta);
585 positions.emplace_back(cru, tb);
596 return huffmanDecode(
ptr,
end, N);
601 std::vector<uint32_t> out;
604 out.push_back(decodeVarintLocal(
ptr,
end));
610 std::vector<uint32_t> out;
614 throw std::runtime_error(
"CMVPerTFCompressed::decompress: unexpected end in raw value stream");
623void CMVPerTFCompressed::decodeSparseValues(
const std::vector<uint32_t>& symbols,
624 const std::vector<std::pair<int, uint32_t>>& positions,
628 for (
uint32_t i = 0; i < static_cast<uint32_t>(positions.size()); ++
i) {
631 raw = signedToCmvLocal(zigzagDecodeLocal(symbols[
i]));
635 cmv->mDataPerTF[positions[
i].first * cmv::NTimeBinsPerTF + positions[
i].second] =
raw;
639void CMVPerTFCompressed::decodeDenseValues(
const std::vector<uint32_t>& symbols,
uint8_t flags, CMVPerTF* cmv)
646 for (
uint32_t i = 0; i < static_cast<uint32_t>(symbols.size()); ++
i) {
647 cmv->mDataPerTF[
i] =
static_cast<uint16_t>(symbols[
i]);
654 for (
int cru = 0; cru < static_cast<int>(
CRU::MaxCRU); ++cru) {
656 for (
uint32_t tb = 0; tb < cmv::NTimeBinsPerTF; ++tb, ++
s) {
662 cmv->mDataPerTF[
s] = signedToCmvLocal(
val);
670 throw std::invalid_argument(
"CMVPerTFCompressed::decompress: cmv pointer is null");
681 auto positions = decodeSparsePositions(
ptr,
end);
688 decodeSparseValues(symbols, positions,
mFlags, cmv);
696 decodeDenseValues(symbols,
mFlags, cmv);
702 auto tree = std::make_unique<TTree>(
"ccdb_object",
"ccdb_object");
703 tree->SetAutoSave(0);
704 tree->SetDirectory(
nullptr);
707 tree->Branch(
"CMVPerTF", &
ptr);
710 tree->ResetBranchAddresses();
716 auto tree = std::make_unique<TTree>(
"ccdb_object",
"ccdb_object");
717 tree->SetAutoSave(0);
718 tree->SetDirectory(
nullptr);
721 tree->Branch(
"CMVPerTFCompressed", &
ptr);
724 tree->ResetBranchAddresses();
732 throw std::runtime_error(fmt::format(
"CMVPerTF::writeToFile: cannot open '{}'",
filename));
Structs for storing CMVs to the CCDB.
Common mode values data format definition.
unsigned short int uint16_t
std::vector< long > entries
o2::raw::RawFileWriter * raw
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error s
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char Directory with events to Events directory is and not inside events Show standalone event Run event generator maximum time bin of continuous for triggered for automatic continuous for automatic continuous triggered uint64_t
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char e
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by when above this local occupancy rowx Shift of TPC revert cluster flag edge bit distance to edge exceeds this ZOffset for secondary multiple of radius max P of electron K BB bands crossing max P of electron p BB bands crossing max P of electron d BB bands crossing max P of electron t BB bands crossing min accepted dEdxMaxTPC of the track Additive extra cluster error for Y2 if edge flag set Additive extra cluster error for Y2 if single set Additive extra cluster error for Y2 if or single set Additive extra cluster error for Z2 if or single set Extra errors to add to track when crossing depending on addErrorsCECrossing Inner portion of y range in sector that is not used in searching for extrapolated track candidates factor2YS for track merging factor2K for track merging Margin in cm of Y position when rejecting edge clusters based on uncorrected track Y Max fraction of shared clusters for tracklet Except pad from cluster finding if total number of charges in a fragment is above this Except pad from cluster finding if number of consecutive charges in a fragment is above this Threshold where a timebin is considered disabling the noisy pad check for that pad Treat signal above this minimum as saturated Smoothing factor for the exponential Highly Ionising Particle tail filter Number of timebins to use for the averaging Add additional custom track errors when crossing relative as fraction of Do N fit passes in final fit of< 0:reject at max -n hits") AddOptionRTC(rejectIFCLowRadiusCluster, uint8_t, 1, "", 0, "Reject clusters that get the IFC mask error during refit") AddOptionRTC(dEdxTruncLow, uint8_t, 2, "", 0, "Low truncation threshold, fraction of 128") AddOptionRTC(dEdxTruncHigh, uint8_t, 77, "", 0, "High truncation threshold, fraction of 128") AddOptionRTC(extrapolationTracking, int8_t, 1, "", 0, "Enable Extrapolation Tracking(prolong tracks to adjacent sectors to find short segments)") AddOptionRTC(disableRefitAttachment, uint8_t, 0, "", 0, "Bitmask to disable certain attachment steps during refit(1:attachment, 2:propagation, 4:loop following, 8:mirroring)") AddOptionRTC(rejectionStrategy, uint8_t, o2::gpu::GPUSettings::RejectionStrategyA, "", 0, "Enable rejection of TPC clusters for compression(0=no, 1=strategy A, 2=strategy B)") AddOptionRTC(mergeLoopersAfterburner, uint8_t, 1, "", 0, "Run afterburner for additional looper merging") AddOptionRTC(compressionTypeMask, uint8_t, o2::gpu::GPUSettings::CompressionFull, "", 0, "TPC Compression mode bits(1=truncate charge/width LSB, 2=differences, 4=track-model)") AddOptionRTC(compressionSortOrder, uint8_t, o2::gpu::GPUSettings::SortTime, "", 0, "Sort order of TPC compression(0=time, 1=pad, 2=Z-time-pad, 3=Z-pad-time, 4=no sorting(use incoming order))") AddOptionRTC(sigBitsCharge, uint8_t, 4, "", 0, "Number of significant bits for TPC cluster charge in compression mode 1") AddOptionRTC(sigBitsWidth, uint8_t, 3, "", 0, "Number of significant bits for TPC cluster width in compression mode 1") AddOptionRTC(dropLoopers, uint8_t, 0, "", 0, "Drop looping tracks starting from second loop") AddOptionRTC(mergerInterpolateErrors, uint8_t, 1, "", 0, "Use interpolation instead of extrapolation for chi2 based cluster rejection") AddOptionRTC(mergerInterpolateRejectAlsoOnCurrentPosition, uint8_t, 1, "", 0, "When using mergerInterpolateErrors, reject based on chi2 twice computed with interpolated and current track position starting from NDF > mergerNonInterpolateRejectMinNDF Merge tracks accross the central electrode Enable PID response Add sub threshold clusters in TPC dEdx computation OR mask of TPC flags that will reject the cluster in alternative dEdx Reject edge clusters based on uncorrected track Y during track fit Increase decrease removal by when on this number of pads from the edge h configStandalone Reconstruction rec_trd Max chi2 for TRD tracklets to be matched to a track Chi2 cut for strict matching mode Number of sigmas for ITS TPC track time error estimate Addition to search road around track prolongation along Y in cm Optimal tracklet rphi resolution in maximum chi2 per attached tracklet for TRD tracks take into account the full likelihood of the slope including exponential tails Tracks with less attached tracklets are discarded after the tracking Post trigger Pile up integration time in BCs fits all iterations but the last one in projections Use material lookup table for TPC refit Apply workarounds to avoid cyclic merge should not be needed tpc dyn configStandalone Processing proc_rtc Replace constant variables static by constexpr expressions Compile RTC in deterministic with NO_FAST_MATH flags and GPUCA_DETERMINISTIC_MODE define Use RTC to optimize GPU code h configStandalone Processing proc_rtctech Use a file lock to serialize access to the cache folder int32_t
GLboolean GLboolean GLboolean b
GLsizei GLsizei GLfloat distance
GLsizei const GLfloat * value
GLint GLint GLsizei GLsizei GLsizei depth
GLenum GLenum GLsizei len
GLboolean GLboolean GLboolean GLboolean a
GLenum GLuint GLenum GLsizei const GLchar * buf
GLdouble GLdouble GLdouble z
Global TPC definitions and constants.
static constexpr uint8_t kDelta
Delta coding between consecutive values (dense only)
static constexpr uint8_t kVarint
Varint compression of the value stream.
static constexpr uint8_t kHuffman
Canonical Huffman compression of the value stream.
static constexpr uint8_t kSparse
Non-zero positions stored sparsely (varint-encoded deltas)
static constexpr uint8_t kZigzag
Zigzag encoding of deltas or signed values.
std::unique_ptr< TTree > toTTree() const
Serialise into a TTree; each Fill() call appends one entry (one TF)
void decompress(CMVPerTF *cmv) const
Restore a CMVPerTF from this compressed object into *cmv (must not be null)
std::vector< uint8_t > mData
Encoded payload.
uint32_t firstOrbitDPL
First orbit of this TF.
uint8_t mFlags
Bitmask of CMVEncoding values.
uint32_t firstOrbit
First orbit of this TF.
uint32_t firstOrbit
First orbit of this TF, from heartbeatOrbit of the first CMV packet.
void zeroSmallValues(float threshold=1.0f)
Zero out raw CMV values whose float magnitude is below threshold.
CMVPerTFCompressed compress(uint8_t flags) const
static void writeToFile(const std::string &filename, const std::unique_ptr< TTree > &tree)
Write the TTree to a ROOT file.
uint16_t mDataPerTF[CRU::MaxCRU *cmv::NTimeBinsPerTF]
void roundToIntegers(uint16_t threshold)
Round values to the nearest integer ADC for all values whose rounded magnitude is <= threshold.
void trimGaussianPrecision(float mean, float sigma)
std::unique_ptr< TTree > toTTree() const
Serialise into a TTree; each Fill() call appends one entry (one TF)
uint32_t firstOrbitDPL
First orbit of this TF, from DPL.
uint16_t getCMV(const int cru, const int timeBin) const
Return the raw 16-bit CMV value for a given CRU and timebin within this TF.
float getCMVFloat(const int cru, const int timeBin) const
Return the float CMV value for a given CRU and timebin within this TF.
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))
char const *restrict const cmp