Project
Loading...
Searching...
No Matches
CalArray.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_CALARRAY_H_
13#define ALICEO2_TPC_CALARRAY_H_
14
15#include <algorithm>
16#include <memory>
17#include <vector>
18#include <string>
19#include <numeric>
20#include <type_traits>
21
22#include "TPCBase/Mapper.h"
23
24#ifndef GPUCA_ALIGPUCODE
25#include "Framework/Logger.h"
26#include <boost/format.hpp>
27#endif
28
29#ifdef NDEBUG
30#undef NDEBUG
31#include <cassert>
32#endif
33
34namespace o2
35{
36namespace tpc
37{
38
43template <class T>
45{
46 public:
48 CalArray() = default;
49
51 ~CalArray() = default;
52
56 CalArray(const PadSubset padSubset, const int padSubsetNumber)
57 : mName(),
58 mData(),
59 mPadSubset(padSubset),
60 mPadSubsetNumber(padSubsetNumber)
61 {
62 // initialize the data array
63 initData();
64 }
65
69 CalArray(const std::string_view name, const int padSubsetNumber)
70 : mName(name),
71 mData(),
72 mPadSubset(PadSubset::ROC),
73 mPadSubsetNumber(padSubsetNumber)
74 {
75 // initialize the data array
76 initData();
77 }
78
82 CalArray(const std::string_view name, const PadSubset padSubset, const int padSubsetNumber)
83 : mName(name),
84 mData(),
85 mPadSubset(padSubset),
86 mPadSubsetNumber(padSubsetNumber)
87 {
88 // initialize the data array
89 initData();
90 }
91
94 PadSubset getPadSubset() const { return mPadSubset; }
95
98 int getPadSubsetNumber() const { return mPadSubsetNumber; }
99
100 void setValue(const size_t channel, const T& value) { mData[channel] = value; }
101 const T getValue(const size_t channel) const
102 {
103 assert(channel < mData.size());
104 return mData[channel];
105 }
106
107 void setValue(const size_t row, const size_t pad, const T& value);
108 const T getValue(const size_t row, const size_t pad) const;
109
110 void setName(const std::string& name) { mName = name; }
111 const std::string& getName() const { return mName; }
112
113 const std::vector<T>& getData() const { return mData; }
114 std::vector<T>& getData() { return mData; }
115
117 template <typename U = T>
118 const U getSum() const
119 {
120 return std::accumulate(mData.begin(), mData.end(), U{});
121 }
122
124 const CalArray<T>& multiply(const T& val) { return *this *= val; }
125
127 const CalArray& operator+=(const CalArray& other);
128
130 const CalArray& operator-=(const CalArray& other);
131
133 const CalArray& operator*=(const CalArray& other);
134
136 const CalArray& operator/=(const CalArray& other);
137
139 bool operator==(const CalArray& other) const;
140
142 const CalArray& operator+=(const T& val);
143
145 const CalArray& operator-=(const T& val);
146
148 const CalArray& operator*=(const T& val);
149
151 const CalArray& operator/=(const T& val);
152
154 const CalArray& operator=(const T& val)
155 {
156 std::fill(mData.begin(), mData.end(), val);
157 return *this;
158 }
159
160 template <typename U = T>
161 U getMean() const
162 {
163 const auto& vals = mData;
164 const U nVal = static_cast<U>(vals.size());
165 const U sum = std::accumulate(vals.begin(), vals.end(), 0.f);
166
167 return (nVal > 0) ? sum / nVal : U{0};
168 }
169
170 private:
171 std::string mName;
172 // better to use std::array?
173 // std::vector<T, Vc::Allocator<T>> mData;
174 // how to use Vc::Allocator in this case? Specialisation for float, double, etc?
175 std::vector<T> mData;
176 PadSubset mPadSubset;
177 int mPadSubsetNumber;
178
180 void initData();
181};
182
183#ifndef GPUCA_ALIGPUCODE
184
185// ===| pad region etc. initialisation |========================================
186template <class T>
187void CalArray<T>::initData()
188{
189 const auto& mapper = Mapper::instance();
190
191 switch (mPadSubset) {
192 case PadSubset::ROC: {
193 mData.resize(ROC(mPadSubsetNumber).rocType() == RocType::IROC ? mapper.getPadsInIROC() : mapper.getPadsInOROC());
194 if (mName.empty()) {
195 setName(boost::str(boost::format("ROC_%1$02d") % mPadSubsetNumber));
196 }
197 break;
198 }
200 mData.resize(mapper.getPartitionInfo(mPadSubsetNumber % mapper.getNumberOfPartitions()).getNumberOfPads());
201 if (mName.empty()) {
202 setName(boost::str(boost::format("Partition_%1$03d") % mPadSubsetNumber));
203 }
204 break;
205 }
206 case PadSubset::Region: {
207 mData.resize(mapper.getPadRegionInfo(mPadSubsetNumber % mapper.getNumberOfPadRegions()).getNumberOfPads());
208 if (mName.empty()) {
209 setName(boost::str(boost::format("Region_%1$03d") % mPadSubsetNumber));
210 }
211 break;
212 }
213 }
214}
215//______________________________________________________________________________
216template <class T>
217inline void CalArray<T>::setValue(const size_t row, const size_t pad, const T& value)
218{
220 const auto& mapper = Mapper::instance();
221 size_t position = mapper.getPadNumber(mPadSubset, mPadSubsetNumber, row, pad);
222 setValue(position, value);
223}
224
225//______________________________________________________________________________
226template <class T>
227inline const T CalArray<T>::getValue(const size_t row, const size_t pad) const
228{
230 const auto& mapper = Mapper::instance();
231 size_t position = mapper.getPadNumber(mPadSubset, mPadSubsetNumber, row, pad);
232 return getValue(position);
233}
234
235//______________________________________________________________________________
236template <class T>
238{
239 if (!((mPadSubset == other.mPadSubset) && (mPadSubsetNumber == other.mPadSubsetNumber))) {
240 LOG(error) << "You are trying to operate on incompatible objects: Pad subset type and number must be the same on both objects";
241 return *this;
242 }
243 for (size_t i = 0; i < mData.size(); ++i) {
244 if constexpr (std::is_same_v<T, bool>) {
245 mData[i] = mData[i] | other.getValue(i);
246 } else {
247 mData[i] += other.getValue(i);
248 }
249 }
250 return *this;
251}
252
253//______________________________________________________________________________
254template <class T>
256{
257 if (!((mPadSubset == other.mPadSubset) && (mPadSubsetNumber == other.mPadSubsetNumber))) {
258 LOG(error) << "You are trying to operate on incompatible objects: Pad subset type and number must be the same on both objects";
259 return *this;
260 }
261 for (size_t i = 0; i < mData.size(); ++i) {
262 mData[i] -= other.getValue(i);
263 }
264 return *this;
265}
266
267//______________________________________________________________________________
268template <class T>
270{
271 if (!((mPadSubset == other.mPadSubset) && (mPadSubsetNumber == other.mPadSubsetNumber))) {
272 LOG(error) << "pad subset type of the objects it not compatible";
273 return *this;
274 }
275 for (size_t i = 0; i < mData.size(); ++i) {
276 if constexpr (std::is_same_v<T, bool>) {
277 mData[i] = mData[i] & other.getValue(i);
278 } else {
279 mData[i] *= other.getValue(i);
280 }
281 }
282 return *this;
283}
284
285//______________________________________________________________________________
286template <class T>
288{
289 if (!((mPadSubset == other.mPadSubset) && (mPadSubsetNumber == other.mPadSubsetNumber))) {
290 LOG(error) << "pad subset type of the objects it not compatible";
291 return *this;
292 }
293 for (size_t i = 0; i < mData.size(); ++i) {
294 if (other.getValue(i) != 0) {
295 mData[i] /= other.getValue(i);
296 } else {
297 mData[i] = 0;
298 LOG(debug) << "Division by 0 detected! Value was set to 0.";
299 }
300 }
301 return *this;
302}
303
304//______________________________________________________________________________
305template <class T>
307{
308 if (!((mPadSubset == other.mPadSubset) && (mPadSubsetNumber == other.mPadSubsetNumber))) {
309 LOG(error) << "pad subset type of the objects it not compatible";
310 return false;
311 }
312 bool isSame = (mData == other.mData);
313 return isSame;
314}
315
316//______________________________________________________________________________
317template <class T>
319{
320 for (auto& data : mData) {
321 data += val;
322 }
323 return *this;
324}
325
326//______________________________________________________________________________
327template <class T>
329{
330 for (auto& data : mData) {
331 data -= val;
332 }
333 return *this;
334}
335
336//______________________________________________________________________________
337template <class T>
339{
340 for (auto& data : mData) {
341 data *= val;
342 }
343 return *this;
344}
345
346//______________________________________________________________________________
347template <class T>
349{
350 for (auto& data : mData) {
351 data /= val;
352 }
353 return *this;
354}
355
357
358#endif // GPUCA_ALIGPUCODE
359
360} // namespace tpc
361} // namespace o2
362
363#endif
std::ostringstream debug
int32_t i
CalArray()=default
Default constructor.
std::vector< T > & getData()
Definition CalArray.h:114
const CalArray< T > & multiply(const T &val)
Multiply all val to all channels.
Definition CalArray.h:124
const CalArray & operator+=(const CalArray &other)
Add other to this channel by channel.
Definition CalArray.h:237
void setValue(const size_t channel, const T &value)
Definition CalArray.h:100
const U getSum() const
calculate the sum of all elements
Definition CalArray.h:118
U getMean() const
Definition CalArray.h:161
const CalArray & operator/=(const CalArray &other)
Divide this by other channel by channel.
Definition CalArray.h:287
const CalArray & operator-=(const CalArray &other)
Subtract other from this channel by channel.
Definition CalArray.h:255
CalArray(const PadSubset padSubset, const int padSubsetNumber)
Definition CalArray.h:56
const std::string & getName() const
Definition CalArray.h:111
PadSubset getPadSubset() const
Definition CalArray.h:94
const CalArray & operator=(const T &val)
assigment to all channls
Definition CalArray.h:154
const std::vector< T > & getData() const
Definition CalArray.h:113
int getPadSubsetNumber() const
Definition CalArray.h:98
CalArray(const std::string_view name, const PadSubset padSubset, const int padSubsetNumber)
Definition CalArray.h:82
const CalArray & operator*=(const CalArray &other)
Multiply other to this channel by channel.
Definition CalArray.h:269
CalArray(const std::string_view name, const int padSubsetNumber)
Definition CalArray.h:69
bool operator==(const CalArray &other) const
check for equality
Definition CalArray.h:306
~CalArray()=default
Default destructor.
void setName(const std::string &name)
Definition CalArray.h:110
const T getValue(const size_t channel) const
Definition CalArray.h:101
static Mapper & instance(const std::string mappingDir="")
Definition Mapper.h:44
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
@ 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)
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 ...
double getValue(DPVAL dp)
VectorOfTObjectPtrs other
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::vector< int > row