LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
MVAWrapperBase.h
Go to the documentation of this file.
1 // \version
3 //
4 // \brief Helper functions for MVAReader and MVAWriter wrappers
5 //
6 // \author robert.sulej@cern.ch
7 //
9 #ifndef ANAB_MVAWRAPPERBASE_H
10 #define ANAB_MVAWRAPPERBASE_H
11 
13 
15 
16 #include <cmath>
17 #include <functional>
18 #include <string>
19 #include <typeinfo>
20 #include <unordered_map>
21 
22 namespace anab {
23 
26  public:
27  protected:
28  std::string getProductName(std::type_info const& ti) const;
29  size_t getProductHash(std::type_info const& ti) const { return ti.hash_code(); }
30  };
31 
34  public:
35  protected:
36  // all mva outputs in the feature vecor sum up to p=1
37 
38  template <class T, size_t N>
39  std::array<float, N> pAccumulate(std::vector<art::Ptr<T>> const& items,
40  std::vector<FeatureVector<N>> const& outs) const;
41 
42  template <class T, size_t N>
43  std::array<float, N> pAccumulate(std::vector<art::Ptr<T>> const& items,
44  std::vector<float> const& weights,
45  std::vector<FeatureVector<N>> const& outs) const;
46 
47  template <class T, size_t N>
48  std::array<float, N> pAccumulate(std::vector<art::Ptr<T>> const& items,
49  std::function<float(T const&)> fweight,
50  std::vector<FeatureVector<N>> const& outs) const;
51 
52  template <class T, size_t N>
53  std::array<float, N> pAccumulate(std::vector<art::Ptr<T>> const& items,
54  std::function<float(art::Ptr<T> const&)> fweight,
55  std::vector<FeatureVector<N>> const& outs) const;
56 
57  // outputs in the feature vecor sum up to p=1 in groups:
58  // - members of a group are tagged in the mask with the same non-negative number
59  // - entries with negative tag in the mask are ignored
60 
61  template <class T, size_t N>
62  std::array<float, N> pAccumulate(std::vector<art::Ptr<T>> const& items,
63  std::vector<FeatureVector<N>> const& outs,
64  std::array<char, N> const& mask) const;
65  };
66 
67 } // namespace anab
68 
69 //----------------------------------------------------------------------------
70 // MVAReader functions.
71 //
72 template <class T, size_t N>
73 std::array<float, N> anab::MVAWrapperBase::pAccumulate(
74  std::vector<art::Ptr<T>> const& items,
75  std::vector<anab::FeatureVector<N>> const& outs) const
76 {
77  std::array<double, N> acc;
78  acc.fill(0);
79 
80  float pmin = 1.0e-6, pmax = 1.0 - pmin;
81  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
82 
83  for (auto const& ptr : items) {
84  auto const& vout = outs[ptr.key()];
85  for (size_t i = 0; i < vout.size(); ++i) {
86  float v;
87  if (vout[i] < pmin)
88  v = log_pmin;
89  else if (vout[i] > pmax)
90  v = log_pmax;
91  else
92  v = std::log(vout[i]);
93 
94  acc[i] += v;
95  }
96  }
97 
98  if (!items.empty()) {
99  double totp = 0.0;
100  for (size_t i = 0; i < N; ++i) {
101  acc[i] = exp(acc[i] / items.size());
102  totp += acc[i];
103  }
104  for (size_t i = 0; i < N; ++i) {
105  acc[i] /= totp;
106  }
107  }
108  else
109  std::fill(acc.begin(), acc.end(), 1.0 / N);
110 
111  std::array<float, N> result;
112  for (size_t i = 0; i < N; ++i)
113  result[i] = acc[i];
114  return result;
115 }
116 //----------------------------------------------------------------------------
117 
118 template <class T, size_t N>
120  std::vector<art::Ptr<T>> const& items,
121  std::vector<float> const& weights,
122  std::vector<anab::FeatureVector<N>> const& outs) const
123 {
124  std::array<double, N> acc;
125  acc.fill(0);
126 
127  float pmin = 1.0e-6, pmax = 1.0 - pmin;
128  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
129  double totw = 0.0;
130 
131  for (size_t k = 0; k < items.size(); ++k) {
132  auto const& ptr = items[k];
133  float w = weights[k];
134 
135  if (w == 0) continue;
136 
137  auto const& vout = outs[ptr.key()];
138  for (size_t i = 0; i < vout.size(); ++i) {
139  float v;
140  if (vout[i] < pmin)
141  v = log_pmin;
142  else if (vout[i] > pmax)
143  v = log_pmax;
144  else
145  v = std::log(vout[i]);
146 
147  acc[i] += w * v;
148  }
149  totw += w;
150  }
151 
152  if (!items.empty()) {
153  double totp = 0.0;
154  for (size_t i = 0; i < N; ++i) {
155  acc[i] = exp(acc[i] / totw);
156  totp += acc[i];
157  }
158  for (size_t i = 0; i < N; ++i) {
159  acc[i] /= totp;
160  }
161  }
162  else
163  std::fill(acc.begin(), acc.end(), 1.0 / N);
164 
165  std::array<float, N> result;
166  for (size_t i = 0; i < N; ++i)
167  result[i] = acc[i];
168  return result;
169 }
170 //----------------------------------------------------------------------------
171 
172 template <class T, size_t N>
174  std::vector<art::Ptr<T>> const& items,
175  std::function<float(T const&)> fweight,
176  std::vector<anab::FeatureVector<N>> const& outs) const
177 {
178  std::array<double, N> acc;
179  acc.fill(0);
180 
181  float pmin = 1.0e-6, pmax = 1.0 - pmin;
182  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
183  double totw = 0.0;
184 
185  for (size_t k = 0; k < items.size(); ++k) {
186  auto const& ptr = items[k];
187  float w = fweight(*ptr);
188 
189  if (w == 0) continue;
190 
191  auto const& vout = outs[ptr.key()];
192  for (size_t i = 0; i < vout.size(); ++i) {
193  float v;
194  if (vout[i] < pmin)
195  v = log_pmin;
196  else if (vout[i] > pmax)
197  v = log_pmax;
198  else
199  v = std::log(vout[i]);
200 
201  acc[i] += w * v;
202  }
203  totw += w;
204  }
205 
206  if (!items.empty()) {
207  double totp = 0.0;
208  for (size_t i = 0; i < N; ++i) {
209  acc[i] = exp(acc[i] / totw);
210  totp += acc[i];
211  }
212  for (size_t i = 0; i < N; ++i) {
213  acc[i] /= totp;
214  }
215  }
216  else
217  std::fill(acc.begin(), acc.end(), 1.0 / N);
218 
219  std::array<float, N> result;
220  for (size_t i = 0; i < N; ++i)
221  result[i] = acc[i];
222  return result;
223 }
224 //----------------------------------------------------------------------------
225 
226 template <class T, size_t N>
228  std::vector<art::Ptr<T>> const& items,
229  std::function<float(art::Ptr<T> const&)> fweight,
230  std::vector<anab::FeatureVector<N>> const& outs) const
231 {
232  std::array<double, N> acc;
233  acc.fill(0);
234 
235  float pmin = 1.0e-6, pmax = 1.0 - pmin;
236  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
237  double totw = 0.0;
238 
239  for (size_t k = 0; k < items.size(); ++k) {
240  auto const& ptr = items[k];
241  float w = fweight(ptr);
242 
243  if (w == 0) continue;
244 
245  auto const& vout = outs[ptr.key()];
246  for (size_t i = 0; i < vout.size(); ++i) {
247  float v;
248  if (vout[i] < pmin)
249  v = log_pmin;
250  else if (vout[i] > pmax)
251  v = log_pmax;
252  else
253  v = std::log(vout[i]);
254 
255  acc[i] += w * v;
256  }
257  totw += w;
258  }
259 
260  if (!items.empty()) {
261  double totp = 0.0;
262  for (size_t i = 0; i < N; ++i) {
263  acc[i] = exp(acc[i] / totw);
264  totp += acc[i];
265  }
266  for (size_t i = 0; i < N; ++i) {
267  acc[i] /= totp;
268  }
269  }
270  else
271  std::fill(acc.begin(), acc.end(), 1.0 / N);
272 
273  std::array<float, N> result;
274  for (size_t i = 0; i < N; ++i)
275  result[i] = acc[i];
276  return result;
277 }
278 //----------------------------------------------------------------------------
279 
280 //----------------------------------------------------------------------------
281 // functions with the mask tagging groups og labels
282 //----------------------------------------------------------------------------
283 
284 template <class T, size_t N>
286  std::vector<art::Ptr<T>> const& items,
287  std::vector<anab::FeatureVector<N>> const& outs,
288  std::array<char, N> const& mask) const
289 {
290  size_t n_groups = 0;
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) {
295  int idx = -1;
296  if (mask[i] >= 0) {
297  auto search = label2group.find(mask[i]);
298  if (search == label2group.end()) {
299  idx = n_groups;
300  label2group[mask[i]] = idx;
301  nb_entries.push_back(0);
302  ++n_groups;
303  }
304  else {
305  idx = search->second;
306  nb_entries[idx]++;
307  }
308  }
309  groupidx[i] = idx;
310  }
311 
312  std::array<double, N> acc;
313  acc.fill(0);
314 
315  float pmin = 1.0e-6, pmax = 1.0 - pmin;
316  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
317 
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;
322 
323  float v;
324  if (vout[i] < pmin)
325  v = log_pmin;
326  else if (vout[i] > pmax)
327  v = log_pmax;
328  else
329  v = std::log(vout[i]);
330 
331  acc[i] += v;
332  }
333  }
334 
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];
341  }
342  }
343  for (size_t i = 0; i < N; ++i) {
344  if (groupidx[i] >= 0) { acc[i] /= totp[groupidx[i]]; }
345  }
346  }
347  else {
348  for (size_t i = 0; i < N; ++i) {
349  if (groupidx[i] >= 0) { acc[i] = 1 / nb_entries[groupidx[i]]; }
350  }
351  }
352 
353  std::array<float, N> result;
354  for (size_t i = 0; i < N; ++i)
355  result[i] = acc[i];
356  return result;
357 }
358 //----------------------------------------------------------------------------
359 
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.
Definition: DumpUtils.h:289
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
Float_t w
Definition: plot.C:20
Definition: fwd.h:26