LArSoft  v06_85_00
Liquid Argon Software toolkit - http://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 170 of file MVAReader.h.

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

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

170  :
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 }
art::Handle< std::vector< T > > fDataHandle
Definition: MVAReader.h:97
std::string getProductName(std::type_info const &ti) const
FVecDescription< N > const * fDescription
Definition: MVAReader.h:95
std::vector< FeatureVector< N > > const * fVectors
Definition: MVAReader.h:96
std::string const & process() const noexcept
Definition: InputTag.h:67
std::string const & instance() const noexcept
Definition: InputTag.h:60
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
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
std::string const & label() const noexcept
Definition: InputTag.h:55
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 203 of file MVAReader.h.

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

203  :
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 }
art::Handle< std::vector< T > > fDataHandle
Definition: MVAReader.h:97
std::string getProductName(std::type_info const &ti) const
FVecDescription< N > const * fDescription
Definition: MVAReader.h:95
std::vector< FeatureVector< N > > const * fVectors
Definition: MVAReader.h:96
std::string const & process() const noexcept
Definition: InputTag.h:67
std::string const & instance() const noexcept
Definition: InputTag.h:60
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
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
std::string const & label() const noexcept
Definition: InputTag.h:55

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 29 of file MVAReader.h.

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

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

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  }
FVectorReader(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:170
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.

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 31 of file MVAWrapperBase.h.

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

31 { 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 54 of file MVAReader.h.

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

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

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  }
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 62 of file MVAReader.h.

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

63  { return getVector(item.key()); }
key_type key() const
Definition: Ptr.h:356
std::array< float, N > getVector(size_t key) const
Get copy of the feature vector at index "key".
Definition: MVAReader.h:54
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 43 of file MVAReader.h.

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

43 { 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 44 of file MVAReader.h.

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

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

44 { 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 47 of file MVAReader.h.

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

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

47 { 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: