-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.h
179 lines (160 loc) · 4.25 KB
/
histogram.h
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
#ifndef HISTOGRAM_H
#define HISTOGRAM_H
#include <inttypes.h>
#include <math.h>
#include <memory>
#include <vector>
#include <algorithm>
#include <array>
#include <type_traits>
#include <cassert>
#include "binning.h"
#include "channel.h"
#include "coord.h"
// Always allow weighted fill
// RMS?
// Check for overflow for char, short
// Automatic bin extension
// average histograms
template <
typename BorderT,
typename ChannelT,
class ChannelStoreT,
class DerivedT
>
class HistogramBase {
//protected:
public:
explicit HistogramBase(const std::vector<Binning<BorderT>> &binnings)
: dimension_(binnings.size())
, binnings_(binnings)
{
size_ = 1;
for (uint16_t i = 0; i < dimension_; ++i) {
num_bins_.push_back(binnings_[i].num_bins());
size_ *= num_bins_[i];
}
channels_ = std::unique_ptr< ChannelStoreBase<ChannelT, ChannelStoreT> >
(new ChannelStoreT(size_));
}
uint64_t Indexes2Channel(const std::vector<uint64_t> &indexes)
const
{
uint64_t result = 0;
uint64_t factor = 1;
for (uint16_t i = 0; i < dimension_; ++i) {
result += factor * indexes[i];
factor *= num_bins_[i];
}
return result;
}
std::vector<uint64_t> Channel2Indexes(uint64_t channel)
const
{
std::vector<uint64_t> indexes;
for (uint16_t i = 0; i < dimension_; ++i) {
indexes.push_back(channel % num_bins_[i]);
channel /= num_bins_[i];
}
return indexes;
}
void Fill(const std::vector<BorderT> &coord) {
std::vector<uint64_t> indexes;
for (uint16_t i = 0; i < dimension_; ++i) {
indexes.push_back(binnings_[i].FindBin(coord[i]));
}
uint64_t channel = Indexes2Channel(indexes);
channels_->Add(channel, 1);
static_cast<DerivedT *>(this)->AddMore(channel, 1);
}
uint64_t size() const { return size_; }
uint64_t occupied() const { return channels_->Occupied(); }
ChannelT sum() const { return channels_->Sum(); }
ChannelStoreT *channels() {
return (ChannelStoreT *)channels_.get();
}
private:
uint16_t dimension_;
std::vector<Binning<BorderT>> binnings_;
// Cache for binnings_[i]->num_bins()
std::vector<uint64_t> num_bins_;
std::unique_ptr< ChannelStoreBase<ChannelT, ChannelStoreT> > channels_;
uint64_t size_;
};
template <
typename BorderT,
typename ChannelT,
class ChannelStoreT
>
class Histogram :
public HistogramBase<
BorderT,
ChannelT,
ChannelStoreT,
Histogram<BorderT, ChannelT, ChannelStoreT>
>
{
typedef
HistogramBase< BorderT, ChannelT, ChannelStoreT,
Histogram<BorderT, ChannelT, ChannelStoreT> >
FatherT;
friend class HistogramBase< BorderT, ChannelT, ChannelStoreT,
Histogram<BorderT, ChannelT, ChannelStoreT> >;
public:
explicit Histogram(const std::vector<Binning<BorderT>> &binnings)
: FatherT(binnings) { }
protected:
void AddMore(const uint64_t channel, const ChannelT value) { }
};
template <
typename BorderT,
typename ChannelT,
class ChannelStoreT
>
class HistogramQsums :
public HistogramBase<
BorderT,
ChannelT,
ChannelStoreT,
HistogramQsums<BorderT, ChannelT, ChannelStoreT>
>
{
friend class HistogramBase<BorderT, ChannelT, ChannelStoreT,
HistogramQsums<BorderT, ChannelT, ChannelStoreT>>;
protected:
void AddMore(const uint64_t channel, const ChannelT value) {
qsums_->Add(channel, value*value);
}
private:
std::unique_ptr< ChannelStoreBase<ChannelT, ChannelStoreT> > qsums_;
};
template <
typename BorderT,
typename ChannelT,
class ChannelStoreT
>
class HistogramProfile :
public HistogramBase<
BorderT,
ChannelT,
ChannelStoreT,
HistogramProfile<BorderT, ChannelT, ChannelStoreT>
>
{
friend class HistogramBase<
BorderT,
ChannelT,
ChannelStoreT,
HistogramProfile<BorderT, ChannelT, ChannelStoreT>
>;
protected:
void AddMore(const uint64_t channel, const ChannelT value) {
// TODO
}
private:
std::unique_ptr< ChannelStoreBase<ChannelT, ChannelStoreT> > qsums_;
// TODO: replace by num_fills
std::unique_ptr< ChannelStoreBase<ChannelT, ChannelStoreT> > qsumsmeans_;
std::unique_ptr< ChannelStoreBase<ChannelT, ChannelStoreT> > means_;
};
#endif