LArSoft  v06_85_00
Liquid Argon Software toolkit - http://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::EDProducer *module, 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 163 of file MVAWriter.h.

Constructor & Destructor Documentation

template<size_t N>
anab::MVAWriter< N >::MVAWriter ( art::EDProducer module,
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 172 of file MVAWriter.h.

172  :
173  FVectorWriter<N>(module, name)
174  { }

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

References anab::FVectorWriter< N >::addVector().

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

Definition at line 182 of file MVAWriter.h.

References anab::FVectorWriter< N >::addVector().

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

Definition at line 183 of file MVAWriter.h.

References anab::FVectorWriter< N >::addVector().

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

Definition at line 184 of file MVAWriter.h.

References anab::FVectorWriter< N >::addVector().

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

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 
)
inlineinherited

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 
)
inlineinherited

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 
)
inlineinherited

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

191  { return pAccumulate<T, N>(items, *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()])); }
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::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 198 of file MVAWriter.h.

200  { return pAccumulate<T, N>(items, weights, *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()])); }
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::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 207 of file MVAWriter.h.

209  { return pAccumulate<T, N>(items, fweight, *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()])); }
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::MVAWriter< N >::getOutput ( std::vector< art::Ptr< T > > const &  items,
std::function< float(art::Ptr< T > const &)>  fweight 
) const
inline

Definition at line 212 of file MVAWriter.h.

214  { return pAccumulate<T, N>(items, fweight, *(FVectorWriter<N>::fVectors[FVectorWriter<N>::template getProductID<T>()])); }
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::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 218 of file MVAWriter.h.

218 { return FVectorWriter<N>::template getVector<T>(key); }
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 222 of file MVAWriter.h.

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

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
inlineinherited

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
inlineinherited

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,"") 
)
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(), 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, "") 
)
inlineinherited

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, "") 
)
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 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, ""))
inlineinherited

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 
)
inherited

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
inlineinherited

Get the length of a single feature vector.

Definition at line 94 of file MVAWriter.h.

94 { 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 80 of file MVAWrapperBase.h.

References trkf::fill().

83 {
84  std::array<double, N> acc;
85  acc.fill(0);
86 
87  float pmin = 1.0e-6, pmax = 1.0 - pmin;
88  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
89 
90  for (auto const & ptr : items)
91  {
92  auto const & vout = outs[ptr.key()];
93  for (size_t i = 0; i < vout.size(); ++i)
94  {
95  float v;
96  if (vout[i] < pmin) v = log_pmin;
97  else if (vout[i] > pmax) v = log_pmax;
98  else v = std::log(vout[i]);
99 
100  acc[i] += v;
101  }
102  }
103 
104  if (!items.empty())
105  {
106  double totp = 0.0;
107  for (size_t i = 0; i < N; ++i)
108  {
109  acc[i] = exp(acc[i] / items.size());
110  totp += acc[i];
111  }
112  for (size_t i = 0; i < N; ++i)
113  {
114  acc[i] /= totp;
115  }
116  }
117  else std::fill(acc.begin(), acc.end(), 1.0 / N);
118 
119 
120  std::array<float, N> result;
121  for (size_t i = 0; i < N; ++i) result[i] = acc[i];
122  return result;
123 }
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 127 of file MVAWrapperBase.h.

References trkf::fill(), and w.

130 {
131  std::array<double, N> acc;
132  acc.fill(0);
133 
134  float pmin = 1.0e-6, pmax = 1.0 - pmin;
135  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
136  double totw = 0.0;
137 
138  for (size_t k = 0; k < items.size(); ++k)
139  {
140  auto const & ptr = items[k];
141  float w = weights[k];
142 
143  if (w == 0) continue;
144 
145  auto const & vout = outs[ptr.key()];
146  for (size_t i = 0; i < vout.size(); ++i)
147  {
148  float v;
149  if (vout[i] < pmin) v = log_pmin;
150  else if (vout[i] > pmax) v = log_pmax;
151  else v = std::log(vout[i]);
152 
153  acc[i] += w * v;
154  }
155  totw += w;
156  }
157 
158  if (!items.empty())
159  {
160  double totp = 0.0;
161  for (size_t i = 0; i < N; ++i)
162  {
163  acc[i] = exp(acc[i] / totw);
164  totp += acc[i];
165  }
166  for (size_t i = 0; i < N; ++i)
167  {
168  acc[i] /= totp;
169  }
170  }
171  else std::fill(acc.begin(), acc.end(), 1.0 / N);
172 
173 
174  std::array<float, N> result;
175  for (size_t i = 0; i < N; ++i) result[i] = acc[i];
176  return result;
177 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Float_t w
Definition: plot.C:23
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 181 of file MVAWrapperBase.h.

References trkf::fill(), and w.

184 {
185  std::array<double, N> acc;
186  acc.fill(0);
187 
188  float pmin = 1.0e-6, pmax = 1.0 - pmin;
189  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
190  double totw = 0.0;
191 
192  for (size_t k = 0; k < items.size(); ++k)
193  {
194  auto const & ptr = items[k];
195  float w = fweight(*ptr);
196 
197  if (w == 0) continue;
198 
199  auto const & vout = outs[ptr.key()];
200  for (size_t i = 0; i < vout.size(); ++i)
201  {
202  float v;
203  if (vout[i] < pmin) v = log_pmin;
204  else if (vout[i] > pmax) v = log_pmax;
205  else v = std::log(vout[i]);
206 
207  acc[i] += w * v;
208  }
209  totw += w;
210  }
211 
212  if (!items.empty())
213  {
214  double totp = 0.0;
215  for (size_t i = 0; i < N; ++i)
216  {
217  acc[i] = exp(acc[i] / totw);
218  totp += acc[i];
219  }
220  for (size_t i = 0; i < N; ++i)
221  {
222  acc[i] /= totp;
223  }
224  }
225  else std::fill(acc.begin(), acc.end(), 1.0 / N);
226 
227 
228  std::array<float, N> result;
229  for (size_t i = 0; i < N; ++i) result[i] = acc[i];
230  return result;
231 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Float_t w
Definition: plot.C:23
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 235 of file MVAWrapperBase.h.

References trkf::fill(), and w.

238 {
239  std::array<double, N> acc;
240  acc.fill(0);
241 
242  float pmin = 1.0e-6, pmax = 1.0 - pmin;
243  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
244  double totw = 0.0;
245 
246  for (size_t k = 0; k < items.size(); ++k)
247  {
248  auto const & ptr = items[k];
249  float w = fweight(ptr);
250 
251  if (w == 0) continue;
252 
253  auto const & vout = outs[ptr.key()];
254  for (size_t i = 0; i < vout.size(); ++i)
255  {
256  float v;
257  if (vout[i] < pmin) v = log_pmin;
258  else if (vout[i] > pmax) v = log_pmax;
259  else v = std::log(vout[i]);
260 
261  acc[i] += w * v;
262  }
263  totw += w;
264  }
265 
266  if (!items.empty())
267  {
268  double totp = 0.0;
269  for (size_t i = 0; i < N; ++i)
270  {
271  acc[i] = exp(acc[i] / totw);
272  totp += acc[i];
273  }
274  for (size_t i = 0; i < N; ++i)
275  {
276  acc[i] /= totp;
277  }
278  }
279  else std::fill(acc.begin(), acc.end(), 1.0 / N);
280 
281 
282  std::array<float, N> result;
283  for (size_t i = 0; i < N; ++i) result[i] = acc[i];
284  return result;
285 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Float_t w
Definition: plot.C:23
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 293 of file MVAWrapperBase.h.

297 {
298  size_t n_groups = 0;
299  std::unordered_map<char, size_t> label2group;
300  std::vector<size_t> nb_entries;
301  std::array<int, N> groupidx;
302  for (size_t i = 0; i < N; ++i)
303  {
304  int idx = -1;
305  if (mask[i] >= 0)
306  {
307  auto search = label2group.find(mask[i]);
308  if (search == label2group.end())
309  {
310  idx = n_groups;
311  label2group[mask[i]] = idx;
312  nb_entries.push_back(0);
313  ++n_groups;
314  }
315  else
316  {
317  idx = search->second;
318  nb_entries[idx]++;
319  }
320  }
321  groupidx[i] = idx;
322  }
323 
324  std::array<double, N> acc;
325  acc.fill(0);
326 
327  float pmin = 1.0e-6, pmax = 1.0 - pmin;
328  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
329 
330  for (auto const & ptr : items)
331  {
332  auto const & vout = outs[ptr.key()];
333  for (size_t i = 0; i < vout.size(); ++i)
334  {
335  if (groupidx[i] < 0) continue;
336 
337  float v;
338  if (vout[i] < pmin) v = log_pmin;
339  else if (vout[i] > pmax) v = log_pmax;
340  else v = std::log(vout[i]);
341 
342  acc[i] += v;
343  }
344  }
345 
346  if (!items.empty())
347  {
348  std::vector<double> totp(n_groups, 0.0);
349  for (size_t i = 0; i < N; ++i)
350  {
351  if (groupidx[i] >= 0)
352  {
353  acc[i] = exp(acc[i] / items.size());
354  totp[groupidx[i]] += acc[i];
355  }
356  }
357  for (size_t i = 0; i < N; ++i)
358  {
359  if (groupidx[i] >= 0) { acc[i] /= totp[groupidx[i]]; }
360  }
361  }
362  else
363  {
364  for (size_t i = 0; i < N; ++i)
365  {
366  if (groupidx[i] >= 0) { acc[i] = 1 / nb_entries[groupidx[i]]; }
367  }
368  }
369 
370  std::array<float, N> result;
371  for (size_t i = 0; i < N; ++i) result[i] = acc[i];
372  return result;
373 }
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 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)
inherited

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 
)
inlineinherited

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::MVAWriter< N >::setOutput ( FVector_ID  id,
size_t  key,
std::array< float, N > const &  values 
)
inline

Definition at line 176 of file MVAWriter.h.

References anab::FVectorWriter< N >::setVector().

176 { FVectorWriter<N>::setVector(id, key, values); }
void setVector(FVector_ID id, size_t key, std::array< float, N > const &values)
Definition: MVAWriter.h:59
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 177 of file MVAWriter.h.

References anab::FVectorWriter< N >::setVector().

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

Definition at line 178 of file MVAWriter.h.

References anab::FVectorWriter< N >::setVector().

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

Definition at line 179 of file MVAWriter.h.

References anab::FVectorWriter< N >::setVector().

179 { FVectorWriter<N>::setVector(id, key, values); }
void setVector(FVector_ID id, size_t key, std::array< float, N > const &values)
Definition: MVAWriter.h:59
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 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 
)
inlineinherited

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 
)
inlineinherited

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 
)
inlineinherited

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
inlineinherited

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

Member Data Documentation


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