Skip to content

Commit 4de1f56

Browse files
authored
Merge a8b53cc into sapling-pr-archive-ktf
2 parents 66b70d6 + a8b53cc commit 4de1f56

22 files changed

Lines changed: 479 additions & 296 deletions

File tree

DataFormats/Detectors/TPC/include/DataFormatsTPC/ClusterNative.h

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef ALICEO2_DATAFORMATSTPC_CLUSTERNATIVE_H
1616
#define ALICEO2_DATAFORMATSTPC_CLUSTERNATIVE_H
1717
#ifndef GPUCA_GPUCODE_DEVICE
18+
#include <climits>
1819
#include <cstdint>
1920
#include <cstddef> // for size_t
2021
#include <utility>
@@ -62,6 +63,9 @@ struct ClusterNative {
6263
static constexpr int scalePadPacked = 64; //< ~60 is needed for 0.1mm precision, but power of two avoids rounding
6364
static constexpr int scaleSigmaTimePacked = 32; // 1/32nd of pad/timebin precision for cluster size
6465
static constexpr int scaleSigmaPadPacked = 32;
66+
static constexpr int scaleSaturatedQtot = 8;
67+
static constexpr int maxRegularQtot = 25 * 1024;
68+
static constexpr int maxSaturatedQtot = (USHRT_MAX - maxRegularQtot) * scaleSaturatedQtot;
6569

6670
uint32_t timeFlagsPacked; //< Contains the time in the lower 24 bits in a packed format, contains the flags in the
6771
// upper 8 bits
@@ -83,7 +87,14 @@ struct ClusterNative {
8387
}
8488

8589
GPUd() uint16_t getQmax() const { return qMax; }
86-
GPUd() uint16_t getQtot() const { return qTot; }
90+
GPUd() uint16_t getQtot() const
91+
{
92+
if (isSaturated()) [[unlikely]] {
93+
auto sQtot = getSaturatedQtot();
94+
return sQtot < USHRT_MAX ? sQtot : USHRT_MAX;
95+
}
96+
return qTot;
97+
}
8798
GPUd() uint8_t getFlags() const { return timeFlagsPacked >> 24; }
8899
GPUd() uint32_t getTimePacked() const { return timeFlagsPacked & 0xFFFFFF; }
89100
GPUd() void setTimePackedFlags(uint32_t timePacked, uint8_t flags)
@@ -119,7 +130,13 @@ struct ClusterNative {
119130
/// Y = (12.4 - 0.5 * (66 - 1)) * 4.16mm = -83.616mm
120131
GPUd() float getPad() const { return unpackPad(padPacked); }
121132
GPUd() void setPad(float pad) { padPacked = packPad(pad); }
122-
GPUd() float getSigmaTime() const { return float(sigmaTimePacked) * (1.f / scaleSigmaTimePacked); }
133+
GPUd() float getSigmaTime() const
134+
{
135+
if (isSaturated()) [[unlikely]] {
136+
return 0;
137+
}
138+
return float(sigmaTimePacked) * (1.f / scaleSigmaTimePacked);
139+
}
123140
GPUd() void setSigmaTime(float sigmaTime)
124141
{
125142
uint32_t tmp = sigmaTime * scaleSigmaTimePacked + 0.5;
@@ -138,6 +155,31 @@ struct ClusterNative {
138155
sigmaPadPacked = tmp;
139156
}
140157

158+
GPUd() bool isSaturated() const { return qTot > maxRegularQtot; }
159+
160+
GPUd() void setSaturatedQtot(uint32_t qtot)
161+
{
162+
this->qTot = USHRT_MAX;
163+
if (qtot < maxSaturatedQtot) {
164+
this->qTot = ((qtot + scaleSaturatedQtot / 2) / scaleSaturatedQtot) + maxRegularQtot;
165+
}
166+
}
167+
168+
GPUd() uint32_t getSaturatedQtot() const
169+
{
170+
return uint32_t(qTot - maxRegularQtot) * scaleSaturatedQtot;
171+
}
172+
173+
GPUd() void setSaturatedTailLength(uint32_t tail)
174+
{
175+
sigmaTimePacked = encodeTailLength(tail);
176+
}
177+
178+
GPUd() uint32_t getSaturatedTailLength() const
179+
{
180+
return decodeTailLength(sigmaTimePacked);
181+
}
182+
141183
GPUd() bool operator<(const ClusterNative& rhs) const
142184
{
143185
if (this->getTimePacked() != rhs.getTimePacked()) {
@@ -167,6 +209,93 @@ struct ClusterNative {
167209
this->qTot == rhs.qTot &&
168210
this->getFlags() == rhs.getFlags();
169211
}
212+
213+
private:
214+
static constexpr GPUd() uint32_t decodeTailLength(uint8_t code)
215+
{
216+
// Quantize tail length into 8bits.
217+
// Max expected length is 1500 tbs.
218+
// But allow outliers up to 8000 tbs.
219+
//
220+
// Full code layout is:
221+
//
222+
// | Code range | Decoded values | Step | Codes |
223+
// | ---------: | -------------: | ----: | ----: |
224+
// | `0..63` | `0..63` | `1` | `64` |
225+
// | `64..95` | `64..126` | `2` | `32` |
226+
// | `96..127` | `128..252` | `4` | `32` |
227+
// | `128..159` | `256..504` | `8` | `32` |
228+
// | `160..223` | `512..1520` | `16` | `64` |
229+
// | `224..239` | `1552..2032` | `32` | `16` |
230+
// | `240..255` | `2048..8048` | `400` | `16` |
231+
//
232+
233+
if (code < 64) {
234+
return code;
235+
}
236+
237+
if (code < 160) {
238+
uint32_t q = (uint32_t)code - 64u;
239+
uint32_t exponent = (q >> 5) + 1u; // 1, 2, 3
240+
uint32_t mantissa = q & 31u; // 0..31
241+
242+
return (32u + mantissa) << exponent;
243+
}
244+
245+
if (code < 224) {
246+
return 512u + 16u * ((uint32_t)code - 160u);
247+
}
248+
249+
if (code < 240) {
250+
return 1552u + 32u * ((uint32_t)code - 224u);
251+
}
252+
253+
return 2048u + 400u * ((uint32_t)code - 240u);
254+
}
255+
256+
static constexpr GPUd() uint8_t encodeTailLength(uint32_t value)
257+
{
258+
// Saturate above representable range.
259+
if (value >= decodeTailLength(255)) [[unlikely]] {
260+
return 255;
261+
}
262+
263+
// Binary search for the first code whose decoded value >= value.
264+
uint8_t lo = 0;
265+
uint8_t hi = 255;
266+
267+
while (lo < hi) {
268+
uint8_t mid = lo + ((hi - lo) >> 1);
269+
uint32_t decoded = decodeTailLength(mid);
270+
271+
if (decoded < value) {
272+
lo = mid + 1;
273+
} else {
274+
hi = mid;
275+
}
276+
}
277+
278+
// lo is now the first code with decoded >= value.
279+
if (lo == 0) [[unlikely]] {
280+
return 0;
281+
}
282+
283+
uint8_t above_code = lo;
284+
uint8_t below_code = lo - 1;
285+
286+
uint32_t above_value = decodeTailLength(above_code);
287+
uint32_t below_value = decodeTailLength(below_code);
288+
289+
uint32_t above_error = above_value - value;
290+
uint32_t below_error = value - below_value;
291+
292+
// Tie-break downward.
293+
if (below_error <= above_error) {
294+
return below_code;
295+
} else {
296+
return above_code;
297+
}
298+
}
170299
};
171300

172301
// This is an index struct to access TPC clusters inside sectors and rows. It shall not own the data, but just point to

Detectors/ITSMFT/ITS/tracking/include/ITStracking/Cluster.h

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <array>
2020
#include "ITStracking/Constants.h"
21+
#include "ITStracking/MathUtils.h"
2122
#include "GPUCommonRtypes.h"
2223
#include "GPUCommonDef.h"
2324

@@ -29,26 +30,21 @@ class IndexTableUtils;
2930

3031
struct Cluster final {
3132
GPUhdDefault() Cluster() = default;
32-
GPUhd() Cluster(const float x, const float y, const float z, const int idx);
33-
template <int nLayers>
34-
GPUhd() Cluster(const int, const IndexTableUtils<nLayers>& utils, const Cluster&);
35-
template <int nLayers>
36-
GPUhd() Cluster(const int, const float3&, const IndexTableUtils<nLayers>& utils, const Cluster&);
37-
GPUhdDefault() Cluster(const Cluster&) = default;
38-
GPUhdDefault() Cluster(Cluster&&) noexcept = default;
39-
GPUhdDefault() ~Cluster() = default;
40-
41-
GPUhdDefault() Cluster& operator=(const Cluster&) = default;
42-
GPUhdDefault() Cluster& operator=(Cluster&&) noexcept = default;
43-
GPUhdDefault() bool operator==(const Cluster&) const = default;
44-
33+
GPUhd() Cluster(const float x, const float y, const float z, const int idx)
34+
: xCoordinate(x),
35+
yCoordinate(y),
36+
zCoordinate(z),
37+
phi(math_utils::computeNormalizedPhi(x, y)),
38+
radius(math_utils::hypot(x, y)),
39+
clusterId(idx),
40+
indexTableBinIndex(0) {}
4541
GPUhd() void print() const;
4642

47-
float xCoordinate{-999.f};
48-
float yCoordinate{-999.f};
49-
float zCoordinate{-999.f};
50-
float phi{-999.f};
51-
float radius{-999.f};
43+
float xCoordinate{constants::UnsetValue};
44+
float yCoordinate{constants::UnsetValue};
45+
float zCoordinate{constants::UnsetValue};
46+
float phi{constants::UnsetValue};
47+
float radius{constants::UnsetValue};
5248
int clusterId{constants::UnusedIndex};
5349
int indexTableBinIndex{constants::UnusedIndex};
5450

@@ -57,23 +53,18 @@ struct Cluster final {
5753

5854
struct TrackingFrameInfo final {
5955
GPUhdDefault() TrackingFrameInfo() = default;
60-
GPUhd() TrackingFrameInfo(float x, float y, float z, float xTF, float alpha, std::array<float, 2>&& posTF, std::array<float, 3>&& covTF);
61-
GPUhdDefault() TrackingFrameInfo(const TrackingFrameInfo&) = default;
62-
GPUhdDefault() TrackingFrameInfo(TrackingFrameInfo&&) noexcept = default;
63-
GPUhdDefault() ~TrackingFrameInfo() = default;
64-
65-
GPUhdDefault() TrackingFrameInfo& operator=(const TrackingFrameInfo&) = default;
66-
GPUhdDefault() TrackingFrameInfo& operator=(TrackingFrameInfo&&) = default;
56+
GPUhd() TrackingFrameInfo(float x, float y, float z, float xTF, float alpha, const std::array<float, 2>& posTF, const std::array<float, 3>& covTF)
57+
: xCoordinate(x), yCoordinate(y), zCoordinate(z), xTrackingFrame(xTF), alphaTrackingFrame(alpha), positionTrackingFrame(posTF), covarianceTrackingFrame(covTF) {}
6758

6859
GPUhd() void print() const;
6960

70-
float xCoordinate{-999.f};
71-
float yCoordinate{-999.f};
72-
float zCoordinate{-999.f};
73-
float xTrackingFrame{-999.f};
74-
float alphaTrackingFrame{-999.f};
75-
std::array<float, 2> positionTrackingFrame = {-999.f, -999.f};
76-
std::array<float, 3> covarianceTrackingFrame = {-999.f, -999.f, -999.f};
61+
float xCoordinate{constants::UnsetValue};
62+
float yCoordinate{constants::UnsetValue};
63+
float zCoordinate{constants::UnsetValue};
64+
float xTrackingFrame{constants::UnsetValue};
65+
float alphaTrackingFrame{constants::UnsetValue};
66+
std::array<float, 2> positionTrackingFrame{constants::UnsetValue, constants::UnsetValue};
67+
std::array<float, 3> covarianceTrackingFrame{constants::UnsetValue, constants::UnsetValue, constants::UnsetValue};
7768

7869
ClassDefNV(TrackingFrameInfo, 1);
7970
};

Detectors/ITSMFT/ITS/tracking/include/ITStracking/MathUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ GPUhdi() constexpr float getNormalizedPhi(float phi)
4141
return phi;
4242
}
4343

44+
GPUhdi() constexpr float computeNormalizedPhi(float x, float y)
45+
{
46+
return getNormalizedPhi(computePhi(x, y));
47+
}
48+
4449
GPUhdi() float computeCurvature(float x1, float y1, float x2, float y2, float x3, float y3)
4550
{
4651
// in case the triangle is degenerate we return infinite curvature.

Detectors/ITSMFT/ITS/tracking/include/ITStracking/ROFLookupTables.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,15 +647,14 @@ class ROFVertexLookupTable : public LayerTimingBase<NLayers>
647647
mFlatTable[flatTableIdx].setEntries(static_cast<T>(count));
648648
}
649649

650-
// Binary search for first vertex where maxBC >= targetBC
650+
// Binary search for first vertex where lowerBC >= targetBC
651651
GPUh() size_t binarySearchFirst(const Vertex* vertices, size_t nVertices, size_t searchStart, BCType targetBC) const
652652
{
653653
size_t left = searchStart;
654654
size_t right = nVertices;
655655
while (left < right) {
656656
size_t mid = left + ((right - left) / 2);
657-
int64_t lower = (int64_t)vertices[mid].getTimeStamp().getTimeStamp() -
658-
(int64_t)vertices[mid].getTimeStamp().getTimeStampError();
657+
int64_t lower = (int64_t)vertices[mid].getTimeStamp().lower();
659658
if (lower < targetBC) {
660659
left = mid + 1;
661660
} else {

Detectors/ITSMFT/ITS/tracking/src/Cluster.cxx

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,77 +8,16 @@
88
// In applying this license CERN does not waive the privileges and immunities
99
// granted to it by virtue of its status as an Intergovernmental Organization
1010
// or submit itself to any jurisdiction.
11-
///
12-
/// \file Cluster.cxx
13-
/// \brief
14-
///
15-
#include "GPUCommonMath.h"
16-
#include "GPUCommonArray.h"
1711

1812
#include "ITStracking/Cluster.h"
19-
#include "ITStracking/Definitions.h"
20-
#include "ITStracking/MathUtils.h"
21-
#include "ITStracking/IndexTableUtils.h"
2213

2314
using namespace o2::its;
2415

25-
using math_utils::computePhi;
26-
using math_utils::getNormalizedPhi;
27-
28-
Cluster::Cluster(const float x, const float y, const float z, const int index)
29-
: xCoordinate{x},
30-
yCoordinate{y},
31-
zCoordinate{z},
32-
phi{getNormalizedPhi(computePhi(x, y))},
33-
radius{o2::gpu::GPUCommonMath::Hypot(x, y)},
34-
clusterId{index},
35-
indexTableBinIndex{0}
36-
{
37-
// Nothing to do
38-
}
39-
40-
template <int nLayers>
41-
Cluster::Cluster(const int layerIndex, const IndexTableUtils<nLayers>& utils, const Cluster& other)
42-
: xCoordinate{other.xCoordinate},
43-
yCoordinate{other.yCoordinate},
44-
zCoordinate{other.zCoordinate},
45-
phi{getNormalizedPhi(computePhi(other.xCoordinate, other.yCoordinate))},
46-
radius{o2::gpu::GPUCommonMath::Hypot(other.xCoordinate, other.yCoordinate)},
47-
clusterId{other.clusterId},
48-
indexTableBinIndex{utils.getBinIndex(utils.getZBinIndex(layerIndex, zCoordinate),
49-
utils.getPhiBinIndex(phi))}
50-
//, montecarloId{ other.montecarloId }
51-
{
52-
// Nothing to do
53-
}
54-
55-
template <int nLayers>
56-
Cluster::Cluster(const int layerIndex, const float3& primaryVertex, const IndexTableUtils<nLayers>& utils, const Cluster& other)
57-
: xCoordinate{other.xCoordinate},
58-
yCoordinate{other.yCoordinate},
59-
zCoordinate{other.zCoordinate},
60-
phi{getNormalizedPhi(
61-
computePhi(xCoordinate - primaryVertex.x, yCoordinate - primaryVertex.y))},
62-
radius{o2::gpu::GPUCommonMath::Hypot(xCoordinate - primaryVertex.x, yCoordinate - primaryVertex.y)},
63-
clusterId{other.clusterId},
64-
indexTableBinIndex{utils.getBinIndex(utils.getZBinIndex(layerIndex, zCoordinate),
65-
utils.getPhiBinIndex(phi))}
66-
{
67-
// Nothing to do
68-
}
69-
7016
GPUhd() void Cluster::print() const
7117
{
7218
printf("Cluster: %f %f %f %f %f %d %d\n", xCoordinate, yCoordinate, zCoordinate, phi, radius, clusterId, indexTableBinIndex);
7319
}
7420

75-
TrackingFrameInfo::TrackingFrameInfo(float x, float y, float z, float xTF, float alpha, std::array<float, 2>&& posTF,
76-
std::array<float, 3>&& covTF)
77-
: xCoordinate{x}, yCoordinate{y}, zCoordinate{z}, xTrackingFrame{xTF}, alphaTrackingFrame{alpha}, positionTrackingFrame{posTF}, covarianceTrackingFrame{covTF}
78-
{
79-
// Nothing to do
80-
}
81-
8221
GPUhd() void TrackingFrameInfo::print() const
8322
{
8423
printf("x: %f y: %f z: %f xTF: %f alphaTF: %f posTF: %f %f covTF: %f %f %f\n",

Detectors/ITSMFT/common/workflow/src/EntropyDecoderSpec.cxx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,28 @@ void EntropyDecoderSpec<N>::run(ProcessingContext& pc)
7171
auto buff = pc.inputs().get<gsl::span<o2::ctf::BufferType>>(getBinding(nm + "CTF", iLayer));
7272
// since the buff is const, we cannot use EncodedBlocks::relocate directly, instead we wrap its data to another flat object
7373
// const auto ctfImage = o2::itsmft::CTF::getImage(buff.data());
74-
const auto& ctf = o2::itsmft::CTF::getImage(buff.data());
75-
if (ctf.getHeader().maxStreams != nLayers) {
76-
LOGP(fatal, "Number of streams {} in the CTF header is not equal to NLayers {} from AlpideParam in {}staggered mode",
77-
ctf.getHeader().maxStreams, nLayers, mDoStaggering ? "" : "non-");
74+
if (buff.size()) {
75+
const auto& ctf = o2::itsmft::CTF::getImage(buff.data());
76+
if (ctf.getHeader().maxStreams != nLayers) {
77+
LOGP(fatal, "Number of streams {} in the CTF header is not equal to NLayers {} from AlpideParam in {}staggered mode",
78+
ctf.getHeader().maxStreams, nLayers, mDoStaggering ? "" : "non-");
79+
}
7880
}
7981
// this produces weird memory problems in unrelated devices, to be understood
8082
// auto& trigs = pc.outputs().make<std::vector<o2::itsmft::PhysTrigger>>(OutputRef{"phystrig"}); // dummy output
8183
auto& rofs = pc.outputs().make<std::vector<o2::itsmft::ROFRecord>>(OutputRef{nm + "ROframes", iLayer});
8284
if (mGetDigits) {
8385
auto& digits = pc.outputs().make<std::vector<o2::itsmft::Digit>>(OutputRef{nm + "Digits", iLayer});
8486
if (buff.size()) {
85-
iosize += mCTFCoder.decode(ctf, rofs, digits, mNoiseMap, mPattIdConverter);
87+
iosize += mCTFCoder.decode(o2::itsmft::CTF::getImage(buff.data()), rofs, digits, mNoiseMap, mPattIdConverter);
8688
}
8789
ndigcl += digits.size();
8890
nrofs += rofs.size();
8991
} else {
9092
auto& compcl = pc.outputs().make<std::vector<o2::itsmft::CompClusterExt>>(OutputRef{nm + "compClusters", iLayer});
9193
auto& patterns = pc.outputs().make<std::vector<unsigned char>>(OutputRef{nm + "patterns", iLayer});
9294
if (buff.size()) {
93-
iosize += mCTFCoder.decode(ctf, rofs, compcl, patterns, mNoiseMap, mPattIdConverter);
95+
iosize += mCTFCoder.decode(o2::itsmft::CTF::getImage(buff.data()), rofs, compcl, patterns, mNoiseMap, mPattIdConverter);
9496
}
9597
ndigcl += compcl.size();
9698
}

0 commit comments

Comments
 (0)