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

#include "MVAWriter.h"

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

Public Member Functions

 MVAWriter (art::ProducesCollector &collector, const char *name="")
 
void setOutput (FVector_ID id, size_t key, std::array< float, N > const &values)
 
void setOutput (FVector_ID id, size_t key, std::array< double, N > const &values)
 
void setOutput (FVector_ID id, size_t key, std::vector< float > const &values)
 
void setOutput (FVector_ID id, size_t key, std::vector< double > const &values)
 
void addOutput (FVector_ID id, std::array< float, N > const &values)
 
void addOutput (FVector_ID id, std::array< double, N > const &values)
 
void addOutput (FVector_ID id, std::vector< float > const &values)
 
void addOutput (FVector_ID id, std::vector< double > const &values)
 
template<class T >
std::array< float, N > getOutput (std::vector< art::Ptr< T >> const &items) const
 
template<class T >
std::array< float, N > getOutput (std::vector< art::Ptr< T >> const &items, std::vector< float > const &weights) const
 
template<class T >
std::array< float, N > getOutput (std::vector< art::Ptr< T >> const &items, std::function< float(T const &)> fweight) const
 
template<class T >
std::array< float, N > getOutput (std::vector< art::Ptr< T >> const &items, std::function< float(art::Ptr< T > const &)> fweight) const
 
template<class T >
std::array< float, N > getOutput (size_t key) const
 Get copy of the MVA output vector for the type T, at index "key". More...
 
template<class T >
std::array< float, N > getOutput (art::Ptr< T > const &item) const
 Get copy of the MVA output vector for the type T, idicated with art::Ptr::key(). More...
 
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,""))
 
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,""))
 
template<class T >
anab::FVector_ID initOutputs (std::string const &dataTag, size_t dataSize, std::vector< std::string > const &names)
 
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)
 
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
 

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
 
template<class T , size_t N>
std::array< float, N > pAccumulate (std::vector< art::Ptr< T >> const &items, std::vector< FeatureVector< N >> const &outs) const
 
template<class T , size_t N>
std::array< float, N > pAccumulate (std::vector< art::Ptr< T >> const &items, std::vector< float > const &weights, std::vector< FeatureVector< N >> const &outs) const
 
template<class T , size_t N>
std::array< float, N > pAccumulate (std::vector< art::Ptr< T >> const &items, std::function< float(T const &)> fweight, std::vector< FeatureVector< N >> const &outs) const
 
template<class T , size_t N>
std::array< float, N > pAccumulate (std::vector< art::Ptr< T >> const &items, std::function< float(art::Ptr< T > const &)> fweight, std::vector< FeatureVector< N >> const &outs) const
 
template<class T , size_t N>
std::array< float, N > pAccumulate (std::vector< art::Ptr< T >> const &items, std::vector< FeatureVector< N >> const &outs, std::array< char, N > const &mask) const
 

Protected Attributes

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

Detailed Description

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

Helper for registering in the art::EDProducer all data products needed for N-output MVA results: keep MVADescriptions<N> for all types T in one collection while separate instance names are used for the MVA output value collections for each type T. Use one instance of this class per one MVA model, applied to one or more types.

Definition at line 202 of file MVAWriter.h.

Constructor & Destructor Documentation

template<size_t N>
anab::MVAWriter< N >::MVAWriter ( art::ProducesCollector collector,
const char *  name = "" 
)
inline

Name provided to the constructor is used as an instance name for MVADescription<N> and FeatureVector<N> (for which it is combined with the processed data product names). Good idea is to use the name as an indication of what MVA model was used on the data (like eg. "emtrack" for outputs from a model distinguishing EM from track-like hits and clusters). The name is used as an instance name for the MVADescription data product which lets you to save multiple MVA results from a single art module.

Definition at line 210 of file MVAWriter.h.

211  : FVectorWriter<N>(collector, name)
212  {}

Member Function Documentation

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

Definition at line 231 of file MVAWriter.h.

Referenced by nnet::EmTrack< N >::make_clusters(), nnet::EmTrackClusterId2out::produce(), nnet::EmTrackClusterId::produce(), and nnet::EmTrackMichelId::produce().

232  {
233  FVectorWriter<N>::addVector(id, values);
234  }
void addVector(FVector_ID id, std::array< float, N > const &values)
Definition: MVAWriter.h:96
template<size_t N>
void anab::MVAWriter< N >::addOutput ( FVector_ID  id,
std::array< double, N > const &  values 
)
inline

Definition at line 235 of file MVAWriter.h.

236  {
237  FVectorWriter<N>::addVector(id, values);
238  }
void addVector(FVector_ID id, std::array< float, N > const &values)
Definition: MVAWriter.h:96
template<size_t N>
void anab::MVAWriter< N >::addOutput ( FVector_ID  id,
std::vector< float > const &  values 
)
inline

Definition at line 239 of file MVAWriter.h.

240  {
241  FVectorWriter<N>::addVector(id, values);
242  }
void addVector(FVector_ID id, std::array< float, N > const &values)
Definition: MVAWriter.h:96
template<size_t N>
void anab::MVAWriter< N >::addOutput ( FVector_ID  id,
std::vector< double > const &  values 
)
inline

Definition at line 243 of file MVAWriter.h.

244  {
245  FVectorWriter<N>::addVector(id, values);
246  }
void addVector(FVector_ID id, std::array< float, N > const &values)
Definition: MVAWriter.h:96
template<size_t N>
void anab::FVectorWriter< N >::addVector ( FVector_ID  id,
std::array< float, N > const &  values 
)
inlineinherited

Definition at line 96 of file MVAWriter.h.

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

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

Definition at line 100 of file MVAWriter.h.

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

Definition at line 104 of file MVAWriter.h.

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

Definition at line 108 of file MVAWriter.h.

109  {
110  fVectors[id]->emplace_back(values);
111  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
template<size_t N>
template<class T >
std::array<float, N> anab::MVAWriter< N >::getOutput ( std::vector< art::Ptr< T >> const &  items) const
inline

Get MVA results accumulated over the vector of items (eg. over hits associated to a cluster). NOTE: MVA outputs for these items has to be added to the MVAWriter first!

Definition at line 251 of file MVAWriter.h.

Referenced by nnet::EmTrackClusterId2out::produce(), nnet::EmTrackClusterId::produce(), and nnet::EmTrackMichelId::produce().

252  {
253  return pAccumulate<T, N>(
254  items, *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()]));
255  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
template<size_t N>
template<class T >
std::array<float, N> anab::MVAWriter< N >::getOutput ( std::vector< art::Ptr< T >> const &  items,
std::vector< float > const &  weights 
) const
inline

Get MVA results accumulated with provided weights over the vector of items (eg. over clusters associated to a track, weighted by the cluster size; or over hits associated to a cluster, weighted by the hit area). NOTE: MVA outputs for these items has to be added to the MVAWriter first!

Definition at line 262 of file MVAWriter.h.

264  {
265  return pAccumulate<T, N>(
266  items,
267  weights,
268  *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()]));
269  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
template<size_t N>
template<class T >
std::array<float, N> anab::MVAWriter< N >::getOutput ( std::vector< art::Ptr< T >> const &  items,
std::function< float(T const &)>  fweight 
) const
inline

Get MVA results accumulated with provided weighting function over the vector of items (eg. over clusters associated to a track, weighted by the cluster size; or over hits associated to a cluster, weighted by the hit area). NOTE: MVA outputs for these items has to be added to the MVAWriter first!

Definition at line 276 of file MVAWriter.h.

278  {
279  return pAccumulate<T, N>(
280  items,
281  fweight,
282  *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()]));
283  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
template<size_t N>
template<class T >
std::array<float, N> anab::MVAWriter< N >::getOutput ( std::vector< art::Ptr< T >> const &  items,
std::function< float(art::Ptr< T > const &)>  fweight 
) const
inline

Definition at line 286 of file MVAWriter.h.

288  {
289  return pAccumulate<T, N>(
290  items,
291  fweight,
292  *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()]));
293  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
template<size_t N>
template<class T >
std::array<float, N> anab::MVAWriter< N >::getOutput ( size_t  key) const
inline

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

Definition at line 297 of file MVAWriter.h.

298  {
299  return FVectorWriter<N>::template getVector<T>(key);
300  }
template<size_t N>
template<class T >
std::array<float, N> anab::MVAWriter< N >::getOutput ( art::Ptr< T > const &  item) const
inline

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

Definition at line 304 of file MVAWriter.h.

305  {
306  return FVectorWriter<N>::template getVector<T>(item);
307  }
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(); }
template<size_t N>
template<class T >
FVector_ID anab::FVectorWriter< N >::getProductID ( ) const
protectedinherited
template<size_t N>
template<class T >
anab::FVector_ID anab::FVectorWriter< N >::getProductID ( ) const
inherited

Definition at line 317 of file MVAWriter.h.

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

318 {
319  auto const& ti = typeid(T);
320  auto search = fTypeHashToID.find(getProductHash(ti));
321  if (search != fTypeHashToID.end()) { return search->second; }
322  else {
323  throw cet::exception("FVectorWriter")
324  << "Feature vectors not initialized for product " << getProductName(ti) << std::endl;
325  }
326 }
std::string getProductName(std::type_info const &ti) const
std::unordered_map< size_t, FVector_ID > fTypeHashToID
Definition: MVAWriter.h:180
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
inlineinherited

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

Definition at line 130 of file MVAWriter.h.

131  {
132  std::array<float, N> vout;
133  auto const& src = (*(fVectors[getProductID<T>()]))[key];
134  for (size_t i = 0; i < N; ++i)
135  vout[i] = src[i];
136  return vout;
137  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
template<size_t N>
template<class T >
std::array<float, N> anab::FVectorWriter< N >::getVector ( art::Ptr< T > const &  item) const
inlineinherited

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

Definition at line 141 of file MVAWriter.h.

142  {
143  std::array<float, N> vout;
144  auto const& src = (*(fVectors[getProductID<T>()]))[item.key()];
145  for (size_t i = 0; i < N; ++i)
146  vout[i] = src[i];
147  return vout;
148  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
key_type key() const noexcept
Definition: Ptr.h:166
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,"") 
)
inherited

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(), hit::DPRawHitFinder::produce(), nnet::EmTrackClusterId2out::produce(), nnet::EmTrackClusterId::produce(), and nnet::EmTrackMichelId::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, "") 
)
inlineinherited

Definition at line 55 of file MVAWriter.h.

58  {
59  return initOutputs<T>(dataTag.encode(), dataSize, names);
60  }
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, "") 
)
inlineinherited

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

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

Definition at line 91 of file MVAWriter.h.

92  {
93  return initOutputs<T>(std::string(""), 0, names);
94  }
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 
)
inherited

Definition at line 374 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().

377 {
378  size_t dataHash = getProductHash(typeid(T));
379  std::string dataName = getProductName(typeid(T));
380 
381  if (!dataTypeRegistered(dataName)) {
382  throw cet::exception("FVectorWriter")
383  << "Type " << dataName << "not registered with produces_using() function." << std::endl;
384  }
385 
386  if (!fDescriptions) { fDescriptions = std::make_unique<std::vector<anab::FVecDescription<N>>>(); }
387  else if (descriptionExists(dataName)) {
388  throw cet::exception("FVectorWriter")
389  << "FVecDescription<N> already initialized for " << dataName << std::endl;
390  }
391  fDescriptions->emplace_back(dataTag, fInstanceName + dataName, names);
392 
393  fVectors.push_back(std::make_unique<std::vector<anab::FeatureVector<N>>>());
394  anab::FVector_ID id = fVectors.size() - 1;
395  fTypeHashToID[dataHash] = id;
396 
397  if (dataSize) { fVectors[id]->resize(dataSize, anab::FeatureVector<N>(0.0F)); }
398 
399  return id;
400 }
bool descriptionExists(const std::string &tname) const
Check if the containers for results prepared for "tname" data type are ready.
Definition: MVAWriter.h:360
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
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
std::unordered_map< size_t, FVector_ID > fTypeHashToID
Definition: MVAWriter.h:180
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:330
std::string fInstanceName
Definition: MVAWriter.h:175
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:182
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
inlineinherited

Get the length of a single feature vector.

Definition at line 126 of file MVAWriter.h.

126 { return N; }
template<class T , size_t N>
std::array< float, N > anab::MVAWrapperBase::pAccumulate ( std::vector< art::Ptr< T >> const &  items,
std::vector< FeatureVector< N >> const &  outs 
) const
protectedinherited

Definition at line 73 of file MVAWrapperBase.h.

References trkf::fill().

76 {
77  std::array<double, N> acc;
78  acc.fill(0);
79 
80  float pmin = 1.0e-6, pmax = 1.0 - pmin;
81  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
82 
83  for (auto const& ptr : items) {
84  auto const& vout = outs[ptr.key()];
85  for (size_t i = 0; i < vout.size(); ++i) {
86  float v;
87  if (vout[i] < pmin)
88  v = log_pmin;
89  else if (vout[i] > pmax)
90  v = log_pmax;
91  else
92  v = std::log(vout[i]);
93 
94  acc[i] += v;
95  }
96  }
97 
98  if (!items.empty()) {
99  double totp = 0.0;
100  for (size_t i = 0; i < N; ++i) {
101  acc[i] = exp(acc[i] / items.size());
102  totp += acc[i];
103  }
104  for (size_t i = 0; i < N; ++i) {
105  acc[i] /= totp;
106  }
107  }
108  else
109  std::fill(acc.begin(), acc.end(), 1.0 / N);
110 
111  std::array<float, N> result;
112  for (size_t i = 0; i < N; ++i)
113  result[i] = acc[i];
114  return result;
115 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
template<class T , size_t N>
std::array< float, N > anab::MVAWrapperBase::pAccumulate ( std::vector< art::Ptr< T >> const &  items,
std::vector< float > const &  weights,
std::vector< FeatureVector< N >> const &  outs 
) const
protectedinherited

Definition at line 119 of file MVAWrapperBase.h.

References trkf::fill(), and w.

123 {
124  std::array<double, N> acc;
125  acc.fill(0);
126 
127  float pmin = 1.0e-6, pmax = 1.0 - pmin;
128  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
129  double totw = 0.0;
130 
131  for (size_t k = 0; k < items.size(); ++k) {
132  auto const& ptr = items[k];
133  float w = weights[k];
134 
135  if (w == 0) continue;
136 
137  auto const& vout = outs[ptr.key()];
138  for (size_t i = 0; i < vout.size(); ++i) {
139  float v;
140  if (vout[i] < pmin)
141  v = log_pmin;
142  else if (vout[i] > pmax)
143  v = log_pmax;
144  else
145  v = std::log(vout[i]);
146 
147  acc[i] += w * v;
148  }
149  totw += w;
150  }
151 
152  if (!items.empty()) {
153  double totp = 0.0;
154  for (size_t i = 0; i < N; ++i) {
155  acc[i] = exp(acc[i] / totw);
156  totp += acc[i];
157  }
158  for (size_t i = 0; i < N; ++i) {
159  acc[i] /= totp;
160  }
161  }
162  else
163  std::fill(acc.begin(), acc.end(), 1.0 / N);
164 
165  std::array<float, N> result;
166  for (size_t i = 0; i < N; ++i)
167  result[i] = acc[i];
168  return result;
169 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Float_t w
Definition: plot.C:20
template<class T , size_t N>
std::array< float, N > anab::MVAWrapperBase::pAccumulate ( std::vector< art::Ptr< T >> const &  items,
std::function< float(T const &)>  fweight,
std::vector< FeatureVector< N >> const &  outs 
) const
protectedinherited

Definition at line 173 of file MVAWrapperBase.h.

References trkf::fill(), and w.

177 {
178  std::array<double, N> acc;
179  acc.fill(0);
180 
181  float pmin = 1.0e-6, pmax = 1.0 - pmin;
182  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
183  double totw = 0.0;
184 
185  for (size_t k = 0; k < items.size(); ++k) {
186  auto const& ptr = items[k];
187  float w = fweight(*ptr);
188 
189  if (w == 0) continue;
190 
191  auto const& vout = outs[ptr.key()];
192  for (size_t i = 0; i < vout.size(); ++i) {
193  float v;
194  if (vout[i] < pmin)
195  v = log_pmin;
196  else if (vout[i] > pmax)
197  v = log_pmax;
198  else
199  v = std::log(vout[i]);
200 
201  acc[i] += w * v;
202  }
203  totw += w;
204  }
205 
206  if (!items.empty()) {
207  double totp = 0.0;
208  for (size_t i = 0; i < N; ++i) {
209  acc[i] = exp(acc[i] / totw);
210  totp += acc[i];
211  }
212  for (size_t i = 0; i < N; ++i) {
213  acc[i] /= totp;
214  }
215  }
216  else
217  std::fill(acc.begin(), acc.end(), 1.0 / N);
218 
219  std::array<float, N> result;
220  for (size_t i = 0; i < N; ++i)
221  result[i] = acc[i];
222  return result;
223 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Float_t w
Definition: plot.C:20
template<class T , size_t N>
std::array< float, N > anab::MVAWrapperBase::pAccumulate ( std::vector< art::Ptr< T >> const &  items,
std::function< float(art::Ptr< T > const &)>  fweight,
std::vector< FeatureVector< N >> const &  outs 
) const
protectedinherited

Definition at line 227 of file MVAWrapperBase.h.

References trkf::fill(), and w.

231 {
232  std::array<double, N> acc;
233  acc.fill(0);
234 
235  float pmin = 1.0e-6, pmax = 1.0 - pmin;
236  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
237  double totw = 0.0;
238 
239  for (size_t k = 0; k < items.size(); ++k) {
240  auto const& ptr = items[k];
241  float w = fweight(ptr);
242 
243  if (w == 0) continue;
244 
245  auto const& vout = outs[ptr.key()];
246  for (size_t i = 0; i < vout.size(); ++i) {
247  float v;
248  if (vout[i] < pmin)
249  v = log_pmin;
250  else if (vout[i] > pmax)
251  v = log_pmax;
252  else
253  v = std::log(vout[i]);
254 
255  acc[i] += w * v;
256  }
257  totw += w;
258  }
259 
260  if (!items.empty()) {
261  double totp = 0.0;
262  for (size_t i = 0; i < N; ++i) {
263  acc[i] = exp(acc[i] / totw);
264  totp += acc[i];
265  }
266  for (size_t i = 0; i < N; ++i) {
267  acc[i] /= totp;
268  }
269  }
270  else
271  std::fill(acc.begin(), acc.end(), 1.0 / N);
272 
273  std::array<float, N> result;
274  for (size_t i = 0; i < N; ++i)
275  result[i] = acc[i];
276  return result;
277 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Float_t w
Definition: plot.C:20
template<class T , size_t N>
std::array< float, N > anab::MVAWrapperBase::pAccumulate ( std::vector< art::Ptr< T >> const &  items,
std::vector< FeatureVector< N >> const &  outs,
std::array< char, N > const &  mask 
) const
protectedinherited

Definition at line 285 of file MVAWrapperBase.h.

289 {
290  size_t n_groups = 0;
291  std::unordered_map<char, size_t> label2group;
292  std::vector<size_t> nb_entries;
293  std::array<int, N> groupidx;
294  for (size_t i = 0; i < N; ++i) {
295  int idx = -1;
296  if (mask[i] >= 0) {
297  auto search = label2group.find(mask[i]);
298  if (search == label2group.end()) {
299  idx = n_groups;
300  label2group[mask[i]] = idx;
301  nb_entries.push_back(0);
302  ++n_groups;
303  }
304  else {
305  idx = search->second;
306  nb_entries[idx]++;
307  }
308  }
309  groupidx[i] = idx;
310  }
311 
312  std::array<double, N> acc;
313  acc.fill(0);
314 
315  float pmin = 1.0e-6, pmax = 1.0 - pmin;
316  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
317 
318  for (auto const& ptr : items) {
319  auto const& vout = outs[ptr.key()];
320  for (size_t i = 0; i < vout.size(); ++i) {
321  if (groupidx[i] < 0) continue;
322 
323  float v;
324  if (vout[i] < pmin)
325  v = log_pmin;
326  else if (vout[i] > pmax)
327  v = log_pmax;
328  else
329  v = std::log(vout[i]);
330 
331  acc[i] += v;
332  }
333  }
334 
335  if (!items.empty()) {
336  std::vector<double> totp(n_groups, 0.0);
337  for (size_t i = 0; i < N; ++i) {
338  if (groupidx[i] >= 0) {
339  acc[i] = exp(acc[i] / items.size());
340  totp[groupidx[i]] += acc[i];
341  }
342  }
343  for (size_t i = 0; i < N; ++i) {
344  if (groupidx[i] >= 0) { acc[i] /= totp[groupidx[i]]; }
345  }
346  }
347  else {
348  for (size_t i = 0; i < N; ++i) {
349  if (groupidx[i] >= 0) { acc[i] = 1 / nb_entries[groupidx[i]]; }
350  }
351  }
352 
353  std::array<float, N> result;
354  for (size_t i = 0; i < N; ++i)
355  result[i] = acc[i];
356  return result;
357 }
template<size_t N>
template<class T >
void anab::FVectorWriter< N >::produces_using ( )
inherited

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

References anab::FVectorWriter< N >::dataTypeRegistered(), anab::FVectorWriter< N >::fCollector, anab::FVectorWriter< N >::fInstanceName, anab::FVectorWriter< N >::fIsDescriptionRegistered, anab::FVectorWriter< N >::fRegisteredDataTypes, anab::FVectorWrapperBase::getProductName(), and art::ProducesCollector::produces().

Referenced by hit::DPRawHitFinder::DPRawHitFinder(), nnet::EmTrackClusterId::EmTrackClusterId(), nnet::EmTrackClusterId2out::EmTrackClusterId2out(), nnet::EmTrackMichelId::EmTrackMichelId(), and anab::FVectorWriter< 4 >::FVectorWriter().

342 {
343  std::string dataName = getProductName(typeid(T));
344  if (dataTypeRegistered(dataName)) {
345  throw cet::exception("FVectorWriter")
346  << "Type " << dataName << "was already registered." << std::endl;
347  }
348 
350  fCollector.produces<std::vector<anab::FVecDescription<N>>>(fInstanceName);
352  }
353 
354  fCollector.produces<std::vector<anab::FeatureVector<N>>>(fInstanceName + dataName);
355  fRegisteredDataTypes.push_back(dataName);
356 }
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:174
std::vector< std::string > fRegisteredDataTypes
Definition: MVAWriter.h:177
bool fIsDescriptionRegistered
Definition: MVAWriter.h:178
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:330
std::string fInstanceName
Definition: MVAWriter.h:175
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
template<size_t N>
void anab::FVectorWriter< N >::saveOutputs ( art::Event evt)
inherited

Check consistency and save all the results in the event.

Definition at line 404 of file MVAWriter.h.

References anab::FVectorWriter< N >::clearEventData(), anab::FVectorWriter< N >::descriptionExists(), util::empty(), 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(), nnet::EmTrack< N >::produce(), nnet::EmTrackClusterId2out::produce(), nnet::EmTrackClusterId::produce(), nnet::EmTrackMichelId::produce(), and anab::FVectorWriter< 4 >::setDataTag().

405 {
406  for (auto const& n : fRegisteredDataTypes) {
407  if (!descriptionExists(n)) {
408  throw cet::exception("FVectorWriter")
409  << "No FVecDescription<N> prepared for type " << n << std::endl;
410  }
411  }
412 
413  if (fVectors.size() != fDescriptions->size()) {
414  throw cet::exception("FVectorWriter")
415  << "FVecDescription<N> vector length not equal to the number of FeatureVector<N> vectors"
416  << std::endl;
417  }
418 
419  for (size_t i = 0; i < fVectors.size(); ++i) {
420  auto const& outInstName = (*fDescriptions)[i].outputInstance();
421  if ((*fDescriptions)[i].dataTag().empty()) {
422  throw cet::exception("FVectorWriter")
423  << "FVecDescription<N> reco data tag not set for " << outInstName << std::endl;
424  }
425  evt.put(std::move(fVectors[i]), outInstName);
426  }
427  evt.put(std::move(fDescriptions), fInstanceName);
428  clearEventData();
429 }
bool descriptionExists(const std::string &tname) const
Check if the containers for results prepared for "tname" data type are ready.
Definition: MVAWriter.h:360
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
std::vector< std::string > fRegisteredDataTypes
Definition: MVAWriter.h:177
std::string fInstanceName
Definition: MVAWriter.h:175
Char_t n[5]
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
Definition: MVAWriter.h:182
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:109
template<size_t N>
void anab::FVectorWriter< N >::setDataTag ( FVector_ID  id,
art::InputTag const &  dataTag 
)
inlineinherited

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

Definition at line 114 of file MVAWriter.h.

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

Definition at line 214 of file MVAWriter.h.

Referenced by nnet::EmTrack< N >::classify_hits(), nnet::EmTrack< N >::make_tracks(), nnet::EmTrackClusterId2out::produce(), nnet::EmTrackClusterId::produce(), and nnet::EmTrackMichelId::produce().

215  {
216  FVectorWriter<N>::setVector(id, key, values);
217  }
void setVector(FVector_ID id, size_t key, std::array< float, N > const &values)
Definition: MVAWriter.h:62
template<size_t N>
void anab::MVAWriter< N >::setOutput ( FVector_ID  id,
size_t  key,
std::array< double, N > const &  values 
)
inline

Definition at line 218 of file MVAWriter.h.

219  {
220  FVectorWriter<N>::setVector(id, key, values);
221  }
void setVector(FVector_ID id, size_t key, std::array< float, N > const &values)
Definition: MVAWriter.h:62
template<size_t N>
void anab::MVAWriter< N >::setOutput ( FVector_ID  id,
size_t  key,
std::vector< float > const &  values 
)
inline

Definition at line 222 of file MVAWriter.h.

223  {
224  FVectorWriter<N>::setVector(id, key, values);
225  }
void setVector(FVector_ID id, size_t key, std::array< float, N > const &values)
Definition: MVAWriter.h:62
template<size_t N>
void anab::MVAWriter< N >::setOutput ( FVector_ID  id,
size_t  key,
std::vector< double > const &  values 
)
inline

Definition at line 226 of file MVAWriter.h.

227  {
228  FVectorWriter<N>::setVector(id, key, values);
229  }
void setVector(FVector_ID id, size_t key, std::array< float, N > const &values)
Definition: MVAWriter.h:62
template<size_t N>
void anab::FVectorWriter< N >::setVector ( FVector_ID  id,
size_t  key,
std::array< float, N > const &  values 
)
inlineinherited

Definition at line 62 of file MVAWriter.h.

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

63  {
64  (*(fVectors[id]))[key] = values;
65  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
template<size_t N>
void anab::FVectorWriter< N >::setVector ( FVector_ID  id,
size_t  key,
std::array< double, N > const &  values 
)
inlineinherited

Definition at line 66 of file MVAWriter.h.

67  {
68  (*(fVectors[id]))[key] = values;
69  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
template<size_t N>
void anab::FVectorWriter< N >::setVector ( FVector_ID  id,
size_t  key,
std::vector< float > const &  values 
)
inlineinherited

Definition at line 70 of file MVAWriter.h.

71  {
72  (*(fVectors[id]))[key] = values;
73  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
template<size_t N>
void anab::FVectorWriter< N >::setVector ( FVector_ID  id,
size_t  key,
std::vector< double > const &  values 
)
inlineinherited

Definition at line 74 of file MVAWriter.h.

75  {
76  (*(fVectors[id]))[key] = values;
77  }
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
Definition: MVAWriter.h:170
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
template<size_t N>
size_t anab::FVectorWriter< N >::size ( FVector_ID  id) const
inlineinherited

Get the number of contained feature vectors.

Definition at line 123 of file MVAWriter.h.

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

Member Data Documentation


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