-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathDigitBlockBase.h
More file actions
319 lines (306 loc) · 13.5 KB
/
DigitBlockBase.h
File metadata and controls
319 lines (306 loc) · 13.5 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
// 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.
//
//file DigitBlockBase.h base class for processing RAW data into Digits
//
// Artur.Furs
// afurs@cern.ch
#ifndef ALICEO2_FIT_DIGITBLOCKBASE_H_
#define ALICEO2_FIT_DIGITBLOCKBASE_H_
#include <iostream>
#include <vector>
#include <algorithm>
#include <type_traits>
#include <utility>
#include <array>
#include <Rtypes.h>
#include <CommonDataFormat/InteractionRecord.h>
#include <CommonDataFormat/RangeReference.h>
#include <Framework/Logger.h>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/remove_if.hpp>
#include <boost/mpl/lambda.hpp>
#include <boost/mpl/not.hpp>
#include <boost/mpl/size.hpp>
#include <gsl/span>
namespace o2
{
namespace fit
{
namespace DigitBlockHelper
{
//Check template specialisation
//Is there analog of this metafunction in Common O2 lib?
template <template <typename...> class Template, typename T>
struct IsSpecOfType : std::false_type {
};
template <template <typename...> class Template, typename... T>
struct IsSpecOfType<Template, Template<T...>> : std::true_type {
};
//Check if RangeReference is a single field in main digit structure
template <typename T, typename = void>
struct HasRef : std::false_type {
};
//For FT0
template <typename T>
struct HasRef<T, std::enable_if_t<std::is_same<decltype(std::declval<T>().ref), typename o2::dataformats::RangeReference<int, int>>::value>> : std::true_type {
};
//For FV0
template <typename T>
struct HasRef<T, std::enable_if_t<std::is_same<decltype(std::declval<T>().ref), typename o2::dataformats::RangeRefComp<6>>::value>> : std::true_type {
};
//For FDD
template <typename T>
struct HasRef<T, std::enable_if_t<std::is_same<decltype(std::declval<T>().ref), typename o2::dataformats::RangeRefComp<5>>::value>> : std::true_type {
};
//Check if RangeReference is an array field in main digit structure
template <typename T, typename = void>
struct HasArrayRef : std::false_type {
};
//For FT0
template <typename T>
struct HasArrayRef<T, std::enable_if_t<std::is_same<decltype(std::declval<T>().ref), typename std::array<typename o2::dataformats::RangeReference<int, int>, std::tuple_size<decltype(std::declval<T>().ref)>::value>>::value>> : std::true_type {
};
//For FV0
template <typename T>
struct HasArrayRef<T, std::enable_if_t<std::is_same<decltype(std::declval<T>().ref), typename std::array<typename o2::dataformats::RangeRefComp<6>, std::tuple_size<decltype(std::declval<T>().ref)>::value>>::value>> : std::true_type {
};
//For FDD
template <typename T>
struct HasArrayRef<T, std::enable_if_t<std::is_same<decltype(std::declval<T>().ref), typename std::array<typename o2::dataformats::RangeRefComp<5>, std::tuple_size<decltype(std::declval<T>().ref)>::value>>::value>> : std::true_type {
};
//Get RangeReference number of dimentions.
template <typename T, typename = void>
struct GetDigitRefsN {
constexpr static std::size_t value = 0;
};
template <typename T>
struct GetDigitRefsN<T, std::enable_if_t<HasRef<T>::value>> {
constexpr static std::size_t value = 1;
};
template <typename T>
struct GetDigitRefsN<T, std::enable_if_t<HasArrayRef<T>::value && (std::tuple_size<decltype(std::declval<T>().ref)>::value > 1)>> {
constexpr static std::size_t value = std::tuple_size<decltype(std::declval<T>().ref)>::value;
};
//Check if InteractionRecord field exists
template <typename T, typename = void>
struct HasIntRecord : std::false_type {
};
template <typename T>
struct HasIntRecord<T, std::enable_if_t<std::is_same<decltype(std::declval<T>().mIntRecord), o2::InteractionRecord>::value>> : std::true_type {
};
//Temporary for FV0
template <typename T>
struct HasIntRecord<T, std::enable_if_t<std::is_same<decltype(std::declval<T>().ir), o2::InteractionRecord>::value>> : std::true_type {
};
//Dividing to sub-digit structures with InteractionRecord(single one, e.g. for TCM extended mode) and without
template <typename T>
using GetVecSubDigit = typename boost::mpl::remove_if<T, boost::mpl::lambda<HasIntRecord<boost::mpl::_1>>::type>::type;
template <typename T>
using GetVecSingleSubDigit = typename boost::mpl::remove_if<T, boost::mpl::lambda<boost::mpl::not_<HasIntRecord<boost::mpl::_1>>>::type>::type;
//Converting empty Boost MPL vector to empty std::tuple
template <typename T, typename = void>
struct GetSubDigitField {
typedef std::tuple<> type;
typedef std::tuple<> vector_type;
constexpr static bool sIsEmpty = true;
constexpr static bool sIsTuple = true;
constexpr static std::size_t size = 0;
};
//Converting Boost MPL vector with 1 element to std::vector
template <typename T>
struct GetSubDigitField<T, std::enable_if_t<boost::mpl::size<T>::value == 1>> {
typedef std::vector<typename boost::mpl::front<T>::type> vector_type;
typedef typename boost::mpl::front<T>::type type;
constexpr static bool sIsEmpty = false;
constexpr static bool sIsTuple = false;
constexpr static std::size_t size = 1;
};
//
//Converting Boost MPL vector to std::tuple of std::vectors
template <typename T>
struct GetSubDigitField<T, std::enable_if_t<(boost::mpl::size<T>::value > 1)>> {
template <typename Arg1, typename Arg2>
struct MakeTuple;
template <typename... Args, typename LastArg>
struct MakeTuple<std::tuple<Args...>, LastArg> {
typedef std::tuple<Args..., LastArg> type;
};
typedef typename boost::mpl::fold<T, std::tuple<>, MakeTuple<boost::mpl::_1, std::vector<boost::mpl::_2>>>::type vector_type;
typedef typename boost::mpl::fold<T, std::tuple<>, MakeTuple<boost::mpl::_1, boost::mpl::_2>>::type type;
constexpr static bool sIsEmpty = false;
constexpr static bool sIsTuple = true;
constexpr static std::size_t size = boost::mpl::size<T>::value;
};
} // namespace DigitBlockHelper
// DigitBlock - digit block, for interacting with Digits structures as one
// Digit - primary digit struct which should contain InteractionRecord field and ReferenceRange to SubDigit(not all of them)
// SubDigit - sub digit structure which shouldn't contain InteractionRecord field
// SingleSubDigit - separated SubDigits which contain InteractionRecord field and not referred by ReferenceRange field in Digit structure.
// For example extended TCM mode uses such SingleSubDigits
template <typename DigitType, typename... SubDigitTypes>
class DigitBlockBase //:public DigitBlock
{
public:
DigitBlockBase(const o2::InteractionRecord& intRec)
{
mDigit.setIntRecord(intRec);
// mSubDigit.reserve(256);
}
DigitBlockBase(const DigitType& digit) : mDigit(digit)
{
}
DigitBlockBase() = default;
DigitBlockBase(const DigitBlockBase& other) = default;
~DigitBlockBase() = default;
typedef DigitType Digit_t;
typedef std::tuple<std::vector<DigitType>, std::vector<SubDigitTypes>...> TupleVecDigitObjs_t;
typedef boost::mpl::vector<SubDigitTypes...> VecAllSubDigit_t;
typedef DigitBlockHelper::GetVecSubDigit<VecAllSubDigit_t> VecSubDigit_t;
typedef DigitBlockHelper::GetVecSingleSubDigit<VecAllSubDigit_t> VecSingleSubDigit_t;
typedef typename DigitBlockHelper::GetSubDigitField<VecSubDigit_t>::vector_type SubDigit_t;
typedef typename DigitBlockHelper::GetSubDigitField<VecSingleSubDigit_t>::type SingleSubDigit_t;
constexpr static std::size_t sNSubDigits = DigitBlockHelper::GetSubDigitField<VecSubDigit_t>::size;
constexpr static std::size_t sNSingleSubDigits = DigitBlockHelper::GetSubDigitField<VecSingleSubDigit_t>::size;
Digit_t mDigit;
SubDigit_t mSubDigit;
SingleSubDigit_t mSingleSubDigit;
template <typename VecDigit, typename... VecSubDigits>
auto getSubDigits(VecDigit& vecDigits, VecSubDigits&... vecSubDigits)
-> std::enable_if_t<sizeof...(VecSubDigits) == sNSubDigits>
{
if constexpr (sNSubDigits > 0) {
getSubDigit<sizeof...(VecSubDigits), sizeof...(VecSubDigits)>(std::tie(vecSubDigits...));
}
vecDigits.push_back(std::move(mDigit));
}
template <typename... T>
auto getSingleSubDigits(T&... vecSingleSubDigits)
-> std::enable_if_t<sizeof...(T) == sNSingleSubDigits>
{
if constexpr (sNSingleSubDigits > 0) {
getSingleSubDigit<sizeof...(T), sizeof...(T)>(std::tie(vecSingleSubDigits...));
}
}
template <std::size_t N, std::size_t N_TOTAL, typename... T>
auto getSubDigit(std::tuple<T...> tupleVecSubDigits) -> std::enable_if_t<(N_TOTAL > 1)>
{
mDigit.ref[N - 1].set(std::get<N - 1>(tupleVecSubDigits).size(), std::get<N - 1>(mSubDigit).size());
std::move(std::get<N - 1>(mSubDigit).begin(), std::get<N - 1>(mSubDigit).end(), std::back_inserter(std::get<N - 1>(tupleVecSubDigits)));
if constexpr (N > 1) {
getSubDigit<N - 1, N_TOTAL>(tupleVecSubDigits);
}
}
template <std::size_t N, std::size_t N_TOTAL, typename... T>
auto getSubDigit(std::tuple<T...> tupleVecSubDigits) -> std::enable_if_t<(N_TOTAL == 1)>
{
mDigit.ref.set(std::get<0>(tupleVecSubDigits).size(), mSubDigit.size());
std::move(mSubDigit.begin(), mSubDigit.end(), std::back_inserter(std::get<0>(tupleVecSubDigits)));
}
template <std::size_t N, std::size_t N_TOTAL, typename... T>
auto getSingleSubDigit(std::tuple<T...> tupleVecSingleSubDigits) -> std::enable_if_t<(N_TOTAL > 1)>
{
std::get<N - 1>(tupleVecSingleSubDigits).push_back(std::move(std::get<N - 1>(mSingleSubDigit)));
if constexpr (N > 1) {
getSingleSubDigit<N - 1, N_TOTAL>(tupleVecSingleSubDigits);
}
}
template <std::size_t N, std::size_t N_TOTAL, typename... T>
auto getSingleSubDigit(std::tuple<T...> tupleVecSingleSubDigits) -> std::enable_if_t<(N_TOTAL == 1)>
{
std::get<0>(tupleVecSingleSubDigits).push_back(std::move(mSingleSubDigit));
}
// 1-Dim SubDigit
template <typename DigitBlockType, typename DigitT, typename SubDigitT>
static auto makeDigitBlock(const std::vector<DigitT>& vecDigits, const std::vector<SubDigitT>& vecSubDigits) -> std::enable_if_t<DigitBlockHelper::GetDigitRefsN<DigitT>::value == 1 && DigitBlockHelper::IsSpecOfType<DigitBlockBase, typename DigitBlockType::DigitBlockBase_t>::value, std::vector<DigitBlockType>>
{
std::vector<DigitBlockType> vecResult;
vecResult.reserve(vecDigits.size());
for (const auto& digit : vecDigits) {
auto itBegin = vecSubDigits.begin();
std::advance(itBegin, digit.ref.getFirstEntry());
auto itLast = itBegin;
std::advance(itLast, digit.ref.getEntries());
vecResult.push_back({digit});
vecResult.back().mSubDigit.reserve(digit.ref.getEntries());
std::copy(itBegin, itLast, std::back_inserter(vecResult.back().mSubDigit));
}
return vecResult;
}
// Multi-Dim SubDigits
template <typename DigitBlockType, typename DigitT, typename... SubDigitT>
static auto makeDigitBlock(const std::vector<DigitT>& vecDigits, const std::vector<SubDigitT>&... vecSubDigits) -> std::enable_if_t<(DigitBlockHelper::GetDigitRefsN<DigitT>::value > 1) && (DigitBlockHelper::IsSpecOfType<DigitBlockBase, typename DigitBlockType::DigitBlockBase_t>::value), std::vector<DigitBlockType>>
{
std::vector<DigitBlockType> vecResult;
vecResult.reserve(vecDigits.size());
for (const auto& digit : vecDigits) {
vecResult.push_back({digit});
auto& refTuple = vecResult.back().mSubDigit;
fillSubDigitTuple<sizeof...(SubDigitT)>(digit, std::tie(vecSubDigits...), refTuple);
}
return vecResult;
}
template <std::size_t N, typename DigitT, typename... T>
static void fillSubDigitTuple(const DigitT& digit, const std::tuple<T...>& tupleSrc, std::tuple<T...>& tupleDest)
{
const auto& vecSrc = std::get<N>(tupleSrc);
auto& vecDest = std::get<N>(tupleSrc);
auto itBegin = vecSrc.begin();
std::advance(itBegin, digit.ref[N - 1].getFirstEntry());
auto itLast = itBegin;
std::advance(itLast, digit.ref[N - 1].getEntries());
vecDest.reserve(digit.ref[N - 1].getEntries());
std::copy(itBegin, itLast, std::back_inserter(vecDest));
if constexpr (N > 1) {
fillSubDigitTuple<N - 1>(digit, tupleSrc, tupleDest);
}
}
void print() const
{
mDigit.printLog();
if constexpr (DigitBlockHelper::IsSpecOfType<std::tuple, decltype(mSubDigit)>::value) {
if constexpr ((std::tuple_size<decltype(mSubDigit)>::value) > 1) {
LOG(info) << "______________SUB DIGITS____________";
std::apply([](const auto&... vecSubDigit) {
((std::for_each(vecSubDigit.begin(), vecSubDigit.end(), [](const auto& subDigit) {
subDigit.printLog();
})),
...);
},
mSubDigit);
}
} else {
LOG(info) << "______________SUB DIGITS____________";
std::for_each(mSubDigit.begin(), mSubDigit.end(), [](const auto& subDigit) {
subDigit.printLog();
});
}
if constexpr (DigitBlockHelper::IsSpecOfType<std::tuple, decltype(mSingleSubDigit)>::value) {
if constexpr ((std::tuple_size<decltype(mSingleSubDigit)>::value) > 1) {
LOG(info) << "______________SINGLE SUB DIGITS____________";
std::apply([](const auto&... singleSubDigit) {
((singleSubDigit.printLog()), ...);
},
mSingleSubDigit);
}
} else {
LOG(info) << "______________SINGLE SUB DIGITS____________";
mSingleSubDigit.printLog();
}
LOG(info) << std::dec;
LOG(info) << "______________________________________";
}
};
} // namespace fit
} // namespace o2
#endif