forked from AliceO2Group/O2Physics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlatLutEntry.h
More file actions
220 lines (186 loc) · 5 KB
/
FlatLutEntry.h
File metadata and controls
220 lines (186 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#ifndef ALICE3_CORE_FLATLUTENTRY_H_
#define ALICE3_CORE_FLATLUTENTRY_H_
#include <cmath>
#include <cstdint>
#include <span>
#include <vector>
#define LUTCOVM_VERSION 20210801
namespace o2::delphes
{
/**
* @brief Flat LUT entry structure
*/
struct lutEntry_t {
float nch = 0.f;
float eta = 0.f;
float pt = 0.f;
bool valid = false;
float eff = 0.f;
float eff2 = 0.f;
float itof = 0.f;
float otof = 0.f;
float covm[15] = {0.f};
float eigval[5] = {0.f};
float eigvec[5][5] = {{0.f}};
float eiginv[5][5] = {{0.f}};
void print() const;
};
/**
* @brief Binning map
*/
struct map_t {
int nbins = 1;
float min = 0.f;
float max = 1.e6f;
bool log = false;
float eval(int bin) const
{
float width = (max - min) / nbins;
float val = min + (bin + 0.5f) * width;
if (log)
return std::pow(10.f, val);
return val;
}
float fracPositionWithinBin(float val) const;
int find(float val) const;
void print() const;
};
/**
* @brief LUT header
*/
struct lutHeader_t {
int version = LUTCOVM_VERSION;
int pdg = 0;
float mass = 0.f;
float field = 0.f;
map_t nchmap;
map_t radmap;
map_t etamap;
map_t ptmap;
bool check_version() const;
void print() const;
};
/**
* @brief Flat LUT data container - single contiguous buffer
* Memory layout: [header][entry_0][entry_1]...[entry_N]
*
* All entries stored sequentially in a single allocation.
* Can be directly mapped from file or shared memory without copying.
*/
class FlatLutData
{
public:
FlatLutData() = default;
FlatLutData(FlatLutData&&) = default;
FlatLutData& operator=(FlatLutData&&) = default;
/**
* @brief Initialize from binning information
* Pre-allocates contiguous memory for all entries
*/
void initialize(const lutHeader_t& header);
/**
* @brief Get LUT entry by bin indices (view)
*/
const lutEntry_t* getEntryRef(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const;
/**
* @brief Get LUT entry by bin indices (owned)
*/
lutEntry_t* getEntry(int nch_bin, int rad_bin, int eta_bin, int pt_bin);
/**
* @brief Get LUT header (view)
*/
const lutHeader_t& getHeaderRef() const;
/**
* @brief Get LUT header (owned)
*/
lutHeader_t& getHeader();
/**
* @brief Get raw data buffer
*/
uint8_t* data() { return mData.data(); } // owned
const uint8_t* data() const { return mDataRef.data(); } // view
/**
* @brief Total size in bytes
*/
size_t bytes() const { return mDataRef.size(); }
/**
* @brief Construct a new FlatLutData from external buffer as a copy
*/
static FlatLutData AdoptFromBuffer(const uint8_t* buffer, size_t size);
/**
* @brief Construct a new FlatLutData from external buffer as a view
*/
static FlatLutData ViewFromBuffer(const uint8_t* buffer, size_t size);
/**
* @brief Construct a new FlatLutData from external span as a view
*/
static FlatLutData ViewFromBuffer(std::span<std::byte> const& span);
/**
* @brief Construct a new FlatLutData from a file
*/
static FlatLutData loadFromFile(std::ifstream& file, const char* filename);
/**
* @brief Preview buffer header for version and other compatibility checks
*/
static lutHeader_t PreviewHeader(const uint8_t* buffer, size_t size);
/**
* @brief Preview file-stored header for version and other compatibility checks
*/
static lutHeader_t PreviewHeader(std::ifstream& file, const char* filename);
/**
* @brief Check if the LUT is loaded
*/
bool isLoaded() const;
/**
* @brief Reset LUT to empty
*/
void reset();
private:
/**
* @brief Linear index calculation for entry access
*/
size_t getEntryOffset(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const;
/**
* @brief Update dimensions from the current header
*/
void cacheDimensions();
/**
* @brief Reset dimensions to 0
*/
void resetDimensions();
/**
* @brief Adopt a buffer by copying
*/
void adopt(const uint8_t* buffer, size_t size);
/**
* @brief Adopt a buffer as a view
*/
void view(const uint8_t* buffer, size_t size);
/**
* @brief Update mDataRef from mData
*/
void updateRef();
/**
* @brief Validate external buffer
*/
static void validateBuffer(const uint8_t* buffer, size_t size);
std::vector<uint8_t> mData;
std::span<uint8_t const> mDataRef;
// Cache dimensions for quick access
int mNchBins = 0;
int mRadBins = 0;
int mEtaBins = 0;
int mPtBins = 0;
};
} // namespace o2::delphes
#endif // ALICE3_CORE_FLATLUTENTRY_H_