LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
MVAWriter.h
Go to the documentation of this file.
1 // \version
3 //
4 // \brief Wrapper for saving MVA results into art::Event
5 //
6 // \author robert.sulej@cern.ch
7 //
9 #ifndef ANAB_MVAWRITER_H
10 #define ANAB_MVAWRITER_H
11 
15 
17 
18 namespace anab {
19 
21  typedef size_t FVector_ID;
22  typedef size_t MVAOutput_ID;
23 
24  template <size_t N>
26  public:
31  FVectorWriter(art::ProducesCollector& collector, const char* name = "")
32  : fCollector(collector)
33  , fInstanceName(name)
35  , fDescriptions(nullptr)
36  {}
37 
41  template <class T>
42  void produces_using();
43 
49  template <class T>
50  FVector_ID initOutputs(std::string const& dataTag,
51  size_t dataSize,
52  std::vector<std::string> const& names = std::vector<std::string>(N, ""));
53 
54  template <class T>
55  FVector_ID initOutputs(art::InputTag const& dataTag,
56  size_t dataSize,
57  std::vector<std::string> const& names = std::vector<std::string>(N, ""))
58  {
59  return initOutputs<T>(dataTag.encode(), dataSize, names);
60  }
61 
62  void setVector(FVector_ID id, size_t key, std::array<float, N> const& values)
63  {
64  (*(fVectors[id]))[key] = values;
65  }
66  void setVector(FVector_ID id, size_t key, std::array<double, N> const& values)
67  {
68  (*(fVectors[id]))[key] = values;
69  }
70  void setVector(FVector_ID id, size_t key, std::vector<float> const& values)
71  {
72  (*(fVectors[id]))[key] = values;
73  }
74  void setVector(FVector_ID id, size_t key, std::vector<double> const& values)
75  {
76  (*(fVectors[id]))[key] = values;
77  }
78 
83  template <class T>
84  FVector_ID initOutputs(art::InputTag const& dataTag,
85  std::vector<std::string> const& names = std::vector<std::string>(N, ""))
86  {
87  return initOutputs<T>(dataTag.encode(), 0, names);
88  }
89 
90  template <class T>
91  FVector_ID initOutputs(std::vector<std::string> const& names = std::vector<std::string>(N, ""))
92  {
93  return initOutputs<T>(std::string(""), 0, names);
94  }
95 
96  void addVector(FVector_ID id, std::array<float, N> const& values)
97  {
98  fVectors[id]->emplace_back(values);
99  }
100  void addVector(FVector_ID id, std::array<double, N> const& values)
101  {
102  fVectors[id]->emplace_back(values);
103  }
104  void addVector(FVector_ID id, std::vector<float> const& values)
105  {
106  fVectors[id]->emplace_back(values);
107  }
108  void addVector(FVector_ID id, std::vector<double> const& values)
109  {
110  fVectors[id]->emplace_back(values);
111  }
112 
114  void setDataTag(FVector_ID id, art::InputTag const& dataTag)
115  {
116  (*fDescriptions)[id].setDataTag(dataTag.encode());
117  }
118 
120  void saveOutputs(art::Event& evt);
121 
123  size_t size(FVector_ID id) const { return fVectors[id]->size(); }
124 
126  size_t length() const { return N; }
127 
129  template <class T>
130  std::array<float, N> getVector(size_t key) const
131  {
132  std::array<float, N> vout;
133  auto const& src = (*(fVectors[getProductID<T>()]))[key];
134  for (size_t i = 0; i < N; ++i)
135  vout[i] = src[i];
136  return vout;
137  }
138 
140  template <class T>
141  std::array<float, N> getVector(art::Ptr<T> const& item) const
142  {
143  std::array<float, N> vout;
144  auto const& src = (*(fVectors[getProductID<T>()]))[item.key()];
145  for (size_t i = 0; i < N; ++i)
146  vout[i] = src[i];
147  return vout;
148  }
149 
150  friend std::ostream& operator<<(std::ostream& o, FVectorWriter const& a)
151  {
152  o << "FVectorWriter for " << a.fInstanceName << ", " << N << " outputs";
153  if (!a.fRegisteredDataTypes.empty()) {
154  o << ", ready to write results made for:" << std::endl;
155  for (auto const& n : a.fRegisteredDataTypes) {
156  o << "\t" << n << std::endl;
157  }
158  }
159  else {
160  o << ", nothing registered for writing to the events" << std::endl;
161  }
162  return o;
163  }
164 
165  protected:
166  // Data collected for each event:
167  template <class T>
168  FVector_ID getProductID() const;
169 
170  std::vector<std::unique_ptr<std::vector<anab::FeatureVector<N>>>> fVectors;
171 
172  private:
173  // Data initialized for the module life:
175  std::string fInstanceName;
176 
177  std::vector<std::string> fRegisteredDataTypes;
179 
180  std::unordered_map<size_t, FVector_ID> fTypeHashToID;
181 
182  std::unique_ptr<std::vector<anab::FVecDescription<N>>> fDescriptions;
184  {
185  fTypeHashToID.clear();
186  fVectors.clear();
187  fDescriptions.reset(nullptr);
188  }
189 
191  bool dataTypeRegistered(const std::string& dname) const;
193  bool descriptionExists(const std::string& tname) const;
194  };
195 
201  template <size_t N>
202  class MVAWriter : public FVectorWriter<N>, public MVAWrapperBase {
203  public:
210  MVAWriter(art::ProducesCollector& collector, const char* name = "")
211  : FVectorWriter<N>(collector, name)
212  {}
213 
214  void setOutput(FVector_ID id, size_t key, std::array<float, N> const& values)
215  {
216  FVectorWriter<N>::setVector(id, key, values);
217  }
218  void setOutput(FVector_ID id, size_t key, std::array<double, N> const& values)
219  {
220  FVectorWriter<N>::setVector(id, key, values);
221  }
222  void setOutput(FVector_ID id, size_t key, std::vector<float> const& values)
223  {
224  FVectorWriter<N>::setVector(id, key, values);
225  }
226  void setOutput(FVector_ID id, size_t key, std::vector<double> const& values)
227  {
228  FVectorWriter<N>::setVector(id, key, values);
229  }
230 
231  void addOutput(FVector_ID id, std::array<float, N> const& values)
232  {
233  FVectorWriter<N>::addVector(id, values);
234  }
235  void addOutput(FVector_ID id, std::array<double, N> const& values)
236  {
237  FVectorWriter<N>::addVector(id, values);
238  }
239  void addOutput(FVector_ID id, std::vector<float> const& values)
240  {
241  FVectorWriter<N>::addVector(id, values);
242  }
243  void addOutput(FVector_ID id, std::vector<double> const& values)
244  {
245  FVectorWriter<N>::addVector(id, values);
246  }
247 
250  template <class T>
251  std::array<float, N> getOutput(std::vector<art::Ptr<T>> const& items) const
252  {
253  return pAccumulate<T, N>(
254  items, *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()]));
255  }
256 
261  template <class T>
262  std::array<float, N> getOutput(std::vector<art::Ptr<T>> const& items,
263  std::vector<float> const& weights) const
264  {
265  return pAccumulate<T, N>(
266  items,
267  weights,
269  }
270 
275  template <class T>
276  std::array<float, N> getOutput(std::vector<art::Ptr<T>> const& items,
277  std::function<float(T const&)> fweight) const
278  {
279  return pAccumulate<T, N>(
280  items,
281  fweight,
283  }
284 
285  template <class T>
286  std::array<float, N> getOutput(std::vector<art::Ptr<T>> const& items,
287  std::function<float(art::Ptr<T> const&)> fweight) const
288  {
289  return pAccumulate<T, N>(
290  items,
291  fweight,
293  }
294 
296  template <class T>
297  std::array<float, N> getOutput(size_t key) const
298  {
299  return FVectorWriter<N>::template getVector<T>(key);
300  }
301 
303  template <class T>
304  std::array<float, N> getOutput(art::Ptr<T> const& item) const
305  {
306  return FVectorWriter<N>::template getVector<T>(item);
307  }
308  };
309 
310 } // namespace anab
311 
312 //----------------------------------------------------------------------------
313 // FVectorWriter functions.
314 //
315 template <size_t N>
316 template <class T>
318 {
319  auto const& ti = typeid(T);
320  auto search = fTypeHashToID.find(getProductHash(ti));
321  if (search != fTypeHashToID.end()) { return search->second; }
322  else {
323  throw cet::exception("FVectorWriter")
324  << "Feature vectors not initialized for product " << getProductName(ti) << std::endl;
325  }
326 }
327 //----------------------------------------------------------------------------
328 
329 template <size_t N>
330 bool anab::FVectorWriter<N>::dataTypeRegistered(const std::string& dname) const
331 {
332  for (auto const& s : fRegisteredDataTypes) {
333  if (s == dname) { return true; }
334  }
335  return false;
336 }
337 //----------------------------------------------------------------------------
338 
339 template <size_t N>
340 template <class T>
342 {
343  std::string dataName = getProductName(typeid(T));
344  if (dataTypeRegistered(dataName)) {
345  throw cet::exception("FVectorWriter")
346  << "Type " << dataName << "was already registered." << std::endl;
347  }
348 
350  fCollector.produces<std::vector<anab::FVecDescription<N>>>(fInstanceName);
352  }
353 
354  fCollector.produces<std::vector<anab::FeatureVector<N>>>(fInstanceName + dataName);
355  fRegisteredDataTypes.push_back(dataName);
356 }
357 //----------------------------------------------------------------------------
358 
359 template <size_t N>
360 bool anab::FVectorWriter<N>::descriptionExists(const std::string& tname) const
361 {
362  if (!fDescriptions) return false;
363 
364  std::string n = fInstanceName + tname;
365  for (auto const& d : *fDescriptions) {
366  if (d.outputInstance() == n) { return true; }
367  }
368  return false;
369 }
370 //----------------------------------------------------------------------------
371 
372 template <size_t N>
373 template <class T>
375  size_t dataSize,
376  std::vector<std::string> const& names)
377 {
378  size_t dataHash = getProductHash(typeid(T));
379  std::string dataName = getProductName(typeid(T));
380 
381  if (!dataTypeRegistered(dataName)) {
382  throw cet::exception("FVectorWriter")
383  << "Type " << dataName << "not registered with produces_using() function." << std::endl;
384  }
385 
386  if (!fDescriptions) { fDescriptions = std::make_unique<std::vector<anab::FVecDescription<N>>>(); }
387  else if (descriptionExists(dataName)) {
388  throw cet::exception("FVectorWriter")
389  << "FVecDescription<N> already initialized for " << dataName << std::endl;
390  }
391  fDescriptions->emplace_back(dataTag, fInstanceName + dataName, names);
392 
393  fVectors.push_back(std::make_unique<std::vector<anab::FeatureVector<N>>>());
394  anab::FVector_ID id = fVectors.size() - 1;
395  fTypeHashToID[dataHash] = id;
396 
397  if (dataSize) { fVectors[id]->resize(dataSize, anab::FeatureVector<N>(0.0F)); }
398 
399  return id;
400 }
401 //----------------------------------------------------------------------------
402 
403 template <size_t N>
405 {
406  for (auto const& n : fRegisteredDataTypes) {
407  if (!descriptionExists(n)) {
408  throw cet::exception("FVectorWriter")
409  << "No FVecDescription<N> prepared for type " << n << std::endl;
410  }
411  }
412 
413  if (fVectors.size() != fDescriptions->size()) {
414  throw cet::exception("FVectorWriter")
415  << "FVecDescription<N> vector length not equal to the number of FeatureVector<N> vectors"
416  << std::endl;
417  }
418 
419  for (size_t i = 0; i < fVectors.size(); ++i) {
420  auto const& outInstName = (*fDescriptions)[i].outputInstance();
421  if ((*fDescriptions)[i].dataTag().empty()) {
422  throw cet::exception("FVectorWriter")
423  << "FVecDescription<N> reco data tag not set for " << outInstName << std::endl;
424  }
425  evt.put(std::move(fVectors[i]), outInstName);
426  }
427  evt.put(std::move(fDescriptions), fInstanceName);
428  clearEventData();
429 }
430 //----------------------------------------------------------------------------
431 
432 #endif //ANAB_MVAREADER
void addVector(FVector_ID id, std::vector< float > const &values)
Definition: MVAWriter.h:104
void setOutput(FVector_ID id, size_t key, std::array< double, N > const &values)
Definition: MVAWriter.h:218
std::array< float, N > getOutput(std::vector< art::Ptr< T >> const &items, std::vector< float > const &weights) const
Definition: MVAWriter.h:262
std::array< float, N > getVector(art::Ptr< T > const &item) const
Get copy of the feature vector for the type T, idicated with art::Ptr::key().
Definition: MVAWriter.h:141
size_t length() const
Get the length of a single feature vector.
Definition: MVAWriter.h:126
void setVector(FVector_ID id, size_t key, std::vector< double > const &values)
Definition: MVAWriter.h:74
void setOutput(FVector_ID id, size_t key, std::array< float, N > const &values)
Definition: MVAWriter.h:214
size_t size(FVector_ID id) const
Get the number of contained feature vectors.
Definition: MVAWriter.h:123
std::array< float, N > getOutput(std::vector< art::Ptr< T >> const &items, std::function< float(T const &)> fweight) const
Definition: MVAWriter.h:276
void setVector(FVector_ID id, size_t key, std::array< double, N > const &values)
Definition: MVAWriter.h:66
void setOutput(FVector_ID id, size_t key, std::vector< double > const &values)
Definition: MVAWriter.h:226
bool descriptionExists(const std::string &tname) const
Check if the containers for results prepared for "tname" data type are ready.
Definition: MVAWriter.h:360
size_t FVector_ID
Index to the MVA output / FeatureVector collection, used when result vectors are added or set...
Definition: MVAWriter.h:21
std::string encode() const
Definition: InputTag.cc:97
std::string getProductName(std::type_info const &ti) const
FVector_ID initOutputs(std::vector< std::string > const &names=std::vector< std::string >(N,""))
Definition: MVAWriter.h:91
void addVector(FVector_ID id, std::array< float, N > const &values)
Definition: MVAWriter.h:96
MVAWriter(art::ProducesCollector &collector, const char *name="")
Definition: MVAWriter.h:210
void addVector(FVector_ID id, std::vector< double > const &values)
Definition: MVAWriter.h:108
std::array< float, N > getVector(size_t key) const
Get copy of the feature vector for the type T, at index "key".
Definition: MVAWriter.h:130
FVector_ID initOutputs(art::InputTag const &dataTag, size_t dataSize, std::vector< std::string > const &names=std::vector< std::string >(N,""))
Definition: MVAWriter.h:55
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
size_t MVAOutput_ID
Definition: MVAWriter.h:22
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
FVector_ID initOutputs(std::string const &dataTag, size_t dataSize, std::vector< std::string > const &names=std::vector< std::string >(N,""))
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
Helper functions for MVAReader and MVAWriter wrappers.
art::ProducesCollector & fCollector
Definition: MVAWriter.h:174
void addOutput(FVector_ID id, std::array< double, N > const &values)
Definition: MVAWriter.h:235
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
std::array< float, N > getOutput(std::vector< art::Ptr< T >> const &items) const
Definition: MVAWriter.h:251
key_type key() const noexcept
Definition: Ptr.h:166
Float_t d
Definition: plot.C:235
std::array< float, N > getOutput(art::Ptr< T > const &item) const
Get copy of the MVA output vector for the type T, idicated with art::Ptr::key().
Definition: MVAWriter.h:304
void addOutput(FVector_ID id, std::vector< double > const &values)
Definition: MVAWriter.h:243
std::array< float, N > getOutput(std::vector< art::Ptr< T >> const &items, std::function< float(art::Ptr< T > const &)> fweight) const
Definition: MVAWriter.h:286
void saveOutputs(art::Event &evt)
Check consistency and save all the results in the event.
Definition: MVAWriter.h:404
std::vector< std::string > fRegisteredDataTypes
Definition: MVAWriter.h:177
void addOutput(FVector_ID id, std::array< float, N > const &values)
Definition: MVAWriter.h:231
std::unordered_map< size_t, FVector_ID > fTypeHashToID
Definition: MVAWriter.h:180
Helper functions for MVAReader/Writer and FVecReader/Writer wrappers.
FVector_ID getProductID() const
bool fIsDescriptionRegistered
Definition: MVAWriter.h:178
FVector_ID initOutputs(art::InputTag const &dataTag, std::vector< std::string > const &names=std::vector< std::string >(N,""))
Definition: MVAWriter.h:84
void setVector(FVector_ID id, size_t key, std::array< float, N > const &values)
Definition: MVAWriter.h:62
void setOutput(FVector_ID id, size_t key, std::vector< float > const &values)
Definition: MVAWriter.h:222
bool dataTypeRegistered(const std::string &dname) const
Check if the the writer is configured to write results for data product type name.
Definition: MVAWriter.h:330
void setDataTag(FVector_ID id, art::InputTag const &dataTag)
Set tag of associated data products in case it was not ready at the initialization time...
Definition: MVAWriter.h:114
std::string fInstanceName
Definition: MVAWriter.h:175
Char_t n[5]
std::array< float, N > getOutput(size_t key) const
Get copy of the MVA output vector for the type T, at index "key".
Definition: MVAWriter.h:297
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:182
void setVector(FVector_ID id, size_t key, std::vector< float > const &values)
Definition: MVAWriter.h:70
TCEvent evt
Definition: DataStructs.cxx:8
size_t getProductHash(std::type_info const &ti) const
void addOutput(FVector_ID id, std::vector< float > const &values)
Definition: MVAWriter.h:239
FVectorWriter(art::ProducesCollector &collector, const char *name="")
Definition: MVAWriter.h:31
void addVector(FVector_ID id, std::array< double, N > const &values)
Definition: MVAWriter.h:100
Definition: fwd.h:26
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:109
friend std::ostream & operator<<(std::ostream &o, FVectorWriter const &a)
Definition: MVAWriter.h:150