Project
Loading...
Searching...
No Matches
Decoder.cxx
Go to the documentation of this file.
1// Copyright 2019-2026 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include "AODNative/Decoder.h"
13#include "AODNative/Codecs.h"
14#include "AODNative/Errors.h"
15#include "Endian.h"
16#include "Cursor.h"
17#include <atomic>
18#include <bit>
19#include <climits>
20#include <cstring>
21#include <exception>
22#include <mutex>
23#include <optional>
24#include <thread>
25
26namespace o2::aod::native
27{
28
29namespace
30{
31
32template <typename T>
33void byteswapAll(uint8_t* data, size_t count)
34{
35 for (size_t i = 0; i < count; ++i) {
36 T v;
37 std::memcpy(&v, data + i * sizeof(T), sizeof(T));
38 v = bswap(v);
39 std::memcpy(data + i * sizeof(T), &v, sizeof(T));
40 }
41}
42} // namespace
43
44void byteswapInPlace(uint8_t* data, size_t count, size_t typeSize)
45{
46 if constexpr (std::endian::native == std::endian::big) {
47 return;
48 }
49 switch (typeSize) {
50 case 2:
51 byteswapAll<uint16_t>(data, count);
52 break;
53 case 4:
54 byteswapAll<uint32_t>(data, count);
55 break;
56 case 8:
57 byteswapAll<uint64_t>(data, count);
58 break;
59 }
60}
61
65 std::span<const uint8_t> plain;
66 std::optional<PayloadDecompressor> compressed;
67 size_t border = 0;
68 size_t consumed = 0;
69
70 void read(uint8_t* dst, size_t n)
71 {
72 if (compressed) {
73 compressed->read(dst, n);
74 } else {
75 std::memcpy(dst, plain.data() + consumed, n);
76 }
77 consumed += n;
78 }
79
81 void readAll(uint8_t* dst, size_t n)
82 {
83 if (compressed) {
84 compressed->readPrefix(dst, n);
85 consumed += n;
86 } else {
87 read(dst, n);
88 }
89 }
90};
91
93 : mSource(source), mOptions(options), mUrl(source.url())
94{
95}
96
97void BranchDecoder::checkBaskets(BranchInfo const& branch) const
98{
99 int64_t next = 0;
100 for (auto& b : branch.baskets) {
101 if (b.firstEntry != next || b.nEntries < 0) {
102 throw CorruptData(mUrl, b.seek, "baskets of branch " + branch.name + " do not cover its entries contiguously");
103 }
104 next += b.nEntries;
105 }
106 if (next != branch.entries) {
107 throw CorruptData(mUrl, 0, "baskets of branch " + branch.name + " hold " + std::to_string(next) + " entries, but the branch has " + std::to_string(branch.entries));
108 }
109}
110
111template <typename F>
112void BranchDecoder::forEachBasket(BranchInfo const& branch, bool parallel, F&& fn)
113{
114 checkBaskets(branch);
115 auto task = [this, &branch, &fn](size_t i) {
116 auto const& ref = branch.baskets[i];
117 BasketValues values;
118 std::vector<uint8_t> storage; // only when the source cannot be mapped
119 if (ref.embedded) {
120 values.plain = ref.embeddedPayload;
121 values.border = ref.embeddedPayload.size();
122 } else {
123 auto bytes = mSource.tryMap(ref.seek, ref.nbytes);
124 if (bytes.empty()) {
125 if (ref.seek > mSource.size() || ref.nbytes > mSource.size() - ref.seek) {
126 throw CorruptData(mUrl, ref.seek, "basket of branch " + branch.name + " past the end of the file");
127 }
128 storage.resize(ref.nbytes);
129 mSource.readInto(ref.seek, ref.nbytes, storage.data());
130 bytes = storage;
131 }
132 Cursor c{bytes, mUrl, ref.seek};
133 auto header = readBasketHeader(c);
134 auto nbytes = header.nbytes;
135 auto objlen = header.objlen;
136 auto keylen = header.keylen;
137 auto last = header.last;
138 if (nbytes != ref.nbytes || keylen > nbytes || header.nevbuf != ref.nEntries || last < keylen || (uint32_t)(last - keylen) > objlen) {
139 c.fail("basket header of branch " + branch.name + " does not match its TBranch");
140 }
141 values.border = last - keylen;
142 auto payload = bytes.subspan(keylen);
143 if (objlen != nbytes - keylen) {
144 values.compressed.emplace(payload, objlen, mUrl, ref.seek + keylen);
145 } else {
146 values.plain = payload;
147 }
148 }
149 fn(i, values);
150 };
151
152 auto n = branch.baskets.size();
153 int threads = parallel ? std::min<int>(mOptions.threads, n) : 1;
154 if (threads <= 1) {
155 for (size_t i = 0; i < n; ++i) {
156 task(i);
157 }
158 return;
159 }
160 // Baskets write disjoint slices of the destination, so no locking is needed.
161 std::atomic<size_t> next = 0;
162 std::exception_ptr error;
163 std::mutex errorMutex;
164 std::vector<std::thread> workers;
165 for (int t = 0; t < threads; ++t) {
166 workers.emplace_back([&] {
167 for (size_t i; (i = next.fetch_add(1)) < n;) {
168 try {
169 task(i);
170 } catch (...) {
171 std::lock_guard lock{errorMutex};
172 if (!error) {
173 error = std::current_exception();
174 }
175 next = n;
176 }
177 }
178 });
179 }
180 for (auto& w : workers) {
181 w.join();
182 }
183 if (error) {
184 std::rethrow_exception(error);
185 }
186}
187
188namespace
189{
190void checkNeeded(std::string const& url, BranchInfo const& branch, BasketRef const& ref, size_t needed, size_t border)
191{
192 if (needed > border) {
193 throw CorruptData(url, ref.seek, "basket of branch " + branch.name + " starting from " + std::to_string(ref.firstEntry) + " holds " + std::to_string(border) + " bytes, but " + std::to_string(needed) + " are needed");
194 }
195}
196} // namespace
197
198void BranchDecoder::decodeValues(BranchInfo const& branch, uint8_t* dst, size_t nbytes)
199{
200 auto elementSize = leafElementSize(branch.leafClass);
201 size_t rowBytes = elementSize * branch.fixedLen;
202 if (nbytes != branch.entries * rowBytes) {
203 throw FormatError("destination of " + std::to_string(nbytes) + " bytes for branch " + branch.name + " which needs " + std::to_string(branch.entries * rowBytes));
204 }
205 forEachBasket(branch, true, [&](size_t i, BasketValues& values) {
206 auto const& ref = branch.baskets[i];
207 size_t needed = ref.nEntries * rowBytes;
208 checkNeeded(mUrl, branch, ref, needed, values.border);
209 auto* out = dst + ref.firstEntry * rowBytes;
210 values.readAll(out, needed);
211 byteswapInPlace(out, needed / elementSize, elementSize);
212 });
213}
214
215void BranchDecoder::decodeVLAValues(BranchInfo const& branch, std::span<const int32_t> offsets, uint8_t* dst, size_t nbytes)
216{
217 auto elementSize = leafElementSize(branch.leafClass);
218 if (offsets.size() != (size_t)branch.entries + 1) {
219 throw FormatError("branch " + branch.name + " has " + std::to_string(branch.entries) + " entries, but " + std::to_string(offsets.size()) + " offsets were given");
220 }
221 if (nbytes != (size_t)offsets.back() * elementSize) {
222 throw FormatError("destination of " + std::to_string(nbytes) + " bytes for branch " + branch.name + " which needs " + std::to_string(offsets.back() * elementSize));
223 }
224 forEachBasket(branch, true, [&](size_t i, BasketValues& values) {
225 auto const& ref = branch.baskets[i];
226 auto begin = offsets[ref.firstEntry];
227 auto end = offsets[ref.firstEntry + ref.nEntries];
228 if (end < begin) {
229 throw CorruptData(mUrl, ref.seek, "invalid offset range for branch " + branch.name);
230 }
231 size_t needed = (size_t)(end - begin) * elementSize;
232 checkNeeded(mUrl, branch, ref, needed, values.border);
233 auto* out = dst + (size_t)begin * elementSize;
234 values.readAll(out, needed);
235 byteswapInPlace(out, needed / elementSize, elementSize);
236 });
237}
238
239void BranchDecoder::decodeBooleans(BranchInfo const& branch, uint8_t* bitmap, size_t nbytes)
240{
241 size_t total = branch.entries * branch.fixedLen;
242 if (nbytes * 8 < total) {
243 throw FormatError("bitmap of " + std::to_string(nbytes) + " bytes for " + std::to_string(total) + " booleans of branch " + branch.name);
244 }
245 std::memset(bitmap, 0, nbytes);
246 std::vector<uint8_t> window(std::min(kBooleanWindow, total));
247 // The value index is global across baskets, so a basket boundary can fall
248 // inside a byte of the bitmap: decode serially.
249 size_t index = 0;
250 forEachBasket(branch, false, [&](size_t i, BasketValues& values) {
251 auto const& ref = branch.baskets[i];
252 size_t remaining = (size_t)ref.nEntries * branch.fixedLen;
253 checkNeeded(mUrl, branch, ref, remaining, values.border);
254 while (remaining > 0) {
255 auto k = std::min(remaining, window.size());
256 values.read(window.data(), k);
257 for (size_t j = 0; j < k; ++j, ++index) {
258 bitmap[index / 8] |= (uint8_t)((window[j] != 0) << (index % 8));
259 }
260 remaining -= k;
261 }
262 });
263}
264
265std::vector<int32_t> BranchDecoder::decodeOffsets(BranchInfo const& sizeBranch)
266{
267 if (sizeBranch.leafClass != 'I' || sizeBranch.fixedLen != 1) {
268 throw UnsupportedLayout("size branch " + sizeBranch.name + " is not a scalar 32-bit integer");
269 }
270 // fEntries sizes the allocation: check it against the baskets first.
271 checkBaskets(sizeBranch);
272 // Decode the sizes after the leading 0, then prefix-sum in place.
273 std::vector<int32_t> offsets(sizeBranch.entries + 1);
274 decodeValues(sizeBranch, (uint8_t*)(offsets.data() + 1), sizeBranch.entries * sizeof(int32_t));
275 int64_t total = 0;
276 offsets[0] = 0;
277 for (int64_t i = 1; i <= sizeBranch.entries; ++i) {
278 total += (uint32_t)offsets[i];
279 // Arrow lists use 32 bit offsets, a larger total can only come from corrupted sizes.
280 if (total > INT32_MAX) {
281 throw CorruptData(mUrl, 0, "invalid sizes for branch " + sizeBranch.name + ": offsets overflow at entry " + std::to_string(i - 1));
282 }
283 offsets[i] = (int32_t)total;
284 }
285 return offsets;
286}
287
288} // namespace o2::aod::native
std::string url
unsigned char uint8_t
unsigned int uint32_t
std::vector< std::string > header
int32_t i
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
char error[CURL_ERROR_SIZE]
uint32_t j
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
size_t bytes
void decodeVLAValues(BranchInfo const &branch, std::span< const int32_t > offsets, uint8_t *dst, size_t nbytes)
Definition Decoder.cxx:215
std::vector< int32_t > decodeOffsets(BranchInfo const &sizeBranch)
Decodes a <col>_size branch into entries + 1 int32 offsets.
Definition Decoder.cxx:265
void decodeValues(BranchInfo const &branch, uint8_t *dst, size_t nbytes)
Definition Decoder.cxx:198
BranchDecoder(ByteSource &source, Options const &options)
Definition Decoder.cxx:92
static constexpr size_t kBooleanWindow
Size of the boolean window, the only decode-side scratch memory.
Definition Decoder.h:35
void decodeBooleans(BranchInfo const &branch, uint8_t *bitmap, size_t nbytes)
Definition Decoder.cxx:239
virtual uint64_t size() const =0
virtual void readInto(uint64_t off, size_t len, void *dst)=0
Throws IoError, or CorruptData when the range is past the end of the file.
virtual std::span< const uint8_t > tryMap(uint64_t off, size_t len)
A view of the bytes without copying them, if the backend can provide it.
Definition ByteSource.h:88
GLint GLint GLsizei GLint border
Definition glcorearb.h:275
GLdouble n
Definition glcorearb.h:1982
GLint GLsizei count
Definition glcorearb.h:399
GLuint GLsizei const GLuint const GLintptr * offsets
Definition glcorearb.h:2595
GLuint GLuint end
Definition glcorearb.h:469
const GLdouble * v
Definition glcorearb.h:832
GLuint index
Definition glcorearb.h:781
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
GLenum GLenum dst
Definition glcorearb.h:1767
GLenum GLsizei GLsizei GLint * values
Definition glcorearb.h:1576
GLint ref
Definition glcorearb.h:291
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
BasketHeader readBasketHeader(Cursor &c)
Definition Cursor.h:144
size_t leafElementSize(char leafClass)
Size in bytes of one value of a leaf class on disk.
Definition Objects.cxx:493
constexpr T bswap(T v)
Definition Endian.h:22
void byteswapInPlace(uint8_t *data, size_t count, size_t typeSize)
Byteswaps count big-endian values of typeSize bytes in place.
Definition Decoder.cxx:44
WorkflowSpec parallel(DataProcessorSpec original, size_t maxIndex, std::function< void(DataProcessorSpec &, size_t id)> amendCallback)
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
void readAll(uint8_t *dst, size_t n)
The first n value bytes as the only read: lets zstd decode in place.
Definition Decoder.cxx:81
void read(uint8_t *dst, size_t n)
Definition Decoder.cxx:70
std::optional< PayloadDecompressor > compressed
Definition Decoder.cxx:66
std::vector< BasketRef > baskets
Definition Objects.h:46