-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatmap.cc
412 lines (336 loc) · 9.41 KB
/
flatmap.cc
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <cstdint>
#include <iostream>
#include <vector>
#include <functional>
#include <iterator>
#include <bs3/pbs.hh>
#include <daq-raw-reader.hh>
#define LAZY true
namespace dr = daq_raw;
namespace pu = pbs::utils;
using pu::optional;
using pu::nullopt;
template <class Iterable>
struct print_iterable_t {
const Iterable& values;
};
using std::begin;
template <class Iterable>
auto operator<<(std::ostream& out, const print_iterable_t<Iterable>& wrapper)
-> decltype(out << *begin(wrapper.values))
{
out << '[';
auto first = begin(wrapper.values);
auto last = end(wrapper.values);
if (first != last) {
out << *first;
for (++first; first!=last; ++first)
out << ',' << *first;
}
return out << ']';
}
template <class Iterable>
print_iterable_t<Iterable>
print_iterable(const Iterable& values)
{
return { values };
}
struct SamplePoint {
int32_t time;
uint16_t value;
bool operator==(const SamplePoint& other) const
{
return time == other.time && value == other.value;
}
};
std::ostream& operator<<(std::ostream& out, const SamplePoint& pt)
{
return out << '[' << pt.time << ',' << pt.value << ']';
// char x = pt.time & pt.value;
// return out << x;
}
using std::begin;
template <class OuterIterator>
struct flattening_iterator {
typedef OuterIterator outer_iterator;
typedef typename std::decay<decltype(begin(std::declval<typename std::iterator_traits<outer_iterator>::value_type&>()))>::type inner_iterator;
typedef typename std::iterator_traits<inner_iterator>::value_type value_type;
typedef typename std::iterator_traits<inner_iterator>::reference reference;
typedef typename std::iterator_traits<inner_iterator>::pointer pointer;
typedef std::ptrdiff_t difference_type;
typedef std::forward_iterator_tag iterator_category;
private:
outer_iterator outer, outer_end;
optional<std::pair<inner_iterator, inner_iterator> > current;
void skip_to_valid()
{
while (outer != outer_end) {
if (!current)
current = std::make_pair(begin(*outer), end(*outer));
if (current->first != current->second)
return;
else
current = nullopt, ++outer;
}
}
public:
flattening_iterator(const outer_iterator& _outer, const outer_iterator& _outer_end)
: outer(_outer), outer_end(_outer_end)
{
skip_to_valid();
}
reference operator*() const
{
return *current->first;
}
pointer operator->() const
{
return &(*(*this));
}
flattening_iterator& operator++()
{
++current->first;
skip_to_valid();
return *this;
}
flattening_iterator operator++(int)
{
auto copy = *this;
++*this;
return copy;
}
bool operator==(const flattening_iterator& other) const
{
return outer==other.outer && outer_end==other.outer_end && current==other.current;
}
bool operator!=(const flattening_iterator& other) const
{
return !((*this) == other);
}
};
using std::begin;
using std::end;
template <class Iterable>
pbs::utils::range<flattening_iterator<decltype(begin(std::declval<Iterable&>()))> >
flatten(Iterable&& r)
{
return { {begin(r), end(r)}, {end(r), end(r)} };
}
using pbs::utils::map;
using pbs::utils::map_indexed;
template <class Function, class Iterable>
auto flat_map(Function f, Iterable&& r)
-> decltype(flatten(map(f, r)))
{
return flatten(map(f, r));
}
struct expander {
int32_t time;
SamplePoint operator()(uint16_t value, int32_t index) const
{
return { time+index, value };
}
};
auto expand_segment(const pbt::RawPmtSegment& seg)
-> decltype(map_indexed(std::declval<expander>(), seg.adcValue))
{
return map_indexed(expander { seg.startTime }, seg.adcValue);
}
#if LAZY
auto expand_waveform(const std::vector<pbt::RawPmtSegment>& wf)
-> decltype(flat_map(expand_segment, wf))
{
return flat_map(expand_segment, wf);
}
#else
// [Segment] -> [Point]
std::vector<SamplePoint>
expand_waveform(const std::vector<pbt::RawPmtSegment>& wf)
{
std::vector<SamplePoint> output;
auto total = pu::reduce(
std::plus<size_t>(),
pu::map([](const pbt::RawPmtSegment& seg) { return seg.adcValue.size(); }, wf));
output.reserve(total);
for (auto& seg : wf) {
auto time = seg.startTime;
for (auto& sample : seg.adcValue)
output.push_back({ time++, sample });
}
return output;
}
#endif
// (Size, [Point]) -> [Point]
// return at most count*2 points
//
// Break up `in` into `count` windows, and each window is reduced to a min
// and max, if both are present, retaining original time position. If min
// and max are the same then the window becomes a single point; empty
// window becomes nothing.
//
// `in` must be ascending ordered by time.
#if true
template <class Finder, class Iterator>
struct window_iterator {
typedef std::forward_iterator_tag iterator_category;
typedef pbs::utils::range<Iterator> value_type;
typedef value_type reference;
typedef std::unique_ptr<value_type> pointer;
typedef std::ptrdiff_t difference_type;
private:
Finder _finder;
Iterator _first;
const Iterator _end;
Iterator _last;
public:
window_iterator(Finder finder, Iterator begin, Iterator end)
: _finder(finder), _first(begin), _end(end), _last(_finder(_first, _end))
{}
reference operator*() const
{
return { _first, _last };
}
window_iterator& operator++()
{
_first = _last;
_last = _finder(_first, _end);
return *this;
}
bool operator==(const window_iterator& other) const
{
return _first==other._first && _end==other._end
// these two are unlikely to be false, if above are true
&& _last==other._last;
}
bool operator!=(const window_iterator& other) const
{
return !((*this)==other);
}
};
template <class Function, class Iterator>
pbs::utils::range<window_iterator<typename std::decay<Function>::type,
typename std::decay<Iterator>::type> >
window_by(Function&& f, Iterator&& first, typename std::enable_if<true, Iterator>::type&& last)
{
return { {f, first, last}, {f, last, last} };
}
template <class Function, class Range>
auto window_by(Function&& f, Range&& r)
-> decltype(window_by(std::forward<Function>(f), begin(r), end(r)))
{
return window_by(std::forward<Function>(f), begin(r), end(r));
}
struct window_end_finder {
size_t count;
int32_t end_time;
size_t i_window;
template <class Iterator>
Iterator operator()(Iterator first, Iterator last)
{
int32_t high = end_time * (i_window+1) / count;
++i_window;
return std::find_if(
first, last,
[=](const SamplePoint& p) { return p.time >= high; });
}
};
struct reduce_window {
template <class Range>
std::vector<SamplePoint> operator()(Range&& window) const
{
if (begin(window) == end(window))
return {};
auto min=*begin(window), max=*begin(window);
for (const auto& point : window) {
if (point.value < min.value) min = point;
if (point.value > max.value) max = point;
}
if (min.time == max.time)
return { min };
else if (min.time < max.time)
return { min, max };
else // max.time < min.time
return { max, min };
}
};
auto down_sample(size_t count, const std::vector<pbt::RawPmtSegment>& input)
-> decltype(flat_map(
reduce_window(),
window_by(
std::declval<window_end_finder>(),
expand_waveform(input))))
{
auto in = expand_waveform(input);
int32_t end_time = 1+ input.back().startTime + input.back().adcValue.size();
return flat_map(
reduce_window(),
window_by(
window_end_finder{count, end_time, 0},
in));
}
#else
std::vector<SamplePoint>
down_sample(size_t count, const std::vector<pbt::RawPmtSegment>& input)
{
std::vector<SamplePoint> out;
out.reserve(2*count);
auto write = [&](const SamplePoint& p) { out.push_back(p); };
int32_t end_time = 1+ input.back().startTime + input.back().adcValue.size();
auto in = expand_waveform(input);
{
auto window_begin = in.begin();
for (size_t i_window=0; i_window<count; ++i_window) {
// auto low = end_time * i_window / count;
int32_t high = end_time * (i_window+1) / count;
const auto window_end = std::find_if(
window_begin, in.end(),
[=](const SamplePoint& p) { return p.time >= high; });
if (window_begin == window_end)
continue;
else {
auto min=*window_begin, max=*window_begin;
for (const auto& point : pu::make_range(++window_begin, window_end)) {
if (point.value < min.value) min = point;
if (point.value > max.value) max = point;
}
if (min.time == max.time)
write(min);
else if (min.time < max.time)
write(min), write(max);
else // max.time < min.time
write(max), write(min);
}
window_begin = window_end;
}
}
return out;
}
#endif
template <class Iterable>
char silly_checksum(const Iterable&& i)
{
char x=0;
for (auto first=begin(i); first!=end(i); ++first)
x |= reinterpret_cast<const char&>(*&*first);
return x;
}
int main()
{
using std::cout;
for (const auto& event : dr::read_file(std::cin)) {
cout << "{\"run\":" << event.runNumber
<< ",\"event\":" << event.eventNumber
<< ",\"waveforms\":{";
bool first = true;
for (auto& wfpair : event.rawWaveforms) {
if (first)
first = false;
else
cout << ',';
cout << '"' << wfpair.first << "\":"
<< print_iterable(down_sample(500, wfpair.second));
}
cout << "}}\n";
}
return 0;
}