PandaRoot
PndMvaUtil.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
13 /* ********************************************
14  * MVA Utility functions and data definitions.*
15  * Author: M.Babai@rug.nl *
16  * Version: *
17  * License: *
18  * *******************************************
19  */
20 //#pragma once
21 #ifndef PND_MVA_UTIL_H
22 #define PND_MVA_UTIL_H
23 
24 #include <typeinfo>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 #include <map>
29 #include <cmath>
30 #include <cassert>
31 
32 // =========================================================
34 struct StepError {
36  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>()){};
37 
45  explicit StepError(size_t step, float trErr, float tsErr, std::map<std::string, float> const &MisClsTest, std::map<std::string, float> const &MisClsTrain)
46  : m_step(step), m_trErr(trErr), m_tsErr(tsErr), m_MisClsTest(MisClsTest), m_MisClsTrain(MisClsTrain){};
47 
49  virtual ~StepError(){};
50 
53 
56  {
57  // check for self-assignment
58  if (this != &ot) { // Not equal, thus deep copy
59  this->m_step = ot.m_step;
60  this->m_trErr = ot.m_trErr;
61  this->m_tsErr = ot.m_tsErr;
62  this->m_MisClsTest = ot.m_MisClsTest;
63  this->m_MisClsTrain = ot.m_MisClsTrain;
64  }
65  return (*this);
66  };
67 
68  unsigned int m_step;
69  float m_trErr;
70  float m_tsErr;
71  std::map<std::string, float> m_MisClsTest;
72  std::map<std::string, float> m_MisClsTrain;
74  private:
76  inline bool operator<(StepError const &other) const;
78  inline bool operator>(StepError const &other) const;
79 };
80 // ========================================================================
81 
87 struct PndMvaDistObj {
89  explicit PndMvaDistObj() : m_idx(0), m_dist(0.0), m_cls("UNKNOWN_LABEL"){};
90 
91  explicit PndMvaDistObj(size_t const id, float const dist, std::string const &cls) : m_idx(id), m_dist(dist), m_cls(cls){};
92 
93  // Copy
94  PndMvaDistObj(PndMvaDistObj const &ot) : m_idx(ot.m_idx), m_dist(ot.m_dist), m_cls(ot.m_cls){};
95 
96  // Destructor
97  virtual ~PndMvaDistObj(){};
98 
101  {
102  // check for self-assignment
103  if (this != &ot) { // Not equal, thus deep copy
104  this->m_idx = ot.m_idx;
105  this->m_dist = ot.m_dist;
106  this->m_cls = ot.m_cls;
107  }
108  return (*this);
109  };
110 
112  inline bool operator<(PndMvaDistObj const &other) const { return (this->m_dist < other.m_dist); };
113 
115  inline bool operator>(PndMvaDistObj const &other) const { return (this->m_dist > other.m_dist); };
116 
117  size_t m_idx;
118  float m_dist;
119  std::string m_cls;
121  private:
122  bool operator==(PndMvaDistObj const &ot) const;
123 }; // End interface PndMvaDistObj
124 
126 inline bool CompLess(PndMvaDistObj const *a, PndMvaDistObj const *b)
127 {
128  assert(a && b);
129  return ((*a).m_dist < (*b).m_dist);
130 };
131 
132 // ========================================================================
133 
138 template <typename T>
139 inline T const &minFunct(T const &a, T const &b)
140 {
141  // or: return comp(a,b)?a:b; for the comp version
142  return (a < b) ? a : b;
143 };
144 
146 template <typename T>
147 inline bool compareL(T const *l, T const *r)
148 {
149  assert(l && r);
150  return ((*l) < (*r));
151 };
152 
162 template <typename Set1, typename Set2>
163 bool is_disjoint(Set1 const &set1, Set2 const &set2)
164 {
165  // IF one of the sets is empty. O(1) true on empty sets per
166  // definition.
167  if (set1.empty() || set2.empty()) {
168  return true;
169  }
170  // Start and end iterators of the first sequence.
171  typename Set1::const_iterator it1 = set1.begin();
172  typename Set1::const_iterator it1End = set1.end();
173 
174  // Start and end iterators of the second sequence.
175  typename Set2::const_iterator it2 = set2.begin();
176  typename Set2::const_iterator it2End = set2.end();
177 
178  // This holds because the sequences are pre-sorted. O(1)
179  if (*it1 > *set2.rbegin() || *it2 > *set1.rbegin()) {
180  return true;
181  }
182 
183  // Investigate element-wise.
184  while ((it1 != it1End) && (it2 != it2End)) {
185  if (*it1 == *it2) {
186  return false;
187  }
188 
189  if (*it1 < *it2) {
190  it1++;
191  } else {
192  it2++;
193  }
194  } // WHILE
195  return true;
196 }
197 
202 float ComputeDist(std::vector<float> const &EvtData, std::vector<float> const &Example);
203 
204 // Convert string to int or size_t
205 int str2int(std::string const &str);
206 unsigned int str2Uint(std::string const &str);
207 
208 // Convert int to string
209 std::string int2str(int n);
210 
211 //______________ C style function declarations
212 #ifdef __cplusplus
213 extern "C" {
214 #endif
215 // Place your C code here.
216 
217 #ifdef __cplusplus
218 }
219 #endif
220 
221 #endif // interface definition
StepError & operator=(StepError const &ot)
Assignment.
Definition: PndMvaUtil.h:55
virtual ~PndMvaDistObj()
Definition: PndMvaUtil.h:97
bool CompLess(PndMvaDistObj const *a, PndMvaDistObj const *b)
Less than, comparison funtion.
Definition: PndMvaUtil.h:126
PndMvaDistObj()
Constructor.
Definition: PndMvaUtil.h:89
STL namespace.
StepError(StepError const &ot)
Copy!
Definition: PndMvaUtil.h:52
float ComputeDist(std::vector< float > const &EvtData, std::vector< float > const &Example)
size_t m_idx
Definition: PndMvaUtil.h:115
float m_tsErr
Train Error.
Definition: PndMvaUtil.h:70
std::string int2str(int n)
bool operator<(PndMvaDistObj const &other) const
Operator <.
Definition: PndMvaUtil.h:112
std::map< std::string, float > m_MisClsTrain
Definition: PndMvaUtil.h:72
std::string m_cls
Definition: PndMvaUtil.h:119
PndMvaDistObj & operator=(PndMvaDistObj const &ot)
operator =
Definition: PndMvaUtil.h:100
bool is_disjoint(Set1 const &set1, Set2 const &set2)
Definition: PndMvaUtil.h:163
virtual ~StepError()
Destructor.
Definition: PndMvaUtil.h:49
PndMvaDistObj(size_t const id, float const dist, std::string const &cls)
Definition: PndMvaUtil.h:91
int str2int(std::string const &str)
Structure to hold the per step error values.
Definition: PndMvaUtil.h:34
bool operator>(PndMvaDistObj const &other) const
Operator >
Definition: PndMvaUtil.h:115
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:45
bool compareL(T const *l, T const *r)
Less than, comparison funtion.
Definition: PndMvaUtil.h:147
T const & minFunct(T const &a, T const &b)
Definition: PndMvaUtil.h:139
unsigned int str2Uint(std::string const &str)
PndMvaDistObj(PndMvaDistObj const &ot)
Definition: PndMvaUtil.h:94
StepError()
Constructor.
Definition: PndMvaUtil.h:36
unsigned int m_step
Definition: PndMvaUtil.h:66
float m_trErr
Step number.
Definition: PndMvaUtil.h:69
friend F32vec4 operator==(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:104
std::map< std::string, float > m_MisClsTest
Test Error.
Definition: PndMvaUtil.h:71