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
33namespace o2
34{
35namespace tpc
36{
39template <class T>
40class CalDet
41{
42 using CalType = CalArray<T>;
43
44 public:
45 CalDet() { initData(); }
46 CalDet(CalDet const&) = default;
47 CalDet& operator=(CalDet const&) = default;
48 ~CalDet() = default;
49
50 CalDet(PadSubset padSusbset) : mName{"PadCalibrationObject"}, mData{}, mPadSubset{padSusbset} { initData(); }
51
52 CalDet(const std::string_view name, const PadSubset padSusbset = PadSubset::ROC) : mName(name), mData(), mPadSubset(padSusbset) { initData(); }
53
56 PadSubset getPadSubset() const { return mPadSubset; }
57
58 const std::vector<CalType>& getData() const { return mData; }
59 std::vector<CalType>& getData() { return mData; }
60 // void setValue(const unsigned int channel, const T value) { mData[channel] = value; }
61 // const T& getValue(const unsigned int channel) const { return mData[channel]; }
62
63 const CalType& getCalArray(size_t position) const { return mData[position]; }
64 CalType& getCalArray(size_t position) { return mData[position]; }
65
68 const T getValue(const int sec, const int globalPadInSector) const;
69 void setValue(const int sec, const int globalPadInSector, const T& value);
70 void setValue(const int sec, const int rowInSector, const int padInRow, const T& value);
71
74 const T getValue(const ROC roc, const size_t row, const size_t pad) const;
75 const T getValue(const CRU cru, const size_t row, const size_t pad) const;
76 const T getValue(const Sector sec, const int rowInSector, const int padInRow) const;
77
78 void setName(const std::string_view name, bool nameCalArrays = true)
79 {
80 mName = name.data();
81 if (nameCalArrays) {
82 initData();
83 }
84 }
85 const std::string& getName() const { return mName; }
86
87 const CalDet& multiply(const T& val) { return *this *= val; }
88 const CalDet& operator+=(const CalDet& other);
89 const CalDet& operator-=(const CalDet& other);
90 const CalDet& operator*=(const CalDet& other);
91 const CalDet& operator/=(const CalDet& other);
92 bool operator==(const CalDet& other) const;
93
94 const CalDet& operator+=(const T& val);
95 const CalDet& operator-=(const T& val);
96 const CalDet& operator*=(const T& val);
97 const CalDet& operator/=(const T& val);
98
99 const CalDet& operator=(const T& val);
100
101 template <class U>
102 friend CalDet<U> operator+(const CalDet<U>&, const CalDet<U>&);
103
104 template <class U>
105 friend CalDet<U> operator-(const CalDet<U>&, const CalDet<U>&);
106
107 template <typename U = T>
108 U getMean() const
109 {
110 if (mData.size() == 0) {
111 return U{0};
112 }
113
114 U nVal = 0;
115 U sum = 0;
116 for (const auto& data : mData) {
117 const auto& vals = data.getData();
118 sum += std::accumulate(vals.begin(), vals.end(), U{0});
119 nVal += static_cast<U>(vals.size());
120 }
121
122 return (nVal > 0) ? sum / nVal : U{0};
123 }
124
125 template <typename U = T>
126 U getSum() const
127 {
128 if (mData.size() == 0) {
129 return U{};
130 }
131
132 U sum{};
133 for (const auto& data : mData) {
134 const auto& vals = data.getData();
135 sum += data.template getSum<U>();
136 }
137
138 return sum;
139 }
140
141 private:
142 std::string mName;
143 std::vector<CalType> mData;
144 PadSubset mPadSubset = PadSubset::ROC;
145
147 void initData();
148
149 ClassDefNV(CalDet, 1)
150};
151
152//______________________________________________________________________________
153template <class T>
154inline const T CalDet<T>::getValue(const int sector, const int globalPadInSector) const
155{
156 // This shold be a temporary speedup, a proper restructuring of Mapper and CalDet/CalArray is needed.
157 // The default granularity for the moment should be ROC, for the assumptions below this should be assured
158 const Mapper& mapper = Mapper::instance();
159 auto padPos = mapper.padPos(globalPadInSector); // global row in sector
160 const auto globalRow = padPos.getRow();
161
162 int rocNumber = sector;
163 int padInROC = globalPadInSector;
164 const int padsInIROC = Mapper::getPadsInIROC();
165 if (globalPadInSector >= padsInIROC) {
166 rocNumber += Mapper::getNumberOfIROCs();
167 padInROC -= padsInIROC;
168 }
169
170 switch (mPadSubset) {
171 case PadSubset::ROC: {
172 return mData[rocNumber].getValue(padInROC);
173 break;
174 }
176 return T{};
177 break;
178 }
179 case PadSubset::Region: {
180 const ROC roc(rocNumber);
181 const auto mappedPad = padPos.getPad();
182 return mData[Mapper::REGION[globalRow] + roc.getSector() * Mapper::NREGIONS].getValue(Mapper::OFFSETCRUGLOBAL[globalRow] + mappedPad);
183 break;
184 }
185 }
186 return T{};
187}
188
189//______________________________________________________________________________
190template <class T>
191inline const T CalDet<T>::getValue(const ROC roc, const size_t row, const size_t pad) const
192{
193 // TODO: might need speedup and beautification
194 const Mapper& mapper = Mapper::instance();
195
196 // bind row and pad to the maximum rows and pads in the requested region
197 const size_t nRows = mapper.getNumberOfRowsROC(roc);
198 const size_t mappedRow = row % nRows;
199 const size_t nPads = mapper.getNumberOfPadsInRowROC(roc, row);
200 const size_t mappedPad = pad % nPads;
201
202 // TODO: implement missing cases
203 switch (mPadSubset) {
204 case PadSubset::ROC: {
205 return mData[roc].getValue(mappedRow, mappedPad);
206 break;
207 }
209 return T{};
210 break;
211 }
212 case PadSubset::Region: {
213 const auto globalRow = roc.isOROC() ? mappedRow + mapper.getNumberOfRowsROC(ROC(0)) : mappedRow;
214 return mData[Mapper::REGION[globalRow] + roc.getSector() * Mapper::NREGIONS].getValue(Mapper::OFFSETCRUGLOBAL[globalRow] + mappedPad);
215 break;
216 }
217 }
218 return T{};
219}
220
221//______________________________________________________________________________
222template <class T>
223inline const T CalDet<T>::getValue(const CRU cru, const size_t row, const size_t pad) const
224{
225 // TODO: might need speedup and beautification
226 const Mapper& mapper = Mapper::instance();
227 const auto& info = mapper.getPadRegionInfo(cru.region());
228
229 // bind row and pad to the maximum rows and pads in the requested region
230 const size_t nRows = info.getNumberOfPadRows();
231 const size_t mappedRow = row % nRows;
232 const size_t nPads = info.getPadsInRowRegion(mappedRow);
233 const size_t mappedPad = pad % nPads;
234
235 switch (mPadSubset) {
236 case PadSubset::ROC: {
237 const ROC roc = cru.roc();
238 const size_t irocOffset = (cru.rocType() == RocType::IROC) ? 0 : mapper.getNumberOfRowsROC(0);
239 const size_t rowROC = mappedRow + info.getGlobalRowOffset() - irocOffset;
240 const size_t channel = mapper.getPadNumberInROC(PadROCPos(roc, rowROC, mappedPad));
241 // printf("roc %2d, row %2zu, pad %3zu, channel: %3zu\n", roc.getRoc(), rowROC, mappedPad, channel);
242 return mData[roc].getValue(channel);
243 break;
244 }
246 break;
247 }
248 case PadSubset::Region: {
249 return mData[cru].getValue(mappedRow, mappedPad);
250 break;
251 }
252 }
253 return T{};
254}
255
256template <class T>
257inline void CalDet<T>::setValue(const int sec, const int globalPadInSector, const T& value)
258{
259 assert(mPadSubset == PadSubset::ROC);
260 int roc = sec;
261 int padInROC = globalPadInSector;
262 const int padsInIROC = Mapper::getPadsInIROC();
263 if (globalPadInSector >= padsInIROC) {
265 padInROC -= padsInIROC;
266 }
267 mData[roc].setValue(padInROC, value);
268}
269
270template <class T>
271inline void CalDet<T>::setValue(const int sec, const int rowInSector, const int padInRow, const T& value)
272{
273 assert(mPadSubset == PadSubset::ROC);
274 int roc = sec;
275 int rowInROC = rowInSector;
276 const int rowsInIROC = 63;
277 if (rowInSector >= rowsInIROC) {
279 rowInROC -= rowsInIROC;
280 }
281 mData[roc].setValue(rowInROC, padInRow, value);
282}
283
284template <class T>
285inline const T CalDet<T>::getValue(const Sector sec, const int rowInSector, const int padInRow) const
286{
287 assert(mPadSubset == PadSubset::ROC);
288 int roc = sec;
289 int rowInROC = rowInSector;
290 const int rowsInIROC = 63;
291 if (rowInSector >= rowsInIROC) {
293 rowInROC -= rowsInIROC;
294 }
295 return mData[roc].getValue(rowInROC, padInRow);
296}
297
298#ifndef GPUCA_ALIGPUCODE // hide from GPU standalone compilation
299
300//______________________________________________________________________________
301template <class T>
303{
304 // make sure the calibration objects have the same substructure
305 // TODO: perhaps make it independed of this
306 if (mPadSubset != other.mPadSubset) {
307 LOG(error) << "Pad subste type of the objects it not compatible";
308 return *this;
309 }
310
311 for (size_t i = 0; i < mData.size(); ++i) {
312 mData[i] += other.mData[i];
313 }
314 return *this;
315}
316
317//______________________________________________________________________________
318template <class T>
320{
321 // make sure the calibration objects have the same substructure
322 // TODO: perhaps make it independed of this
323 if (mPadSubset != other.mPadSubset) {
324 LOG(error) << "Pad subste type of the objects it not compatible";
325 return *this;
326 }
327
328 for (size_t i = 0; i < mData.size(); ++i) {
329 mData[i] -= other.mData[i];
330 }
331 return *this;
332}
333
334//______________________________________________________________________________
335template <class T>
337{
338 // make sure the calibration objects have the same substructure
339 // TODO: perhaps make it independed of this
340 if (mPadSubset != other.mPadSubset) {
341 LOG(error) << "Pad subste type of the objects it not compatible";
342 return *this;
343 }
344
345 for (size_t i = 0; i < mData.size(); ++i) {
346 mData[i] *= other.mData[i];
347 }
348 return *this;
349}
350
351//______________________________________________________________________________
352template <class T>
354{
355 // make sure the calibration objects have the same substructure
356 // TODO: perhaps make it independed of this
357 if (mPadSubset != other.mPadSubset) {
358 LOG(error) << "Pad subste type of the objects it not compatible";
359 return *this;
360 }
361
362 for (size_t i = 0; i < mData.size(); ++i) {
363 mData[i] /= other.mData[i];
364 }
365 return *this;
366}
367
368//______________________________________________________________________________
369template <class T>
370inline const CalDet<T>& CalDet<T>::operator+=(const T& val)
371{
372 for (auto& cal : mData) {
373 cal += val;
374 }
375 return *this;
376}
377
378//______________________________________________________________________________
379template <class T>
380inline const CalDet<T>& CalDet<T>::operator-=(const T& val)
381{
382 for (auto& cal : mData) {
383 cal -= val;
384 }
385 return *this;
386}
387
388//______________________________________________________________________________
389template <class T>
390inline const CalDet<T>& CalDet<T>::operator*=(const T& val)
391{
392 for (auto& cal : mData) {
393 cal *= val;
394 }
395 return *this;
396}
397
398//______________________________________________________________________________
399template <class T>
400inline const CalDet<T>& CalDet<T>::operator/=(const T& val)
401{
402 for (auto& cal : mData) {
403 cal /= val;
404 }
405 return *this;
406}
407
408//______________________________________________________________________________
409template <class T>
410inline const CalDet<T>& CalDet<T>::operator=(const T& val)
411{
412 for (auto& cal : mData) {
413 cal = val;
414 }
415 return *this;
416}
417
418//______________________________________________________________________________
419template <class T>
420inline bool CalDet<T>::operator==(const CalDet& other) const
421{
422 // make sure the calibration objects have the same substructure
423 // TODO: perhaps make it independed of this
424 if (mPadSubset != other.mPadSubset) {
425 LOG(error) << "Pad subste type of the objects it not compatible";
426 return false;
427 }
428
429 for (size_t i = 0; i < mData.size(); ++i) {
430 if (!(mData[i] == other.mData[i])) {
431 return false;
432 }
433 }
434 return true;
435}
436
437//______________________________________________________________________________
438template <class T>
440{
441 CalDet<T> ret(c1);
442 ret += c2;
443 return ret;
444}
445
446//______________________________________________________________________________
447template <class T>
449{
450 CalDet<T> ret(c1);
451 ret -= c2;
452 return ret;
453}
454// ===| Full detector initialisation |==========================================
455template <class T>
456void CalDet<T>::initData()
457{
458 const auto& mapper = Mapper::instance();
459
460 // ---| Define number of sub pad regions |------------------------------------
461 size_t size = 0;
462 bool hasData = mData.size() > 0;
463 std::string frmt;
464 switch (mPadSubset) {
465 case PadSubset::ROC: {
467 frmt = "{}_ROC_{:02d}";
468 break;
469 }
471 size = Sector::MAXSECTOR * mapper.getNumberOfPartitions();
472 frmt = "{}_Partition_{:02d}";
473 break;
474 }
475 case PadSubset::Region: {
476 size = Sector::MAXSECTOR * mapper.getNumberOfPadRegions();
477 frmt = "{}_Region_{:02d}";
478 break;
479 }
480 }
481
482 for (size_t i = 0; i < size; ++i) {
483 if (!hasData) {
484 mData.push_back(CalType(mPadSubset, i));
485 }
486 mData[i].setName(fmt::format(fmt::runtime(frmt), mName, i));
487 }
488}
489
490#endif // GPUCA_ALIGPUCODE
491
493} // namespace tpc
494} // namespace o2
495
496#endif
int32_t i
bool const GPUTPCGMMerger::trackCluster * c1
bool const GPUTPCGMMerger::trackCluster const clcomparestruct * c2
std::enable_if_t< std::is_signed< T >::value, bool > hasData(const CalArray< T > &cal)
Definition Painter.cxx:515
uint32_t roc
Definition RawData.h:3
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:319
const CalDet & operator-=(const T &val)
Definition CalDet.h:380
CalDet(PadSubset padSusbset)
Definition CalDet.h:50
const T getValue(const CRU cru, const size_t row, const size_t pad) const
Definition CalDet.h:223
const CalDet & operator+=(const CalDet &other)
Definition CalDet.h:302
friend CalDet< U > operator+(const CalDet< U > &, const CalDet< U > &)
const CalType & getCalArray(size_t position) const
Definition CalDet.h:63
CalDet & operator=(CalDet const &)=default
void setValue(const int sec, const int globalPadInSector, const T &value)
Definition CalDet.h:257
const CalDet & operator=(const T &val)
Definition CalDet.h:410
void setValue(const int sec, const int rowInSector, const int padInRow, const T &value)
Definition CalDet.h:271
const CalDet & operator/=(const T &val)
Definition CalDet.h:400
bool operator==(const CalDet &other) const
Definition CalDet.h:420
CalDet(const std::string_view name, const PadSubset padSusbset=PadSubset::ROC)
Definition CalDet.h:52
const CalDet & operator*=(const CalDet &other)
Definition CalDet.h:336
PadSubset getPadSubset() const
Definition CalDet.h:56
const CalDet & multiply(const T &val)
Definition CalDet.h:87
const CalDet & operator*=(const T &val)
Definition CalDet.h:390
U getMean() const
Definition CalDet.h:108
const T getValue(const ROC roc, const size_t row, const size_t pad) const
Definition CalDet.h:191
U getSum() const
Definition CalDet.h:126
CalDet(CalDet const &)=default
const CalDet & operator/=(const CalDet &other)
Definition CalDet.h:353
const T getValue(const int sec, const int globalPadInSector) const
Definition CalDet.h:154
CalType & getCalArray(size_t position)
Definition CalDet.h:64
void setName(const std::string_view name, bool nameCalArrays=true)
Definition CalDet.h:78
const T getValue(const Sector sec, const int rowInSector, const int padInRow) const
Definition CalDet.h:285
const std::vector< CalType > & getData() const
Definition CalDet.h:58
~CalDet()=default
const CalDet & operator+=(const T &val)
Definition CalDet.h:370
friend CalDet< U > operator-(const CalDet< U > &, const CalDet< U > &)
const std::string & getName() const
Definition CalDet.h:85
std::vector< CalType > & getData()
Definition CalDet.h:59
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 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:439
@ 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:448
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