LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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:
25 
29  static std::unique_ptr<FVectorReader> create(const art::Event & evt, const art::InputTag & tag)
30  {
31  bool success;
32  std::unique_ptr<FVectorReader> ptr(new FVectorReader(evt, tag, success));
33  if (success) { return ptr; }
34  else { return nullptr; }
35  }
36 
40  FVectorReader(const art::Event & evt, const art::InputTag & tag);
41 
43  T const & item(size_t key) const { return (*fDataHandle)[key]; }
44  std::vector<T> const & items() const { return *fDataHandle; }
45 
47  std::vector< FeatureVector<N> > const & vectors() const { return *fVectors; }
48 
51  //const std::array<float, N> & getVector(size_t key) const { return (*fVectors)[key].data(); }
52 
54  std::array<float, N> getVector(size_t key) const
55  {
56  std::array<float, N> vout;
57  for (size_t i = 0; i < N; ++i) vout[i] = (*fVectors)[key][i];
58  return vout;
59  }
60 
62  std::array<float, N> getVector(art::Ptr<T> const & item) const
63  { return getVector(item.key()); }
64 
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 
76  const art::Handle< std::vector<T> > & dataHandle() const { return fDataHandle; }
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 };
100 
103 template <class T, size_t N>
104 class MVAReader : public FVectorReader<T, N>, public MVAWrapperBase {
105 public:
106 
110  static std::unique_ptr<MVAReader> create(const art::Event & evt, const art::InputTag & tag)
111  {
112  bool success;
113  std::unique_ptr<MVAReader> ptr(new MVAReader(evt, tag, success));
114  if (success) { return ptr; }
115  else { return nullptr; }
116  }
117 
121  MVAReader(const art::Event & evt, const art::InputTag & tag) :
122  FVectorReader<T, N>(evt, tag)
123  { }
124 
126  std::vector< FeatureVector<N> > const & outputs() const { return FVectorReader<T, N>::vectors(); }
127 
129  std::array<float, N> getOutput(size_t key) const
130  { return FVectorReader<T, N>::getVector(key); }
131 
133  std::array<float, N> getOutput(art::Ptr<T> const & item) const
134  { return FVectorReader<T, N>::getVector(item.key()); }
135 
137  std::array<float, N> getOutput(std::vector< art::Ptr<T> > const & items) const
138  { return pAccumulate(items, FVectorReader<T, N>::vectors()); }
139 
143  std::array<float, N> getOutput(std::vector< art::Ptr<T> > const & items,
144  std::vector<float> const & weights) const
145  { return pAccumulate(items, weights, FVectorReader<T, N>::vectors()); }
146 
150  std::array<float, N> getOutput(std::vector< art::Ptr<T> > const & items,
151  std::function<float (T const &)> fweight) const
152  { return pAccumulate(items, fweight, FVectorReader<T, N>::vectors()); }
153 
155  const std::string & outputName(size_t index) const { return FVectorReader<T, N>::columnName(index); }
156 
157 private:
159  MVAReader(const art::Event & evt, const art::InputTag & tag, bool & success) :
160  FVectorReader<T, N>(evt, tag, success)
161  { }
162 };
163 
164 } // namespace anab
165 
166 //----------------------------------------------------------------------------
167 // FVectorReader functions.
168 //
169 template <class T, size_t N>
171  fDescription(0)
172 {
173  if (!N) { throw cet::exception("FVectorReader") << "Vector size should be > 0." << std::endl; }
174 
175  auto descriptionHandle = evt.getValidHandle< std::vector< anab::FVecDescription<N> > >(tag);
176 
177  // search for FVecDescription<N> produced for the type T, with the instance name from the tag
178  std::string outputInstanceName = tag.instance() + getProductName(typeid(T));
179  for (auto const & dscr : *descriptionHandle)
180  {
181  if (dscr.outputInstance() == outputInstanceName)
182  {
183  fDescription = &dscr; break;
184  }
185  }
186  if (!fDescription) { throw cet::exception("FVectorReader") << "Vectors description not found for " << outputInstanceName << std::endl; }
187 
188  fVectors = &*(evt.getValidHandle< std::vector< FeatureVector<N> > >( art::InputTag(tag.label(), fDescription->outputInstance(), tag.process()) ));
189 
190  if (!evt.getByLabel( fDescription->dataTag(), fDataHandle ))
191  {
192  throw cet::exception("FVectorReader") << "Associated data product handle failed: " << *(fDataHandle.whyFailed()) << std::endl;
193  }
194 
195  if (fVectors->size() != fDataHandle->size())
196  {
197  throw cet::exception("FVectorReader") << "Feature vectors and data products sizes inconsistent: " << fVectors->size() << "!=" << fDataHandle->size() << std::endl;
198  }
199 }
200 //----------------------------------------------------------------------------
201 
202 template <class T, size_t N>
204  fDescription(0)
205 {
206  success = false; // until all is done correctly
207 
208  if (!N) { std::cout << "FVectorReader: Vector size should be > 0." << std::endl; return; }
209 
211  if (!evt.getByLabel( tag, descriptionHandle )) { return; }
212 
213  // search for FVecDescription<N> produced for the type T, with the instance name from the tag
214  std::string outputInstanceName = tag.instance() + getProductName(typeid(T));
215  for (auto const & dscr : *descriptionHandle)
216  {
217  if (dscr.outputInstance() == outputInstanceName)
218  {
219  fDescription = &dscr; break;
220  }
221  }
222  if (!fDescription) { std::cout << "FVectorReader: Vectors description not found for " << outputInstanceName << std::endl; return; }
223 
224  fVectors = &*(evt.getValidHandle< std::vector< FeatureVector<N> > >( art::InputTag(tag.label(), fDescription->outputInstance(), tag.process()) ));
225 
226  if (!evt.getByLabel( fDescription->dataTag(), fDataHandle ))
227  {
228  std::cout << "FVectorReader: Associated data product handle failed: " << *(fDataHandle.whyFailed()) << std::endl; return;
229  }
230 
231  if (fVectors->size() != fDataHandle->size())
232  {
233  std::cout << "FVectorReader: Feature vectors and data products sizes inconsistent: " << fVectors->size() << "!=" << fDataHandle->size() << std::endl; return;
234  }
235 
236  success = true; // ok, all data found in the event
237 }
238 //----------------------------------------------------------------------------
239 
240 #endif //ANAB_MVAREADER
241 
key_type key() const
Definition: Ptr.h:356
const art::Handle< std::vector< T > > & dataHandle() const
Access the data product handle.
Definition: MVAReader.h:76
art::Handle< std::vector< T > > fDataHandle
Definition: MVAReader.h:97
std::array< float, N > getVector(size_t key) const
Get copy of the feature vector at index "key".
Definition: MVAReader.h:54
static std::unique_ptr< FVectorReader > create(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:29
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items, std::function< float(T const &)> fweight) const
Definition: MVAReader.h:150
int getIndex(const std::string &name) const
Index of column with given name, or -1 if name not found.
Definition: MVAReader.h:82
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:155
std::vector< FeatureVector< N > > const & vectors() const
Access the vector of the feature vectors.
Definition: MVAReader.h:47
T const & item(size_t key) const
Access data product at index "key".
Definition: MVAReader.h:43
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:133
std::vector< FeatureVector< N > > const * fVectors
Definition: MVAReader.h:96
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:265
std::vector< T > const & items() const
Definition: MVAReader.h:44
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:62
Helper functions for MVAReader and MVAWriter wrappers.
std::string const & process() const noexcept
Definition: InputTag.h:67
std::string const & instance() const noexcept
Definition: InputTag.h:60
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 & outputs() const
Access the vector of the feature vectors.
Definition: MVAReader.h:126
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:170
Helper functions for MVAReader/Writer and FVecReader/Writer wrappers.
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
std::shared_ptr< art::Exception const > whyFailed() const
Definition: Handle.h:220
friend std::ostream & operator<<(std::ostream &o, FVectorReader const &a)
Definition: MVAReader.h:84
TCEvent evt
Definition: DataStructs.cxx:5
ValidHandle< PROD > getValidHandle(InputTag const &tag) 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:137
std::array< float, N > getOutput(size_t key) const
Get copy of the MVA output vector at index "key".
Definition: MVAReader.h:129
std::string const & label() const noexcept
Definition: InputTag.h:55
Definition: fwd.h:25
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items, std::vector< float > const &weights) const
Definition: MVAReader.h:143
static std::unique_ptr< MVAReader > create(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:110
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:159
const std::string & dataTag() const
Get the input tag (string representation) of data product used to calculate feature vectors...
Definition: MVAReader.h:73