PandaRoot
PndMvaUtil.h
Go to the documentation of this file.
1 /* ********************************************
2  * MVA Utility functions and data definitions.*
3  * Author: M.Babai@rug.nl *
4  * Version: *
5  * License: *
6  * *******************************************
7  */
8 //#pragma once
9 #ifndef PND_MVA_UTIL_H
10 #define PND_MVA_UTIL_H
11 
12 #include <typeinfo>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 #include <map>
17 #include <cmath>
18 #include <cassert>
19 
20 // =========================================================
22 struct StepError {
24  StepError() : m_step(0), m_trErr(0.0), m_tsErr(0.0), m_MisClsTest(std::map<std::string, float>()), m_MisClsTrain(std::map<std::string, float>()){};
25 
33  explicit StepError(size_t step, float trErr, float tsErr, std::map<std::string, float> const &MisClsTest, std::map<std::string, float> const &MisClsTrain)
34  : m_step(step), m_trErr(trErr), m_tsErr(tsErr), m_MisClsTest(MisClsTest), m_MisClsTrain(MisClsTrain){};
35 
37  virtual ~StepError(){};
38 
41 
44  {
45  // check for self-assignment
46  if (this != &ot) { // Not equal, thus deep copy
47  this->m_step = ot.m_step;
48  this->m_trErr = ot.m_trErr;
49  this->m_tsErr = ot.m_tsErr;
50  this->m_MisClsTest = ot.m_MisClsTest;
51  this->m_MisClsTrain = ot.m_MisClsTrain;
52  }
53  return (*this);
54  };
55 
56  unsigned int m_step;
57  float m_trErr;
58  float m_tsErr;
59  std::map<std::string, float> m_MisClsTest;
60  std::map<std::string, float> m_MisClsTrain;
62  private:
64  inline bool operator<(StepError const &other) const;
66  inline bool operator>(StepError const &other) const;
67 };
68 // ========================================================================
69 
75 struct PndMvaDistObj {
77  explicit PndMvaDistObj() : m_idx(0), m_dist(0.0), m_cls("UNKNOWN_LABEL"){};
78 
79  explicit PndMvaDistObj(size_t const id, float const dist, std::string const &cls) : m_idx(id), m_dist(dist), m_cls(cls){};
80 
81  // Copy
82  PndMvaDistObj(PndMvaDistObj const &ot) : m_idx(ot.m_idx), m_dist(ot.m_dist), m_cls(ot.m_cls){};
83 
84  // Destructor
85  virtual ~PndMvaDistObj(){};
86 
89  {
90  // check for self-assignment
91  if (this != &ot) { // Not equal, thus deep copy
92  this->m_idx = ot.m_idx;
93  this->m_dist = ot.m_dist;
94  this->m_cls = ot.m_cls;
95  }
96  return (*this);
97  };
98 
100  inline bool operator<(PndMvaDistObj const &other) const { return (this->m_dist < other.m_dist); };
101 
103  inline bool operator>(PndMvaDistObj const &other) const { return (this->m_dist > other.m_dist); };
104 
105  size_t m_idx;
106  float m_dist;
107  std::string m_cls;
109  private:
110  bool operator==(PndMvaDistObj const &ot) const;
111 }; // End interface PndMvaDistObj
112 
114 inline bool CompLess(PndMvaDistObj const *a, PndMvaDistObj const *b)
115 {
116  assert(a && b);
117  return ((*a).m_dist < (*b).m_dist);
118 };
119 
120 // ========================================================================
121 
126 template <typename T>
127 inline T const &minFunct(T const &a, T const &b)
128 {
129  // or: return comp(a,b)?a:b; for the comp version
130  return (a < b) ? a : b;
131 };
132 
134 template <typename T>
135 inline bool compareL(T const *l, T const *r)
136 {
137  assert(l && r);
138  return ((*l) < (*r));
139 };
140 
150 template <typename Set1, typename Set2>
151 bool is_disjoint(Set1 const &set1, Set2 const &set2)
152 {
153  // IF one of the sets is empty. O(1) true on empty sets per
154  // definition.
155  if (set1.empty() || set2.empty()) {
156  return true;
157  }
158  // Start and end iterators of the first sequence.
159  typename Set1::const_iterator it1 = set1.begin();
160  typename Set1::const_iterator it1End = set1.end();
161 
162  // Start and end iterators of the second sequence.
163  typename Set2::const_iterator it2 = set2.begin();
164  typename Set2::const_iterator it2End = set2.end();
165 
166  // This holds because the sequences are pre-sorted. O(1)
167  if (*it1 > *set2.rbegin() || *it2 > *set1.rbegin()) {
168  return true;
169  }
170 
171  // Investigate element-wise.
172  while ((it1 != it1End) && (it2 != it2End)) {
173  if (*it1 == *it2) {
174  return false;
175  }
176 
177  if (*it1 < *it2) {
178  it1++;
179  } else {
180  it2++;
181  }
182  } // WHILE
183  return true;
184 }
185 
190 float ComputeDist(std::vector<float> const &EvtData, std::vector<float> const &Example);
191 
192 // Convert string to int or size_t
193 int str2int(std::string const &str);
194 unsigned int str2Uint(std::string const &str);
195 
196 // Convert int to string
197 std::string int2str(int n);
198 
199 //______________ C style function declarations
200 #ifdef __cplusplus
201 extern "C" {
202 #endif
203 // Place your C code here.
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif // interface definition
StepError & operator=(StepError const &ot)
Assignment.
Definition: PndMvaUtil.h:43
virtual ~PndMvaDistObj()
Definition: PndMvaUtil.h:85
bool CompLess(PndMvaDistObj const *a, PndMvaDistObj const *b)
Less than, comparison funtion.
Definition: PndMvaUtil.h:114
PndMvaDistObj()
Constructor.
Definition: PndMvaUtil.h:77
STL namespace.
StepError(StepError const &ot)
Copy!
Definition: PndMvaUtil.h:40
float ComputeDist(std::vector< float > const &EvtData, std::vector< float > const &Example)
size_t m_idx
Definition: PndMvaUtil.h:103
float m_tsErr
Train Error.
Definition: PndMvaUtil.h:58
std::string int2str(int n)
bool operator<(PndMvaDistObj const &other) const
Operator <.
Definition: PndMvaUtil.h:100
std::map< std::string, float > m_MisClsTrain
Definition: PndMvaUtil.h:60
std::string m_cls
Definition: PndMvaUtil.h:107
PndMvaDistObj & operator=(PndMvaDistObj const &ot)
operator =
Definition: PndMvaUtil.h:88
bool is_disjoint(Set1 const &set1, Set2 const &set2)
Definition: PndMvaUtil.h:151
virtual ~StepError()
Destructor.
Definition: PndMvaUtil.h:37
PndMvaDistObj(size_t const id, float const dist, std::string const &cls)
Definition: PndMvaUtil.h:79
int str2int(std::string const &str)
Structure to hold the per step error values.
Definition: PndMvaUtil.h:22
bool operator>(PndMvaDistObj const &other) const
Operator >
Definition: PndMvaUtil.h:103
StepError(size_t step, float trErr, float tsErr, std::map< std::string, float > const &MisClsTest, std::map< std::string, float > const &MisClsTrain)
Definition: PndMvaUtil.h:33
bool compareL(T const *l, T const *r)
Less than, comparison funtion.
Definition: PndMvaUtil.h:135
T const & minFunct(T const &a, T const &b)
Definition: PndMvaUtil.h:127
unsigned int str2Uint(std::string const &str)
PndMvaDistObj(PndMvaDistObj const &ot)
Definition: PndMvaUtil.h:82
StepError()
Constructor.
Definition: PndMvaUtil.h:24
unsigned int m_step
Definition: PndMvaUtil.h:54
float m_trErr
Step number.
Definition: PndMvaUtil.h:57
friend F32vec4 operator==(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:92
std::map< std::string, float > m_MisClsTest
Test Error.
Definition: PndMvaUtil.h:59