-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathCalArray.h
More file actions
363 lines (313 loc) · 10.7 KB
/
CalArray.h
File metadata and controls
363 lines (313 loc) · 10.7 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// 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 ALICEO2_TPC_CALARRAY_H_
#define ALICEO2_TPC_CALARRAY_H_
#include <algorithm>
#include <memory>
#include <vector>
#include <string>
#include <numeric>
#include <type_traits>
#include "TPCBase/Mapper.h"
#ifndef GPUCA_ALIGPUCODE
#include "Framework/Logger.h"
#include <boost/format.hpp>
#endif
#ifdef NDEBUG
#undef NDEBUG
#include <cassert>
#endif
namespace o2
{
namespace tpc
{
/// Class to hold calibration data on a pad level
///
/// Calibration data per pad for a certain subset of pads:
/// Full readout chamber, readout partition, or pad region
template <class T>
class CalArray
{
public:
/// Default constructor
CalArray() = default;
/// Default destructor
~CalArray() = default;
/// Constructor sets a default name depending on the pad subset type and number
/// \param padSubset pad subset type (e.g. PadSubset::ROC)
/// \param padSubsetNumber number of the pad subset (e.g. 0 for ROC 0)
CalArray(const PadSubset padSubset, const int padSubsetNumber)
: mName(),
mData(),
mPadSubset(padSubset),
mPadSubsetNumber(padSubsetNumber)
{
// initialize the data array
initData();
}
/// Constructor assumes PadSubset::ROC
/// \param name name of the calibration array
/// \param padSubsetNumber number of the pad subset (e.g. 0 for ROC 0)
CalArray(const std::string_view name, const int padSubsetNumber)
: mName(name),
mData(),
mPadSubset(PadSubset::ROC),
mPadSubsetNumber(padSubsetNumber)
{
// initialize the data array
initData();
}
/// Constructor
/// \param name name of the calibration array
/// \param padSubsetNumber number of the pad subset (e.g. 0 for ROC 0)
CalArray(const std::string_view name, const PadSubset padSubset, const int padSubsetNumber)
: mName(name),
mData(),
mPadSubset(padSubset),
mPadSubsetNumber(padSubsetNumber)
{
// initialize the data array
initData();
}
/// Return the pad subset type
/// \return pad subset type
PadSubset getPadSubset() const { return mPadSubset; }
/// Return the pad subset number (e.g. ROC number)
/// \return pad subset number (e.g. ROC number)
int getPadSubsetNumber() const { return mPadSubsetNumber; }
void setValue(const size_t channel, const T& value) { mData[channel] = value; }
const T getValue(const size_t channel) const
{
assert(channel < mData.size());
return mData[channel];
}
void setValue(const size_t row, const size_t pad, const T& value);
const T getValue(const size_t row, const size_t pad) const;
void setName(const std::string& name) { mName = name; }
const std::string& getName() const { return mName; }
const std::vector<T>& getData() const { return mData; }
std::vector<T>& getData() { return mData; }
/// calculate the sum of all elements
template <typename U = T>
const U getSum() const
{
return std::accumulate(mData.begin(), mData.end(), U{});
}
/// Multiply all val to all channels
const CalArray<T>& multiply(const T& val) { return *this *= val; }
/// Add other to this channel by channel
const CalArray& operator+=(const CalArray& other);
/// Subtract other from this channel by channel
const CalArray& operator-=(const CalArray& other);
/// Multiply other to this channel by channel
const CalArray& operator*=(const CalArray& other);
/// Divide this by other channel by channel
const CalArray& operator/=(const CalArray& other);
/// check for equality
bool operator==(const CalArray& other) const;
/// Add value to all channels
const CalArray& operator+=(const T& val);
/// Subtract value from all channels
const CalArray& operator-=(const T& val);
/// Multiply value to all channels
const CalArray& operator*=(const T& val);
/// Divide value on all channels
const CalArray& operator/=(const T& val);
/// assigment to all channls
const CalArray& operator=(const T& val)
{
std::fill(mData.begin(), mData.end(), val);
return *this;
}
template <typename U = T>
U getMean() const
{
const auto& vals = mData;
const U nVal = static_cast<U>(vals.size());
const U sum = std::accumulate(vals.begin(), vals.end(), 0.f);
return (nVal > 0) ? sum / nVal : U{0};
}
private:
std::string mName;
// better to use std::array?
// std::vector<T, Vc::Allocator<T>> mData;
// how to use Vc::Allocator in this case? Specialisation for float, double, etc?
std::vector<T> mData; ///< calibration data
PadSubset mPadSubset; ///< Subset type
int mPadSubsetNumber; ///< Number of the pad subset, e.g. ROC 0 is IROC A00
/// initialize the data array depending on what is set as PadSubset
void initData();
};
#ifndef GPUCA_ALIGPUCODE
// ===| pad region etc. initialisation |========================================
template <class T>
void CalArray<T>::initData()
{
const auto& mapper = Mapper::instance();
switch (mPadSubset) {
case PadSubset::ROC: {
mData.resize(ROC(mPadSubsetNumber).rocType() == RocType::IROC ? mapper.getPadsInIROC() : mapper.getPadsInOROC());
if (mName.empty()) {
setName(boost::str(boost::format("ROC_%1$02d") % mPadSubsetNumber));
}
break;
}
case PadSubset::Partition: {
mData.resize(mapper.getPartitionInfo(mPadSubsetNumber % mapper.getNumberOfPartitions()).getNumberOfPads());
if (mName.empty()) {
setName(boost::str(boost::format("Partition_%1$03d") % mPadSubsetNumber));
}
break;
}
case PadSubset::Region: {
mData.resize(mapper.getPadRegionInfo(mPadSubsetNumber % mapper.getNumberOfPadRegions()).getNumberOfPads());
if (mName.empty()) {
setName(boost::str(boost::format("Region_%1$03d") % mPadSubsetNumber));
}
break;
}
}
}
//______________________________________________________________________________
template <class T>
inline void CalArray<T>::setValue(const size_t row, const size_t pad, const T& value)
{
/// \todo might need check for row, pad or position limits
const auto& mapper = Mapper::instance();
size_t position = mapper.getPadNumber(mPadSubset, mPadSubsetNumber, row, pad);
setValue(position, value);
}
//______________________________________________________________________________
template <class T>
inline const T CalArray<T>::getValue(const size_t row, const size_t pad) const
{
/// \todo might need check for row, pad or position limits
const auto& mapper = Mapper::instance();
size_t position = mapper.getPadNumber(mPadSubset, mPadSubsetNumber, row, pad);
return getValue(position);
}
//______________________________________________________________________________
template <class T>
inline const CalArray<T>& CalArray<T>::operator+=(const CalArray<T>& other)
{
if (!((mPadSubset == other.mPadSubset) && (mPadSubsetNumber == other.mPadSubsetNumber))) {
LOG(error) << "You are trying to operate on incompatible objects: Pad subset type and number must be the same on both objects";
return *this;
}
for (size_t i = 0; i < mData.size(); ++i) {
if constexpr (std::is_same_v<T, bool>) {
mData[i] = mData[i] | other.getValue(i);
} else {
mData[i] += other.getValue(i);
}
}
return *this;
}
//______________________________________________________________________________
template <class T>
inline const CalArray<T>& CalArray<T>::operator-=(const CalArray<T>& other)
{
if (!((mPadSubset == other.mPadSubset) && (mPadSubsetNumber == other.mPadSubsetNumber))) {
LOG(error) << "You are trying to operate on incompatible objects: Pad subset type and number must be the same on both objects";
return *this;
}
for (size_t i = 0; i < mData.size(); ++i) {
mData[i] -= other.getValue(i);
}
return *this;
}
//______________________________________________________________________________
template <class T>
inline const CalArray<T>& CalArray<T>::operator*=(const CalArray<T>& other)
{
if (!((mPadSubset == other.mPadSubset) && (mPadSubsetNumber == other.mPadSubsetNumber))) {
LOG(error) << "pad subset type of the objects it not compatible";
return *this;
}
for (size_t i = 0; i < mData.size(); ++i) {
if constexpr (std::is_same_v<T, bool>) {
mData[i] = mData[i] & other.getValue(i);
} else {
mData[i] *= other.getValue(i);
}
}
return *this;
}
//______________________________________________________________________________
template <class T>
inline const CalArray<T>& CalArray<T>::operator/=(const CalArray<T>& other)
{
if (!((mPadSubset == other.mPadSubset) && (mPadSubsetNumber == other.mPadSubsetNumber))) {
LOG(error) << "pad subset type of the objects it not compatible";
return *this;
}
for (size_t i = 0; i < mData.size(); ++i) {
if (other.getValue(i) != 0) {
mData[i] /= other.getValue(i);
} else {
mData[i] = 0;
LOG(debug) << "Division by 0 detected! Value was set to 0.";
}
}
return *this;
}
//______________________________________________________________________________
template <class T>
inline bool CalArray<T>::operator==(const CalArray<T>& other) const
{
if (!((mPadSubset == other.mPadSubset) && (mPadSubsetNumber == other.mPadSubsetNumber))) {
LOG(error) << "pad subset type of the objects it not compatible";
return false;
}
bool isSame = (mData == other.mData);
return isSame;
}
//______________________________________________________________________________
template <class T>
inline const CalArray<T>& CalArray<T>::operator+=(const T& val)
{
for (auto& data : mData) {
data += val;
}
return *this;
}
//______________________________________________________________________________
template <class T>
inline const CalArray<T>& CalArray<T>::operator-=(const T& val)
{
for (auto& data : mData) {
data -= val;
}
return *this;
}
//______________________________________________________________________________
template <class T>
inline const CalArray<T>& CalArray<T>::operator*=(const T& val)
{
for (auto& data : mData) {
data *= val;
}
return *this;
}
//______________________________________________________________________________
template <class T>
inline const CalArray<T>& CalArray<T>::operator/=(const T& val)
{
for (auto& data : mData) {
data /= val;
}
return *this;
}
using CalROC = CalArray<float>;
#endif // GPUCA_ALIGPUCODE
} // namespace tpc
} // namespace o2
#endif