TrdMCClusterR Fit Classifier
Utilities.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <vector>
5 #include <unordered_map>
6 #include <algorithm>
7 #include <numeric>
8 #include <iostream>
9 #include <fstream>
10 
11 #include <TVectorD.h>
12 
13 using std::cerr;
14 using std::endl;
15 
16 
17 namespace Utilities
18 {
19  //________________________________________________________________________________________________________
20  bool does_file_exist(const char *fileName)
21  {
24 
25  return static_cast<bool>(std::ifstream(fileName));
26  }
27 
28 
29  //________________________________________________________________________________________________________
30  int CalculatePairs(int n)
31  {
33  int value = n * (n - 1) / 2;
34  return value > 0 ? value : 0;
35  }
36 }
37 
38 
39 
40 
41 namespace VectorUtilities
42 {
43  //________________________________________________________________________________________________________
44  double sumVector(const std::vector<double> &v)
45  {
51  //return accumulate(v.begin(), v.end(), decltype(v)::value_type(0));
52  return std::accumulate(v.begin(), v.end(), 0.0);
53  }
54 
55 
56  //________________________________________________________________________________________________________
57  template <typename T>
58  void printVector(const std::vector<T> &v)
59  {
60  for (const T &x : v)
61  std::cout << x << ", ";
62  std::cout << std::endl;
63  }
64 
65 
66  //________________________________________________________________________________________________________
67  void printVectorOfVectors(std::vector<std::vector<double>> const &mat)
68  {
73  for (const std::vector<double> &v : mat)
74  {
75  for (double x : v)
76  std::cout << x <<' ';
77  std::cout<<std::endl;
78  }
79  }
80 
81 
82  //________________________________________________________________________________________________________
83  std::map<int, int> GetUniqueElementsAndFrequency(const std::vector<int>& vec)
84  {
85  std::map<int, int> frequencyMap;
86 
87  for(const int &val : vec)
88  {
89  frequencyMap[val]++;
90  }
91 
92  return frequencyMap;
93  }
94 
95 
96  //________________________________________________________________________________________________________
97  void filterAndShrinkMap(std::map<int, int>& frequencyMap, int threshold = 2)
98  {
99  for (auto it = frequencyMap.begin(); it != frequencyMap.end();) {
100  if (it->second < threshold)
101  it = frequencyMap.erase(it);
102  else
103  ++it;
104  }
105 
107  std::map<int, int>(frequencyMap).swap(frequencyMap);
108  }
109 
110 
111  //________________________________________________________________________________________________________
112  template <typename T>
113  bool areAllElementsEqual(const std::vector<T>& vec)
114  {
115  if (vec.size() == 0)
116  std::cerr << "\n\nWARNING: Empty Vector! Size is Zero (0).\n\n";
117  return std::adjacent_find(vec.begin(), vec.end(), [](const T& a, const T& b) { return a != b; }) == vec.end();
118  }
119 
120 
121  //________________________________________________________________________________________________________
122  int getMajorityElement(const std::vector<int>& vec)
123  {
124  std::unordered_map<int, int> freq;
125 
127  for (int num : vec) {
128  freq[num]++;
129  }
130 
132  int majorityElement = vec[0];
133  int maxCount = 0;
134  for (const auto& pair : freq) {
135  if (pair.second > maxCount) {
136  maxCount = pair.second;
137  majorityElement = pair.first;
138  }
139  }
140 
141  return majorityElement;
142  }
143 
144 
145  //________________________________________________________________________________________________________
146  int countDifferentFromMajority(const std::vector<int>& vec)
147  {
148  // Obtain the majority element by calling getMajorityElement
149  int majorityElement = getMajorityElement(vec);
150 
151  // Count the number of elements that are different from the majority element
152  int count = 0;
153  for (int num : vec) {
154  if (num != majorityElement) {
155  count++;
156  }
157  }
158 
159  return count;
160  }
161 
162 
163  //________________________________________________________________________________________________________
164  std::vector<double> TVectorDToStdVector(const TVectorD &vec)
165  {
166  std::vector<double> stdVec(vec.GetNrows());
167  for (int i = 0; i < vec.GetNrows(); ++i) {
168  stdVec[i] = vec(i);
169  }
170  return stdVec;
171  }
172 
173 
174  //________________________________________________________________________________________________________
175  bool AreVectorsEqual(const std::vector<double>& vec1,
176  const std::vector<double>& vec2,
177  double tol = 1e-4)
178  {
179  if (vec1.size() != vec2.size())
180  return false;
181 
182  for (size_t i = 0; i < vec1.size(); ++i)
183  {
184  if (std::abs(vec1[i] - vec2[i]) > tol)
185  return false;
186  }
187 
188  return true;
189  }
190 }
Definition: Utilities.h:18
int CalculatePairs(int n)
Definition: Utilities.h:30
bool does_file_exist(const char *fileName)
Definition: Utilities.h:20
Definition: Utilities.h:42
bool AreVectorsEqual(const std::vector< double > &vec1, const std::vector< double > &vec2, double tol=1e-4)
Definition: Utilities.h:175
bool areAllElementsEqual(const std::vector< T > &vec)
Definition: Utilities.h:113
std::vector< double > TVectorDToStdVector(const TVectorD &vec)
Definition: Utilities.h:164
void filterAndShrinkMap(std::map< int, int > &frequencyMap, int threshold=2)
Definition: Utilities.h:97
int getMajorityElement(const std::vector< int > &vec)
Definition: Utilities.h:122
std::map< int, int > GetUniqueElementsAndFrequency(const std::vector< int > &vec)
Definition: Utilities.h:83
void printVectorOfVectors(std::vector< std::vector< double >> const &mat)
Definition: Utilities.h:67
void printVector(const std::vector< T > &v)
Definition: Utilities.h:58
int countDifferentFromMajority(const std::vector< int > &vec)
Definition: Utilities.h:146
double sumVector(const std::vector< double > &v)
Definition: Utilities.h:44