FairRoot/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
23 {
26  : m_step(0), m_trErr(0.0), m_tsErr(0.0),
27  m_MisClsTest ( std::map <std::string, float>() ),
28  m_MisClsTrain( std::map <std::string, float>() )
29  {};
30 
38  explicit StepError(size_t step, float trErr, float tsErr,
39  std::map <std::string, float> const& MisClsTest,
40  std::map <std::string, float> const& MisClsTrain
41  )
42  : m_step(step), m_trErr(trErr), m_tsErr(tsErr),
43  m_MisClsTest (MisClsTest),
44  m_MisClsTrain(MisClsTrain)
45  {};
46 
48  virtual ~StepError()
49  {};
50 
52  StepError(StepError const& ot)
53  : m_step(ot.m_step), m_trErr(ot.m_trErr), m_tsErr(ot.m_tsErr),
56  {};
57 
60  {
61  // check for self-assignment
62  if (this != &ot)
63  {// Not equal, thus deep copy
64  this->m_step = ot.m_step;
65  this->m_trErr = ot.m_trErr;
66  this->m_tsErr = ot.m_tsErr;
67  this->m_MisClsTest = ot.m_MisClsTest;
68  this->m_MisClsTrain = ot.m_MisClsTrain;
69  }
70  return (*this);
71  };
72 
73  unsigned int m_step;
74  float m_trErr;
75  float m_tsErr;
76  std::map <std::string, float> m_MisClsTest;
77  std::map <std::string, float> m_MisClsTrain;
79 private:
81  inline bool operator< (StepError const& other) const;
83  inline bool operator> (StepError const& other) const;
84 };
85 // ========================================================================
86 
93 {
95  explicit PndMvaDistObj()
96  : m_idx(0), m_dist(0.0), m_cls("UNKNOWN_LABEL")
97  {};
98 
99  explicit PndMvaDistObj(size_t const id, float const dist, std::string const& cls)
100  : m_idx(id), m_dist(dist), m_cls(cls)
101  {};
102 
103  // Copy
105  : m_idx(ot.m_idx), m_dist(ot.m_dist), m_cls(ot.m_cls)
106  {};
107 
108  // Destructor
109  virtual ~PndMvaDistObj()
110  {};
111 
114  {
115  // check for self-assignment
116  if (this != &ot)
117  {// Not equal, thus deep copy
118  this->m_idx = ot.m_idx;
119  this->m_dist = ot.m_dist;
120  this->m_cls = ot.m_cls;
121  }
122  return (*this);
123  };
124 
126  inline bool operator< (PndMvaDistObj const& other) const
127  {
128  return (this->m_dist < other.m_dist);
129  };
130 
132  inline bool operator> (PndMvaDistObj const& other) const
133  {
134  return (this->m_dist > other.m_dist);
135  };
136 
137  size_t m_idx ;
138  float m_dist;
139  std::string m_cls;
141 private:
142  bool operator==(PndMvaDistObj const& ot) const;
143 };// End interface PndMvaDistObj
144 
146 inline bool CompLess(PndMvaDistObj const* a, PndMvaDistObj const* b)
147 {
148  assert(a && b);
149  return ( (*a).m_dist < (*b).m_dist );
150 };
151 
152 // ========================================================================
153 
158 template <typename T>
159 inline T const& minFunct ( T const& a, T const& b )
160 {
161  // or: return comp(a,b)?a:b; for the comp version
162  return (a < b) ? a : b;
163 };
164 
166 template<typename T>
167 inline bool compareL(T const* l, T const* r)
168 {
169  assert(l && r);
170  return ( (*l) < (*r) );
171 };
172 
182 template< typename Set1, typename Set2>
183 bool is_disjoint(Set1 const& set1, Set2 const& set2)
184 {
185  // IF one of the sets is empty. O(1) true on empty sets per
186  // definition.
187  if( set1.empty() || set2.empty())
188  {
189  return true;
190  }
191  // Start and end iterators of the first sequence.
192  typename Set1::const_iterator it1 = set1.begin();
193  typename Set1::const_iterator it1End = set1.end();
194 
195  // Start and end iterators of the second sequence.
196  typename Set2::const_iterator it2 = set2.begin();
197  typename Set2::const_iterator it2End = set2.end();
198 
199  // This holds because the sequences are pre-sorted. O(1)
200  if( *it1 > *set2.rbegin() || *it2 > *set1.rbegin() )
201  {
202  return true;
203  }
204 
205  // Investigate element-wise.
206  while( (it1 != it1End) && (it2 != it2End) )
207  {
208  if( *it1 == *it2 )
209  {
210  return false;
211  }
212 
213  if( *it1 < *it2 )
214  {
215  it1++;
216  }
217  else
218  {
219  it2++;
220  }
221  }// WHILE
222  return true;
223 }
224 
229 float ComputeDist(std::vector<float> const& EvtData,
230  std::vector<float> const& Example);
231 
232 // Convert string to int or size_t
233 int str2int (std::string const& str);
234 unsigned int str2Uint (std::string const& str);
235 
236 // Convert int to string
237 std::string int2str (int n);
238 
239 //______________ C style function declarations
240 #ifdef __cplusplus
241 extern "C"
242 {
243 #endif
244  // Place your C code here.
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 #endif// interface definition
StepError & operator=(StepError const &ot)
Assignment.
Definition: PndMvaUtil.h:59
virtual ~PndMvaDistObj()
Definition: PndMvaUtil.h:109
double r
Definition: RiemannTest.C:14
TTree * b
PndTransMap * map
Definition: sim_emc_apd.C:99
bool CompLess(PndMvaDistObj const *a, PndMvaDistObj const *b)
Less than, comparison funtion.
Definition: PndMvaUtil.h:146
PndMvaDistObj()
Constructor.
Definition: PndMvaUtil.h:95
int n
StepError(StepError const &ot)
Copy!
Definition: PndMvaUtil.h:52
bool operator<(PndMvaDistObj const &other) const
Operator &lt;.
Definition: PndMvaUtil.h:126
float ComputeDist(std::vector< float > const &EvtData, std::vector< float > const &Example)
size_t m_idx
Definition: PndMvaUtil.h:135
float m_tsErr
Train Error.
Definition: PndMvaUtil.h:75
std::string int2str(int n)
bool operator>(StepError const &other) const
Operator &gt;
std::map< std::string, float > m_MisClsTrain
Definition: PndMvaUtil.h:77
TTree * T
Definition: anaLmdReco.C:32
std::string m_cls
Definition: PndMvaUtil.h:139
Int_t a
Definition: anaLmdDigi.C:126
PndMvaDistObj & operator=(PndMvaDistObj const &ot)
operator =
Definition: PndMvaUtil.h:113
bool is_disjoint(Set1 const &set1, Set2 const &set2)
Definition: PndMvaUtil.h:183
bool operator>(PndMvaDistObj const &other) const
Operator &gt;
Definition: PndMvaUtil.h:132
virtual ~StepError()
Destructor.
Definition: PndMvaUtil.h:48
PndMvaDistObj(size_t const id, float const dist, std::string const &cls)
Definition: PndMvaUtil.h:99
int str2int(std::string const &str)
Structure to hold the per step error values.
Definition: PndMvaUtil.h:22
bool operator==(PndMvaDistObj const &ot) const
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:38
bool compareL(T const *l, T const *r)
Less than, comparison funtion.
Definition: PndMvaUtil.h:167
T const & minFunct(T const &a, T const &b)
Definition: PndMvaUtil.h:159
unsigned int str2Uint(std::string const &str)
PndMvaDistObj(PndMvaDistObj const &ot)
Definition: PndMvaUtil.h:104
StepError()
Constructor.
Definition: PndMvaUtil.h:25
unsigned int m_step
Definition: PndMvaUtil.h:71
float m_trErr
Step number.
Definition: PndMvaUtil.h:74
bool operator<(StepError const &other) const
Operator &lt;.
std::map< std::string, float > m_MisClsTest
Test Error.
Definition: PndMvaUtil.h:76