LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
MVAOutput.h
Go to the documentation of this file.
1 // \version
3 //
4 // \brief Data products to hold feature vectors and their metadata, see MVAReader/MVAWriter wrappers for convenient usage.
5 //
6 // \author robert.sulej@cern.ch
7 //
9 #ifndef ANAB_FEATUREVECTORS_H
10 #define ANAB_FEATUREVECTORS_H
11 
12 #include "cetlib_except/exception.h"
13 #include <iosfwd>
14 
15 #include <array>
16 #include <string>
17 #include <vector>
18 
19 namespace anab {
20 
23  template <size_t N>
24  class FeatureVector {
25  public:
27 
28  // MUST UPDATE WHEN CLASS IS CHANGED!
29  static short Class_Version() { return 10; }
30 
31  private:
32  float fData[N];
33 
34  public:
35  FeatureVector(float init) { set(init); }
36  FeatureVector(std::array<float, N> const& values) { set(values); }
37  FeatureVector(std::array<double, N> const& values) { set(values); }
38  FeatureVector(std::vector<float> const& values) { set(values); }
39  FeatureVector(std::vector<double> const& values) { set(values); }
40 
42  FeatureVector(float const* values)
43  {
44  for (size_t i = 0; i < N; ++i) {
45  fData[i] = values[i];
46  }
47  }
48  FeatureVector(double const* values)
49  {
50  for (size_t i = 0; i < N; ++i) {
51  fData[i] = values[i];
52  }
53  }
54 
56  FeatureVector& operator=(float init)
57  {
58  set(init);
59  return *this;
60  }
61  FeatureVector& operator=(std::array<float, N> const& values)
62  {
63  set(values);
64  return *this;
65  }
66  FeatureVector& operator=(std::array<double, N> const& values)
67  {
68  set(values);
69  return *this;
70  }
71  FeatureVector& operator=(std::vector<float> const& values)
72  {
73  set(values);
74  return *this;
75  }
76  FeatureVector& operator=(std::vector<double> const& values)
77  {
78  set(values);
79  return *this;
80  }
81 
82  friend std::ostream& operator<<(std::ostream& o, FeatureVector const& a)
83  {
84  o << "FeatureVector values:";
85  for (size_t i = 0; i < N; ++i) {
86  o << " " << a.fData[i];
87  }
88  o << std::endl;
89  return o;
90  }
91 
92  size_t size() const { return N; }
93 
94  float at(size_t index) const
95  {
96  if (index < N) { return fData[index]; }
97  else {
98  throw cet::exception("FeatureVector") << "Index out of range: " << index << std::endl;
99  }
100  }
101 
102  float operator[](size_t index) const { return fData[index]; }
103 
106  //const std::array<float, N> & data() const { return fData; }
107 
108  private:
109  void set(float init)
110  {
111  for (size_t i = 0; i < N; ++i) {
112  fData[i] = init;
113  }
114  }
115  void set(std::array<float, N> const& values)
116  {
117  for (size_t i = 0; i < N; ++i) {
118  fData[i] = values[i];
119  }
120  }
121  void set(std::array<double, N> const& values)
122  {
123  for (size_t i = 0; i < N; ++i) {
124  fData[i] = values[i];
125  }
126  }
127  void set(std::vector<float> const& values)
128  {
129  if (values.size() == N) {
130  for (size_t i = 0; i < N; ++i) {
131  fData[i] = values[i];
132  }
133  }
134  else {
135  throw cet::exception("FeatureVector")
136  << "Expected length: " << N << ", provided: " << values.size() << std::endl;
137  }
138  }
139  void set(std::vector<double> const& values)
140  {
141  if (values.size() == N) {
142  for (size_t i = 0; i < N; ++i) {
143  fData[i] = values[i];
144  }
145  }
146  else {
147  throw cet::exception("FeatureVector")
148  << "Expected length: " << N << ", provided: " << values.size() << std::endl;
149  }
150  }
151 
152  }; // class FeatureVector
153 
156  template <size_t N>
158  public:
160 
161  // MUST UPDATE WHEN CLASS IS CHANGED!
162  static short Class_Version() { return 10; }
163 
164  private:
165  std::string fDataTag;
166  std::string fOutputInstance;
167  std::string fOutputNames[N];
168 
169  public:
170  MVADescription(std::string const& dataTag,
171  std::string const& outputInstance,
172  std::vector<std::string> const& outputNames = std::vector<std::string>(N, ""))
173  : fDataTag(dataTag), fOutputInstance(outputInstance)
174  {
175  setOutputNames(outputNames);
176  }
177 
178  MVADescription(std::string const& outputInstance,
179  std::vector<std::string> const& outputNames = std::vector<std::string>(N, ""))
180  : fDataTag("")
181  , // initialize with empty data tag, so it should be assigned later
182  fOutputInstance(outputInstance)
183  {
184  setOutputNames(outputNames);
185  }
186 
187  friend std::ostream& operator<<(std::ostream& o, MVADescription const& a)
188  {
189  o << "MVADescription: prepared for " << a.fDataTag << ", instance name " << a.fOutputInstance
190  << ", " << N << " outputs:" << std::endl;
191  for (size_t i = 0; i < N; ++i) {
192  o << " " << a.fOutputNames[i] << std::endl;
193  }
194  return o;
195  }
196 
197  const std::string& outputInstance() const { return fOutputInstance; }
198 
199  size_t size() const { return N; }
200 
201  const std::string& dataTag() const { return fDataTag; }
202  void setDataTag(const std::string& tag)
203  {
204  if (fDataTag.empty()) { fDataTag = tag; }
205  else {
206  throw cet::exception("MVADescription")
207  << "Data tag already assigned: " << fDataTag << std::endl;
208  }
209  }
210 
211  const std::string& outputName(size_t index) const
212  {
213  if (index < N) { return fOutputNames[index]; }
214  else {
215  throw cet::exception("MVADescription") << "Index out of range: " << index << std::endl;
216  }
217  }
218  void setOutputNames(std::vector<std::string> const& outputNames)
219  {
220  if (outputNames.size() <= N) {
221  for (size_t i = 0; i < outputNames.size(); ++i) {
222  fOutputNames[i] = outputNames[i];
223  }
224  }
225  else {
226  throw cet::exception("FeatureVector") << "Expected max length of outputNames: " << N
227  << ", provided: " << outputNames.size() << std::endl;
228  }
229  }
230 
231  int getIndex(const std::string& name) const
232  {
233  for (size_t i = 0; i < N; ++i) {
234  if (fOutputNames[i] == name) { return i; }
235  }
236  return -1; // not found
237  }
238 
239  }; // class MVADescription
240 
241  template <size_t N>
243 
244 } // namespace anab
245 
246 #endif //ANAB_FEATUREVECTORS
void setDataTag(const std::string &tag)
Definition: MVAOutput.h:202
FeatureVector & operator=(std::array< float, N > const &values)
Definition: MVAOutput.h:61
static short Class_Version()
Definition: MVAOutput.h:162
friend std::ostream & operator<<(std::ostream &o, FeatureVector const &a)
Definition: MVAOutput.h:82
const std::string & dataTag() const
Definition: MVAOutput.h:201
void setOutputNames(std::vector< std::string > const &outputNames)
Definition: MVAOutput.h:218
std::string fOutputInstance
Instance name of the feature vector collection.
Definition: MVAOutput.h:166
static short Class_Version()
Definition: MVAOutput.h:29
int getIndex(const std::string &name) const
Definition: MVAOutput.h:231
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
FeatureVector(float init)
Definition: MVAOutput.h:35
FeatureVector(float const *values)
If you really have to use C arrays:
Definition: MVAOutput.h:42
size_t size() const
Definition: MVAOutput.h:92
FeatureVector(std::vector< double > const &values)
Definition: MVAOutput.h:39
float fData[N]
Vector values.
Definition: MVAOutput.h:32
const std::string & outputInstance() const
Definition: MVAOutput.h:197
const std::string & outputName(size_t index) const
Definition: MVAOutput.h:211
MVADescription(std::string const &outputInstance, std::vector< std::string > const &outputNames=std::vector< std::string >(N,""))
Definition: MVAOutput.h:178
FeatureVector(std::vector< float > const &values)
Definition: MVAOutput.h:38
FeatureVector & operator=(std::vector< float > const &values)
Definition: MVAOutput.h:71
std::string fOutputNames[N]
Feature vector entries names/meaning.
Definition: MVAOutput.h:167
float at(size_t index) const
Definition: MVAOutput.h:94
FeatureVector(std::array< double, N > const &values)
Definition: MVAOutput.h:37
FeatureVector(std::array< float, N > const &values)
Definition: MVAOutput.h:36
MVADescription(std::string const &dataTag, std::string const &outputInstance, std::vector< std::string > const &outputNames=std::vector< std::string >(N,""))
Definition: MVAOutput.h:170
float operator[](size_t index) const
Definition: MVAOutput.h:102
FeatureVector(double const *values)
Definition: MVAOutput.h:48
FeatureVector & operator=(std::array< double, N > const &values)
Definition: MVAOutput.h:66
std::string fDataTag
Tag of the reco data products (art::InputTag format)
Definition: MVAOutput.h:165
friend std::ostream & operator<<(std::ostream &o, MVADescription const &a)
Definition: MVAOutput.h:187
size_t size() const
Definition: MVAOutput.h:199
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
FeatureVector & operator=(float init)
Assignment operators, from the same types as constructors.
Definition: MVAOutput.h:56
FeatureVector & operator=(std::vector< double > const &values)
Definition: MVAOutput.h:76