ATLAS Offline Software
Loading...
Searching...
No Matches
TileCalibDrawerOfc.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef TILECALIBDRAWEROFC_H
6#define TILECALIBDRAWEROFC_H
7
25
29#include "CoralBase/Blob.h"
30#include <iosfwd>
31#include <vector>
32#include <cstdint>
33#include <cmath>
34#include <string>
35#include <cstring> //std::memcpy, std::memset
36
37#define PHASE_PRECISION 0.1
38
40
41 public:
42
44 enum FIELD{FieldA = 0, FieldB = 1, FieldG = 2, FieldC = 3, FieldDG = 4};
45
48
50 virtual uint16_t getType()const {return TileCalibType::OFC;}
51
63 static TileCalibDrawerOfc* getInstance(coral::Blob& blob
64 , uint16_t objVersion
65 , uint32_t nSamples
66 , int32_t nPhases
67 , uint16_t nChans
68 , uint16_t nGains
69 , std::string_view author = ""
70 , std::string_view comment = ""
71 , uint64_t timeStamp = 0);
72
74 static const TileCalibDrawerOfc* getInstance(const coral::Blob& blob);
75
76
77 //==================================================================
78 //== Accessors to extra header fields
79 //==================================================================
81 uint32_t getNSamples() const;
83 int32_t getNPhases() const;
88 float getPhase(unsigned int channel, unsigned int adc, unsigned int phaseIdx) const;
93 uint32_t getNFields(uint16_t objVersion = 0) const;
94
95 //==================================================================
96 //== Accessors to OFC data
97 //==================================================================
98
108 float getOfc(unsigned int field, unsigned int channel, unsigned int adc, float phase, unsigned int sample) const;
109
117 void fillOfc (unsigned int channel,unsigned int adc, float& phase, float* w_a, float* w_b, float* w_c, float* g, float* dg ) const;
118
119
121 virtual void dump() const;
124 virtual void dump(std::ostream& stm) const;
125
126 //==================================================================
127 //== Setter methods
128 //==================================================================
141 void init(uint16_t objVersion
142 , uint32_t nSamples
143 , int32_t nPhases
144 , uint16_t nChans
145 , uint16_t nGains
146 , std::string_view author=""
147 , std::string_view comment=""
148 , uint64_t timeStamp=0);
149
157 void setOfc(unsigned int field, unsigned int channel, unsigned int adc, float phase, unsigned int sample, float value);
158
163 void setPhases(unsigned int channel, unsigned int adc, const std::vector<float>& phases);
164
165 protected:
167 TileCalibDrawerOfc(const coral::Blob& blob);
169 TileCalibDrawerOfc(coral::Blob& blob);
170
171 private:
172 void initCheck();
173
179 const float* getOfcStartAddress(unsigned int field, unsigned int channel, unsigned int adc, float& phase) const;
180 float* getOfcStartAddress(unsigned int field, unsigned int channel, unsigned int adc, float& phase);
181
182 unsigned int getOfcStartOffset(unsigned int field, unsigned int channel, unsigned int adc, float& phase) const;
183
188 const int32_t* getPhaseStartAddress(unsigned int channel, unsigned int adc, unsigned int phaseIdx) const;
189 int32_t* getPhaseStartAddress(unsigned int channel, unsigned int adc, unsigned int phaseIdx);
190
191 unsigned int getPhaseStartOffset(unsigned int channel, unsigned int adc, unsigned int phaseIdx) const;
192
197 unsigned int getPhaseNumber(unsigned int channel, unsigned int adc, float& phase) const;
198};
199
200//
201//______________________________________________________________
202__attribute__((always_inline))
203inline uint32_t TileCalibDrawerOfc::getNSamples() const {
204 return *(static_cast<const uint32_t*>(getAddress(0)));
205}
206
207//
208//______________________________________________________________
209__attribute__((always_inline))
210inline int32_t TileCalibDrawerOfc::getNPhases() const {
211 return *(static_cast<const int32_t*>(getAddress(1)));
212}
213
214//
215//______________________________________________________________
216__attribute__((always_inline))
217inline unsigned int TileCalibDrawerOfc::getPhaseNumber(unsigned int channel, unsigned int adc, float& phase) const {
218
219 if (std::abs(phase) > 1e6F) {
220 // Bad phase is requested (probably, bad value is in the DB)
221 phase = 0.0F;
222 }
223 int db_phase = (int) std::round(phase * (1 / PHASE_PRECISION)); // Phases are stored as int(10*phase) in DB
224 const int32_t* beg = getPhaseStartAddress(channel, adc, 0);
225 const int32_t* end = beg + std::abs(getNPhases());
226 const int32_t* pos = std::lower_bound(beg, end, db_phase);
227
228 if (pos == end || (*pos != db_phase && pos != beg && (*pos - db_phase) > (db_phase - *(pos - 1)))) {
229 --pos;
230 }
231
233 return (pos - beg);
234}
235
236//
237//______________________________________________________________
238__attribute__((always_inline))
239inline float TileCalibDrawerOfc::getPhase(unsigned int channel, unsigned int adc, unsigned int phaseIdx) const {
240 return *(getPhaseStartAddress(channel, adc, phaseIdx)) * PHASE_PRECISION; // Phases are stored as int(10*phase) in DB
241}
242
243//
244//______________________________________________________________
245__attribute__((always_inline))
246inline uint32_t TileCalibDrawerOfc::getNFields(uint16_t objVersion) const {
247 //=== determine objVersion from Blob if default is passed
248 if(objVersion==0) objVersion = getObjVersion();
249
250 if( objVersion == 3){ return 5; }
251 else if(objVersion == 2){ return 4; }
252 else if(objVersion == 1){ return 3; }
253 else{
254 throw TileCalib::VersionConflict("TileCalibDrawerOfc::getNFields", objVersion);
255 }
256}
257
258//
259//______________________________________________________________
260inline float TileCalibDrawerOfc::getOfc(unsigned int field, unsigned int channel, unsigned int adc
261 , float phase, unsigned int sample) const {
262 if(sample >= getNSamples())
263 throw TileCalib::IndexOutOfRange("TileCalibDrawerOfc::getA", sample, getNSamples());
264 return getOfcStartAddress(field, channel, adc, phase)[sample];
265}
266
267inline void TileCalibDrawerOfc::fillOfc (unsigned int channel,unsigned int adc, float& phase
268 , float* w_a, float* w_b, float* w_c, float* g, float* dg) const {
269
270
271 const float* startAddress = getOfcStartAddress(TileCalibDrawerOfc::FieldA, channel, adc, phase);
272 size_t allSamplesSize = getNSamples() * sizeof(float);
273 size_t fieldSize = getObjSizeUint32() * getNSamples();
274
275 std::memcpy(w_a, startAddress, allSamplesSize);
276 startAddress += fieldSize;
277 std::memcpy(w_b, startAddress, allSamplesSize);
278 startAddress += fieldSize;
279 std::memcpy(g, startAddress, allSamplesSize);
280 startAddress += fieldSize;
281 std::memcpy(w_c, startAddress, allSamplesSize);
282 if (getNFields() > 4) {
283 startAddress += fieldSize;
284 std::memcpy(dg, startAddress, allSamplesSize);
285 } else {
286 std::memset(dg, 0, allSamplesSize);
287 }
288}
289
290
291//
292//______________________________________________________________
293inline void TileCalibDrawerOfc::setOfc(unsigned int field,unsigned int channel, unsigned int adc
294 , float phase, unsigned int sample, float value) {
295 if(sample >= getNSamples())
296 throw TileCalib::IndexOutOfRange("TileCalibDrawerOfc::setOfc", sample, getNSamples());
297 getOfcStartAddress(field, channel, adc, phase)[sample] = value;
298}
299
300//
301//______________________________________________________________
302__attribute__((always_inline))
303inline unsigned int TileCalibDrawerOfc::getPhaseStartOffset(unsigned int channel, unsigned int adc, unsigned int phaseIdx) const
304{
305 if(phaseIdx >= static_cast<unsigned int>(std::abs(getNPhases()))){
306 throw TileCalib::IndexOutOfRange("TileCalibDrawerOfc::getPhaseStartAddress", phaseIdx, std::abs(getNPhases()));
307 }
308 //=== use only one phase table for all ADCs
309 if(getNPhases() < 0){
310 channel = 0;
311 adc = 0;
312 }
313 //=== check for out of bounds channel, adc
314 if(channel > getNChans())
315 throw TileCalib::IndexOutOfRange("TileCalibDrawerOfc::getPhaseStartAddress", channel, getNChans());
316 if(adc > getNGains())
317 throw TileCalib::IndexOutOfRange("TileCalibDrawerOfc::getPhaseStartAddress", adc, getNGains());
318
319 //=== extra header
320 unsigned int offset = 2;
321 unsigned int nPhases = std::abs(getNPhases());
322 offset += channel * nPhases * getNGains() + adc * nPhases + phaseIdx;
323 return offset;
324}
325
326//
327//______________________________________________________________
328__attribute__((always_inline))
329inline const int32_t* TileCalibDrawerOfc::getPhaseStartAddress(unsigned int channel, unsigned int adc, unsigned int phaseIdx) const
330{
331 return static_cast<const int32_t*>(getAddress(getPhaseStartOffset(channel, adc, phaseIdx)));
332}
333
334//
335//______________________________________________________________
336__attribute__((always_inline))
337inline int32_t* TileCalibDrawerOfc::getPhaseStartAddress(unsigned int channel, unsigned int adc, unsigned int phaseIdx)
338{
339 return static_cast<int32_t*>(getAddress(getPhaseStartOffset(channel, adc, phaseIdx)));
340}
341
342//
343//______________________________________________________________
344__attribute__((always_inline))
345inline unsigned int TileCalibDrawerOfc::getOfcStartOffset(unsigned int field, unsigned int channel, unsigned int adc, float& phase) const
346{
347 //=== check default policy
348
349 if(channel >= getNChans()) {
350 if (channel <= TileCalibUtils::MAX_CHAN) {
351 channel = 0;
352 } else {
354 if (channel >= getNChans()) channel = 0;
355 }
356 }
357
358
359 if(adc >= getNGains()) {adc = 0;}
360
361 //=== this calculation is only valid for a blob layout according
362 //=== to version 1 or version 2
363 uint16_t objVer = getObjVersion();
364 if(objVer!=1 && objVer!=2 && objVer!=3){
365 throw TileCalib::VersionConflict("TileCalibDrawerOfc::getOfcStartAddress", objVer);
366 }
367
368 //=== number of fields:
369 //=== for Version 1 = OFC1 we have 3 fields: ai, bi, gi
370 //=== for Version 2 = OFC2 we have 4 fields: ai, bi, gi, ci
371 //=== for Version 3 = OFC2 we have 5 fields: ai, bi, gi, ci, dgi
372 unsigned int nField = getNFields(objVer);
373 if(field >= nField){
374 throw TileCalib::IndexOutOfRange("TileCalibDrawerOfc::getOfcStartAddress", field, nField);
375 }
376
377 //=== calculate the offset
378 //=== first get rid of ofc specific header
379 unsigned int nPhaseAbs = std::abs(getNPhases());
380 int nPhaseSgn = getNPhases() < 0 ? -1 : 1;
381 unsigned int offset = 2;
382 if(nPhaseSgn < 0) { offset += nPhaseAbs; }
383 else { offset += nPhaseAbs * getNChans() * getNGains(); }
384
385 //=== add offset for requested field
386 unsigned int iPhase = getPhaseNumber(channel, adc, phase);
387 unsigned int nGain = getNGains();
388 unsigned int nSample = getNSamples();
389 unsigned int lPhase = nSample * nField;
390 unsigned int lAdc = lPhase * nPhaseAbs;
391 unsigned int lChan = lAdc * nGain;
392 offset += channel * lChan + adc * lAdc + iPhase * lPhase + field * nSample;
393
394 return offset;
395}
396
397//
398//______________________________________________________________
399__attribute__((always_inline))
400inline const float* TileCalibDrawerOfc::getOfcStartAddress(unsigned int field, unsigned int channel, unsigned int adc, float& phase) const
401{
402 return static_cast<const float*>(getAddress(getOfcStartOffset(field, channel, adc, phase)));
403}
404
405//
406//______________________________________________________________
407__attribute__((always_inline))
408inline float* TileCalibDrawerOfc::getOfcStartAddress(unsigned int field, unsigned int channel, unsigned int adc, float& phase)
409{
410 return static_cast<float*>(getAddress(getOfcStartOffset(field, channel, adc, phase)));
411}
412
413#endif
__attribute__((always_inline)) inline uint32_t TileCalibDrawerOfc
#define PHASE_PRECISION
Class for storing Optimal Filtering Coefficients (OFCs) in a coral::Blob.
uint16_t getNChans() const
Returns the number of channels stored in the BLOB.
uint16_t getNGains() const
Returns the number of gains stored for each channel.
TileCalibDrawerBase(const TileCalibDrawerBase &other)
Copy Ctor.
uint16_t getObjVersion() const
Returns the BLOB object version.
uint32_t getObjSizeUint32() const
Returns the size of a data object in units of uint32_t.
const void * getAddress(unsigned int iEle) const
Returns start address of iEle-th basic unit.
TileCalibDrawerOfc(const coral::Blob &blob)
Ctor (const).
unsigned int getPhaseNumber(unsigned int channel, unsigned int adc, float &phase) const
Returns the index for a given phase.
int32_t getNPhases() const
Returns the number of phases (WARNING: Can be negative!).
void init(uint16_t objVersion, uint32_t nSamples, int32_t nPhases, uint16_t nChans, uint16_t nGains, std::string_view author="", std::string_view comment="", uint64_t timeStamp=0)
Function for initializing a TileCalibDrawerOfc BLOB.
void setOfc(unsigned int field, unsigned int channel, unsigned int adc, float phase, unsigned int sample, float value)
Sets OFC data.
float getPhase(unsigned int channel, unsigned int adc, unsigned int phaseIdx) const
Returns the stored phase.
void fillOfc(unsigned int channel, unsigned int adc, float &phase, float *w_a, float *w_b, float *w_c, float *g, float *dg) const
Fill all OFC for optimazation.
uint32_t getNSamples() const
Returns the number of sample stored.
virtual uint16_t getType() const
Returns TileCalibType::OFC.
unsigned int getOfcStartOffset(unsigned int field, unsigned int channel, unsigned int adc, float &phase) const
void setPhases(unsigned int channel, unsigned int adc, const std::vector< float > &phases)
Sets a phase value.
FIELD
OFC field identifier.
const float * getOfcStartAddress(unsigned int field, unsigned int channel, unsigned int adc, float &phase) const
Returns pointer to first data OFC for a given field, ADC & phase.
int32_t * getPhaseStartAddress(unsigned int channel, unsigned int adc, unsigned int phaseIdx)
float getOfc(unsigned int field, unsigned int channel, unsigned int adc, float phase, unsigned int sample) const
Returns OFC data.
virtual ~TileCalibDrawerOfc()
Dtor.
virtual void dump() const
Prints out the object content to std::cout.
float * getOfcStartAddress(unsigned int field, unsigned int channel, unsigned int adc, float &phase)
const int32_t * getPhaseStartAddress(unsigned int channel, unsigned int adc, unsigned int phaseIdx) const
Returns pointer to the requested phase value.
uint32_t getNFields(uint16_t objVersion=0) const
Returns the number of fields.
static TileCalibDrawerOfc * getInstance(coral::Blob &blob, uint16_t objVersion, uint32_t nSamples, int32_t nPhases, uint16_t nChans, uint16_t nGains, std::string_view author="", std::string_view comment="", uint64_t timeStamp=0)
Returns a pointer to a non-const TileCalibDrawerOfc.
unsigned int getPhaseStartOffset(unsigned int channel, unsigned int adc, unsigned int phaseIdx) const
@ OFC
Enum for TileCalibDrawerOfc class.
static const unsigned int MAX_CHAN
Number of channels in drawer.
setWord1 uint16_t
setEventNumber uint32_t