LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
anab::FVectorWriter< N > Class Template Reference

#include "MVAWriter.h"

Inheritance diagram for anab::FVectorWriter< N >:
anab::FVectorWrapperBase anab::MVAWriter< N >

Public Member Functions

 FVectorWriter (art::EDProducer *module, const char *name="")
 
template<class T >
void produces_using ()
 
template<class T >
FVector_ID initOutputs (std::string const &dataTag, size_t dataSize, std::vector< std::string > const &names=std::vector< std::string >(N,""))
 
template<class T >
FVector_ID initOutputs (art::InputTag const &dataTag, size_t dataSize, std::vector< std::string > const &names=std::vector< std::string >(N,""))
 
void setVector (FVector_ID id, size_t key, std::array< float, N > const &values)
 
void setVector (FVector_ID id, size_t key, std::array< double, N > const &values)
 
void setVector (FVector_ID id, size_t key, std::vector< float > const &values)
 
void setVector (FVector_ID id, size_t key, std::vector< double > const &values)
 
template<class T >
FVector_ID initOutputs (art::InputTag const &dataTag, std::vector< std::string > const &names=std::vector< std::string >(N,""))
 
template<class T >
FVector_ID initOutputs (std::vector< std::string > const &names=std::vector< std::string >(N,""))
 
void addVector (FVector_ID id, std::array< float, N > const &values)
 
void addVector (FVector_ID id, std::array< double, N > const &values)
 
void addVector (FVector_ID id, std::vector< float > const &values)
 
void addVector (FVector_ID id, std::vector< double > const &values)
 
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. More...
 
void saveOutputs (art::Event &evt)
 Check consistency and save all the results in the event. More...
 
size_t size (FVector_ID id) const
 Get the number of contained feature vectors. More...
 
size_t length () const
 Get the length of a single feature vector. More...
 
template<class T >
std::array< float, N > getVector (size_t key) const
 Get copy of the feature vector for the type T, at index "key". More...
 
template<class T >
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(). More...
 
template<class T >
anab::FVector_ID getProductID () const
 
template<class T >
anab::FVector_ID initOutputs (std::string const &dataTag, size_t dataSize, std::vector< std::string > const &names)
 

Protected Member Functions

template<class T >
FVector_ID getProductID () const
 
std::string getProductName (std::type_info const &ti) const
 
size_t getProductHash (std::type_info const &ti) const
 

Protected Attributes

std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
 

Private Member Functions

void clearEventData ()
 
bool dataTypeRegistered (const std::string &dname) const
 Check if the the writer is configured to write results for data product type name. More...
 
bool descriptionExists (const std::string &tname) const
 Check if the containers for results prepared for "tname" data type are ready. More...
 

Private Attributes

art::EDProducerfProducer
 
std::string fInstanceName
 
std::vector< std::string > fRegisteredDataTypes
 
bool fIsDescriptionRegistered
 
std::unordered_map< size_t, FVector_IDfTypeHashToID
 
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
 

Friends

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

Detailed Description

template<size_t N>
class anab::FVectorWriter< N >

Definition at line 26 of file MVAWriter.h.

Constructor & Destructor Documentation

template<size_t N>
anab::FVectorWriter< N >::FVectorWriter ( art::EDProducer module,
const char *  name = "" 
)
inline

Name provided to the constructor is used as an instance name for FVecDescription<N> and FeatureVector<N> (for which it is combined with the processed data product names). The name is used as an instance name for the FVecDescription data product which lets you to save multiple vector collections from a single art module.

Definition at line 33 of file MVAWriter.h.

33  :
34  fProducer(module), fInstanceName(name),
36  fDescriptions(nullptr)
37  { }
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:144
art::EDProducer * fProducer
Definition: MVAWriter.h:136
bool fIsDescriptionRegistered
Definition: MVAWriter.h:140
std::string fInstanceName
Definition: MVAWriter.h:137

Member Function Documentation

template<size_t N>
void anab::FVectorWriter< N >::addVector ( FVector_ID  id,
std::array< float, N > const &  values 
)
inline

Definition at line 79 of file MVAWriter.h.

Referenced by anab::MVAWriter< N >::addOutput(), and hit::DPRawHitFinder::produce().

79 { fVectors[id]->emplace_back(values); }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
template<size_t N>
void anab::FVectorWriter< N >::addVector ( FVector_ID  id,
std::array< double, N > const &  values 
)
inline

Definition at line 80 of file MVAWriter.h.

80 { fVectors[id]->emplace_back(values); }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
template<size_t N>
void anab::FVectorWriter< N >::addVector ( FVector_ID  id,
std::vector< float > const &  values 
)
inline

Definition at line 81 of file MVAWriter.h.

81 { fVectors[id]->emplace_back(values); }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
template<size_t N>
void anab::FVectorWriter< N >::addVector ( FVector_ID  id,
std::vector< double > const &  values 
)
inline

Definition at line 82 of file MVAWriter.h.

82 { fVectors[id]->emplace_back(values); }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
template<size_t N>
void anab::FVectorWriter< N >::clearEventData ( )
inlineprivate

Definition at line 145 of file MVAWriter.h.

Referenced by anab::FVectorWriter< N >::saveOutputs().

146  {
147  fTypeHashToID.clear(); fVectors.clear();
148  fDescriptions.reset(nullptr);
149  }
std::unordered_map< size_t, FVector_ID > fTypeHashToID
Definition: MVAWriter.h:142
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:144
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
template<size_t N>
bool anab::FVectorWriter< N >::dataTypeRegistered ( const std::string &  dname) const
private

Check if the the writer is configured to write results for data product type name.

Definition at line 246 of file MVAWriter.h.

References anab::FVectorWriter< N >::fRegisteredDataTypes, and s.

Referenced by anab::FVectorWriter< 4 >::clearEventData(), anab::FVectorWriter< N >::initOutputs(), and anab::FVectorWriter< N >::produces_using().

247 {
248  for (auto const & s : fRegisteredDataTypes)
249  {
250  if (s == dname) { return true; }
251  }
252  return false;
253 }
Float_t s
Definition: plot.C:23
std::vector< std::string > fRegisteredDataTypes
Definition: MVAWriter.h:139
template<size_t N>
bool anab::FVectorWriter< N >::descriptionExists ( const std::string &  tname) const
private

Check if the containers for results prepared for "tname" data type are ready.

Definition at line 278 of file MVAWriter.h.

References d, anab::FVectorWriter< N >::fDescriptions, anab::FVectorWriter< N >::fInstanceName, and n.

Referenced by anab::FVectorWriter< 4 >::clearEventData(), anab::FVectorWriter< N >::initOutputs(), and anab::FVectorWriter< N >::saveOutputs().

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 }
Float_t d
Definition: plot.C:237
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:144
std::string fInstanceName
Definition: MVAWriter.h:137
Char_t n[5]
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(); }
template<size_t N>
template<class T >
FVector_ID anab::FVectorWriter< N >::getProductID ( ) const
protected
template<size_t N>
template<class T >
anab::FVector_ID anab::FVectorWriter< N >::getProductID ( ) const

Definition at line 233 of file MVAWriter.h.

References anab::FVectorWriter< N >::fTypeHashToID, anab::FVectorWrapperBase::getProductHash(), and anab::FVectorWrapperBase::getProductName().

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 }
std::unordered_map< size_t, FVector_ID > fTypeHashToID
Definition: MVAWriter.h:142
std::string getProductName(std::type_info const &ti) const
size_t getProductHash(std::type_info const &ti) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
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<size_t N>
template<class T >
std::array<float, N> anab::FVectorWriter< N >::getVector ( size_t  key) const
inline

Get copy of the feature vector for the type T, at index "key".

Definition at line 98 of file MVAWriter.h.

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

Get copy of the feature vector for the type T, idicated with art::Ptr::key().

Definition at line 108 of file MVAWriter.h.

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  }
key_type key() const
Definition: Ptr.h:356
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
template<size_t N>
template<class T >
FVector_ID anab::FVectorWriter< N >::initOutputs ( std::string const &  dataTag,
size_t  dataSize,
std::vector< std::string > const &  names = std::vector< std::string >(N,"") 
)

Initialize container for FeatureVectors and, if not yet done, the container for metadata, then creates metadata for data products of type T. FeatureVector container is initialized to hold dataSize vectors (if dataSize > 0): use setOutput() to store values. Returns index of collection which should be used when saving actual output values.

Referenced by anab::FVectorWriter< 4 >::FVectorWriter(), and hit::DPRawHitFinder::produce().

template<size_t N>
template<class T >
FVector_ID anab::FVectorWriter< N >::initOutputs ( art::InputTag const &  dataTag,
size_t  dataSize,
std::vector< std::string > const &  names = std::vector< std::string >(N, "") 
)
inline

Definition at line 55 of file MVAWriter.h.

57  { return initOutputs<T>(dataTag.encode(), dataSize, names); }
template<size_t N>
template<class T >
FVector_ID anab::FVectorWriter< N >::initOutputs ( art::InputTag const &  dataTag,
std::vector< std::string > const &  names = std::vector< std::string >(N, "") 
)
inline

Initialize container for FeatureVectors and, if not yet done, the container for metadata, then creates metadata for data products of type T. FeatureVector container is initialized as EMPTY and vectors should be added with addOutput() function. Returns index of collection which should be used when adding actual output values.

Definition at line 70 of file MVAWriter.h.

72  { return initOutputs<T>(dataTag.encode(), 0, names); }
template<size_t N>
template<class T >
FVector_ID anab::FVectorWriter< N >::initOutputs ( std::vector< std::string > const &  names = std::vector< std::string >(N, ""))
inline

Definition at line 75 of file MVAWriter.h.

77  { return initOutputs<T>(std::string(""), 0, names); }
template<size_t N>
template<class T >
anab::FVector_ID anab::FVectorWriter< N >::initOutputs ( std::string const &  dataTag,
size_t  dataSize,
std::vector< std::string > const &  names 
)

Definition at line 293 of file MVAWriter.h.

References anab::FVectorWriter< N >::dataTypeRegistered(), anab::FVectorWriter< N >::descriptionExists(), anab::FVectorWriter< N >::fDescriptions, anab::FVectorWriter< N >::fInstanceName, anab::FVectorWriter< N >::fTypeHashToID, anab::FVectorWriter< N >::fVectors, anab::FVectorWrapperBase::getProductHash(), anab::FVectorWrapperBase::getProductName(), and lar::dump::vector().

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 }
std::unordered_map< size_t, FVector_ID > fTypeHashToID
Definition: MVAWriter.h:142
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
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:144
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
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
std::string fInstanceName
Definition: MVAWriter.h:137
size_t getProductHash(std::type_info const &ti) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
template<size_t N>
size_t anab::FVectorWriter< N >::length ( ) const
inline

Get the length of a single feature vector.

Definition at line 94 of file MVAWriter.h.

94 { return N; }
template<size_t N>
template<class T >
void anab::FVectorWriter< N >::produces_using ( )

Register the collection of metadata type FVecDescription<N> (once for all data types for which vectors are saved) and the collection of FeatureVectors<N> (using data type name added to fInstanceName as instance name of the collection made for the type T).

Definition at line 258 of file MVAWriter.h.

References anab::FVectorWriter< N >::dataTypeRegistered(), anab::FVectorWriter< N >::fInstanceName, anab::FVectorWriter< N >::fIsDescriptionRegistered, anab::FVectorWriter< N >::fProducer, anab::FVectorWriter< N >::fRegisteredDataTypes, and anab::FVectorWrapperBase::getProductName().

Referenced by hit::DPRawHitFinder::DPRawHitFinder(), and anab::FVectorWriter< 4 >::FVectorWriter().

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 }
std::string getProductName(std::type_info const &ti) const
art::EDProducer * fProducer
Definition: MVAWriter.h:136
std::vector< std::string > fRegisteredDataTypes
Definition: MVAWriter.h:139
bool fIsDescriptionRegistered
Definition: MVAWriter.h:140
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
std::string fInstanceName
Definition: MVAWriter.h:137
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
template<size_t N>
void anab::FVectorWriter< N >::saveOutputs ( art::Event evt)

Check consistency and save all the results in the event.

Definition at line 326 of file MVAWriter.h.

References anab::FVectorWriter< N >::clearEventData(), anab::FVectorWriter< N >::descriptionExists(), anab::FVectorWriter< N >::fDescriptions, anab::FVectorWriter< N >::fInstanceName, anab::FVectorWriter< N >::fRegisteredDataTypes, anab::FVectorWriter< N >::fVectors, n, and art::Event::put().

Referenced by hit::DPRawHitFinder::produce(), and anab::FVectorWriter< 4 >::setDataTag().

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 }
bool descriptionExists(const std::string &tname) const
Check if the containers for results prepared for "tname" data type are ready.
Definition: MVAWriter.h:278
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:144
std::vector< std::string > fRegisteredDataTypes
Definition: MVAWriter.h:139
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
std::string fInstanceName
Definition: MVAWriter.h:137
Char_t n[5]
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
template<size_t N>
void anab::FVectorWriter< N >::setDataTag ( FVector_ID  id,
art::InputTag const &  dataTag 
)
inline

Set tag of associated data products in case it was not ready at the initialization time.

Definition at line 85 of file MVAWriter.h.

85 { (*fDescriptions)[id].setDataTag(dataTag.encode()); }
template<size_t N>
void anab::FVectorWriter< N >::setVector ( FVector_ID  id,
size_t  key,
std::array< float, N > const &  values 
)
inline

Definition at line 59 of file MVAWriter.h.

Referenced by anab::MVAWriter< N >::setOutput().

59 { (*(fVectors[id]))[key] = values; }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
template<size_t N>
void anab::FVectorWriter< N >::setVector ( FVector_ID  id,
size_t  key,
std::array< double, N > const &  values 
)
inline

Definition at line 60 of file MVAWriter.h.

60 { (*(fVectors[id]))[key] = values; }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
template<size_t N>
void anab::FVectorWriter< N >::setVector ( FVector_ID  id,
size_t  key,
std::vector< float > const &  values 
)
inline

Definition at line 61 of file MVAWriter.h.

61 { (*(fVectors[id]))[key] = values; }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
template<size_t N>
void anab::FVectorWriter< N >::setVector ( FVector_ID  id,
size_t  key,
std::vector< double > const &  values 
)
inline

Definition at line 62 of file MVAWriter.h.

62 { (*(fVectors[id]))[key] = values; }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132
template<size_t N>
size_t anab::FVectorWriter< N >::size ( FVector_ID  id) const
inline

Get the number of contained feature vectors.

Definition at line 91 of file MVAWriter.h.

91 { return fVectors[id]->size(); }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:132

Friends And Related Function Documentation

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

Definition at line 116 of file MVAWriter.h.

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  }
Char_t n[5]

Member Data Documentation

template<size_t N>
std::unique_ptr< std::vector< anab::FVecDescription<N> > > anab::FVectorWriter< N >::fDescriptions
private
template<size_t N>
bool anab::FVectorWriter< N >::fIsDescriptionRegistered
private

Definition at line 140 of file MVAWriter.h.

Referenced by anab::FVectorWriter< N >::produces_using().

template<size_t N>
art::EDProducer* anab::FVectorWriter< N >::fProducer
private

Definition at line 136 of file MVAWriter.h.

Referenced by anab::FVectorWriter< N >::produces_using().

template<size_t N>
std::vector< std::string > anab::FVectorWriter< N >::fRegisteredDataTypes
private
template<size_t N>
std::unordered_map< size_t, FVector_ID > anab::FVectorWriter< N >::fTypeHashToID
private

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