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