LArSoft  v06_85_00
Liquid Argon Software toolkit - http://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 
16 
18 
19 namespace anab {
20 
22 typedef size_t FVector_ID;
23 typedef size_t MVAOutput_ID;
24 
25 template <size_t N>
27 public:
28 
33  FVectorWriter(art::EDProducer* module, const char* name = "") :
34  fProducer(module), fInstanceName(name),
36  fDescriptions(nullptr)
37  { }
38 
42  template <class T>
43  void produces_using();
44 
50  template <class T>
51  FVector_ID initOutputs(std::string const & dataTag, 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, size_t dataSize,
56  std::vector< std::string > const & names = std::vector< std::string >(N, ""))
57  { return initOutputs<T>(dataTag.encode(), dataSize, names); }
58 
59  void setVector(FVector_ID id, size_t key, std::array<float, N> const & values) { (*(fVectors[id]))[key] = values; }
60  void setVector(FVector_ID id, size_t key, std::array<double, N> const & values) { (*(fVectors[id]))[key] = values; }
61  void setVector(FVector_ID id, size_t key, std::vector<float> const & values) { (*(fVectors[id]))[key] = values; }
62  void setVector(FVector_ID id, size_t key, std::vector<double> const & values) { (*(fVectors[id]))[key] = values; }
63 
64 
69  template <class T>
70  FVector_ID initOutputs(art::InputTag const & dataTag,
71  std::vector< std::string > const & names = std::vector< std::string >(N, ""))
72  { return initOutputs<T>(dataTag.encode(), 0, names); }
73 
74  template <class T>
75  FVector_ID initOutputs(
76  std::vector< std::string > const & names = std::vector< std::string >(N, ""))
77  { return initOutputs<T>(std::string(""), 0, names); }
78 
79  void addVector(FVector_ID id, std::array<float, N> const & values) { fVectors[id]->emplace_back(values); }
80  void addVector(FVector_ID id, std::array<double, N> const & values) { fVectors[id]->emplace_back(values); }
81  void addVector(FVector_ID id, std::vector<float> const & values) { fVectors[id]->emplace_back(values); }
82  void addVector(FVector_ID id, std::vector<double> const & values) { fVectors[id]->emplace_back(values); }
83 
85  void setDataTag(FVector_ID id, art::InputTag const & dataTag) { (*fDescriptions)[id].setDataTag(dataTag.encode()); }
86 
88  void saveOutputs(art::Event & evt);
89 
91  size_t size(FVector_ID id) const { return fVectors[id]->size(); }
92 
94  size_t length() const { return N; }
95 
97  template <class T>
98  std::array<float, N> getVector(size_t key) const
99  {
100  std::array<float, N> vout;
101  auto const & src = ( *(fVectors[getProductID<T>()]) )[key];
102  for (size_t i = 0; i < N; ++i) vout[i] = src[i];
103  return vout;
104  }
105 
107  template <class T>
108  std::array<float, N> getVector(art::Ptr<T> const & item) const
109  {
110  std::array<float, N> vout;
111  auto const & src = ( *(fVectors[getProductID<T>()]) )[item.key()];
112  for (size_t i = 0; i < N; ++i) vout[i] = src[i];
113  return vout;
114  }
115 
116  friend std::ostream& operator<< (std::ostream &o, FVectorWriter const& a)
117  {
118  o << "FVectorWriter for " << a.fInstanceName << ", " << N << " outputs";
119  if (!a.fRegisteredDataTypes.empty())
120  {
121  o << ", ready to write results made for:" << std::endl;
122  for (auto const & n : a.fRegisteredDataTypes) { o << "\t" << n << std::endl; }
123  }
124  else { o << ", nothing registered for writing to the events" << std::endl; }
125  return o;
126  }
127 
128 protected:
129  // Data collected for each event:
130  template <class T> FVector_ID getProductID() const;
131 
132  std::vector< std::unique_ptr< std::vector< anab::FeatureVector<N> > > > fVectors;
133 
134 private:
135  // Data initialized for the module life:
137  std::string fInstanceName;
138 
139  std::vector< std::string > fRegisteredDataTypes;
141 
142  std::unordered_map< size_t, FVector_ID > fTypeHashToID;
143 
144  std::unique_ptr< std::vector< anab::FVecDescription<N> > > fDescriptions;
146  {
147  fTypeHashToID.clear(); fVectors.clear();
148  fDescriptions.reset(nullptr);
149  }
150 
152  bool dataTypeRegistered(const std::string & dname) const;
154  bool descriptionExists(const std::string & tname) const;
155 };
156 
162 template <size_t N>
163 class MVAWriter : public FVectorWriter<N>, public MVAWrapperBase {
164 public:
165 
172  MVAWriter(art::EDProducer* module, const char* name = "") :
173  FVectorWriter<N>(module, name)
174  { }
175 
176  void setOutput(FVector_ID id, size_t key, std::array<float, N> const & values) { FVectorWriter<N>::setVector(id, key, values); }
177  void setOutput(FVector_ID id, size_t key, std::array<double, N> const & values) { FVectorWriter<N>::setVector(id, key, values); }
178  void setOutput(FVector_ID id, size_t key, std::vector<float> const & values) { FVectorWriter<N>::setVector(id, key, values); }
179  void setOutput(FVector_ID id, size_t key, std::vector<double> const & values) { FVectorWriter<N>::setVector(id, key, values); }
180 
181  void addOutput(FVector_ID id, std::array<float, N> const & values) { FVectorWriter<N>::addVector(id, values); }
182  void addOutput(FVector_ID id, std::array<double, N> const & values) { FVectorWriter<N>::addVector(id, values); }
183  void addOutput(FVector_ID id, std::vector<float> const & values) { FVectorWriter<N>::addVector(id, values); }
184  void addOutput(FVector_ID id, std::vector<double> const & values) { FVectorWriter<N>::addVector(id, values); }
185 
186 
189  template <class T>
190  std::array<float, N> getOutput(std::vector< art::Ptr<T> > const & items) const
191  { return pAccumulate<T, N>(items, *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()])); }
192 
197  template <class T>
198  std::array<float, N> getOutput(std::vector< art::Ptr<T> > const & items,
199  std::vector<float> const & weights) const
200  { return pAccumulate<T, N>(items, weights, *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()])); }
201 
206  template <class T>
207  std::array<float, N> getOutput(std::vector< art::Ptr<T> > const & items,
208  std::function<float (T const &)> fweight) const
209  { return pAccumulate<T, N>(items, fweight, *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()])); }
210 
211  template <class T>
212  std::array<float, N> getOutput(std::vector< art::Ptr<T> > const & items,
213  std::function<float (art::Ptr<T> const &)> fweight) const
214  { return pAccumulate<T, N>(items, fweight, *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()])); }
215 
217  template <class T>
218  std::array<float, N> getOutput(size_t key) const { return FVectorWriter<N>::template getVector<T>(key); }
219 
221  template <class T>
222  std::array<float, N> getOutput(art::Ptr<T> const & item) const { return FVectorWriter<N>::template getVector<T>(item); }
223 };
224 
225 } // namespace anab
226 
227 
228 //----------------------------------------------------------------------------
229 // FVectorWriter functions.
230 //
231 template <size_t N>
232 template <class T>
234 {
235  auto const & ti = typeid(T);
236  auto search = fTypeHashToID.find(getProductHash(ti));
237  if (search != fTypeHashToID.end()) { return search->second; }
238  else
239  {
240  throw cet::exception("FVectorWriter") << "Feature vectors not initialized for product " << getProductName(ti) << std::endl;
241  }
242 }
243 //----------------------------------------------------------------------------
244 
245 template <size_t N>
246 bool anab::FVectorWriter<N>::dataTypeRegistered(const std::string & dname) const
247 {
248  for (auto const & s : fRegisteredDataTypes)
249  {
250  if (s == dname) { return true; }
251  }
252  return false;
253 }
254 //----------------------------------------------------------------------------
255 
256 template <size_t N>
257 template <class T>
259 {
260  std::string dataName = getProductName(typeid(T));
261  if (dataTypeRegistered(dataName))
262  {
263  throw cet::exception("FVectorWriter") << "Type " << dataName << "was already registered." << std::endl;
264  }
265 
267  {
268  fProducer->produces< std::vector< anab::FVecDescription<N> > >(fInstanceName);
270  }
271 
272  fProducer->produces< std::vector< anab::FeatureVector<N> > >(fInstanceName + dataName);
273  fRegisteredDataTypes.push_back(dataName);
274 }
275 //----------------------------------------------------------------------------
276 
277 template <size_t N>
278 bool anab::FVectorWriter<N>::descriptionExists(const std::string & tname) const
279 {
280  if (!fDescriptions) return false;
281 
282  std::string n = fInstanceName + tname;
283  for (auto const & d : *fDescriptions)
284  {
285  if (d.outputInstance() == n) { return true; }
286  }
287  return false;
288 }
289 //----------------------------------------------------------------------------
290 
291 template <size_t N>
292 template <class T>
294  std::string const & dataTag, size_t dataSize,
295  std::vector< std::string > const & names)
296 {
297  size_t dataHash = getProductHash(typeid(T));
298  std::string dataName = getProductName(typeid(T));
299 
300  if (!dataTypeRegistered(dataName))
301  {
302  throw cet::exception("FVectorWriter") << "Type " << dataName << "not registered with produces_using() function." << std::endl;
303  }
304 
305  if (!fDescriptions)
306  {
307  fDescriptions = std::make_unique< std::vector< anab::FVecDescription<N> > >();
308  }
309  else if (descriptionExists(dataName))
310  {
311  throw cet::exception("FVectorWriter") << "FVecDescription<N> already initialized for " << dataName << std::endl;
312  }
313  fDescriptions->emplace_back(dataTag, fInstanceName + dataName, names);
314 
315  fVectors.push_back( std::make_unique< std::vector< anab::FeatureVector<N> > >() );
316  anab::FVector_ID id = fVectors.size() - 1;
317  fTypeHashToID[dataHash] = id;
318 
319  if (dataSize) { fVectors[id]->resize(dataSize, anab::FeatureVector<N>(0.0F)); }
320 
321  return id;
322 }
323 //----------------------------------------------------------------------------
324 
325 template <size_t N>
327 {
328  for (auto const & n : fRegisteredDataTypes)
329  {
330  if (!descriptionExists(n))
331  {
332  throw cet::exception("FVectorWriter") << "No FVecDescription<N> prepared for type " << n << std::endl;
333  }
334  }
335 
336  if (fVectors.size() != fDescriptions->size())
337  {
338  throw cet::exception("FVectorWriter") << "FVecDescription<N> vector length not equal to the number of FeatureVector<N> vectors" << std::endl;
339  }
340 
341  for (size_t i = 0; i < fVectors.size(); ++i)
342  {
343  auto const & outInstName = (*fDescriptions)[i].outputInstance();
344  if ((*fDescriptions)[i].dataTag().empty())
345  {
346  throw cet::exception("FVectorWriter") << "FVecDescription<N> reco data tag not set for " << outInstName << std::endl;
347  }
348  evt.put(std::move(fVectors[i]), outInstName);
349  }
350  evt.put(std::move(fDescriptions), fInstanceName);
351  clearEventData();
352 }
353 //----------------------------------------------------------------------------
354 
355 #endif //ANAB_MVAREADER
356 
void addVector(FVector_ID id, std::vector< float > const &values)
Definition: MVAWriter.h:81
key_type key() const
Definition: Ptr.h:356
Float_t s
Definition: plot.C:23
void setOutput(FVector_ID id, size_t key, std::array< double, N > const &values)
Definition: MVAWriter.h:177
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:108
std::unordered_map< size_t, FVector_ID > fTypeHashToID
Definition: MVAWriter.h:142
size_t length() const
Get the length of a single feature vector.
Definition: MVAWriter.h:94
void setVector(FVector_ID id, size_t key, std::vector< double > const &values)
Definition: MVAWriter.h:62
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items, std::function< float(art::Ptr< T > const &)> fweight) const
Definition: MVAWriter.h:212
void setOutput(FVector_ID id, size_t key, std::array< float, N > const &values)
Definition: MVAWriter.h:176
size_t size(FVector_ID id) const
Get the number of contained feature vectors.
Definition: MVAWriter.h:91
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items, std::vector< float > const &weights) const
Definition: MVAWriter.h:198
void setVector(FVector_ID id, size_t key, std::array< double, N > const &values)
Definition: MVAWriter.h:60
void setOutput(FVector_ID id, size_t key, std::vector< double > const &values)
Definition: MVAWriter.h:179
bool descriptionExists(const std::string &tname) const
Check if the containers for results prepared for "tname" data type are ready.
Definition: MVAWriter.h:278
size_t FVector_ID
Index to the MVA output / FeatureVector collection, used when result vectors are added or set...
Definition: MVAWriter.h:22
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:75
void addVector(FVector_ID id, std::array< float, N > const &values)
Definition: MVAWriter.h:79
void addVector(FVector_ID id, std::vector< double > const &values)
Definition: MVAWriter.h:82
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:98
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
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
size_t MVAOutput_ID
Definition: MVAWriter.h:23
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
FVector_ID initOutputs(std::string const &dataTag, size_t dataSize, std::vector< std::string > const &names=std::vector< std::string >(N,""))
Helper functions for MVAReader and MVAWriter wrappers.
MVAWriter(art::EDProducer *module, const char *name="")
Definition: MVAWriter.h:172
void addOutput(FVector_ID id, std::array< double, N > const &values)
Definition: MVAWriter.h:182
std::string encode() const
Definition: InputTag.cc:36
Float_t d
Definition: plot.C:237
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:222
void addOutput(FVector_ID id, std::vector< double > const &values)
Definition: MVAWriter.h:184
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:144
FVectorWriter(art::EDProducer *module, const char *name="")
Definition: MVAWriter.h:33
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items) const
Definition: MVAWriter.h:190
art::EDProducer * fProducer
Definition: MVAWriter.h:136
void saveOutputs(art::Event &evt)
Check consistency and save all the results in the event.
Definition: MVAWriter.h:326
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items, std::function< float(T const &)> fweight) const
Definition: MVAWriter.h:207
void addOutput(FVector_ID id, std::array< float, N > const &values)
Definition: MVAWriter.h:181
Helper functions for MVAReader/Writer and FVecReader/Writer wrappers.
FVector_ID getProductID() const
std::vector< std::string > fRegisteredDataTypes
Definition: MVAWriter.h:139
bool fIsDescriptionRegistered
Definition: MVAWriter.h:140
FVector_ID initOutputs(art::InputTag const &dataTag, std::vector< std::string > const &names=std::vector< std::string >(N,""))
Definition: MVAWriter.h:70
void setVector(FVector_ID id, size_t key, std::array< float, N > const &values)
Definition: MVAWriter.h:59
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
void setOutput(FVector_ID id, size_t key, std::vector< float > const &values)
Definition: MVAWriter.h:178
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:246
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:85
std::string fInstanceName
Definition: MVAWriter.h:137
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:218
void setVector(FVector_ID id, size_t key, std::vector< float > const &values)
Definition: MVAWriter.h:61
size_t getProductHash(std::type_info const &ti) const
void addOutput(FVector_ID id, std::vector< float > const &values)
Definition: MVAWriter.h:183
void addVector(FVector_ID id, std::array< double, N > const &values)
Definition: MVAWriter.h:80
Definition: fwd.h:25
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
friend std::ostream & operator<<(std::ostream &o, FVectorWriter const &a)
Definition: MVAWriter.h:116