LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
MVAReader.h
Go to the documentation of this file.
1 // \version
3 //
4 // \brief Wrappers for accessing MVA results and associated data products
5 //
6 // \author robert.sulej@cern.ch
7 //
9 #ifndef ANAB_MVAREADER_H
10 #define ANAB_MVAREADER_H
11 
15 
17 
18 namespace anab {
19 
22  template <class T, size_t N>
24  public:
28  static std::unique_ptr<FVectorReader> create(const art::Event& evt, const art::InputTag& tag)
29  {
30  bool success;
31  std::unique_ptr<FVectorReader> ptr(new FVectorReader(evt, tag, success));
32  if (success) { return ptr; }
33  else {
34  return nullptr;
35  }
36  }
37 
41  FVectorReader(const art::Event& evt, const art::InputTag& tag);
42 
44  T const& item(size_t key) const { return (*fDataHandle)[key]; }
45  std::vector<T> const& items() const { return *fDataHandle; }
46 
48  std::vector<FeatureVector<N>> const& vectors() const { return *fVectors; }
49 
52  //const std::array<float, N> & getVector(size_t key) const { return (*fVectors)[key].data(); }
53 
55  std::array<float, N> getVector(size_t key) const
56  {
57  std::array<float, N> vout;
58  for (size_t i = 0; i < N; ++i)
59  vout[i] = (*fVectors)[key][i];
60  return vout;
61  }
62 
64  std::array<float, N> getVector(art::Ptr<T> const& item) const { return getVector(item.key()); }
65 
67  size_t size() const { return fVectors->size(); }
68 
70  size_t length() const { return N; }
71 
73  const std::string& dataTag() const { return fDescription->dataTag(); }
74 
77 
79  const std::string& columnName(size_t index) const { return fDescription->outputName(index); }
80 
82  int getIndex(const std::string& name) const { return fDescription->getIndex(name); }
83 
84  friend std::ostream& operator<<(std::ostream& o, FVectorReader const& a)
85  {
86  o << "FVectorReader:" << std::endl << *(a.fDescription) << std::endl;
87  return o;
88  }
89 
90  protected:
92  FVectorReader(const art::Event& evt, const art::InputTag& tag, bool& success);
93 
94  private:
96  std::vector<FeatureVector<N>> const* fVectors;
98  };
99 
102  template <class T, size_t N>
103  class MVAReader : public FVectorReader<T, N>, public MVAWrapperBase {
104  public:
108  static std::unique_ptr<MVAReader> create(const art::Event& evt, const art::InputTag& tag)
109  {
110  bool success;
111  std::unique_ptr<MVAReader> ptr(new MVAReader(evt, tag, success));
112  if (success) { return ptr; }
113  else {
114  return nullptr;
115  }
116  }
117 
121  MVAReader(const art::Event& evt, const art::InputTag& tag) : FVectorReader<T, N>(evt, tag) {}
122 
124  std::vector<FeatureVector<N>> const& outputs() const { return FVectorReader<T, N>::vectors(); }
125 
127  std::array<float, N> getOutput(size_t key) const { return FVectorReader<T, N>::getVector(key); }
128 
130  std::array<float, N> getOutput(art::Ptr<T> const& item) const
131  {
132  return FVectorReader<T, N>::getVector(item.key());
133  }
134 
136  std::array<float, N> getOutput(std::vector<art::Ptr<T>> const& items) const
137  {
138  return pAccumulate(items, FVectorReader<T, N>::vectors());
139  }
140 
144  std::array<float, N> getOutput(std::vector<art::Ptr<T>> const& items,
145  std::vector<float> const& weights) const
146  {
147  return pAccumulate(items, weights, FVectorReader<T, N>::vectors());
148  }
149 
153  std::array<float, N> getOutput(std::vector<art::Ptr<T>> const& items,
154  std::function<float(T const&)> fweight) const
155  {
156  return pAccumulate(items, fweight, FVectorReader<T, N>::vectors());
157  }
158 
160  const std::string& outputName(size_t index) const
161  {
162  return FVectorReader<T, N>::columnName(index);
163  }
164 
165  private:
167  MVAReader(const art::Event& evt, const art::InputTag& tag, bool& success)
168  : FVectorReader<T, N>(evt, tag, success)
169  {}
170  };
171 
172 } // namespace anab
173 
174 //----------------------------------------------------------------------------
175 // FVectorReader functions.
176 //
177 template <class T, size_t N>
179  : fDescription(0)
180 {
181  if (!N) { throw cet::exception("FVectorReader") << "Vector size should be > 0." << std::endl; }
182 
183  auto descriptionHandle = evt.getValidHandle<std::vector<anab::FVecDescription<N>>>(tag);
184 
185  // search for FVecDescription<N> produced for the type T, with the instance name from the tag
186  std::string outputInstanceName = tag.instance() + getProductName(typeid(T));
187  for (auto const& dscr : *descriptionHandle) {
188  if (dscr.outputInstance() == outputInstanceName) {
189  fDescription = &dscr;
190  break;
191  }
192  }
193  if (!fDescription) {
194  throw cet::exception("FVectorReader")
195  << "Vectors description not found for " << outputInstanceName << std::endl;
196  }
197 
198  fVectors = &*(evt.getValidHandle<std::vector<FeatureVector<N>>>(
199  art::InputTag(tag.label(), fDescription->outputInstance(), tag.process())));
200 
201  if (!evt.getByLabel(fDescription->dataTag(), fDataHandle)) {
202  throw cet::exception("FVectorReader")
203  << "Associated data product handle failed: " << *(fDataHandle.whyFailed()) << std::endl;
204  }
205 
206  if (fVectors->size() != fDataHandle->size()) {
207  throw cet::exception("FVectorReader")
208  << "Feature vectors and data products sizes inconsistent: " << fVectors->size()
209  << "!=" << fDataHandle->size() << std::endl;
210  }
211 }
212 //----------------------------------------------------------------------------
213 
214 template <class T, size_t N>
216  const art::InputTag& tag,
217  bool& success)
218  : fDescription(0)
219 {
220  success = false; // until all is done correctly
221 
222  if (!N) {
223  std::cout << "FVectorReader: Vector size should be > 0." << std::endl;
224  return;
225  }
226 
228  if (!evt.getByLabel(tag, descriptionHandle)) { return; }
229 
230  // search for FVecDescription<N> produced for the type T, with the instance name from the tag
231  std::string outputInstanceName = tag.instance() + getProductName(typeid(T));
232  for (auto const& dscr : *descriptionHandle) {
233  if (dscr.outputInstance() == outputInstanceName) {
234  fDescription = &dscr;
235  break;
236  }
237  }
238  if (!fDescription) {
239  std::cout << "FVectorReader: Vectors description not found for " << outputInstanceName
240  << std::endl;
241  return;
242  }
243 
244  fVectors = &*(evt.getValidHandle<std::vector<FeatureVector<N>>>(
245  art::InputTag(tag.label(), fDescription->outputInstance(), tag.process())));
246 
247  if (!evt.getByLabel(fDescription->dataTag(), fDataHandle)) {
248  std::cout << "FVectorReader: Associated data product handle failed: "
249  << *(fDataHandle.whyFailed()) << std::endl;
250  return;
251  }
252 
253  if (fVectors->size() != fDataHandle->size()) {
254  std::cout << "FVectorReader: Feature vectors and data products sizes inconsistent: "
255  << fVectors->size() << "!=" << fDataHandle->size() << std::endl;
256  return;
257  }
258 
259  success = true; // ok, all data found in the event
260 }
261 //----------------------------------------------------------------------------
262 
263 #endif //ANAB_MVAREADER
const art::Handle< std::vector< T > > & dataHandle() const
Access the data product handle.
Definition: MVAReader.h:76
std::array< float, N > getVector(size_t key) const
Get copy of the feature vector at index "key".
Definition: MVAReader.h:55
art::Handle< std::vector< T > > fDataHandle
Definition: MVAReader.h:97
static std::unique_ptr< FVectorReader > create(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:28
int getIndex(const std::string &name) const
Index of column with given name, or -1 if name not found.
Definition: MVAReader.h:82
std::string const & instance() const noexcept
Definition: InputTag.cc:85
std::array< float, N > getOutput(std::vector< art::Ptr< T >> const &items, std::vector< float > const &weights) const
Definition: MVAReader.h:144
const std::string & outputName(size_t index) const
Meaning/name of the index&#39;th column in the collection of MVA output vectors.
Definition: MVAReader.h:160
T const & item(size_t key) const
Access data product at index "key".
Definition: MVAReader.h:44
std::string const & process() const noexcept
Definition: InputTag.cc:91
std::string getProductName(std::type_info const &ti) const
FVecDescription< N > const * fDescription
Definition: MVAReader.h:95
std::array< float, N > getOutput(art::Ptr< T > const &item) const
Get copy of the MVA output vector idicated with art::Ptr::key().
Definition: MVAReader.h:130
MVAReader(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:121
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
std::string const & label() const noexcept
Definition: InputTag.cc:79
std::vector< T > const & items() const
Definition: MVAReader.h:45
std::array< float, N > getVector(art::Ptr< T > const &item) const
Get copy of the feature vector idicated with art::Ptr::key().
Definition: MVAReader.h:64
Helper functions for MVAReader and MVAWriter wrappers.
key_type key() const noexcept
Definition: Ptr.h:166
std::vector< FeatureVector< N > > const & outputs() const
Access the vector of the feature vectors.
Definition: MVAReader.h:124
const std::string & columnName(size_t index) const
Meaning/name of the index&#39;th column in the collection of feature vectors.
Definition: MVAReader.h:79
std::vector< FeatureVector< N > > const & vectors() const
Access the vector of the feature vectors.
Definition: MVAReader.h:48
std::vector< FeatureVector< N > > const * fVectors
Definition: MVAReader.h:96
size_t size() const
Get the number of contained items (no. of data product objects equal to no. of feature vectors)...
Definition: MVAReader.h:67
size_t length() const
Get the length of a single feature vector.
Definition: MVAReader.h:70
FVectorReader(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:178
std::array< float, N > getOutput(std::vector< art::Ptr< T >> const &items, std::function< float(T const &)> fweight) const
Definition: MVAReader.h:153
Helper functions for MVAReader/Writer and FVecReader/Writer wrappers.
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
std::array< float, N > getOutput(std::vector< art::Ptr< T >> const &items) const
Get MVA results accumulated over the vector of items (eg. over hits associated to a cluster)...
Definition: MVAReader.h:136
std::shared_ptr< art::Exception const > whyFailed() const
Definition: Handle.h:231
friend std::ostream & operator<<(std::ostream &o, FVectorReader const &a)
Definition: MVAReader.h:84
TCEvent evt
Definition: DataStructs.cxx:8
std::array< float, N > getOutput(size_t key) const
Get copy of the MVA output vector at index "key".
Definition: MVAReader.h:127
Definition: fwd.h:26
static std::unique_ptr< MVAReader > create(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:108
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
MVAReader(const art::Event &evt, const art::InputTag &tag, bool &success)
Not-throwing constructor.
Definition: MVAReader.h:167
const std::string & dataTag() const
Get the input tag (string representation) of data product used to calculate feature vectors...
Definition: MVAReader.h:73