9 #ifndef ANAB_MVAWRAPPERBASE_H 10 #define ANAB_MVAWRAPPERBASE_H 20 #include <unordered_map> 29 size_t getProductHash(std::type_info
const& ti)
const {
return ti.hash_code(); }
38 template <
class T,
size_t N>
42 template <
class T,
size_t N>
44 std::vector<float>
const& weights,
47 template <
class T,
size_t N>
49 std::function<
float(T
const&)> fweight,
52 template <
class T,
size_t N>
61 template <
class T,
size_t N>
64 std::array<char, N>
const& mask)
const;
72 template <
class T,
size_t N>
77 std::array<double, N> acc;
80 float pmin = 1.0e-6, pmax = 1.0 - pmin;
81 float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
83 for (
auto const& ptr : items) {
84 auto const& vout = outs[ptr.key()];
85 for (
size_t i = 0; i < vout.size(); ++i) {
89 else if (vout[i] > pmax)
92 v = std::log(vout[i]);
100 for (
size_t i = 0; i < N; ++i) {
101 acc[i] = exp(acc[i] / items.size());
104 for (
size_t i = 0; i < N; ++i) {
109 std::fill(acc.begin(), acc.end(), 1.0 / N);
111 std::array<float, N> result;
112 for (
size_t i = 0; i < N; ++i)
118 template <
class T,
size_t N>
121 std::vector<float>
const& weights,
124 std::array<double, N> acc;
127 float pmin = 1.0e-6, pmax = 1.0 - pmin;
128 float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
131 for (
size_t k = 0; k < items.size(); ++k) {
132 auto const& ptr = items[k];
133 float w = weights[k];
135 if (w == 0)
continue;
137 auto const& vout = outs[ptr.key()];
138 for (
size_t i = 0; i < vout.size(); ++i) {
142 else if (vout[i] > pmax)
145 v = std::log(vout[i]);
152 if (!items.empty()) {
154 for (
size_t i = 0; i < N; ++i) {
155 acc[i] = exp(acc[i] / totw);
158 for (
size_t i = 0; i < N; ++i) {
163 std::fill(acc.begin(), acc.end(), 1.0 / N);
165 std::array<float, N> result;
166 for (
size_t i = 0; i < N; ++i)
172 template <
class T,
size_t N>
175 std::function<
float(T
const&)> fweight,
178 std::array<double, N> acc;
181 float pmin = 1.0e-6, pmax = 1.0 - pmin;
182 float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
185 for (
size_t k = 0; k < items.size(); ++k) {
186 auto const& ptr = items[k];
187 float w = fweight(*ptr);
189 if (w == 0)
continue;
191 auto const& vout = outs[ptr.key()];
192 for (
size_t i = 0; i < vout.size(); ++i) {
196 else if (vout[i] > pmax)
199 v = std::log(vout[i]);
206 if (!items.empty()) {
208 for (
size_t i = 0; i < N; ++i) {
209 acc[i] = exp(acc[i] / totw);
212 for (
size_t i = 0; i < N; ++i) {
217 std::fill(acc.begin(), acc.end(), 1.0 / N);
219 std::array<float, N> result;
220 for (
size_t i = 0; i < N; ++i)
226 template <
class T,
size_t N>
232 std::array<double, N> acc;
235 float pmin = 1.0e-6, pmax = 1.0 - pmin;
236 float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
239 for (
size_t k = 0; k < items.size(); ++k) {
240 auto const& ptr = items[k];
241 float w = fweight(ptr);
243 if (w == 0)
continue;
245 auto const& vout = outs[ptr.key()];
246 for (
size_t i = 0; i < vout.size(); ++i) {
250 else if (vout[i] > pmax)
253 v = std::log(vout[i]);
260 if (!items.empty()) {
262 for (
size_t i = 0; i < N; ++i) {
263 acc[i] = exp(acc[i] / totw);
266 for (
size_t i = 0; i < N; ++i) {
271 std::fill(acc.begin(), acc.end(), 1.0 / N);
273 std::array<float, N> result;
274 for (
size_t i = 0; i < N; ++i)
284 template <
class T,
size_t N>
288 std::array<char, N>
const& mask)
const 291 std::unordered_map<char, size_t> label2group;
292 std::vector<size_t> nb_entries;
293 std::array<int, N> groupidx;
294 for (
size_t i = 0; i < N; ++i) {
297 auto search = label2group.find(mask[i]);
298 if (search == label2group.end()) {
300 label2group[mask[i]] = idx;
301 nb_entries.push_back(0);
305 idx = search->second;
312 std::array<double, N> acc;
315 float pmin = 1.0e-6, pmax = 1.0 - pmin;
316 float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
318 for (
auto const& ptr : items) {
319 auto const& vout = outs[ptr.key()];
320 for (
size_t i = 0; i < vout.size(); ++i) {
321 if (groupidx[i] < 0)
continue;
326 else if (vout[i] > pmax)
329 v = std::log(vout[i]);
335 if (!items.empty()) {
336 std::vector<double> totp(n_groups, 0.0);
337 for (
size_t i = 0; i < N; ++i) {
338 if (groupidx[i] >= 0) {
339 acc[i] = exp(acc[i] / items.size());
340 totp[groupidx[i]] += acc[i];
343 for (
size_t i = 0; i < N; ++i) {
344 if (groupidx[i] >= 0) { acc[i] /= totp[groupidx[i]]; }
348 for (
size_t i = 0; i < N; ++i) {
349 if (groupidx[i] >= 0) { acc[i] = 1 / nb_entries[groupidx[i]]; }
353 std::array<float, N> result;
354 for (
size_t i = 0; i < N; ++i)
360 #endif //ANAB_MVAWRAPPERBASE std::string getProductName(std::type_info const &ti) const
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
std::array< float, N > pAccumulate(std::vector< art::Ptr< T >> const &items, std::vector< FeatureVector< N >> const &outs) const
Helper functions for MVAReader and MVAWriter wrappers.
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Helper functions for MVAReader/Writer and FVecReader/Writer wrappers.
size_t getProductHash(std::type_info const &ti) const