Project
Loading...
Searching...
No Matches
testTPCCalDet.cxx
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#include <algorithm>
13#define BOOST_TEST_MODULE Test TPC CalDet class
14#define BOOST_TEST_MAIN
15#define BOOST_TEST_DYN_LINK
16#include <boost/range/combine.hpp>
17#include <boost/test/unit_test.hpp>
18#include <vector>
19#include <limits>
20
21#include "TMath.h"
22#include "TPCBase/Mapper.h"
23#include "TPCBase/CalArray.h"
24#include "TPCBase/CalDet.h"
25#include "TFile.h"
27
28namespace o2::tpc
29{
30
31// templated euqality check
32// for integer one would need a specialisation to check for == instead of <
33template <typename T>
34bool isEqualAbs(T x, T y, int n = 1)
35{
36 // Since `epsilon()` is the gap size (ULP, unit in the last place)
37 // of floating-point numbers in interval [1, 2), we can scale it to
38 // the gap size in interval [2^e, 2^{e+1}), where `e` is the exponent
39 // of `x` and `y`.
40
41 // If `x` and `y` have different gap sizes (which means they have
42 // different exponents), we take the smaller one. Taking the bigger
43 // one is also reasonable, I guess.
44 const T m = std::min(std::fabs(x), std::fabs(y));
45
46 // Subnormal numbers have fixed exponent, which is `min_exponent - 1`.
47 const int exp = m < std::numeric_limits<T>::min()
48 ? std::numeric_limits<T>::min_exponent - 1
49 : std::ilogb(m);
50
51 // We consider `x` and `y` equal if the difference between them is
52 // within `n` ULPs.
53 return std::fabs(x - y) <= n * std::ldexp(std::numeric_limits<T>::epsilon(), exp);
54}
55
56template <typename T>
57 requires(std::integral<T>)
58bool isEqualAbs(T val1, T val2)
59{
60 return val1 == val2;
61}
62
63BOOST_AUTO_TEST_CASE(CalArray_ROOTIO)
64{
65 // CalROC roc(PadSubset::ROC, 10);
67
68 int iter = 0;
69 // unsigned iter=0;
70 for (auto& val : roc.getData()) {
71 val = iter++;
72 }
73
74 auto f = TFile::Open("CalArray_ROOTIO.root", "recreate");
75 f->WriteObject(&roc, "roc");
76 delete f;
77
78 // CalROC *rocRead = nullptr;
79 CalArray<unsigned>* rocRead = nullptr;
80 f = TFile::Open("CalArray_ROOTIO.root");
81 f->GetObject("roc", rocRead);
82 delete f;
83
84 BOOST_REQUIRE(rocRead != nullptr);
85
86 float sumROC = 0;
87 for (auto const& val : boost::combine(roc.getData(), rocRead->getData())) {
88 sumROC += (val.get<0>() - val.get<1>());
89 }
90
91 BOOST_CHECK_CLOSE(sumROC, 0., 1.E-12);
92}
93
95{
96
97 auto& mapper = Mapper::instance();
98 const auto numberOfPads = mapper.getPadsInSector() * 36;
99
100 CalPad padROC(PadSubset::ROC);
101 CalPad padPartition(PadSubset::Partition);
102 CalPad padRegion(PadSubset::Region);
103
104 // ===| Fill Data |===========================================================
105 int iter = 0;
106 // --- ROC type
107 padROC.setName("ROCData");
108 for (auto& calArray : padROC.getData()) {
109 for (auto& value : calArray.getData()) {
110 value = iter++;
111 }
112 }
113
114 // --- Partition type
115 padPartition.setName("PartitionData");
116 for (auto& calArray : padPartition.getData()) {
117 for (auto& value : calArray.getData()) {
118 value = iter++;
119 }
120 }
121
122 // --- Region type
123 padRegion.setName("RegionData");
124 for (auto& calArray : padRegion.getData()) {
125 for (auto& value : calArray.getData()) {
126 value = iter++;
127 }
128 }
129
130 // ===| dump all objects to file |============================================
131 auto f = TFile::Open("CalDet.root", "recreate");
132 f->WriteObject(&padROC, "CalDetROC");
133 f->WriteObject(&padPartition, "CalDetPartition");
134 f->WriteObject(&padRegion, "CalDetRegion");
135 f->Close();
136 delete f;
137
138 // ===| read back all values |================================================
139 CalPad* padROCRead = nullptr;
140 CalPad* padPartitionRead = nullptr;
141 CalPad* padRegionRead = nullptr;
142
143 f = TFile::Open("CalDet.root");
144 f->GetObject("CalDetROC", padROCRead);
145 f->GetObject("CalDetPartition", padPartitionRead);
146 f->GetObject("CalDetRegion", padRegionRead);
147
148 delete f;
149
150 BOOST_REQUIRE(padROCRead != nullptr);
151 BOOST_REQUIRE(padPartitionRead != nullptr);
152 BOOST_REQUIRE(padRegionRead != nullptr);
153
154 // ===| compare values before and after |=====================================
155 float sumROC = 0.f;
156 float sumPartition = 0.f;
157 float sumRegion = 0.f;
158
159 int numberOfPadsROC = 0;
160 int numberOfPadsPartition = 0;
161 int numberOfPadsRegion = 0;
162
163 for (auto const& arrays : boost::combine(padROC.getData(), padROCRead->getData())) {
164 for (auto const& val : boost::combine(arrays.get<0>().getData(), arrays.get<1>().getData())) {
165 sumROC += (val.get<0>() - val.get<1>());
166 ++numberOfPadsROC;
167 }
168 }
169
170 for (auto const& arrays : boost::combine(padPartition.getData(), padPartitionRead->getData())) {
171 for (auto const& val : boost::combine(arrays.get<0>().getData(), arrays.get<1>().getData())) {
172 sumPartition += (val.get<0>() - val.get<1>());
173 ++numberOfPadsPartition;
174 }
175 }
176
177 for (auto const& arrays : boost::combine(padRegion.getData(), padRegionRead->getData())) {
178 for (auto const& val : boost::combine(arrays.get<0>().getData(), arrays.get<1>().getData())) {
179 sumRegion += (val.get<0>() - val.get<1>());
180 ++numberOfPadsRegion;
181 }
182 }
183
184 // ===| checks |==============================================================
185 BOOST_CHECK_EQUAL(padROC.getName(), padROCRead->getName());
186 BOOST_CHECK_CLOSE(sumROC, 0.f, 1.E-12);
187 BOOST_CHECK_EQUAL(numberOfPadsROC, numberOfPads);
188
189 BOOST_CHECK_EQUAL(padPartition.getName(), padPartitionRead->getName());
190 BOOST_CHECK_CLOSE(sumPartition, 0.f, 1.E-12);
191 BOOST_CHECK_EQUAL(numberOfPadsPartition, numberOfPads);
192
193 BOOST_CHECK_EQUAL(padRegion.getName(), padRegionRead->getName());
194 BOOST_CHECK_CLOSE(sumRegion, 0.f, 1.E-12);
195 BOOST_CHECK_EQUAL(numberOfPadsRegion, numberOfPads);
196} // BOOST_AUTO_TEST_CASE
197
198BOOST_AUTO_TEST_CASE(CalDet_Arithmetics)
199{
200 // data
202
203 // data 2 for testing operators on objects
205
206 // for applying the operators on
207 CalPad padCmp(PadSubset::ROC);
208
209 // ===| fill with data |======================================================
210 int iter = 0;
211 // --- ROC type
212 for (auto& calArray : pad.getData()) {
213 for (auto& value : calArray.getData()) {
214 value = iter++;
215 }
216 }
217
218 iter = 1;
219 for (auto& calArray : pad2.getData()) {
220 for (auto& value : calArray.getData()) {
221 value = iter++;
222 }
223 }
224
225 //
226 // ===| test operators with simple numbers |==================================
227 //
228 const float number = 0.2f;
229 bool isEqual = true;
230
231 // + operator
232 isEqual = true;
233 padCmp = pad;
234 padCmp += number;
235
236 for (auto const& arrays : boost::combine(padCmp.getData(), pad.getData())) {
237 for (auto const& val : boost::combine(arrays.get<0>().getData(), arrays.get<1>().getData())) {
238 isEqual &= isEqualAbs(val.get<0>(), val.get<1>() + number);
239 }
240 }
241 BOOST_CHECK_EQUAL(isEqual, true);
242
243 // - operator
244 isEqual = true;
245 padCmp = pad;
246 padCmp -= number;
247
248 for (auto const& arrays : boost::combine(padCmp.getData(), pad.getData())) {
249 for (auto const& val : boost::combine(arrays.get<0>().getData(), arrays.get<1>().getData())) {
250 isEqual &= isEqualAbs(val.get<0>(), val.get<1>() - number);
251 }
252 }
253 BOOST_CHECK_EQUAL(isEqual, true);
254
255 // * operator
256 isEqual = true;
257 padCmp = pad;
258 padCmp *= number;
259
260 for (auto const& arrays : boost::combine(padCmp.getData(), pad.getData())) {
261 for (auto const& val : boost::combine(arrays.get<0>().getData(), arrays.get<1>().getData())) {
262 isEqual &= isEqualAbs(val.get<0>(), val.get<1>() * number);
263 }
264 }
265 BOOST_CHECK_EQUAL(isEqual, true);
266
267 // / operator
268 isEqual = true;
269 padCmp = pad;
270 padCmp /= number;
271
272 for (auto const& arrays : boost::combine(padCmp.getData(), pad.getData())) {
273 for (auto const& val : boost::combine(arrays.get<0>().getData(), arrays.get<1>().getData())) {
274 isEqual &= isEqualAbs(val.get<0>(), val.get<1>() / number);
275 }
276 }
277 BOOST_CHECK_EQUAL(isEqual, true);
278
279 //
280 // ===| test operators with full object |=====================================
281 //
282 // + operator
283 isEqual = true;
284 padCmp = pad;
285 padCmp += pad2;
286
287 for (auto itpad = pad.getData().begin(), itpad2 = pad2.getData().begin(), itpadCmp = padCmp.getData().begin(); itpad != pad.getData().end(); ++itpad, ++itpad2, ++itpadCmp) {
288 for (auto itval1 = (*itpad).getData().begin(), itval2 = (*itpad2).getData().begin(), itval3 = (*itpadCmp).getData().begin(); itval1 != (*itpad).getData().end(); ++itval1, ++itval2, ++itval3) {
289 isEqual &= isEqualAbs(*itval3, *itval1 + *itval2);
290 }
291 }
292 BOOST_CHECK_EQUAL(isEqual, true);
293
294 // - operator
295 isEqual = true;
296 padCmp = pad;
297 padCmp -= pad2;
298
299 for (auto itpad = pad.getData().begin(), itpad2 = pad2.getData().begin(), itpadCmp = padCmp.getData().begin(); itpad != pad.getData().end(); ++itpad, ++itpad2, ++itpadCmp) {
300 for (auto itval1 = (*itpad).getData().begin(), itval2 = (*itpad2).getData().begin(), itval3 = (*itpadCmp).getData().begin(); itval1 != (*itpad).getData().end(); ++itval1, ++itval2, ++itval3) {
301 isEqual &= isEqualAbs(*itval3, *itval1 - *itval2);
302 }
303 }
304 BOOST_CHECK_EQUAL(isEqual, true);
305
306 // * operator
307 isEqual = true;
308 padCmp = pad;
309 padCmp *= pad2;
310
311 for (auto itpad = pad.getData().begin(), itpad2 = pad2.getData().begin(), itpadCmp = padCmp.getData().begin(); itpad != pad.getData().end(); ++itpad, ++itpad2, ++itpadCmp) {
312 for (auto itval1 = (*itpad).getData().begin(), itval2 = (*itpad2).getData().begin(), itval3 = (*itpadCmp).getData().begin(); itval1 != (*itpad).getData().end(); ++itval1, ++itval2, ++itval3) {
313 isEqual &= isEqualAbs(*itval3, *itval1 * *itval2);
314 }
315 }
316 BOOST_CHECK_EQUAL(isEqual, true);
317
318 // / operator
319 isEqual = true;
320 padCmp = pad;
321 padCmp /= pad2;
322
323 for (auto itpad = pad.getData().begin(), itpad2 = pad2.getData().begin(), itpadCmp = padCmp.getData().begin(); itpad != pad.getData().end(); ++itpad, ++itpad2, ++itpadCmp) {
324 for (auto itval1 = (*itpad).getData().begin(), itval2 = (*itpad2).getData().begin(), itval3 = (*itpadCmp).getData().begin(); itval1 != (*itpad).getData().end(); ++itval1, ++itval2, ++itval3) {
325 isEqual &= isEqualAbs(*itval3, *itval1 / *itval2);
326 }
327 }
328 BOOST_CHECK_EQUAL(isEqual, true);
329
330 // = operator
331 isEqual = true;
332 padCmp = 10.f;
333 for (const auto& calArr : padCmp.getData()) {
334 isEqual &= std::all_of(calArr.getData().begin(), calArr.getData().end(), [](const auto val) { return isEqualAbs(val, 10.f); });
335 }
336 BOOST_CHECK_EQUAL(isEqual, true);
337}
338
346
347} // namespace o2::tpc
uint32_t pad2
uint64_t exp(uint64_t base, uint8_t exp) noexcept
uint32_t roc
Definition RawData.h:3
const std::vector< T > & getData() const
Definition CalArray.h:104
void setName(const std::string_view name, bool nameCalArrays=true)
Definition CalDet.h:78
const std::vector< CalType > & getData() const
Definition CalDet.h:58
const std::string & getName() const
Definition CalDet.h:85
static Mapper & instance(const std::string mappingDir="")
Definition Mapper.h:44
GLdouble n
Definition glcorearb.h:1982
GLint GLenum GLint x
Definition glcorearb.h:403
const GLfloat * m
Definition glcorearb.h:4066
GLdouble f
Definition glcorearb.h:310
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLuint GLfloat * val
Definition glcorearb.h:1582
const GLuint * arrays
Definition glcorearb.h:1314
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
Global TPC definitions and constants.
Definition SimTraits.h:167
BOOST_AUTO_TEST_CASE(ClusterHardware_test1)
bool isEqualAbs(T x, T y, int n=1)
@ Partition
Partitions (up to 36*5)
@ ROC
ROCs (up to 72)
@ Region
Regions (up to 36*10)
BOOST_CHECK(tree)
BOOST_CHECK_EQUAL(triggersD.size(), triggers.size())