MVAOutput.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // \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 //
8 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 
21 /// Feature vector of size N. Values are saved as 32bit fp's, this is usually enough for the classification purposes
22 /// and the precision one can expect from MVA algorithms.
23 template <size_t N>
25 public:
26 
28 
29  // MUST UPDATE WHEN CLASS IS CHANGED!
30  static short Class_Version() { return 10; }
31 
32 private:
33  float fData[N]; ///< Vector values
34 
35 public:
36 
37  FeatureVector(float init) { set(init); }
38  FeatureVector(std::array<float, N> const & values) { set(values); }
39  FeatureVector(std::array<double, N> const & values) { set(values); }
40  FeatureVector(std::vector<float> const & values) { set(values); }
41  FeatureVector(std::vector<double> const & values) { set(values); }
42 
43  /// If you really have to use C arrays:
44  FeatureVector(float const * values) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
45  FeatureVector(double const * values) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
46 
47  /// Assignment operators, from the same types as constructors.
48  FeatureVector& operator =(float init) { set(init); return *this; }
49  FeatureVector& operator =(std::array<float, N> const & values) { set(values); return *this; }
50  FeatureVector& operator =(std::array<double, N> const & values) { set(values); return *this; }
51  FeatureVector& operator =(std::vector<float> const & values) { set(values); return *this; }
52  FeatureVector& operator =(std::vector<double> const & values) { set(values); return *this; }
53 
54  friend std::ostream& operator<< (std::ostream &o, FeatureVector const& a)
55  {
56  o << "FeatureVector values:";
57  for (size_t i = 0; i < N; ++i) { o << " " << a.fData[i]; }
58  o << std::endl;
59  return o;
60  }
61 
62  size_t size() const { return N; }
63 
64  float at(size_t index) const
65  {
66  if (index < N) { return fData[index]; }
67  else { throw cet::exception("FeatureVector") << "Index out of range: " << index << std::endl; }
68  }
69 
70  float operator[] (size_t index) const { return fData[index]; }
71 
72  /// Access the contained array.
73  /// *** WOULD LIKE TO CHANGE TYPE OF DATA MEMBER TO std::array AND THEN ENABLE THIS FUNCTION ***
74  //const std::array<float, N> & data() const { return fData; }
75 
76 private:
77 
78  void set(float init) { for (size_t i = 0; i < N; ++i) { fData[i] = init; } }
79  void set(std::array<float, N> const & values) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
80  void set(std::array<double, N> const & values) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
81  void set(std::vector<float> const & values)
82  {
83  if (values.size() == N) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
84  else { throw cet::exception("FeatureVector") << "Expected length: " << N << ", provided: " << values.size() << std::endl; }
85  }
86  void set(std::vector<double> const & values)
87  {
88  if (values.size() == N) { for (size_t i = 0; i < N; ++i) { fData[i] = values[i]; } }
89  else { throw cet::exception("FeatureVector") << "Expected length: " << N << ", provided: " << values.size() << std::endl; }
90  }
91 
92 
93 }; // class FeatureVector
94 
95 /// Metadata associated to feature vectors. The idea is to link entire collection of objects to the collection
96 /// of vectors, and add metadata like the meaning of columns in vectors using a single object.
97 template <size_t N>
99 public:
100 
102 
103  // MUST UPDATE WHEN CLASS IS CHANGED!
104  static short Class_Version() { return 10; }
105 
106 private:
107  std::string fDataTag; ///< Tag of the reco data products (art::InputTag format)
108  std::string fOutputInstance; ///< Instance name of the feature vector collection
109  std::string fOutputNames[N]; ///< Feature vector entries names/meaning
110 
111 public:
112 
113  MVADescription(std::string const & dataTag, std::string const & outputInstance,
114  std::vector< std::string > const & outputNames = std::vector< std::string >(N, "")) :
115  fDataTag(dataTag),
116  fOutputInstance(outputInstance)
117  {
118  setOutputNames(outputNames);
119  }
120 
121  MVADescription(std::string const & outputInstance,
122  std::vector< std::string > const & outputNames = std::vector< std::string >(N, "")) :
123  fDataTag(""), // initialize with empty data tag, so it should be assigned later
124  fOutputInstance(outputInstance)
125  {
126  setOutputNames(outputNames);
127  }
128 
129  friend std::ostream& operator<< (std::ostream &o, MVADescription const& a)
130  {
131  o << "MVADescription: prepared for " << a.fDataTag << ", instance name " << a.fOutputInstance << ", " << N << " outputs:" << std::endl;
132  for (size_t i = 0; i < N; ++i) { o << " " << a.fOutputNames[i] << std::endl; }
133  return o;
134  }
135 
136  const std::string & outputInstance() const { return fOutputInstance; }
137 
138  size_t size() const { return N; }
139 
140  const std::string & dataTag() const { return fDataTag; }
141  void setDataTag(const std::string & tag)
142  {
143  if (fDataTag.empty()) { fDataTag = tag; }
144  else { throw cet::exception("MVADescription") << "Data tag already assigned: " << fDataTag << std::endl; }
145  }
146 
147  const std::string & outputName(size_t index) const
148  {
149  if (index < N) { return fOutputNames[index]; }
150  else { throw cet::exception("MVADescription") << "Index out of range: " << index << std::endl; }
151  }
152  void setOutputNames(std::vector< std::string > const & outputNames)
153  {
154  if (outputNames.size() <= N) { for (size_t i = 0; i < outputNames.size(); ++i) { fOutputNames[i] = outputNames[i]; } }
155  else { throw cet::exception("FeatureVector") << "Expected max length of outputNames: " << N << ", provided: " << outputNames.size() << std::endl; }
156  }
157 
158  int getIndex(const std::string & name) const
159  {
160  for (size_t i = 0; i < N; ++i) { if (fOutputNames[i] == name) { return i; } }
161  return -1; // not found
162  }
163 
164 }; // class MVADescription
165 
166 template <size_t N>
168 
169 } // namespace anab
170 
171 #endif //ANAB_FEATUREVECTORS
static QCString name
Definition: declinfo.cpp:673
void setDataTag(const std::string &tag)
Definition: MVAOutput.h:141
std::string string
Definition: nybbler.cc:12
static short Class_Version()
Definition: MVAOutput.h:104
friend std::ostream & operator<<(std::ostream &o, FeatureVector const &a)
Definition: MVAOutput.h:54
init
Definition: train.py:42
const std::string & dataTag() const
Definition: MVAOutput.h:140
void setOutputNames(std::vector< std::string > const &outputNames)
Definition: MVAOutput.h:152
std::string fOutputInstance
Instance name of the feature vector collection.
Definition: MVAOutput.h:108
static short Class_Version()
Definition: MVAOutput.h:30
int getIndex(const std::string &name) const
Definition: MVAOutput.h:158
const double a
FeatureVector(float init)
Definition: MVAOutput.h:37
FeatureVector(float const *values)
If you really have to use C arrays:
Definition: MVAOutput.h:44
size_t size() const
Definition: MVAOutput.h:62
FeatureVector(std::vector< double > const &values)
Definition: MVAOutput.h:41
float fData[N]
Vector values.
Definition: MVAOutput.h:33
const std::string & outputInstance() const
Definition: MVAOutput.h:136
Q_UINT16 values[128]
const std::string & outputName(size_t index) const
Definition: MVAOutput.h:147
MVADescription(std::string const &outputInstance, std::vector< std::string > const &outputNames=std::vector< std::string >(N,""))
Definition: MVAOutput.h:121
FeatureVector(std::vector< float > const &values)
Definition: MVAOutput.h:40
std::string fOutputNames[N]
Feature vector entries names/meaning.
Definition: MVAOutput.h:109
float at(size_t index) const
Definition: MVAOutput.h:64
FeatureVector(std::array< double, N > const &values)
Definition: MVAOutput.h:39
FeatureVector(std::array< float, N > const &values)
Definition: MVAOutput.h:38
MVADescription(std::string const &dataTag, std::string const &outputInstance, std::vector< std::string > const &outputNames=std::vector< std::string >(N,""))
Definition: MVAOutput.h:113
float operator[](size_t index) const
Definition: MVAOutput.h:70
FeatureVector(double const *values)
Definition: MVAOutput.h:45
std::string fDataTag
Tag of the reco data products (art::InputTag format)
Definition: MVAOutput.h:107
size_t size() const
Definition: MVAOutput.h:138
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
FeatureVector & operator=(float init)
Assignment operators, from the same types as constructors.
Definition: MVAOutput.h:48