1 |
|
|
/*************************************** |
2 |
|
|
Auteur : Pierre Aubert |
3 |
|
|
Mail : pierre.aubert@lapp.in2p3.fr |
4 |
|
|
Licence : CeCILL-C |
5 |
|
|
****************************************/ |
6 |
|
|
|
7 |
|
|
#ifndef __PHISTOGRAM_IMPL_H__ |
8 |
|
|
#define __PHISTOGRAM_IMPL_H__ |
9 |
|
|
|
10 |
|
|
#include "pMinMax.h" |
11 |
|
|
#include "PHistogram.h" |
12 |
|
|
|
13 |
|
|
///Default constructor of PHistogram |
14 |
|
|
/** @param nbValue : number of bins of PHistogramme |
15 |
|
|
*/ |
16 |
|
|
template<typename T> |
17 |
|
6 |
PHistogram<T>::PHistogram(size_t nbValue) |
18 |
|
6 |
:p_nbValue(0lu), p_value(NULL) |
19 |
|
|
{ |
20 |
|
6 |
initialisationPHistogram(); |
21 |
|
6 |
allocateHist(nbValue); |
22 |
|
6 |
reset(); |
23 |
|
6 |
} |
24 |
|
|
|
25 |
|
|
///PHistogram constructor |
26 |
|
|
/** @param min : min value of PHistogram |
27 |
|
|
* @param max : max value of PHistogram |
28 |
|
|
* @param nbValue : number of bins of PHistogramme |
29 |
|
|
*/ |
30 |
|
|
template<typename T> |
31 |
|
2 |
PHistogram<T>::PHistogram(T min, T max, size_t nbValue) |
32 |
|
2 |
:p_nbValue(0lu), p_value(NULL) |
33 |
|
|
{ |
34 |
|
2 |
initialisationPHistogram(); |
35 |
|
2 |
allocateHist(nbValue); |
36 |
|
2 |
setRange(min, max); |
37 |
|
2 |
reset(); |
38 |
|
2 |
} |
39 |
|
|
|
40 |
|
|
///Copy constructor of PHistogram |
41 |
|
|
/** @param other : PHistogram to be copied |
42 |
|
|
*/ |
43 |
|
|
template<typename T> |
44 |
|
2 |
PHistogram<T>::PHistogram(const PHistogram<T> & other) |
45 |
|
2 |
:p_nbValue(0lu), p_value(NULL) |
46 |
|
|
{ |
47 |
|
2 |
copyPHistogram(other); |
48 |
|
2 |
} |
49 |
|
|
|
50 |
|
|
///Destructor du PHistogram |
51 |
|
|
template<typename T> |
52 |
|
20 |
PHistogram<T>::~PHistogram(){ |
53 |
|
20 |
if(p_value != NULL){ |
54 |
✓✗ |
14 |
delete [] p_value; |
55 |
|
|
} |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
///Fonction qui permet de sauvegarder la PFunction dans un fichier |
59 |
|
|
/** @param fileName : nom du fichier de sauvegarde |
60 |
|
|
* @return true si la fonction a réussie, false sinon |
61 |
|
|
*/ |
62 |
|
|
template<typename T> |
63 |
|
|
bool PHistogram<T>::save(const std::string & fileName) const{ |
64 |
|
|
std::ofstream fs; |
65 |
|
|
fs.open(fileName.c_str()); |
66 |
|
|
if(!fs.is_open()){return false;} |
67 |
|
|
T dx = p_xRange/((T)p_nbValue); |
68 |
|
|
|
69 |
|
|
for(long unsigned int i(0lu); i < p_nbValue; ++i){ |
70 |
|
|
fs << (dx*((T)i) + p_xMin) << "\t" << p_value[i] << "\n"; |
71 |
|
|
} |
72 |
|
|
fs << (dx*((T)p_nbValue) + p_xMin) << "\t" << 0 << "\n"; |
73 |
|
|
fs.close(); |
74 |
|
|
return true; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
///Resize the PHistogram |
78 |
|
|
/** @param nbValue ; number of bins of the PHistogram |
79 |
|
|
*/ |
80 |
|
|
template<typename T> |
81 |
|
3 |
void PHistogram<T>::resize(size_t nbValue){ |
82 |
|
3 |
allocateHist(nbValue); |
83 |
|
3 |
reset(); |
84 |
|
3 |
} |
85 |
|
|
|
86 |
|
|
///Set PHistogram boundaries |
87 |
|
|
/** @param min : min boundaries of PHistogram |
88 |
|
|
* @param max : max boundaries of PHistogram |
89 |
|
|
*/ |
90 |
|
|
template<typename T> |
91 |
|
5 |
void PHistogram<T>::setRange(T min, T max){ |
92 |
|
5 |
p_xMin = min; |
93 |
|
5 |
p_xMax = max; |
94 |
|
5 |
p_xRange = p_xMax - p_xMin; |
95 |
|
5 |
} |
96 |
|
|
|
97 |
|
|
///Reset histogram value to 0 |
98 |
|
|
template<typename T> |
99 |
|
13 |
void PHistogram<T>::reset(){ |
100 |
|
13 |
initialisationPHistogram(); |
101 |
✓✓ |
104 |
for(size_t i(0lu); i < p_nbValue; ++i){ |
102 |
|
91 |
p_value[i] = 0; |
103 |
|
|
} |
104 |
|
13 |
} |
105 |
|
|
|
106 |
|
|
///Remove all the 0 in the histogram, they are replaced by lowValue to avoid problem when plotting histogram in log scale |
107 |
|
|
/** @param lowValue : value to be used to replace values lower than zero |
108 |
|
|
*/ |
109 |
|
|
template<typename T> |
110 |
|
3 |
void PHistogram<T>::checkLogValue(T lowValue){ |
111 |
✓✓ |
33 |
for(size_t i(0lu); i < p_nbValue; ++i){ |
112 |
✓✓ |
30 |
if(p_value[i] < 0.0){ |
113 |
|
10 |
p_value[i] = lowValue; |
114 |
|
|
} |
115 |
|
|
} |
116 |
|
3 |
} |
117 |
|
|
|
118 |
|
|
///Add value in the PHistogram |
119 |
|
|
/** @param value : value to be added in the PHistogram |
120 |
|
|
*/ |
121 |
|
|
template<typename T> |
122 |
|
1229 |
void PHistogram<T>::addValue(T value){ |
123 |
✓✓ |
1229 |
if(p_value == NULL) return; |
124 |
✓✓✓✓
|
1228 |
if(value >= p_xMax || value < p_xMin){ |
125 |
✓✓ |
888 |
if(value >= p_xMax) ++p_overXMax; |
126 |
✓✓ |
888 |
if(value < p_xMin) ++p_underXMin; |
127 |
|
888 |
return; |
128 |
|
|
} |
129 |
|
340 |
size_t pos(((value - p_xMin)*((double)p_nbValue))/p_xRange); |
130 |
|
340 |
p_value[pos]++; |
131 |
|
340 |
++p_nbTirIn; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
///Add value in the histogram |
135 |
|
|
/** @param tabValue : table of value we want to add in the histogram |
136 |
|
|
* @param nbValues : number of values we want to add |
137 |
|
|
*/ |
138 |
|
|
template<typename T> |
139 |
|
6 |
void PHistogram<T>::addValue(const T * tabValue, size_t nbValues){ |
140 |
✓✓✓✓ ✓✓ |
6 |
if(p_value == NULL || tabValue == NULL || nbValues == 0lu) return; |
141 |
✓✓ |
30 |
for(size_t i(0lu); i < nbValues; ++i){ |
142 |
|
28 |
addValue(tabValue[i]); |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
///Add value in the histogram and calulate the range of the histogram |
147 |
|
|
/** @param tabValue : table of value we want to add in the histogram |
148 |
|
|
* @param nbValues : number of values we want to add |
149 |
|
|
*/ |
150 |
|
|
template<typename T> |
151 |
|
4 |
void PHistogram<T>::setValueRange(const T * tabValue, size_t nbValues){ |
152 |
✓✓✓✓ ✓✓ |
4 |
if(p_value == NULL || tabValue == NULL || nbValues == 0lu) return; |
153 |
|
1 |
pminmaxTab(p_xMin, p_xMax, tabValue, nbValues); |
154 |
|
1 |
setRange(p_xMin, p_xMax); |
155 |
|
1 |
reset(); |
156 |
|
1 |
addValue(tabValue, nbValues); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
///Add value in the histogram with amplitude |
160 |
|
|
/** @param value : value to be added in the histogram |
161 |
|
|
* @param amplitude : amplitude of the value |
162 |
|
|
*/ |
163 |
|
|
template<typename T> |
164 |
|
201 |
void PHistogram<T>::addAmplitude(T value, T amplitude){ |
165 |
✓✓ |
201 |
if(p_value == NULL) return; |
166 |
✓✓✓✓
|
200 |
if(value >= p_xMax || value < p_xMin){ |
167 |
✓✓ |
95 |
if(value >= p_xMax) ++p_overXMax; |
168 |
✓✓ |
95 |
if(value < p_xMin) ++p_underXMin; |
169 |
|
95 |
return; |
170 |
|
|
} |
171 |
|
105 |
size_t pos(((value - p_xMin)*((double)p_nbValue))/p_xRange); |
172 |
|
105 |
p_value[pos] += amplitude; |
173 |
|
105 |
++p_nbTirIn; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
///Load histogram bins |
177 |
|
|
/** @param tabValues : tableau de valeurs à mettre dans l'histogramme |
178 |
|
|
* @param nbValues : number of values of the input table |
179 |
|
|
*/ |
180 |
|
|
template<typename T> |
181 |
|
|
void PHistogram<T>::loadBins(const T* tabValues, size_t nbValues){ |
182 |
|
|
if(tabValues == NULL || nbValues == 0lu) return; |
183 |
|
|
allocateHist(nbValues); |
184 |
|
|
for(size_t i(0lu); i < nbValues; ++i){ |
185 |
|
|
addValue(tabValues[i]); |
186 |
|
|
} |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
///Fonction qui normalise le PHistogram |
190 |
|
|
template<typename T> |
191 |
|
4 |
void PHistogram<T>::normalize(){ |
192 |
✓✓ |
4 |
if(p_value == NULL) return; |
193 |
|
2 |
T coef(((p_nbTirIn)*p_xRange)/((T)p_nbValue)); |
194 |
✓✓ |
2 |
if(coef == 0.0) return; |
195 |
✓✓ |
11 |
for(size_t i(0lu); i < p_nbValue; ++i){ |
196 |
|
10 |
p_value[i] /= coef; |
197 |
|
|
} |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
///Returns the number of hits under the xmin boundary |
201 |
|
|
/** @return number of hits under the xmin boundary |
202 |
|
|
*/ |
203 |
|
|
template<typename T> |
204 |
|
1 |
size_t PHistogram<T>::getUnderXMin() const{ |
205 |
|
1 |
return p_underXMin; |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
///Returns the number of hits over the xmax boundary |
209 |
|
|
/** @return number of hits over the xmax boundary |
210 |
|
|
*/ |
211 |
|
|
template<typename T> |
212 |
|
1 |
size_t PHistogram<T>::getOverXMax() const{ |
213 |
|
1 |
return p_overXMax; |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
///Returns the minimum value in x of the histogram |
217 |
|
|
/** @return minimum value in x of the histogram |
218 |
|
|
*/ |
219 |
|
|
template<typename T> |
220 |
|
1 |
T PHistogram<T>::getMin() const{return p_xMin;} |
221 |
|
|
|
222 |
|
|
///Returns the maximum value in x of the histogram |
223 |
|
|
/** @return maximum value in x of the histogram |
224 |
|
|
*/ |
225 |
|
|
template<typename T> |
226 |
|
1 |
T PHistogram<T>::getMax() const{return p_xMax;} |
227 |
|
|
|
228 |
|
|
///Returns the range of the histogram |
229 |
|
|
/** @return range of the histogram |
230 |
|
|
*/ |
231 |
|
|
template<typename T> |
232 |
|
1 |
T PHistogram<T>::getRange() const{return p_xRange;} |
233 |
|
|
|
234 |
|
|
///Equal operator |
235 |
|
|
/** @param other : PHistogram to be copied |
236 |
|
|
*/ |
237 |
|
|
template<typename T> |
238 |
|
1 |
PHistogram<T> PHistogram<T>::operator = (const PHistogram<T> & other){ |
239 |
|
1 |
copyPHistogram(other); |
240 |
|
1 |
return *this; |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
///Initialise the histogram |
244 |
|
|
template<typename T> |
245 |
|
21 |
void PHistogram<T>::initialisationPHistogram(){ |
246 |
|
21 |
p_nbTirIn = 0lu; |
247 |
|
21 |
p_underXMin = 0lu; |
248 |
|
21 |
p_overXMax = 0lu; |
249 |
|
21 |
} |
250 |
|
|
|
251 |
|
|
///Allocate the histogram |
252 |
|
|
/** @param nbValue : number of value |
253 |
|
|
*/ |
254 |
|
|
template<typename T> |
255 |
|
14 |
void PHistogram<T>::allocateHist(size_t nbValue){ |
256 |
✓✓ |
14 |
if(nbValue == p_nbValue){return;} |
257 |
✓✓ |
10 |
if(p_value != NULL){ |
258 |
✓✗ |
2 |
delete [] p_value; |
259 |
|
|
} |
260 |
|
10 |
p_nbValue = nbValue; |
261 |
✓✓ |
10 |
if(nbValue == 0lu){ |
262 |
|
1 |
p_value = NULL; |
263 |
|
|
}else{ |
264 |
✓✗ |
9 |
p_value = new T[p_nbValue]; |
265 |
|
|
} |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
///Copy function of a PHistogram |
269 |
|
|
/** @param other : PHistogram to be copied |
270 |
|
|
*/ |
271 |
|
|
template<typename T> |
272 |
|
3 |
void PHistogram<T>::copyPHistogram(const PHistogram<T> & other){ |
273 |
|
3 |
allocateHist(other.p_nbValue); |
274 |
✓✓ |
33 |
for(size_t i(0lu); i < p_nbValue; ++i){ |
275 |
|
30 |
p_value[i] = other.p_value[i]; |
276 |
|
|
} |
277 |
|
3 |
p_xMin = other.p_xMin; |
278 |
|
3 |
p_xMax = other.p_xMax; |
279 |
|
3 |
p_xRange = other.p_xRange; |
280 |
|
3 |
p_underXMin = other.p_underXMin; |
281 |
|
3 |
p_overXMax = other.p_overXMax; |
282 |
|
3 |
p_nbTirIn = other.p_nbTirIn; |
283 |
|
3 |
} |
284 |
|
|
|
285 |
|
|
#endif |
286 |
|
|
|