Skip to content

Commit 1da3d2e

Browse files
authored
Refactor LUT entry functions and improve readability
1 parent f050c63 commit 1da3d2e

1 file changed

Lines changed: 32 additions & 23 deletions

File tree

ALICE3/Core/FlatLutEntry.cxx

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
// granted to it by virtue of its status as an Intergovernmental Organization
1010
// or submit itself to any jurisdiction.
1111

12+
/// \file FlatLutEntry.cxx
13+
/// \brief Flat LUT implementation for compact helper tables used by the ALICE3 track smearing workflow.
14+
1215
#include "FlatLutEntry.h"
1316

1417
#include <Framework/Logger.h>
@@ -24,31 +27,37 @@
2427

2528
namespace o2::delphes
2629
{
30+
namespace
31+
{
32+
constexpr int kNumCovarianceTerms = 15;
33+
constexpr int kNumEigenModes = 5;
34+
constexpr float kBinCenterOffset = 0.5f;
35+
} // namespace
2736

2837
void lutEntry_t::print() const
2938
{
3039
LOGF(info, " nch = %f, eta = %f, pt = %f, valid = %s\n", nch, eta, pt, valid ? "true" : "false");
3140
LOGF(info, " eff = %f, eff2 = %f, itof = %f, otof = %f\n", eff, eff2, itof, otof);
3241
LOGF(info, " covm: ");
33-
for (int i = 0; i < 15; ++i) {
42+
for (int i = 0; i < kNumCovarianceTerms; ++i) {
3443
LOGF(info, "%f ", covm[i]);
3544
}
3645
LOGF(info, "\n");
3746
LOGF(info, " eigval: ");
38-
for (int i = 0; i < 5; ++i) {
47+
for (int i = 0; i < kNumEigenModes; ++i) {
3948
LOGF(info, "%f ", eigval[i]);
4049
}
4150
LOGF(info, "\n");
4251
LOGF(info, " eigvec:\n");
43-
for (int i = 0; i < 5; ++i) {
44-
for (int j = 0; j < 5; ++j) {
52+
for (int i = 0; i < kNumEigenModes; ++i) {
53+
for (int j = 0; j < kNumEigenModes; ++j) {
4554
LOGF(info, "%f ", eigvec[i][j]);
4655
}
4756
LOGF(info, "\n");
4857
}
4958
LOGF(info, " eiginv:\n");
50-
for (int i = 0; i < 5; ++i) {
51-
for (int j = 0; j < 5; ++j) {
59+
for (int i = 0; i < kNumEigenModes; ++i) {
60+
for (int j = 0; j < kNumEigenModes; ++j) {
5261
LOGF(info, "%f ", eiginv[i][j]);
5362
}
5463
LOGF(info, "\n");
@@ -59,7 +68,7 @@ float map_t::fracPositionWithinBin(float val) const
5968
{
6069
float width = (max - min) / nbins;
6170
int bin;
62-
float returnVal = 0.5f;
71+
float returnVal = kBinCenterOffset;
6372
if (log) {
6473
bin = static_cast<int>((std::log10(val) - min) / width);
6574
returnVal = ((std::log10(val) - min) / width) - bin;
@@ -93,7 +102,7 @@ void map_t::print() const
93102
LOGF(info, "nbins = %d, min = %f, max = %f, log = %s \n", nbins, min, max, log ? "on" : "off");
94103
}
95104

96-
bool lutHeader_t::check_version() const
105+
bool lutHeader_t::checkVersion() const
97106
{
98107
return (version == LUTCOVM_VERSION);
99108
}
@@ -120,7 +129,7 @@ void FlatLutData::initialize(const lutHeader_t& header)
120129
mEtaBins = header.etamap.nbins;
121130
mPtBins = header.ptmap.nbins;
122131

123-
const size_t headerSize = sizeof(lutHeader_t);
132+
constexpr size_t headerSize = sizeof(lutHeader_t);
124133
const size_t numEntries = static_cast<size_t>(mNchBins) * mRadBins * mEtaBins * mPtBins;
125134
const size_t entriesSize = numEntries * sizeof(lutEntry_t);
126135
const size_t totalSize = headerSize + entriesSize;
@@ -133,10 +142,10 @@ void FlatLutData::initialize(const lutHeader_t& header)
133142

134143
size_t FlatLutData::getEntryOffset(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const
135144
{
136-
static constexpr size_t headerSize = sizeof(lutHeader_t);
145+
static constexpr size_t HeaderSize = sizeof(lutHeader_t);
137146
const size_t linearIdx = getEntryIndex(nch_bin, rad_bin, eta_bin, pt_bin);
138-
static constexpr size_t entrySize = sizeof(lutEntry_t);
139-
return headerSize + linearIdx * entrySize;
147+
static constexpr size_t EntrySize = sizeof(lutEntry_t);
148+
return HeaderSize + linearIdx * EntrySize;
140149
}
141150

142151
const lutEntry_t* FlatLutData::getEntryRef(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const
@@ -200,26 +209,26 @@ void FlatLutData::view(const uint8_t* buffer, size_t size)
200209

201210
void FlatLutData::validateBuffer(const uint8_t* buffer, size_t size)
202211
{
203-
auto header = PreviewHeader(buffer, size);
204-
auto mNchBins = header.nchmap.nbins;
205-
auto mRadBins = header.radmap.nbins;
206-
auto mEtaBins = header.etamap.nbins;
207-
auto mPtBins = header.ptmap.nbins;
212+
auto header = previewHeader(buffer, size);
213+
const auto nchBins = header.nchmap.nbins;
214+
const auto radBins = header.radmap.nbins;
215+
const auto etaBins = header.etamap.nbins;
216+
const auto ptBins = header.ptmap.nbins;
208217

209-
size_t expectedSize = sizeof(lutHeader_t) + static_cast<size_t>(mNchBins) * mRadBins * mEtaBins * mPtBins * sizeof(lutEntry_t);
218+
const size_t expectedSize = sizeof(lutHeader_t) + static_cast<size_t>(nchBins) * radBins * etaBins * ptBins * sizeof(lutEntry_t);
210219

211220
if (size < expectedSize) {
212221
throw framework::runtime_error_f("Buffer size mismatch: expected %zu, got %zu", expectedSize, size);
213222
}
214223
}
215224

216-
lutHeader_t FlatLutData::PreviewHeader(const uint8_t* buffer, size_t size)
225+
lutHeader_t FlatLutData::previewHeader(const uint8_t* buffer, size_t size)
217226
{
218227
if (size < sizeof(lutHeader_t)) {
219228
throw framework::runtime_error_f("Buffer too small for LUT header: expected at least %zu, got %zu", sizeof(lutHeader_t), size);
220229
}
221230
const auto* header = reinterpret_cast<const lutHeader_t*>(buffer);
222-
if (!header->check_version()) {
231+
if (!header->checkVersion()) {
223232
throw framework::runtime_error_f("LUT header version mismatch: expected %d, got %d", LUTCOVM_VERSION, header->version);
224233
}
225234
return *header;
@@ -256,14 +265,14 @@ bool FlatLutData::isLoaded() const
256265
return ((!mData.empty()) || (!mDataRef.empty()));
257266
}
258267

259-
lutHeader_t FlatLutData::PreviewHeader(std::ifstream& file, const char* filename)
268+
lutHeader_t FlatLutData::previewHeader(std::ifstream& file, const char* filename)
260269
{
261270
lutHeader_t tempHeader;
262271
file.read(reinterpret_cast<char*>(&tempHeader), sizeof(lutHeader_t));
263272
if (file.gcount() != static_cast<std::streamsize>(sizeof(lutHeader_t))) {
264273
throw framework::runtime_error_f("Failed to read LUT header from %s", filename);
265274
}
266-
if (!tempHeader.check_version()) {
275+
if (!tempHeader.checkVersion()) {
267276
throw framework::runtime_error_f("LUT header version mismatch: expected %d, got %d", LUTCOVM_VERSION, tempHeader.version);
268277
}
269278
return tempHeader;
@@ -272,7 +281,7 @@ lutHeader_t FlatLutData::PreviewHeader(std::ifstream& file, const char* filename
272281
FlatLutData FlatLutData::loadFromFile(std::ifstream& file, const char* filename)
273282
{
274283
// Read header first
275-
lutHeader_t tempHeader = PreviewHeader(file, filename);
284+
lutHeader_t tempHeader = previewHeader(file, filename);
276285

277286
FlatLutData data;
278287

0 commit comments

Comments
 (0)