LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
anab::FVectorReader< T, N > Class Template Reference

#include "MVAReader.h"

Inheritance diagram for anab::FVectorReader< T, N >:
anab::FVectorWrapperBase anab::MVAReader< T, N >

Public Member Functions

 FVectorReader (const art::Event &evt, const art::InputTag &tag)
 
T const & item (size_t key) const
 Access data product at index "key". More...
 
std::vector< T > const & items () const
 
std::vector< FeatureVector< N > > const & vectors () const
 Access the vector of the feature vectors. More...
 
std::array< float, N > getVector (size_t key) const
 Get copy of the feature vector at index "key". More...
 
std::array< float, N > getVector (art::Ptr< T > const &item) const
 Get copy of the feature vector idicated with art::Ptr::key(). More...
 
size_t size () const
 Get the number of contained items (no. of data product objects equal to no. of feature vectors). More...
 
size_t length () const
 Get the length of a single feature vector. More...
 
const std::string & dataTag () const
 Get the input tag (string representation) of data product used to calculate feature vectors. More...
 
const art::Handle< std::vector< T > > & dataHandle () const
 Access the data product handle. More...
 
const std::string & columnName (size_t index) const
 Meaning/name of the index'th column in the collection of feature vectors. More...
 
int getIndex (const std::string &name) const
 Index of column with given name, or -1 if name not found. More...
 

Static Public Member Functions

static std::unique_ptr< FVectorReadercreate (const art::Event &evt, const art::InputTag &tag)
 

Protected Member Functions

 FVectorReader (const art::Event &evt, const art::InputTag &tag, bool &success)
 Not-throwing constructor. More...
 
std::string getProductName (std::type_info const &ti) const
 
size_t getProductHash (std::type_info const &ti) const
 

Private Attributes

FVecDescription< N > const * fDescription
 
std::vector< FeatureVector< N > > const * fVectors
 
art::Handle< std::vector< T > > fDataHandle
 

Friends

std::ostream & operator<< (std::ostream &o, FVectorReader const &a)
 

Detailed Description

template<class T, size_t N>
class anab::FVectorReader< T, N >

Helper for reading the reconstructed objects of type T together with associated N-ellement feature vectors with their metadata (this class is not a data product).

Definition at line 23 of file MVAReader.h.

Constructor & Destructor Documentation

template<class T , size_t N>
anab::FVectorReader< T, N >::FVectorReader ( const art::Event evt,
const art::InputTag tag 
)

Create the wrapper for feature vectors stored in the event evt with the provided input tag (the same tag which was used to save vectors with FVectorWriter class). Throws exception if data products not found in the event.

Definition at line 178 of file MVAReader.h.

References anab::FVectorReader< T, N >::fDataHandle, anab::FVectorReader< T, N >::fDescription, anab::FVectorReader< T, N >::fVectors, art::ProductRetriever::getByLabel(), anab::FVectorWrapperBase::getProductName(), art::ProductRetriever::getValidHandle(), art::InputTag::instance(), art::InputTag::label(), art::InputTag::process(), and art::Handle< T >::whyFailed().

Referenced by anab::FVectorReader< T, N >::create().

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 }
art::Handle< std::vector< T > > fDataHandle
Definition: MVAReader.h:97
std::string const & instance() const noexcept
Definition: InputTag.cc:85
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::string const & label() const noexcept
Definition: InputTag.cc:79
std::vector< FeatureVector< N > > const * fVectors
Definition: MVAReader.h:96
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
std::shared_ptr< art::Exception const > whyFailed() const
Definition: Handle.h:231
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
template<class T , size_t N>
anab::FVectorReader< T, N >::FVectorReader ( const art::Event evt,
const art::InputTag tag,
bool &  success 
)
protected

Not-throwing constructor.

Definition at line 215 of file MVAReader.h.

References anab::FVectorReader< T, N >::fDataHandle, anab::FVectorReader< T, N >::fDescription, anab::FVectorReader< T, N >::fVectors, art::ProductRetriever::getByLabel(), anab::FVectorWrapperBase::getProductName(), art::ProductRetriever::getValidHandle(), art::InputTag::instance(), art::InputTag::label(), art::InputTag::process(), and art::Handle< T >::whyFailed().

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 }
art::Handle< std::vector< T > > fDataHandle
Definition: MVAReader.h:97
std::string const & instance() const noexcept
Definition: InputTag.cc:85
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::string const & label() const noexcept
Definition: InputTag.cc:79
std::vector< FeatureVector< N > > const * fVectors
Definition: MVAReader.h:96
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
std::shared_ptr< art::Exception const > whyFailed() const
Definition: Handle.h:231

Member Function Documentation

template<class T , size_t N>
const std::string& anab::FVectorReader< T, N >::columnName ( size_t  index) const
inline

Meaning/name of the index'th column in the collection of feature vectors.

Definition at line 79 of file MVAReader.h.

References anab::FVectorReader< T, N >::fDescription.

Referenced by anab::MVAReader< T, N >::outputName().

79 { return fDescription->outputName(index); }
FVecDescription< N > const * fDescription
Definition: MVAReader.h:95
template<class T , size_t N>
static std::unique_ptr<FVectorReader> anab::FVectorReader< T, N >::create ( const art::Event evt,
const art::InputTag tag 
)
inlinestatic

Create the helper for feature vectors stored in the event evt with the provided input tag (the same tag which was used to save vectors with FVectorWriter class). Returns nullptr if data products not found in the event.

Definition at line 28 of file MVAReader.h.

References tca::evt, and anab::FVectorReader< T, N >::FVectorReader().

Referenced by evdb_tool::DrawSkewHits::Draw(), and evd::RecoBaseDrawer::FillTQHistoDP().

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  }
FVectorReader(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:178
template<class T , size_t N>
const art::Handle<std::vector<T> >& anab::FVectorReader< T, N >::dataHandle ( ) const
inline

Access the data product handle.

Definition at line 76 of file MVAReader.h.

References anab::FVectorReader< T, N >::fDataHandle.

76 { return fDataHandle; }
art::Handle< std::vector< T > > fDataHandle
Definition: MVAReader.h:97
template<class T , size_t N>
const std::string& anab::FVectorReader< T, N >::dataTag ( ) const
inline

Get the input tag (string representation) of data product used to calculate feature vectors.

Definition at line 73 of file MVAReader.h.

References anab::FVectorReader< T, N >::fDescription.

73 { return fDescription->dataTag(); }
FVecDescription< N > const * fDescription
Definition: MVAReader.h:95
template<class T , size_t N>
int anab::FVectorReader< T, N >::getIndex ( const std::string &  name) const
inline

Index of column with given name, or -1 if name not found.

Definition at line 82 of file MVAReader.h.

References anab::FVectorReader< T, N >::fDescription.

Referenced by pdsp::CheckCNNScore::analyze(), and nnet::PointIdEffTest::analyze().

82 { return fDescription->getIndex(name); }
FVecDescription< N > const * fDescription
Definition: MVAReader.h:95
size_t anab::FVectorWrapperBase::getProductHash ( std::type_info const &  ti) const
inlineprotectedinherited

Definition at line 29 of file MVAWrapperBase.h.

Referenced by anab::FVectorWriter< N >::getProductID(), and anab::FVectorWriter< N >::initOutputs().

29 { return ti.hash_code(); }
std::string anab::FVectorWrapperBase::getProductName ( std::type_info const &  ti) const
protectedinherited

Definition at line 17 of file MVAWrapperBase.cxx.

Referenced by anab::FVectorReader< T, N >::FVectorReader(), anab::FVectorWriter< N >::getProductID(), anab::FVectorWriter< N >::initOutputs(), and anab::FVectorWriter< N >::produces_using().

18  {
19  char* realname;
20  int status;
21 
22  realname = abi::__cxa_demangle(ti.name(), 0, 0, &status);
23  std::string pname(realname);
24  free(realname);
25 
26  pname.erase(std::remove(pname.begin(), pname.end(), ' '), pname.end());
27  pname.erase(std::remove(pname.begin(), pname.end(), ':'), pname.end());
28 
29  return pname;
30  }
template<class T , size_t N>
std::array<float, N> anab::FVectorReader< T, N >::getVector ( size_t  key) const
inline

Get copy of the feature vector at index "key".

Access feature vector data at index "key". *** WOULD LIKE TO CHANGE TYPE OF FVEC DATA MEMBER TO std::array AND THEN ENABLE THIS FUNCTION ***

Definition at line 55 of file MVAReader.h.

References anab::FVectorReader< T, N >::fVectors.

Referenced by anab::MVAReader< T, N >::getOutput().

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  }
std::vector< FeatureVector< N > > const * fVectors
Definition: MVAReader.h:96
template<class T , size_t N>
std::array<float, N> anab::FVectorReader< T, N >::getVector ( art::Ptr< T > const &  item) const
inline

Get copy of the feature vector idicated with art::Ptr::key().

Definition at line 64 of file MVAReader.h.

References anab::FVectorReader< T, N >::getVector(), and art::Ptr< T >::key().

Referenced by anab::FVectorReader< T, N >::getVector().

64 { return getVector(item.key()); }
std::array< float, N > getVector(size_t key) const
Get copy of the feature vector at index "key".
Definition: MVAReader.h:55
key_type key() const noexcept
Definition: Ptr.h:166
template<class T , size_t N>
T const& anab::FVectorReader< T, N >::item ( size_t  key) const
inline

Access data product at index "key".

Definition at line 44 of file MVAReader.h.

References anab::FVectorReader< T, N >::fDataHandle.

44 { return (*fDataHandle)[key]; }
art::Handle< std::vector< T > > fDataHandle
Definition: MVAReader.h:97
template<class T , size_t N>
std::vector<T> const& anab::FVectorReader< T, N >::items ( ) const
inline

Definition at line 45 of file MVAReader.h.

References anab::FVectorReader< T, N >::fDataHandle.

Referenced by anab::MVAReader< T, N >::getOutput().

45 { return *fDataHandle; }
art::Handle< std::vector< T > > fDataHandle
Definition: MVAReader.h:97
template<class T , size_t N>
size_t anab::FVectorReader< T, N >::length ( ) const
inline

Get the length of a single feature vector.

Definition at line 70 of file MVAReader.h.

70 { return N; }
template<class T , size_t N>
size_t anab::FVectorReader< T, N >::size ( ) const
inline

Get the number of contained items (no. of data product objects equal to no. of feature vectors).

Definition at line 67 of file MVAReader.h.

References anab::FVectorReader< T, N >::fVectors.

67 { return fVectors->size(); }
std::vector< FeatureVector< N > > const * fVectors
Definition: MVAReader.h:96
template<class T , size_t N>
std::vector<FeatureVector<N> > const& anab::FVectorReader< T, N >::vectors ( ) const
inline

Access the vector of the feature vectors.

Definition at line 48 of file MVAReader.h.

References anab::FVectorReader< T, N >::fVectors.

Referenced by anab::MVAReader< T, N >::outputs().

48 { return *fVectors; }
std::vector< FeatureVector< N > > const * fVectors
Definition: MVAReader.h:96

Friends And Related Function Documentation

template<class T , size_t N>
std::ostream& operator<< ( std::ostream &  o,
FVectorReader< T, N > const &  a 
)
friend

Definition at line 84 of file MVAReader.h.

85  {
86  o << "FVectorReader:" << std::endl << *(a.fDescription) << std::endl;
87  return o;
88  }

Member Data Documentation

template<class T , size_t N>
art::Handle<std::vector<T> > anab::FVectorReader< T, N >::fDataHandle
private
template<class T , size_t N>
std::vector<FeatureVector<N> > const* anab::FVectorReader< T, N >::fVectors
private

The documentation for this class was generated from the following file: