9 #ifndef ANAB_MVAWRITER_H
10 #define ANAB_MVAWRITER_H
12 #include "art/Framework/Core/ProducesCollector.h"
13 #include "art/Framework/Principal/Event.h"
14 #include "canvas/Utilities/InputTag.h"
51 std::vector< std::string >
const &
names = std::vector< std::string >(
N,
""));
55 std::vector< std::string >
const &
names = std::vector< std::string >(
N,
""))
56 {
return initOutputs<T>(dataTag.encode(), dataSize,
names); }
70 std::vector< std::string >
const &
names = std::vector< std::string >(
N,
""))
71 {
return initOutputs<T>(dataTag.encode(), 0,
names); }
75 std::vector< std::string >
const &
names = std::vector< std::string >(
N,
""))
76 {
return initOutputs<T>(std::string(
""), 0,
names); }
84 void setDataTag(
FVector_ID id, art::InputTag
const & dataTag) { (*fDescriptions)[id].setDataTag(dataTag.encode()); }
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];
107 std::array<float, N>
getVector(art::Ptr<T>
const & item)
const
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];
117 o <<
"FVectorWriter for " << a.
fInstanceName <<
", " <<
N <<
" outputs";
120 o <<
", ready to write results made for:" << std::endl;
123 else { o <<
", nothing registered for writing to the events" << std::endl; }
131 std::vector< std::unique_ptr< std::vector< anab::FeatureVector<N> > > >
fVectors;
198 std::vector<float>
const & weights)
const
207 std::function<
float (T
const &)> fweight)
const
212 std::function<
float (art::Ptr<T>
const &)> fweight)
const
234 auto const & ti =
typeid(T);
235 auto search = fTypeHashToID.find(getProductHash(ti));
236 if (search != fTypeHashToID.end()) {
return search->second; }
239 throw cet::exception(
"FVectorWriter") <<
"Feature vectors not initialized for product " << getProductName(ti) << std::endl;
247 for (
auto const &
s : fRegisteredDataTypes)
249 if (
s == dname) {
return true; }
259 std::string dataName = getProductName(
typeid(T));
260 if (dataTypeRegistered(dataName))
262 throw cet::exception(
"FVectorWriter") <<
"Type " << dataName <<
"was already registered." << std::endl;
265 if (!fIsDescriptionRegistered)
267 fCollector.produces< std::vector< anab::FVecDescription<N> > >(fInstanceName);
268 fIsDescriptionRegistered =
true;
271 fCollector.produces< std::vector< anab::FeatureVector<N> > >(fInstanceName + dataName);
272 fRegisteredDataTypes.push_back(dataName);
279 if (!fDescriptions)
return false;
281 std::string
n = fInstanceName +
tname;
282 for (
auto const & d : *fDescriptions)
284 if (d.outputInstance() ==
n) {
return true; }
293 std::string
const & dataTag,
size_t dataSize,
294 std::vector< std::string >
const &
names)
296 size_t dataHash = getProductHash(
typeid(T));
297 std::string dataName = getProductName(
typeid(T));
299 if (!dataTypeRegistered(dataName))
301 throw cet::exception(
"FVectorWriter") <<
"Type " << dataName <<
"not registered with produces_using() function." << std::endl;
306 fDescriptions = std::make_unique< std::vector< anab::FVecDescription<N> > >();
308 else if (descriptionExists(dataName))
310 throw cet::exception(
"FVectorWriter") <<
"FVecDescription<N> already initialized for " << dataName << std::endl;
312 fDescriptions->emplace_back(dataTag, fInstanceName + dataName, names);
316 fTypeHashToID[dataHash] = id;
327 for (
auto const &
n : fRegisteredDataTypes)
329 if (!descriptionExists(
n))
331 throw cet::exception(
"FVectorWriter") <<
"No FVecDescription<N> prepared for type " <<
n << std::endl;
335 if (fVectors.size() != fDescriptions->size())
337 throw cet::exception(
"FVectorWriter") <<
"FVecDescription<N> vector length not equal to the number of FeatureVector<N> vectors" << std::endl;
340 for (
size_t i = 0; i < fVectors.size(); ++i)
342 auto const & outInstName = (*fDescriptions)[i].outputInstance();
343 if ((*fDescriptions)[i].dataTag().
empty())
345 throw cet::exception(
"FVectorWriter") <<
"FVecDescription<N> reco data tag not set for " << outInstName << std::endl;
347 evt.put(std::move(fVectors[i]), outInstName);
349 evt.put(std::move(fDescriptions), fInstanceName);
354 #endif //ANAB_MVAREADER
void addVector(FVector_ID id, std::vector< float > const &values)
void setOutput(FVector_ID id, size_t key, std::array< double, N > const &values)
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().
std::unordered_map< size_t, FVector_ID > fTypeHashToID
size_t length() const
Get the length of a single feature vector.
void setVector(FVector_ID id, size_t key, std::vector< double > const &values)
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items, std::function< float(art::Ptr< T > const &)> fweight) const
void setOutput(FVector_ID id, size_t key, std::array< float, N > const &values)
size_t size(FVector_ID id) const
Get the number of contained feature vectors.
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items, std::vector< float > const &weights) const
void setVector(FVector_ID id, size_t key, std::array< double, N > const &values)
void setOutput(FVector_ID id, size_t key, std::vector< double > const &values)
bool descriptionExists(const std::string &tname) const
Check if the containers for results prepared for "tname" data type are ready.
size_t FVector_ID
Index to the MVA output / FeatureVector collection, used when result vectors are added or set...
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)
MVAWriter(art::ProducesCollector &collector, const char *name="")
void addVector(FVector_ID id, std::vector< double > const &values)
std::array< float, N > getVector(size_t key) const
Get copy of the feature vector for the type T, at index "key".
FVector_ID initOutputs(art::InputTag const &dataTag, size_t dataSize, std::vector< std::string > const &names=std::vector< std::string >(N,""))
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
FVector_ID initOutputs(std::string const &dataTag, size_t dataSize, std::vector< std::string > const &names=std::vector< std::string >(N,""))
Helper functions for MVAReader and MVAWriter wrappers.
art::ProducesCollector & fCollector
void addOutput(FVector_ID id, std::array< double, N > const &values)
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().
void addOutput(FVector_ID id, std::vector< double > const &values)
std::unique_ptr< std::vector< anab::FVecDescription< N > > > fDescriptions
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items) const
void saveOutputs(art::Event &evt)
Check consistency and save all the results in the event.
std::array< float, N > getOutput(std::vector< art::Ptr< T > > const &items, std::function< float(T const &)> fweight) const
void addOutput(FVector_ID id, std::array< float, N > const &values)
static const std::vector< std::string > names
Helper functions for MVAReader/Writer and FVecReader/Writer wrappers.
FVector_ID getProductID() const
std::vector< std::string > fRegisteredDataTypes
bool fIsDescriptionRegistered
then echo File list $list not found else cat $list while read file do echo $file sed s
FVector_ID initOutputs(art::InputTag const &dataTag, 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)
process_name largeant stream1 can override from command line with o or output physics producers generator N
std::vector< std::unique_ptr< std::vector< anab::FeatureVector< N > > > > fVectors
void setOutput(FVector_ID id, size_t key, std::vector< float > const &values)
bool dataTypeRegistered(const std::string &dname) const
Check if the the writer is configured to write results for data product type name.
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...
std::string fInstanceName
std::array< float, N > getOutput(size_t key) const
Get copy of the MVA output vector for the type T, at index "key".
void setVector(FVector_ID id, size_t key, std::vector< float > const &values)
void addOutput(FVector_ID id, std::vector< float > const &values)
bool empty(FixedBins< T, C > const &) noexcept
FVectorWriter(art::ProducesCollector &collector, const char *name="")
void addVector(FVector_ID id, std::array< double, N > const &values)
friend std::ostream & operator<<(std::ostream &o, FVectorWriter const &a)