Skip to content

Commit df4f545

Browse files
committed
make file reading make sense
1 parent 0db84cc commit df4f545

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

ALICE3/Core/FlatLutEntry.cxx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ void FlatLutData::cacheDimensions()
142142
mPtBins = header.ptmap.nbins;
143143
}
144144

145+
void FlatLutData::resetDimensions()
146+
{
147+
mNchBins = 0;
148+
mRadBins = 0;
149+
mEtaBins = 0;
150+
mPtBins = 0;
151+
}
152+
145153
void FlatLutData::adopt(const uint8_t* buffer, size_t size)
146154
{
147155
mData.resize(size);
@@ -198,4 +206,54 @@ FlatLutData FlatLutData::ViewFromBuffer(const uint8_t* buffer, size_t size)
198206
return data;
199207
}
200208

209+
bool FlatLutData::isLoaded() const
210+
{
211+
return ((!mData.empty()) || (!mDataRef.empty()));
212+
}
213+
214+
FlatLutData FlatLutData::loadFromFile(const char* filename)
215+
{
216+
std::ifstream lutFile(filename, std::ifstream::binary);
217+
if (!lutFile.is_open()) {
218+
throw framework::runtime_error_f("Cannot open LUT file: %s", filename);
219+
}
220+
221+
// Read header first
222+
lutHeader_t tempHeader;
223+
lutFile.read(reinterpret_cast<char*>(&tempHeader), sizeof(lutHeader_t));
224+
if (lutFile.gcount() != static_cast<std::streamsize>(sizeof(lutHeader_t))) {
225+
throw framework::runtime_error_f("Failed to read LUT header from %s", filename);
226+
}
227+
228+
if (!tempHeader.check_version()) {
229+
throw framework::runtime_error_f("LUT header version mismatch: expected %d, got %d", LUTCOVM_VERSION, tempHeader.version);
230+
}
231+
232+
FlatLutData data;
233+
234+
// Initialize flat data structure
235+
data.initialize(tempHeader);
236+
237+
// Read all entries sequentially into flat buffer
238+
size_t headerSize = sizeof(lutHeader_t);
239+
size_t numEntries = static_cast<size_t>(data.mNchBins) * data.mRadBins * data.mEtaBins * data.mPtBins;
240+
size_t entriesSize = numEntries * sizeof(lutEntry_t);
241+
242+
lutFile.read(reinterpret_cast<char*>(data.data() + headerSize), entriesSize);
243+
if (lutFile.gcount() != static_cast<std::streamsize>(entriesSize)) {
244+
throw framework::runtime_error_f("Failed to read LUT entries from %s: expected %zu bytes, got %zu", filename, entriesSize, static_cast<size_t>(lutFile.gcount()));
245+
}
246+
247+
lutFile.close();
248+
LOGF(info, "Successfully loaded LUT from %s: %zu entries", filename, numEntries);
249+
return data;
250+
}
251+
252+
void FlatLutData::reset()
253+
{
254+
mData.clear();
255+
updateRef();
256+
resetDimensions();
257+
}
258+
201259
} // namespace o2::delphes

ALICE3/Core/FlatLutEntry.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class FlatLutData
9393
{
9494
public:
9595
FlatLutData() = default;
96+
FlatLutData(FlatLutData&&) = default;
97+
FlatLutData& operator=(FlatLutData&&) = default;
9698

9799
/**
98100
* @brief Initialize from binning information
@@ -141,6 +143,21 @@ class FlatLutData
141143
*/
142144
static FlatLutData ViewFromBuffer(const uint8_t* buffer, size_t size);
143145

146+
/**
147+
* @brief Construct a new FlatLutData from a file
148+
*/
149+
static FlatLutData loadFromFile(const char* filename);
150+
151+
/**
152+
* @brief Check if the LUT is loaded
153+
*/
154+
bool isLoaded() const;
155+
156+
/**
157+
* @brief Reset LUT to empty
158+
*/
159+
void reset();
160+
144161
private:
145162
/**
146163
* @brief Linear index calculation for entry access
@@ -151,6 +168,10 @@ class FlatLutData
151168
* @brief Update dimensions from the current header
152169
*/
153170
void cacheDimensions();
171+
/**
172+
* @brief Reset dimensions to 0
173+
*/
174+
void resetDimensions();
154175

155176
/**
156177
* @brief Adopt a buffer by copying

ALICE3/Core/FlatTrackSmearer.cxx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
#include "FlatTrackSmearer.h"
1313

14+
#include "ALICE3/Core/GeometryContainer.h"
15+
16+
#include <Framework/RuntimeError.h>
17+
1418
namespace o2::delphes
1519
{
1620
int TrackSmearer::getIndexPDG(int pdg) const
@@ -64,4 +68,63 @@ const char* TrackSmearer::getParticleName(int pdg) const
6468
return "pion"; // Default: pion
6569
}
6670
}
71+
72+
bool TrackSmearer::loadTable(int pdg, const char* filename, bool forceReload)
73+
{
74+
if (!filename || filename[0] == '\0') {
75+
LOGF(info, "No LUT file provided for PDG %d. Skipping load.", pdg);
76+
return false;
77+
}
78+
79+
const auto ipdg = getIndexPDG(pdg);
80+
LOGF(info, "Loading %s LUT file: '%s'", getParticleName(pdg), filename);
81+
82+
if (mLUTData[ipdg].isLoaded() && !forceReload) {
83+
LOGF(info, "LUT table for PDG %d already loaded (index %d)", pdg, ipdg);
84+
return false;
85+
}
86+
87+
const std::string localFilename = o2::fastsim::GeometryEntry::accessFile(filename, "./.ALICE3/LUTs/", mCcdbManager, 10);
88+
89+
try {
90+
mLUTData[ipdg] = FlatLutData::loadFromFile(localFilename.c_str());
91+
} catch (framework::RuntimeErrorRef ref) {
92+
LOGF(error, "%s", framework::error_from_ref(ref).what);
93+
return false;
94+
}
95+
96+
// Validate header
97+
const auto& header = mLUTData[ipdg].getHeaderRef();
98+
99+
bool specialPdgCase = false;
100+
switch (pdg) {
101+
case o2::constants::physics::kAlpha:
102+
// Special case: Allow Alpha particles to use He3 LUT
103+
specialPdgCase = (header.pdg == o2::constants::physics::kHelium3);
104+
if (specialPdgCase) {
105+
LOGF(info, "Alpha particles (PDG %d) will use He3 LUT data (PDG %d)", pdg, header.pdg);
106+
}
107+
break;
108+
default:
109+
break;
110+
}
111+
112+
if (header.pdg != pdg && !specialPdgCase) {
113+
LOGF(error, "LUT header PDG mismatch: expected %d, got %d", pdg, header.pdg);
114+
mLUTData[ipdg].reset();
115+
return false;
116+
}
117+
118+
LOGF(info, "Successfully read LUT for PDG %d: %s", pdg, localFilename.c_str());
119+
header.print();
120+
121+
return true;
122+
}
123+
124+
bool TrackSmearer::hasTable(int pdg) const
125+
{
126+
const int ipdg = getIndexPDG(pdg);
127+
return mLUTData[ipdg].isLoaded();
128+
}
129+
67130
} // namespace o2::delphes

ALICE3/Core/FlatTrackSmearer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class TrackSmearer
2727
{
2828
public:
2929
TrackSmearer() = default;
30-
~TrackSmearer() = default;
3130

3231
/** LUT methods **/
3332
bool loadTable(int pdg, const char* filename, bool forceReload = false);
@@ -60,7 +59,7 @@ class TrackSmearer
6059
protected:
6160
static constexpr unsigned int nLUTs = 9; // Number of LUT available
6261
lutHeader_t const* mHeaders[nLUTs]; // header references for quick access
63-
FlatLutData mLUTData[nLUTs]; // NEW: Flat data storage
62+
FlatLutData mLUTData[nLUTs]; // Flat data storage
6463

6564
bool mUseEfficiency = true;
6665
bool mInterpolateEfficiency = false;

0 commit comments

Comments
 (0)