Project
Loading...
Searching...
No Matches
CalDet.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef ALICEO2_TPC_CALDET_H_
13#define ALICEO2_TPC_CALDET_H_
14
15#include <memory>
16#include <numeric>
17#include <vector>
18#include <string>
19#include <cassert>
20
21#include "DataFormatsTPC/Defs.h"
22#include "TPCBase/Mapper.h"
23#include "TPCBase/ROC.h"
24#include "TPCBase/Sector.h"
25#include "TPCBase/CalArray.h"
26
27#ifndef GPUCA_ALIGPUCODE
28#include <Framework/Logger.h>
29#include <fmt/format.h>
30#include "Rtypes.h"
31#endif
32
33#ifndef NDEBUG
34#undef NDEBUG
35// always enable assert
36#include <cassert>
37#endif
38
39namespace o2
40{
41namespace tpc
42{
45template <class T>
46class CalDet
47{
48 using CalType = CalArray<T>;
49
50 public:
51 CalDet() { initData(); }
52 CalDet(CalDet const&) = default;
53 CalDet& operator=(CalDet const&) = default;
54 ~CalDet() = default;
55
56 CalDet(PadSubset padSusbset) : mName{"PadCalibrationObject"}, mData{}, mPadSubset{padSusbset} { initData(); }
57
58 CalDet(const std::string_view name, const PadSubset padSusbset = PadSubset::ROC) : mName(name), mData(), mPadSubset(padSusbset) { initData(); }
59
62 PadSubset getPadSubset() const { return mPadSubset; }
63
64 const std::vector<CalType>& getData() const { return mData; }
65 std::vector<CalType>& getData() { return mData; }
66 // void setValue(const unsigned int channel, const T value) { mData[channel] = value; }
67 // const T& getValue(const unsigned int channel) const { return mData[channel]; }
68
69 const CalType& getCalArray(size_t position) const { return mData[position]; }
70 CalType& getCalArray(size_t position) { return mData[position]; }
71
74 const T getValue(const int sec, const int globalPadInSector) const;
75 void setValue(const int sec, const int globalPadInSector, const T& value);
76 void setValue(const int sec, const int rowInSector, const int padInRow, const T& value);
77
80 const T getValue(const ROC roc, const size_t row, const size_t pad) const;
81 const T getValue(const CRU cru, const size_t row, const size_t pad) const;
82 const T getValue(const Sector sec, const int rowInSector, const int padInRow) const;
83
84 void setName(const std::string_view name, bool nameCalArrays = true)
85 {
86 mName = name.data();
87 if (nameCalArrays) {
88 initData();
89 }
90 }
91 const std::string& getName() const { return mName; }
92
93 const CalDet& multiply(const T& val) { return *this *= val; }
94 const CalDet& operator+=(const CalDet& other);
95 const CalDet& operator-=(const CalDet& other);
96 const CalDet& operator*=(const CalDet& other);
97 const CalDet& operator/=(const CalDet& other);
98 bool operator==(const CalDet& other) const;
99
100 const CalDet& operator+=(const T& val);
101 const CalDet& operator-=(const T& val);
102 const CalDet& operator*=(const T& val);
103 const CalDet& operator/=(const T& val);
104
105 const CalDet& operator=(const T& val);
106
107 template <class U>
108 friend CalDet<U> operator+(const CalDet<U>&, const CalDet<U>&);
109
110 template <class U>
111 friend CalDet<U> operator-(const CalDet<U>&, const CalDet<U>&);
112
113 template <typename U = T>
114 U getMean() const
115 {
116 if (mData.size() == 0) {
117 return U{0};
118 }
119
120 U nVal = 0;
121 U sum = 0;
122 for (const auto& data : mData) {
123 const auto& vals = data.getData();
124 sum += std::accumulate(vals.begin(), vals.end(), U{0});
125 nVal += static_cast<U>(vals.size());
126 }
127
128 return (nVal > 0) ? sum / nVal : U{0};
129 }
130
131 template <typename U = T>
132 U getSum() const
133 {
134 if (mData.size() == 0) {
135 return U{};
136 }
137
138 U sum{};
139 for (const auto& data : mData) {
140 const auto& vals = data.getData();
141 sum += data.template getSum<U>();
142 }
143
144 return sum;
145 }
146
147 private:
148 std::string mName;
149 std::vector<CalType> mData;
150 PadSubset mPadSubset = PadSubset::ROC;
151
153 void initData();
154
155 ClassDefNV(CalDet, 1)
156};
157
158//______________________________________________________________________________
159template <class T>
160inline const T CalDet<T>::getValue(const int sector, const int globalPadInSector) const
161{
162 // This shold be a temporary speedup, a proper restructuring of Mapper and CalDet/CalArray is needed.
163 // The default granularity for the moment should be ROC, for the assumptions below this should be assured
164 const Mapper& mapper = Mapper::instance();
165 auto padPos = mapper.padPos(globalPadInSector); // global row in sector
166 const auto globalRow = padPos.getRow();
167
168 int rocNumber = sector;
169 int padInROC = globalPadInSector;
170 const int padsInIROC = Mapper::getPadsInIROC();
171 if (globalPadInSector >= padsInIROC) {
172 rocNumber += Mapper::getNumberOfIROCs();
173 padInROC -= padsInIROC;
174 }
175
176 switch (mPadSubset) {
177 case PadSubset::ROC: {
178 return mData[rocNumber].getValue(padInROC);
179 break;
180 }
182 return T{};
183 break;
184 }
185 case PadSubset::Region: {
186 const ROC roc(rocNumber);
187 const auto mappedPad = padPos.getPad();
188 return mData[Mapper::REGION[globalRow] + roc.getSector() * Mapper::NREGIONS].getValue(Mapper::OFFSETCRUGLOBAL[globalRow] + mappedPad);
189 break;
190 }
191 }
192 return T{};
193}
194
195//______________________________________________________________________________
196template <class T>
197inline const T CalDet<T>::getValue(const ROC roc, const size_t row, const size_t pad) const
198{
199 // TODO: might need speedup and beautification
200 const Mapper& mapper = Mapper::instance();
201
202 // bind row and pad to the maximum rows and pads in the requested region
203 const size_t nRows = mapper.getNumberOfRowsROC(roc);
204 const size_t mappedRow = row % nRows;
205 const size_t nPads = mapper.getNumberOfPadsInRowROC(roc, row);
206 const size_t mappedPad = pad % nPads;
207
208 // TODO: implement missing cases
209 switch (mPadSubset) {
210 case PadSubset::ROC: {
211 return mData[roc].getValue(mappedRow, mappedPad);
212 break;
213 }
215 return T{};
216 break;
217 }
218 case PadSubset::Region: {
219 const auto globalRow = roc.isOROC() ? mappedRow + mapper.getNumberOfRowsROC(ROC(0)) : mappedRow;
220 const auto dataRow = Mapper::REGION[globalRow] + roc.getSector() * Mapper::NREGIONS;
221 const auto index = Mapper::OFFSETCRUGLOBAL[globalRow] + mappedPad;
222 assert(dataRow < mData.size());
223 if (index >= mData[dataRow].getData().size()) {
224 // S. Wenzel: We shouldn't come here but we do. For instance for CalDet calibrations loaded from
225 // creator.loadIDCPadFlags(1731274461770);
226
227 // In this case there is an index overflow, leading to invalid reads and potentially a segfault.
228 // To increase stability, for now returning a trivial answer. This can be removed once either the algorithm
229 // or the calibration data has been fixed.
230#ifndef GPUCA_ALIGPUCODE // hide from GPU standalone compilation
231 static bool printMsg = true;
232 if (printMsg) {
233 LOG(error) << "Out of bound access in TPC CalDet ROC " << roc << " row " << row << " pad " << pad << " (no more messages printed)";
234 }
235 printMsg = false;
236#endif
237 return T{};
238 }
239 return mData[dataRow].getValue(index);
240 break;
241 }
242 }
243 return T{};
244}
245
246//______________________________________________________________________________
247template <class T>
248inline const T CalDet<T>::getValue(const CRU cru, const size_t row, const size_t pad) const
249{
250 // TODO: might need speedup and beautification
251 const Mapper& mapper = Mapper::instance();
252 const auto& info = mapper.getPadRegionInfo(cru.region());
253
254 // bind row and pad to the maximum rows and pads in the requested region
255 const size_t nRows = info.getNumberOfPadRows();
256 const size_t mappedRow = row % nRows;
257 const size_t nPads = info.getPadsInRowRegion(mappedRow);
258 const size_t mappedPad = pad % nPads;
259
260 switch (mPadSubset) {
261 case PadSubset::ROC: {
262 const ROC roc = cru.roc();
263 const size_t irocOffset = (cru.rocType() == RocType::IROC) ? 0 : mapper.getNumberOfRowsROC(0);
264 const size_t rowROC = mappedRow + info.getGlobalRowOffset() - irocOffset;
265 const size_t channel = mapper.getPadNumberInROC(PadROCPos(roc, rowROC, mappedPad));
266 // printf("roc %2d, row %2zu, pad %3zu, channel: %3zu\n", roc.getRoc(), rowROC, mappedPad, channel);
267 return mData[roc].getValue(channel);
268 break;
269 }
271 break;
272 }
273 case PadSubset::Region: {
274 return mData[cru].getValue(mappedRow, mappedPad);
275 break;
276 }
277 }
278 return T{};
279}
280
281template <class T>
282inline void CalDet<T>::setValue(const int sec, const int globalPadInSector, const T& value)
283{
284 assert(mPadSubset == PadSubset::ROC);
285 int roc = sec;
286 int padInROC = globalPadInSector;
287 const int padsInIROC = Mapper::getPadsInIROC();
288 if (globalPadInSector >= padsInIROC) {
290 padInROC -= padsInIROC;
291 }
292 mData[roc].setValue(padInROC, value);
293}
294
295template <class T>
296inline void CalDet<T>::setValue(const int sec, const int rowInSector, const int padInRow, const T& value)
297{
298 assert(mPadSubset == PadSubset::ROC);
299 int roc = sec;
300 int rowInROC = rowInSector;
301 const int rowsInIROC = 63;
302 if (rowInSector >= rowsInIROC) {
304 rowInROC -= rowsInIROC;
305 }
306 mData[roc].setValue(rowInROC, padInRow, value);
307}
308
309template <class T>
310inline const T CalDet<T>::getValue(const Sector sec, const int rowInSector, const int padInRow) const
311{
312 assert(mPadSubset == PadSubset::ROC);
313 int roc = sec;
314 int rowInROC = rowInSector;
315 const int rowsInIROC = 63;
316 if (rowInSector >= rowsInIROC) {
318 rowInROC -= rowsInIROC;
319 }
320 return mData[roc].getValue(rowInROC, padInRow);
321}
322
323#ifndef GPUCA_ALIGPUCODE // hide from GPU standalone compilation
324
325//______________________________________________________________________________
326template <class T>
328{
329 // make sure the calibration objects have the same substructure
330 // TODO: perhaps make it independed of this
331 if (mPadSubset != other.mPadSubset) {
332 LOG(error) << "Pad subste type of the objects it not compatible";
333 return *this;
334 }
335
336 for (size_t i = 0; i < mData.size(); ++i) {
337 mData[i] += other.mData[i];
338 }
339 return *this;
340}
341
342//______________________________________________________________________________
343template <class T>
345{
346 // make sure the calibration objects have the same substructure
347 // TODO: perhaps make it independed of this
348 if (mPadSubset != other.mPadSubset) {
349 LOG(error) << "Pad subste type of the objects it not compatible";
350 return *this;
351 }
352
353 for (size_t i = 0; i < mData.size(); ++i) {
354 mData[i] -= other.mData[i];
355 }
356 return *this;
357}
358
359//______________________________________________________________________________
360template <class T>
362{
363 // make sure the calibration objects have the same substructure
364 // TODO: perhaps make it independed of this
365 if (mPadSubset != other.mPadSubset) {
366 LOG(error) << "Pad subste type of the objects it not compatible";
367 return *this;
368 }
369
370 for (size_t i = 0; i < mData.size(); ++i) {
371 mData[i] *= other.mData[i];
372 }
373 return *this;
374}
375
376//______________________________________________________________________________
377template <class T>
379{
380 // make sure the calibration objects have the same substructure
381 // TODO: perhaps make it independed of this
382 if (mPadSubset != other.mPadSubset) {
383 LOG(error) << "Pad subste type of the objects it not compatible";
384 return *this;
385 }
386
387 for (size_t i = 0; i < mData.size(); ++i) {
388 mData[i] /= other.mData[i];
389 }
390 return *this;
391}
392
393//______________________________________________________________________________
394template <class T>
395inline const CalDet<T>& CalDet<T>::operator+=(const T& val)
396{
397 for (auto& cal : mData) {
398 cal += val;
399 }
400 return *this;
401}
402
403//______________________________________________________________________________
404template <class T>
405inline const CalDet<T>& CalDet<T>::operator-=(const T& val)
406{
407 for (auto& cal : mData) {
408 cal -= val;
409 }
410 return *this;
411}
412
413//______________________________________________________________________________
414template <class T>
415inline const CalDet<T>& CalDet<T>::operator*=(const T& val)
416{
417 for (auto& cal : mData) {
418 cal *= val;
419 }
420 return *this;
421}
422
423//______________________________________________________________________________
424template <class T>
425inline const CalDet<T>& CalDet<T>::operator/=(const T& val)
426{
427 for (auto& cal : mData) {
428 cal /= val;
429 }
430 return *this;
431}
432
433//______________________________________________________________________________
434template <class T>
435inline const CalDet<T>& CalDet<T>::operator=(const T& val)
436{
437 for (auto& cal : mData) {
438 cal = val;
439 }
440 return *this;
441}
442
443//______________________________________________________________________________
444template <class T>
445inline bool CalDet<T>::operator==(const CalDet& other) const
446{
447 // make sure the calibration objects have the same substructure
448 // TODO: perhaps make it independed of this
449 if (mPadSubset != other.mPadSubset) {
450 LOG(error) << "Pad subste type of the objects it not compatible";
451 return false;
452 }
453
454 for (size_t i = 0; i < mData.size(); ++i) {
455 if (!(mData[i] == other.mData[i])) {
456 return false;
457 }
458 }
459 return true;
460}
461
462//______________________________________________________________________________
463template <class T>
465{
466 CalDet<T> ret(c1);
467 ret += c2;
468 return ret;
469}
470
471//______________________________________________________________________________
472template <class T>
474{
475 CalDet<T> ret(c1);
476 ret -= c2;
477 return ret;
478}
479// ===| Full detector initialisation |==========================================
480template <class T>
481void CalDet<T>::initData()
482{
483 const auto& mapper = Mapper::instance();
484
485 // ---| Define number of sub pad regions |------------------------------------
486 size_t size = 0;
487 bool hasData = mData.size() > 0;
488 std::string frmt;
489 switch (mPadSubset) {
490 case PadSubset::ROC: {
492 frmt = "{}_ROC_{:02d}";
493 break;
494 }
496 size = Sector::MAXSECTOR * mapper.getNumberOfPartitions();
497 frmt = "{}_Partition_{:02d}";
498 break;
499 }
500 case PadSubset::Region: {
501 size = Sector::MAXSECTOR * mapper.getNumberOfPadRegions();
502 frmt = "{}_Region_{:02d}";
503 break;
504 }
505 }
506
507 for (size_t i = 0; i < size; ++i) {
508 if (!hasData) {
509 mData.push_back(CalType(mPadSubset, i));
510 }
511 mData[i].setName(fmt::format(fmt::runtime(frmt), mName, i));
512 }
513}
514
515#endif // GPUCA_ALIGPUCODE
516
518} // namespace tpc
519} // namespace o2
520
521#endif
int32_t i
std::enable_if_t< std::is_signed< T >::value, bool > hasData(const CalArray< T > &cal)
Definition Painter.cxx:599
uint32_t roc
Definition RawData.h:3
const auto & getData()
unsigned char region() const
Definition CRU.h:64
RocType rocType() const
Definition CRU.h:66
const ROC roc() const
Definition CRU.h:61
const CalDet & operator-=(const CalDet &other)
Definition CalDet.h:344
const CalDet & operator-=(const T &val)
Definition CalDet.h:405
CalDet(PadSubset padSusbset)
Definition CalDet.h:56
const T getValue(const CRU cru, const size_t row, const size_t pad) const
Definition CalDet.h:248
const CalDet & operator+=(const CalDet &other)
Definition CalDet.h:327
friend CalDet< U > operator+(const CalDet< U > &, const CalDet< U > &)
const CalType & getCalArray(size_t position) const
Definition CalDet.h:69
CalDet & operator=(CalDet const &)=default
void setValue(const int sec, const int globalPadInSector, const T &value)
Definition CalDet.h:282
const CalDet & operator=(const T &val)
Definition CalDet.h:435
void setValue(const int sec, const int rowInSector, const int padInRow, const T &value)
Definition CalDet.h:296
const CalDet & operator/=(const T &val)
Definition CalDet.h:425
bool operator==(const CalDet &other) const
Definition CalDet.h:445
CalDet(const std::string_view name, const PadSubset padSusbset=PadSubset::ROC)
Definition CalDet.h:58
const CalDet & operator*=(const CalDet &other)
Definition CalDet.h:361
PadSubset getPadSubset() const
Definition CalDet.h:62
const CalDet & multiply(const T &val)
Definition CalDet.h:93
const CalDet & operator*=(const T &val)
Definition CalDet.h:415
U getMean() const
Definition CalDet.h:114
const T getValue(const ROC roc, const size_t row, const size_t pad) const
Definition CalDet.h:197
U getSum() const
Definition CalDet.h:132
CalDet(CalDet const &)=default
const CalDet & operator/=(const CalDet &other)
Definition CalDet.h:378
const T getValue(const int sec, const int globalPadInSector) const
Definition CalDet.h:160
CalType & getCalArray(size_t position)
Definition CalDet.h:70
void setName(const std::string_view name, bool nameCalArrays=true)
Definition CalDet.h:84
const T getValue(const Sector sec, const int rowInSector, const int padInRow) const
Definition CalDet.h:310
const std::vector< CalType > & getData() const
Definition CalDet.h:64
~CalDet()=default
const CalDet & operator+=(const T &val)
Definition CalDet.h:395
friend CalDet< U > operator-(const CalDet< U > &, const CalDet< U > &)
const std::string & getName() const
Definition CalDet.h:91
std::vector< CalType > & getData()
Definition CalDet.h:65
const PadPos & padPos(GlobalPadNumber padNumber) const
Definition Mapper.h:50
int getNumberOfRowsROC(ROC roc) const
Definition Mapper.h:305
int getNumberOfPadsInRowROC(int roc, int row) const
Definition Mapper.h:342
static Mapper & instance(const std::string mappingDir="")
Definition Mapper.h:44
static constexpr unsigned short getPadsInIROC()
Definition Mapper.h:409
const PadRegionInfo & getPadRegionInfo(const unsigned char region) const
Definition Mapper.h:385
static constexpr unsigned int NREGIONS
total number of regions in one sector
Definition Mapper.h:527
GlobalPadNumber getPadNumberInROC(const PadROCPos &rocPadPosition) const
Definition Mapper.h:96
static constexpr unsigned int OFFSETCRUGLOBAL[PADROWS]
row offset in cru for given global pad row
Definition Mapper.h:579
static constexpr unsigned short getNumberOfIROCs()
Definition Mapper.h:407
static constexpr unsigned REGION[PADROWS]
region for global pad row
Definition Mapper.h:537
Pad and row inside a ROC.
Definition PadROCPos.h:37
@ MaxROC
Definition ROC.h:47
static constexpr int MAXSECTOR
Definition Sector.h:44
GLsizeiptr size
Definition glcorearb.h:659
GLuint index
Definition glcorearb.h:781
GLuint const GLchar * name
Definition glcorearb.h:781
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLboolean * data
Definition glcorearb.h:298
GLuint GLfloat * val
Definition glcorearb.h:1582
CalDet< T > operator+(const CalDet< T > &c1, const CalDet< T > &c2)
Definition CalDet.h:464
@ IROC
Definition Defs.h:48
PadSubset
Definition of the different pad subsets.
Definition Defs.h:63
@ Partition
Partitions (up to 36*5)
@ ROC
ROCs (up to 72)
@ Region
Regions (up to 36*10)
CalDet< T > operator-(const CalDet< T > &c1, const CalDet< T > &c2)
Definition CalDet.h:473
DataT sum(const Vector< DataT, N > &a)
Definition Vector.h:107
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
VectorOfTObjectPtrs other
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::vector< int > row