Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Friends | List of all members
anab::FVectorWriter< N > Class Template Reference

#include <MVAWriter.h>

Inheritance diagram for anab::FVectorWriter< N >:
anab::FVectorWrapperBase anab::MVAWriter< N > anab::MVAWriter< 2 > anab::MVAWriter< 3 > anab::MVAWriter< 4 >

Public Member Functions

 FVectorWriter (art::ProducesCollector &collector, 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
 
- Protected Member Functions inherited from anab::FVectorWrapperBase
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::ProducesCollectorfCollector
 
std::string fInstanceName
 
std::vector< std::stringfRegisteredDataTypes
 
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 25 of file MVAWriter.h.

Constructor & Destructor Documentation

template<size_t N>
anab::FVectorWriter< N >::FVectorWriter ( art::ProducesCollector collector,
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 32 of file MVAWriter.h.

32  :
33  fCollector(collector), fInstanceName(name),
35  fDescriptions(nullptr)
36  { }
static QCString name
Definition: declinfo.cpp:673
art::ProducesCollector & fCollector
Definition: MVAWriter.h:135
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:143
bool fIsDescriptionRegistered
Definition: MVAWriter.h:139
std::string fInstanceName
Definition: MVAWriter.h:136

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 78 of file MVAWriter.h.

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

Definition at line 79 of file MVAWriter.h.

79 { fVectors[id]->emplace_back(values); }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:131
template<size_t N>
void anab::FVectorWriter< N >::addVector ( FVector_ID  id,
std::vector< float > 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:131
template<size_t N>
void anab::FVectorWriter< N >::addVector ( FVector_ID  id,
std::vector< double > 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:131
template<size_t N>
void anab::FVectorWriter< N >::clearEventData ( )
inlineprivate

Definition at line 144 of file MVAWriter.h.

145  {
146  fTypeHashToID.clear(); fVectors.clear();
147  fDescriptions.reset(nullptr);
148  }
std::unordered_map< size_t, FVector_ID > fTypeHashToID
Definition: MVAWriter.h:141
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:143
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:131
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 245 of file MVAWriter.h.

246 {
247  for (auto const & s : fRegisteredDataTypes)
248  {
249  if (s == dname) { return true; }
250  }
251  return false;
252 }
std::vector< std::string > fRegisteredDataTypes
Definition: MVAWriter.h:138
static QCString * s
Definition: config.cpp:1042
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 277 of file MVAWriter.h.

278 {
279  if (!fDescriptions) return false;
280 
281  std::string n = fInstanceName + tname;
282  for (auto const & d : *fDescriptions)
283  {
284  if (d.outputInstance() == n) { return true; }
285  }
286  return false;
287 }
std::string string
Definition: nybbler.cc:12
std::void_t< T > n
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:143
std::string fInstanceName
Definition: MVAWriter.h:136
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 232 of file MVAWriter.h.

233 {
234  auto const & ti = typeid(T);
235  auto search = fTypeHashToID.find(getProductHash(ti));
236  if (search != fTypeHashToID.end()) { return search->second; }
237  else
238  {
239  throw cet::exception("FVectorWriter") << "Feature vectors not initialized for product " << getProductName(ti) << std::endl;
240  }
241 }
std::unordered_map< size_t, FVector_ID > fTypeHashToID
Definition: MVAWriter.h:141
std::string getProductName(std::type_info const &ti) const
Definition: search.py:1
size_t getProductHash(std::type_info const &ti) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
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 97 of file MVAWriter.h.

98  {
99  std::array<float, N> vout;
100  auto const & src = ( *(fVectors[getProductID<T>()]) )[key];
101  for (size_t i = 0; i < N; ++i) vout[i] = src[i];
102  return vout;
103  }
def key(type, name=None)
Definition: graph.py:13
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:131
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 107 of file MVAWriter.h.

108  {
109  std::array<float, N> vout;
110  auto const & src = ( *(fVectors[getProductID<T>()]) )[item.key()];
111  for (size_t i = 0; i < N; ++i) vout[i] = src[i];
112  return vout;
113  }
key_type key() const noexcept
Definition: Ptr.h:216
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:131
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::vectorstd::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.

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::vectorstd::string >(N, "") 
)
inline

Definition at line 54 of file MVAWriter.h.

56  { return initOutputs<T>(dataTag.encode(), dataSize, names); }
static std::vector< std::string > const names
Definition: FragmentType.hh:8
template<size_t N>
template<class T >
FVector_ID anab::FVectorWriter< N >::initOutputs ( art::InputTag const &  dataTag,
std::vector< std::string > const &  names = std::vectorstd::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 69 of file MVAWriter.h.

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

Definition at line 74 of file MVAWriter.h.

76  { return initOutputs<T>(std::string(""), 0, names); }
std::string string
Definition: nybbler.cc:12
static std::vector< std::string > const names
Definition: FragmentType.hh:8
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 292 of file MVAWriter.h.

295 {
296  size_t dataHash = getProductHash(typeid(T));
297  std::string dataName = getProductName(typeid(T));
298 
299  if (!dataTypeRegistered(dataName))
300  {
301  throw cet::exception("FVectorWriter") << "Type " << dataName << "not registered with produces_using() function." << std::endl;
302  }
303 
304  if (!fDescriptions)
305  {
306  fDescriptions = std::make_unique< std::vector< anab::FVecDescription<N> > >();
307  }
308  else if (descriptionExists(dataName))
309  {
310  throw cet::exception("FVectorWriter") << "FVecDescription<N> already initialized for " << dataName << std::endl;
311  }
312  fDescriptions->emplace_back(dataTag, fInstanceName + dataName, names);
313 
314  fVectors.push_back( std::make_unique< std::vector< anab::FeatureVector<N> > >() );
315  anab::FVector_ID id = fVectors.size() - 1;
316  fTypeHashToID[dataHash] = id;
317 
318  if (dataSize) { fVectors[id]->resize(dataSize, anab::FeatureVector<N>(0.0F)); }
319 
320  return id;
321 }
std::unordered_map< size_t, FVector_ID > fTypeHashToID
Definition: MVAWriter.h:141
std::string string
Definition: nybbler.cc:12
struct vector vector
bool descriptionExists(const std::string &tname) const
Check if the containers for results prepared for "tname" data type are ready.
Definition: MVAWriter.h:277
size_t FVector_ID
Index to the MVA output / FeatureVector collection, used when result vectors are added or set...
Definition: MVAWriter.h:21
std::string getProductName(std::type_info const &ti) const
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:143
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:131
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:245
std::string fInstanceName
Definition: MVAWriter.h:136
size_t getProductHash(std::type_info const &ti) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
template<size_t N>
size_t anab::FVectorWriter< N >::length ( ) const
inline

Get the length of a single feature vector.

Definition at line 93 of file MVAWriter.h.

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 257 of file MVAWriter.h.

258 {
259  std::string dataName = getProductName(typeid(T));
260  if (dataTypeRegistered(dataName))
261  {
262  throw cet::exception("FVectorWriter") << "Type " << dataName << "was already registered." << std::endl;
263  }
264 
266  {
267  fCollector.produces< std::vector< anab::FVecDescription<N> > >(fInstanceName);
269  }
270 
271  fCollector.produces< std::vector< anab::FeatureVector<N> > >(fInstanceName + dataName);
272  fRegisteredDataTypes.push_back(dataName);
273 }
std::string string
Definition: nybbler.cc:12
std::string getProductName(std::type_info const &ti) const
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
art::ProducesCollector & fCollector
Definition: MVAWriter.h:135
std::vector< std::string > fRegisteredDataTypes
Definition: MVAWriter.h:138
bool fIsDescriptionRegistered
Definition: MVAWriter.h:139
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:245
std::string fInstanceName
Definition: MVAWriter.h:136
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
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 325 of file MVAWriter.h.

326 {
327  for (auto const & n : fRegisteredDataTypes)
328  {
329  if (!descriptionExists(n))
330  {
331  throw cet::exception("FVectorWriter") << "No FVecDescription<N> prepared for type " << n << std::endl;
332  }
333  }
334 
335  if (fVectors.size() != fDescriptions->size())
336  {
337  throw cet::exception("FVectorWriter") << "FVecDescription<N> vector length not equal to the number of FeatureVector<N> vectors" << std::endl;
338  }
339 
340  for (size_t i = 0; i < fVectors.size(); ++i)
341  {
342  auto const & outInstName = (*fDescriptions)[i].outputInstance();
343  if ((*fDescriptions)[i].dataTag().empty())
344  {
345  throw cet::exception("FVectorWriter") << "FVecDescription<N> reco data tag not set for " << outInstName << std::endl;
346  }
347  evt.put(std::move(fVectors[i]), outInstName);
348  }
350  clearEventData();
351 }
bool descriptionExists(const std::string &tname) const
Check if the containers for results prepared for "tname" data type are ready.
Definition: MVAWriter.h:277
std::void_t< T > n
def move(depos, offset)
Definition: depos.py:107
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:143
std::vector< std::string > fRegisteredDataTypes
Definition: MVAWriter.h:138
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:131
std::string fInstanceName
Definition: MVAWriter.h:136
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97
QTextStream & endl(QTextStream &s)
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 84 of file MVAWriter.h.

84 { (*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 58 of file MVAWriter.h.

58 { (*(fVectors[id]))[key] = values; }
def key(type, name=None)
Definition: graph.py:13
Q_UINT16 values[128]
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:131
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 59 of file MVAWriter.h.

59 { (*(fVectors[id]))[key] = values; }
def key(type, name=None)
Definition: graph.py:13
Q_UINT16 values[128]
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:131
template<size_t N>
void anab::FVectorWriter< N >::setVector ( FVector_ID  id,
size_t  key,
std::vector< float > const &  values 
)
inline

Definition at line 60 of file MVAWriter.h.

60 { (*(fVectors[id]))[key] = values; }
def key(type, name=None)
Definition: graph.py:13
Q_UINT16 values[128]
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:131
template<size_t N>
void anab::FVectorWriter< N >::setVector ( FVector_ID  id,
size_t  key,
std::vector< double > const &  values 
)
inline

Definition at line 61 of file MVAWriter.h.

61 { (*(fVectors[id]))[key] = values; }
def key(type, name=None)
Definition: graph.py:13
Q_UINT16 values[128]
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:131
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 90 of file MVAWriter.h.

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

Friends And Related Function Documentation

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

Definition at line 115 of file MVAWriter.h.

116  {
117  o << "FVectorWriter for " << a.fInstanceName << ", " << N << " outputs";
118  if (!a.fRegisteredDataTypes.empty())
119  {
120  o << ", ready to write results made for:" << std::endl;
121  for (auto const & n : a.fRegisteredDataTypes) { o << "\t" << n << std::endl; }
122  }
123  else { o << ", nothing registered for writing to the events" << std::endl; }
124  return o;
125  }
std::void_t< T > n
const double a
QTextStream & endl(QTextStream &s)

Member Data Documentation

template<size_t N>
art::ProducesCollector& anab::FVectorWriter< N >::fCollector
private

Definition at line 135 of file MVAWriter.h.

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

Definition at line 143 of file MVAWriter.h.

template<size_t N>
std::string anab::FVectorWriter< N >::fInstanceName
private

Definition at line 136 of file MVAWriter.h.

template<size_t N>
bool anab::FVectorWriter< N >::fIsDescriptionRegistered
private

Definition at line 139 of file MVAWriter.h.

template<size_t N>
std::vector< std::string > anab::FVectorWriter< N >::fRegisteredDataTypes
private

Definition at line 138 of file MVAWriter.h.

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

Definition at line 141 of file MVAWriter.h.

template<size_t N>
std::vector< std::unique_ptr< std::vector< anab::FeatureVector<N> > > > anab::FVectorWriter< N >::fVectors
protected

Definition at line 131 of file MVAWriter.h.


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