9 #ifndef ANAB_MVAWRAPPERBASE_H
10 #define ANAB_MVAWRAPPERBASE_H
12 #include "canvas/Persistency/Common/Ptr.h"
19 #include <unordered_map>
31 size_t getProductHash(std::type_info
const & ti)
const {
return ti.hash_code(); }
43 template <
class T,
size_t N>
48 template <
class T,
size_t N>
50 std::vector< art::Ptr<T> >
const & items, std::vector<float>
const & weights,
53 template <
class T,
size_t N>
55 std::vector< art::Ptr<T> >
const & items, std::function<
float (T
const &)> fweight,
58 template <
class T,
size_t N>
60 std::vector< art::Ptr<T> >
const & items, std::function<
float (art::Ptr<T>
const &)> fweight,
67 template <
class T,
size_t N>
71 std::array<char, N>
const &
mask)
const;
79 template <
class T,
size_t N>
84 std::array<double, N> acc;
87 float pmin = 1.0e-6, pmax = 1.0 - pmin;
88 float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
90 for (
auto const & ptr : items)
92 auto const & vout = outs[ptr.key()];
93 for (
size_t i = 0; i < vout.size(); ++i)
96 if (vout[i] < pmin) v = log_pmin;
97 else if (vout[i] > pmax) v = log_pmax;
98 else v = std::log(vout[i]);
107 for (
size_t i = 0; i <
N; ++i)
109 acc[i] =
exp(acc[i] / items.size());
112 for (
size_t i = 0; i <
N; ++i)
117 else std::fill(acc.begin(), acc.end(), 1.0 /
N);
120 std::array<float, N> result;
121 for (
size_t i = 0; i <
N; ++i) result[i] = acc[i];
126 template <
class T,
size_t N>
128 std::vector< art::Ptr<T> >
const & items, std::vector<float>
const & weights,
131 std::array<double, N> acc;
134 float pmin = 1.0e-6, pmax = 1.0 - pmin;
135 float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
138 for (
size_t k = 0;
k < items.size(); ++
k)
140 auto const & ptr = items[
k];
141 float w = weights[
k];
143 if (w == 0)
continue;
145 auto const & vout = outs[ptr.key()];
146 for (
size_t i = 0; i < vout.size(); ++i)
149 if (vout[i] < pmin) v = log_pmin;
150 else if (vout[i] > pmax) v = log_pmax;
151 else v = std::log(vout[i]);
161 for (
size_t i = 0; i <
N; ++i)
163 acc[i] =
exp(acc[i] / totw);
166 for (
size_t i = 0; i <
N; ++i)
171 else std::fill(acc.begin(), acc.end(), 1.0 /
N);
174 std::array<float, N> result;
175 for (
size_t i = 0; i <
N; ++i) result[i] = acc[i];
180 template <
class T,
size_t N>
182 std::vector< art::Ptr<T> >
const & items, std::function<
float (T
const &)> fweight,
185 std::array<double, N> acc;
188 float pmin = 1.0e-6, pmax = 1.0 - pmin;
189 float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
192 for (
size_t k = 0;
k < items.size(); ++
k)
194 auto const & ptr = items[
k];
195 float w = fweight(*ptr);
197 if (w == 0)
continue;
199 auto const & vout = outs[ptr.key()];
200 for (
size_t i = 0; i < vout.size(); ++i)
203 if (vout[i] < pmin) v = log_pmin;
204 else if (vout[i] > pmax) v = log_pmax;
205 else v = std::log(vout[i]);
215 for (
size_t i = 0; i <
N; ++i)
217 acc[i] =
exp(acc[i] / totw);
220 for (
size_t i = 0; i <
N; ++i)
225 else std::fill(acc.begin(), acc.end(), 1.0 /
N);
228 std::array<float, N> result;
229 for (
size_t i = 0; i <
N; ++i) result[i] = acc[i];
234 template <
class T,
size_t N>
236 std::vector< art::Ptr<T> >
const & items, std::function<
float (art::Ptr<T>
const &)> fweight,
239 std::array<double, N> acc;
242 float pmin = 1.0e-6, pmax = 1.0 - pmin;
243 float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
246 for (
size_t k = 0;
k < items.size(); ++
k)
248 auto const & ptr = items[
k];
249 float w = fweight(ptr);
251 if (w == 0)
continue;
253 auto const & vout = outs[ptr.key()];
254 for (
size_t i = 0; i < vout.size(); ++i)
257 if (vout[i] < pmin) v = log_pmin;
258 else if (vout[i] > pmax) v = log_pmax;
259 else v = std::log(vout[i]);
269 for (
size_t i = 0; i <
N; ++i)
271 acc[i] =
exp(acc[i] / totw);
274 for (
size_t i = 0; i <
N; ++i)
279 else std::fill(acc.begin(), acc.end(), 1.0 /
N);
282 std::array<float, N> result;
283 for (
size_t i = 0; i <
N; ++i) result[i] = acc[i];
292 template <
class T,
size_t N>
296 std::array<char, N>
const &
mask)
const
299 std::unordered_map<char, size_t> label2group;
300 std::vector<size_t> nb_entries;
301 std::array<int, N> groupidx;
302 for (
size_t i = 0; i <
N; ++i)
307 auto search = label2group.find(mask[i]);
308 if (search == label2group.end())
311 label2group[mask[i]] = idx;
312 nb_entries.push_back(0);
317 idx = search->second;
324 std::array<double, N> acc;
327 float pmin = 1.0e-6, pmax = 1.0 - pmin;
328 float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
330 for (
auto const & ptr : items)
332 auto const & vout = outs[ptr.key()];
333 for (
size_t i = 0; i < vout.size(); ++i)
335 if (groupidx[i] < 0)
continue;
338 if (vout[i] < pmin) v = log_pmin;
339 else if (vout[i] > pmax) v = log_pmax;
340 else v = std::log(vout[i]);
348 std::vector<double> totp(n_groups, 0.0);
349 for (
size_t i = 0; i <
N; ++i)
351 if (groupidx[i] >= 0)
353 acc[i] =
exp(acc[i] / items.size());
354 totp[groupidx[i]] += acc[i];
357 for (
size_t i = 0; i <
N; ++i)
359 if (groupidx[i] >= 0) { acc[i] /= totp[groupidx[i]]; }
364 for (
size_t i = 0; i <
N; ++i)
366 if (groupidx[i] >= 0) { acc[i] = 1 / nb_entries[groupidx[i]]; }
370 std::array<float, N> result;
371 for (
size_t i = 0; i <
N; ++i) result[i] = acc[i];
376 #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.
Helper functions for MVAReader and MVAWriter wrappers.
constexpr mask_t< EnumType > mask(EnumType bit, OtherBits...otherBits)
Returns a mask with all specified bits set.
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Helper functions for MVAReader/Writer and FVecReader/Writer wrappers.
process_name largeant stream1 can override from command line with o or output physics producers generator N
std::array< float, N > pAccumulate(std::vector< art::Ptr< T > > const &items, std::vector< FeatureVector< N > > const &outs) const
size_t getProductHash(std::type_info const &ti) const