LArSoft  v06_85_00
Liquid Argon Software toolkit - http://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 #include <iostream>
15 #include <iomanip>
16 
17 #include <array>
18 #include <vector>
19 
20 namespace anab {
21 
24 template <size_t N>
26 public:
27 
29 
30  // MUST UPDATE WHEN CLASS IS CHANGED!
31  static short Class_Version() { return 10; }
32 
33 private:
34  float fData[N];
35 
36 public:
37 
38  FeatureVector(float init) { set(init); }
39  FeatureVector(std::array<float, N> const & values) { set(values); }
40  FeatureVector(std::array<double, N> const & values) { set(values); }
41  FeatureVector(std::vector<float> const & values) { set(values); }
42  FeatureVector(std::vector<double> const & values) { set(values); }
43 
45  FeatureVector(float const * values) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
46  FeatureVector(double const * values) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
47 
49  FeatureVector& operator =(float init) { set(init); return *this; }
50  FeatureVector& operator =(std::array<float, N> const & values) { set(values); return *this; }
51  FeatureVector& operator =(std::array<double, N> const & values) { set(values); return *this; }
52  FeatureVector& operator =(std::vector<float> const & values) { set(values); return *this; }
53  FeatureVector& operator =(std::vector<double> const & values) { set(values); return *this; }
54 
55  friend std::ostream& operator<< (std::ostream &o, FeatureVector const& a)
56  {
57  o << "FeatureVector values:";
58  for (size_t i = 0; i < N; ++i) { o << " " << a.fData[i]; }
59  o << std::endl;
60  return o;
61  }
62 
63  size_t size() const { return N; }
64 
65  float at(size_t index) const
66  {
67  if (index < N) { return fData[index]; }
68  else { throw cet::exception("FeatureVector") << "Index out of range: " << index << std::endl; }
69  }
70 
71  float operator[] (size_t index) const { return fData[index]; }
72 
75  //const std::array<float, N> & data() const { return fData; }
76 
77 private:
78 
79  void set(float init) { for (size_t i = 0; i < N; ++i) { fData[i] = init; } }
80  void set(std::array<float, N> const & values) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
81  void set(std::array<double, N> const & values) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
82  void set(std::vector<float> const & values)
83  {
84  if (values.size() == N) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
85  else { throw cet::exception("FeatureVector") << "Expected length: " << N << ", provided: " << values.size() << std::endl; }
86  }
87  void set(std::vector<double> const & values)
88  {
89  if (values.size() == N) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
90  else { throw cet::exception("FeatureVector") << "Expected length: " << N << ", provided: " << values.size() << std::endl; }
91  }
92 
93 
94 }; // class FeatureVector
95 
98 template <size_t N>
100 public:
101 
103 
104  // MUST UPDATE WHEN CLASS IS CHANGED!
105  static short Class_Version() { return 10; }
106 
107 private:
108  std::string fDataTag;
109  std::string fOutputInstance;
110  std::string fOutputNames[N];
111 
112 public:
113 
114  MVADescription(std::string const & dataTag, std::string const & outputInstance,
115  std::vector< std::string > const & outputNames = std::vector< std::string >(N, "")) :
116  fDataTag(dataTag),
117  fOutputInstance(outputInstance)
118  {
119  setOutputNames(outputNames);
120  }
121 
122  MVADescription(std::string const & outputInstance,
123  std::vector< std::string > const & outputNames = std::vector< std::string >(N, "")) :
124  fDataTag(""), // initialize with empty data tag, so it should be assigned later
125  fOutputInstance(outputInstance)
126  {
127  setOutputNames(outputNames);
128  }
129 
130  friend std::ostream& operator<< (std::ostream &o, MVADescription const& a)
131  {
132  o << "MVADescription: prepared for " << a.fDataTag << ", instance name " << a.fOutputInstance << ", " << N << " outputs:" << std::endl;
133  for (size_t i = 0; i < N; ++i) { o << " " << a.fOutputNames[i] << std::endl; }
134  return o;
135  }
136 
137  const std::string & outputInstance() const { return fOutputInstance; }
138 
139  size_t size() const { return N; }
140 
141  const std::string & dataTag() const { return fDataTag; }
142  void setDataTag(const std::string & tag)
143  {
144  if (fDataTag.empty()) { fDataTag = tag; }
145  else { throw cet::exception("MVADescription") << "Data tag already assigned: " << fDataTag << std::endl; }
146  }
147 
148  const std::string & outputName(size_t index) const
149  {
150  if (index < N) { return fOutputNames[index]; }
151  else { throw cet::exception("MVADescription") << "Index out of range: " << index << std::endl; }
152  }
153  void setOutputNames(std::vector< std::string > const & outputNames)
154  {
155  if (outputNames.size() <= N) { for (size_t i = 0; i < outputNames.size(); ++i) { fOutputNames[i] = outputNames[i]; } }
156  else { throw cet::exception("FeatureVector") << "Expected max length of outputNames: " << N << ", provided: " << outputNames.size() << std::endl; }
157  }
158 
159  int getIndex(const std::string & name) const
160  {
161  for (size_t i = 0; i < N; ++i) { if (fOutputNames[i] == name) { return i; } }
162  return -1; // not found
163  }
164 
165 }; // class MVADescription
166 
167 template <size_t N>
169 
170 } // namespace anab
171 
172 #endif //ANAB_FEATUREVECTORS
173 
void setDataTag(const std::string &tag)
Definition: MVAOutput.h:142
static short Class_Version()
Definition: MVAOutput.h:105
friend std::ostream & operator<<(std::ostream &o, FeatureVector const &a)
Definition: MVAOutput.h:55
const std::string & dataTag() const
Definition: MVAOutput.h:141
void setOutputNames(std::vector< std::string > const &outputNames)
Definition: MVAOutput.h:153
std::string fOutputInstance
Instance name of the feature vector collection.
Definition: MVAOutput.h:109
static short Class_Version()
Definition: MVAOutput.h:31
int getIndex(const std::string &name) const
Definition: MVAOutput.h:159
FeatureVector(float init)
Definition: MVAOutput.h:38
FeatureVector(float const *values)
If you really have to use C arrays:
Definition: MVAOutput.h:45
size_t size() const
Definition: MVAOutput.h:63
FeatureVector(std::vector< double > const &values)
Definition: MVAOutput.h:42
float fData[N]
Vector values.
Definition: MVAOutput.h:34
const std::string & outputInstance() const
Definition: MVAOutput.h:137
const std::string & outputName(size_t index) const
Definition: MVAOutput.h:148
MVADescription(std::string const &outputInstance, std::vector< std::string > const &outputNames=std::vector< std::string >(N,""))
Definition: MVAOutput.h:122
FeatureVector(std::vector< float > const &values)
Definition: MVAOutput.h:41
std::string fOutputNames[N]
Feature vector entries names/meaning.
Definition: MVAOutput.h:110
float at(size_t index) const
Definition: MVAOutput.h:65
FeatureVector(std::array< double, N > const &values)
Definition: MVAOutput.h:40
FeatureVector(std::array< float, N > const &values)
Definition: MVAOutput.h:39
MVADescription(std::string const &dataTag, std::string const &outputInstance, std::vector< std::string > const &outputNames=std::vector< std::string >(N,""))
Definition: MVAOutput.h:114
float operator[](size_t index) const
Definition: MVAOutput.h:71
FeatureVector(double const *values)
Definition: MVAOutput.h:46
std::string fDataTag
Tag of the reco data products (art::InputTag format)
Definition: MVAOutput.h:108
size_t size() const
Definition: MVAOutput.h:139
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:49