Histo1DFunction.hh
6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Histo1Dfunction.hh
*
* Created on: Jan 30, 1013
* Author: AKKODIS
*/
#ifndef HISTO1DFUNCTION_HH_
#define HISTO1DFUNCTION_HH_
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include "Matrix.hh"
#include "Range.hh"
namespace plot {
/**
* Class that manages the function to apply on the 1Dhistogram
*/
class Histo1DFunction {
public:
Histo1DFunction(){
}
~Histo1DFunction(){}
virtual std::string getFunctionName() = 0;
void apply(std::shared_ptr<Histo1DFunction> functionToApply, std::vector<std::pair<double,double>> &grid, double* xData, double* yData, int nbData, Range xRange, double& xBinSize,
bool yIsLog, double& yMin, double& yMax);
virtual std::string getTextLegend()
{
return "";
}
double mean(const std::vector<double>& data) {
double sum = 0.0;
for (const auto& value : data) {
sum += value;
}
return sum / data.size();
}
double standardDeviation(const std::vector<double>& data, double mean) {
double sum = 0.0;
for (const auto& value : data) {
sum += std::pow(value - mean, 2);
}
return std::sqrt(sum / data.size());
}
double sum(const std::vector<double>& data) {
double sum = 0.0;
for (const auto& value : data) {
sum += value;
}
return sum;
}
double max(const std::vector<double>& data) {
if (data.empty()) {
std::cerr << "Error: Empty data for max calculation." << std::endl;
return 0.0;
}
return *std::max_element(data.begin(), data.end());
}
double min(const std::vector<double>& data) {
if (data.empty()) {
std::cerr << "Error: Empty data for min calculation." << std::endl;
return 0.0;
}
return *std::min_element(data.begin(), data.end());
}
double median(const std::vector<double>& data) {
if (data.empty()) {
std::cerr << "Error: Empty data for median calculation." << std::endl;
return 0.0;
}
std::vector<double> sortedData = data;
std::sort(sortedData.begin(), sortedData.end());
size_t size = sortedData.size();
if (size % 2 == 0) {
// If the size is even, take the average of the two middle elements.
return (sortedData[size / 2 - 1] + sortedData[size / 2]) / 2.0;
} else {
// If the size is odd, return the middle element.
return sortedData[size / 2];
}
}
std::map<int,std::vector<double>> getGridValues(double* xData, double* yData, int nbData, Range xRange, double xBinSize);
virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog) = 0;
void resetCache() {
_cacheGrid.clear();
}
protected:
std::vector<std::pair<double,double>> _cacheGrid;
};
/**
* All the way down, different classes that apply a function on 1Dhistogram and return result
*/
class Density1DFunction : public Histo1DFunction {
public:
Density1DFunction() {}
~Density1DFunction() {}
virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
virtual std::string getFunctionName(){return "density";};
virtual std::string getTextLegend()
{
return "Events";
}
};
class NormDensity1DFunction : public Histo1DFunction {
public:
NormDensity1DFunction() {}
~NormDensity1DFunction() {}
virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
virtual std::string getFunctionName(){return "normdensity";};
virtual std::string getTextLegend()
{
return "Frequency";
}
};
class Mean1DFunction : public Histo1DFunction {
public:
Mean1DFunction() {}
~Mean1DFunction() {}
virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
virtual std::string getFunctionName(){return "mean";};
virtual std::string getTextLegend(){return "Mean for ";}
};
class Max1DFunction : public Histo1DFunction {
public:
Max1DFunction() {}
~Max1DFunction() {}
virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
virtual std::string getFunctionName(){return "max";};
virtual std::string getTextLegend(){return "Max for ";}
};
class Min1DFunction : public Histo1DFunction {
public:
Min1DFunction() {}
~Min1DFunction() {}
virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
virtual std::string getFunctionName(){return "min";};
virtual std::string getTextLegend(){return "Min for ";}
};
class Median1DFunction : public Histo1DFunction {
public:
Median1DFunction() {}
~Median1DFunction() {}
virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
virtual std::string getFunctionName(){return "median";};
virtual std::string getTextLegend(){return "Median for ";}
};
class StandardDeviation1DFunction : public Histo1DFunction {
public:
StandardDeviation1DFunction() {}
~StandardDeviation1DFunction() {}
virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
virtual std::string getFunctionName(){return "stadev";};
virtual std::string getTextLegend(){return "Sta. dev. for ";}
};
class Kurtosis1DFunction : public Histo1DFunction {
public:
Kurtosis1DFunction() {}
~Kurtosis1DFunction() {}
virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
virtual std::string getFunctionName(){return "kurtosis";};
virtual std::string getTextLegend(){return "Kurtosis for ";}
};
class Skewness1DFunction : public Histo1DFunction {
public:
Skewness1DFunction() {}
~Skewness1DFunction() {}
virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
virtual std::string getFunctionName(){return "skewness";};
virtual std::string getTextLegend(){return "Skewness for ";}
};
} /* namespace plot */
#endif /* HISTO1DFUNCTION_HH_ */