| 1 |
|
| #include "mh_histogram.h" |
| 2 |
|
| |
| 3 |
|
| #include <stdio.h> |
| 4 |
|
| #include <stdlib.h> |
| 5 |
|
| #include <string.h> |
| 6 |
|
| #include <float.h> |
| 7 |
|
| #include <math.h> |
| 8 |
|
| #include <limits.h> |
| 9 |
|
| |
| 10 |
|
| static unsigned int |
| 11 |
11 |
| _mh_hist_compute_total_nbins(mh_histogram_t *hist) |
| 12 |
|
| { |
| 13 |
|
| unsigned int i; |
| 14 |
|
| unsigned int bins = 1; |
| 15 |
11 |
| unsigned int ndim = MH_HIST_NDIM(hist); |
| 16 |
11 |
| mh_axis_t **axises = hist->axises; |
| 17 |
|
| |
| 18 |
36 |
▶ | for (i = 0; i < ndim; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ndim; ++i) |
25 |
11 |
36 |
|
| 19 |
25 |
| bins *= MH_AXIS_NBINS(axises[i])+2; |
| 20 |
|
| /* printf("Total number of bins: %u\n", bins); */ |
| 21 |
11 |
| return bins; |
| 22 |
|
| } |
| 23 |
|
| |
| 24 |
|
| static void |
| 25 |
11 |
| _mh_hist_populate_overflow_bin_bitfield(mh_histogram_t *hist) |
| 26 |
|
| { |
| 27 |
11 |
| const unsigned int ndims = MH_HIST_NDIM(hist); |
| 28 |
11 |
| const unsigned int nlinearbins = hist->nbins_total; |
| 29 |
|
| unsigned int ilinear, i; |
| 30 |
11 |
| mh_axis_t **axises = hist->axises; |
| 31 |
11 |
| unsigned int *bin_buffer = hist->bin_buffer; |
| 32 |
11 |
| mh_bitfield_t bitfield = hist->overflow_bin_bitfield; |
| 33 |
|
| |
| 34 |
36 |
▶ | for (i = 0; i < ndims; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ndims; ++i) |
25 |
11 |
36 |
|
| 35 |
25 |
| bin_buffer[i] = 0; |
| 36 |
|
| |
| 37 |
10511 |
▶ | for (ilinear = 0; ilinear < nlinearbins;) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ilinear = 0; ilinear < nlinearbins;) { |
10500 |
11 |
10511 |
|
| 38 |
|
| /* |
| 39 |
|
| * printf("L=%u", ilinear); |
| 40 |
|
| * for (i = 0; i < ndims; ++i) |
| 41 |
|
| * printf(" %u", bin_buffer[i]); |
| 42 |
|
| * printf("\n"); |
| 43 |
|
| */ |
| 44 |
|
| /* FIXME there must be a better algorithm than a full iteration! */ |
| 45 |
24250 |
▶ | for (i = 0; i < ndims; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ndims; ++i) { |
20826 |
3424 |
24250 |
|
| 46 |
20826 |
▶ | if (bin_buffer[i] == 0 |
Branch: 1 branch
| Branch | True | False | Total |
| if (bin_buffer[i] == 0 |
17288 |
3538 |
20826 |
|
| 47 |
17288 |
▶ | || bin_buffer[i] > MH_AXIS_NBINS(axises[i])) { |
Branch: 1 branch
| Branch | True | False | Total |
| || bin_buffer[i] > MH_AXIS_NBINS(axises[i])) { |
3538 |
13750 |
17288 |
|
| 48 |
7076 |
| MH_BITFIELD_SET(bitfield, ilinear); |
| 49 |
7076 |
| break; |
| 50 |
|
| } |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
|
| /* Iterate both linear bin and bin vector */ |
| 54 |
10500 |
| ++ilinear; |
| 55 |
|
| i = 0; |
| 56 |
10500 |
| ++bin_buffer[i]; |
| 57 |
13161 |
▶ | while (i < ndims && bin_buffer[i] >= MH_AXIS_NBINS(axises[i])+2) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < ndims && bin_buffer[i] >= MH_AXIS_NBINS(axises[i])+2) { |
13150 |
11 |
13161 |
| while (i < ndims && bin_buffer[i] >= MH_AXIS_NBINS(axises[i])+2) { |
2661 |
10489 |
13150 |
|
| 58 |
2661 |
| bin_buffer[i] = 0; |
| 59 |
2661 |
| ++i; |
| 60 |
2661 |
| ++bin_buffer[i]; |
| 61 |
|
| } |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| /* printf("\n"); */ |
| 65 |
11 |
| } |
| 66 |
|
| |
| 67 |
|
| mh_histogram_t * |
| 68 |
11 |
| mh_hist_create(unsigned short ndim, mh_axis_t **axises) |
| 69 |
|
| { |
| 70 |
|
| unsigned int nbins, i; |
| 71 |
11 |
| mh_histogram_t *hist = malloc(sizeof(mh_histogram_t)); |
| 72 |
11 |
▶ | if (hist == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist == NULL) |
0 |
11 |
11 |
|
| 73 |
|
| return NULL; |
| 74 |
11 |
| hist->ndim = ndim; |
| 75 |
|
| |
| 76 |
11 |
| hist->bin_buffer = malloc(sizeof(unsigned int) * ndim * 2); |
| 77 |
11 |
▶ | if (hist->bin_buffer == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->bin_buffer == NULL) { |
0 |
11 |
11 |
|
| 78 |
0 |
| free(hist); |
| 79 |
0 |
| return NULL; |
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
|
| /* share the alloc/free */ |
| 83 |
11 |
| hist->arg_bin_buffer = &hist->bin_buffer[ndim]; |
| 84 |
|
| |
| 85 |
11 |
| hist->arg_coord_buffer = malloc(sizeof(double) * ndim); |
| 86 |
11 |
▶ | if (hist->arg_coord_buffer == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->arg_coord_buffer == NULL) { |
0 |
11 |
11 |
|
| 87 |
0 |
| free(hist); |
| 88 |
0 |
| free(hist->bin_buffer); |
| 89 |
0 |
| return NULL; |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
|
| |
| 93 |
11 |
| hist->axises = malloc(sizeof(mh_axis_t *) * ndim); |
| 94 |
11 |
▶ | if (hist->axises == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->axises == NULL) { |
0 |
11 |
11 |
|
| 95 |
0 |
| free(hist->bin_buffer); |
| 96 |
0 |
| free(hist->arg_coord_buffer); |
| 97 |
0 |
| free(hist); |
| 98 |
0 |
| return NULL; |
| 99 |
|
| } |
| 100 |
36 |
▶ | for (i = 0; i < ndim; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ndim; ++i) |
25 |
11 |
36 |
|
| 101 |
25 |
| hist->axises[i] = axises[i]; |
| 102 |
|
| |
| 103 |
11 |
| nbins = _mh_hist_compute_total_nbins(hist); |
| 104 |
11 |
| hist->nbins_total = nbins; |
| 105 |
11 |
| hist->data = (double *)calloc(nbins, sizeof(double)); |
| 106 |
11 |
▶ | if (hist->data == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->data == NULL) { |
0 |
11 |
11 |
|
| 107 |
0 |
| free(hist->bin_buffer); |
| 108 |
0 |
| free(hist->axises); |
| 109 |
0 |
| free(hist->arg_coord_buffer); |
| 110 |
0 |
| free(hist); |
| 111 |
0 |
| return NULL; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
11 |
| hist->overflow_bin_bitfield = MH_BITFIELD_CALLOC(nbins); |
| 115 |
11 |
▶ | if (hist->overflow_bin_bitfield == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->overflow_bin_bitfield == NULL) { |
0 |
11 |
11 |
|
| 116 |
0 |
| free(hist->data); |
| 117 |
0 |
| free(hist->bin_buffer); |
| 118 |
0 |
| free(hist->axises); |
| 119 |
0 |
| free(hist->arg_coord_buffer); |
| 120 |
0 |
| free(hist); |
| 121 |
0 |
| return NULL; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
11 |
| _mh_hist_populate_overflow_bin_bitfield(hist); |
| 125 |
|
| |
| 126 |
|
| /* TODO should initialization live elsewhere? */ |
| 127 |
11 |
| hist->total = 0.; |
| 128 |
11 |
| hist->nfills = 0; |
| 129 |
|
| |
| 130 |
11 |
| return hist; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
|
| mh_histogram_t * |
| 134 |
4 |
| mh_hist_clone(mh_histogram_t *hist_proto, int do_copy_data) |
| 135 |
|
| { |
| 136 |
|
| unsigned int nbins, i; |
| 137 |
4 |
| mh_histogram_t *hist = malloc(sizeof(mh_histogram_t)); |
| 138 |
4 |
▶ | if (hist == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist == NULL) |
0 |
4 |
4 |
|
| 139 |
|
| return NULL; |
| 140 |
4 |
| hist->ndim = MH_HIST_NDIM(hist_proto); |
| 141 |
|
| |
| 142 |
4 |
| hist->bin_buffer = malloc(sizeof(unsigned int) * MH_HIST_NDIM(hist) * 2); |
| 143 |
4 |
▶ | if (hist->bin_buffer == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->bin_buffer == NULL) { |
0 |
4 |
4 |
|
| 144 |
0 |
| free(hist); |
| 145 |
0 |
| return NULL; |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
|
| /* share the alloc/free */ |
| 149 |
4 |
| hist->arg_bin_buffer = &(hist->bin_buffer[MH_HIST_NDIM(hist)]); |
| 150 |
|
| |
| 151 |
4 |
| hist->arg_coord_buffer = malloc(sizeof(double) * hist->ndim); |
| 152 |
4 |
▶ | if (hist->arg_coord_buffer == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->arg_coord_buffer == NULL) { |
0 |
4 |
4 |
|
| 153 |
0 |
| free(hist->bin_buffer); |
| 154 |
|
| free(hist->arg_coord_buffer); |
| 155 |
0 |
| free(hist); |
| 156 |
0 |
| return NULL; |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
4 |
| hist->axises = malloc(sizeof(mh_axis_t *) * MH_HIST_NDIM(hist)); |
| 160 |
4 |
▶ | if (hist->axises == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->axises == NULL) { |
0 |
4 |
4 |
|
| 161 |
0 |
| free(hist->bin_buffer); |
| 162 |
0 |
| free(hist->arg_coord_buffer); |
| 163 |
0 |
| free(hist); |
| 164 |
0 |
| return NULL; |
| 165 |
|
| } |
| 166 |
13 |
▶ | for (i = 0; i < hist->ndim; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < hist->ndim; ++i) |
9 |
4 |
13 |
|
| 167 |
9 |
| hist->axises[i] = mh_axis_clone(hist_proto->axises[i]); |
| 168 |
|
| |
| 169 |
4 |
| hist->nbins_total = hist_proto->nbins_total; |
| 170 |
|
| nbins = hist->nbins_total; |
| 171 |
|
| |
| 172 |
4 |
▶ | if (do_copy_data != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (do_copy_data != 0) { |
3 |
1 |
4 |
|
| 173 |
3 |
| hist->data = (double *)malloc(nbins * sizeof(double)); |
| 174 |
3 |
▶ | if (hist->data == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->data == NULL) { |
0 |
3 |
3 |
|
| 175 |
0 |
| free(hist->bin_buffer); |
| 176 |
0 |
| free(hist->arg_coord_buffer); |
| 177 |
0 |
| free(hist->axises); |
| 178 |
0 |
| free(hist); |
| 179 |
0 |
| return NULL; |
| 180 |
|
| } |
| 181 |
3 |
| memcpy(hist->data, hist_proto->data, nbins * sizeof(double)); |
| 182 |
|
| |
| 183 |
|
| /* TODO should initialization live elsewhere? */ |
| 184 |
3 |
| hist->total = MH_HIST_TOTAL(hist_proto); |
| 185 |
3 |
| hist->nfills = MH_HIST_NFILLS(hist_proto); |
| 186 |
|
| } |
| 187 |
|
| else { |
| 188 |
1 |
| hist->data = (double *)calloc(nbins, sizeof(double)); |
| 189 |
1 |
▶ | if (hist->data == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->data == NULL) { |
0 |
1 |
1 |
|
| 190 |
0 |
| free(hist->bin_buffer); |
| 191 |
0 |
| free(hist->arg_coord_buffer); |
| 192 |
0 |
| free(hist->axises); |
| 193 |
0 |
| free(hist); |
| 194 |
0 |
| return NULL; |
| 195 |
|
| } |
| 196 |
|
| /* TODO should initialization live elsewhere? */ |
| 197 |
1 |
| hist->total = 0.; |
| 198 |
1 |
| hist->nfills = 0; |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
4 |
| i = (unsigned int)(ceilf((float)nbins/32)); |
| 202 |
4 |
| hist->overflow_bin_bitfield = MH_BITFIELD_MALLOC(nbins); |
| 203 |
4 |
▶ | if (hist->overflow_bin_bitfield == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hist->overflow_bin_bitfield == NULL) { |
0 |
4 |
4 |
|
| 204 |
0 |
| free(hist->data); |
| 205 |
0 |
| free(hist->bin_buffer); |
| 206 |
0 |
| free(hist->arg_coord_buffer); |
| 207 |
0 |
| free(hist->axises); |
| 208 |
0 |
| free(hist); |
| 209 |
0 |
| return NULL; |
| 210 |
|
| } |
| 211 |
4 |
| MH_BITFIELD_COPY(hist->overflow_bin_bitfield, hist_proto->overflow_bin_bitfield, nbins); |
| 212 |
|
| |
| 213 |
4 |
| return hist; |
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
|
| |
| 217 |
|
| void |
| 218 |
15 |
| mh_hist_free(mh_histogram_t *hist) |
| 219 |
|
| { |
| 220 |
15 |
| unsigned int i, ndim = MH_HIST_NDIM(hist); |
| 221 |
15 |
| mh_axis_t **axises = hist->axises; |
| 222 |
49 |
▶ | for (i = 0; i < ndim; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ndim; ++i) |
34 |
15 |
49 |
|
| 223 |
34 |
| mh_axis_free(axises[i]); |
| 224 |
|
| |
| 225 |
15 |
| free(hist->bin_buffer); /* frees arg_bin_buffer as well */ |
| 226 |
15 |
| free(hist->arg_coord_buffer); |
| 227 |
15 |
| free(hist->axises); |
| 228 |
15 |
| free(hist->data); |
| 229 |
15 |
| MH_BITFIELD_FREE(hist->overflow_bin_bitfield); |
| 230 |
15 |
| free(hist); |
| 231 |
15 |
| } |
| 232 |
|
| |
| 233 |
|
| |
| 234 |
|
| unsigned int |
| 235 |
4601 |
| mh_hist_flat_bin_number(mh_histogram_t *hist, unsigned int dim_bins[]) |
| 236 |
|
| { |
| 237 |
4601 |
| const unsigned short ndim = MH_HIST_NDIM(hist); |
| 238 |
4601 |
▶ | if (ndim == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ndim == 1) |
14 |
4587 |
4601 |
|
| 239 |
14 |
| return dim_bins[0]; |
| 240 |
|
| else { |
| 241 |
|
| register unsigned int bin_index; |
| 242 |
|
| register int i; |
| 243 |
4587 |
| mh_axis_t **axises = hist->axises; |
| 244 |
|
| |
| 245 |
|
| /* Suppose we have dim_bins = {5, 3, 4}; |
| 246 |
|
| * Then the index into the 1D data array is |
| 247 |
|
| * 4 * (dim_bins[2]+2)*(dim_bins[1]+2) + 3 * (dim_bins[1]+2) + 5 |
| 248 |
|
| * which can be done more efficiently as |
| 249 |
|
| * ((4)*(dim_bins[2]+2) + 3)*(dim_bins[1]+2) + 5; |
| 250 |
|
| * parenthesis hint at the execution order. |
| 251 |
|
| */ |
| 252 |
|
| |
| 253 |
4587 |
| bin_index = dim_bins[ndim-1]; |
| 254 |
|
| /* printf("%u %u\n", bin_index, ndim); */ |
| 255 |
11241 |
▶ | for (i = (int)ndim-2; i >= 0; --i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = (int)ndim-2; i >= 0; --i) |
6654 |
4587 |
11241 |
|
| 256 |
6654 |
| bin_index = bin_index*(MH_AXIS_NBINS(axises[i])+2) + dim_bins[i]; |
| 257 |
|
| |
| 258 |
4587 |
| return bin_index; |
| 259 |
|
| } |
| 260 |
|
| } |
| 261 |
|
| |
| 262 |
|
| |
| 263 |
|
| /* just as an API */ |
| 264 |
|
| unsigned int |
| 265 |
0 |
| mh_hist_total_nbins(mh_histogram_t *hist) |
| 266 |
|
| { |
| 267 |
0 |
| return hist->nbins_total; |
| 268 |
|
| } |
| 269 |
|
| |
| 270 |
|
| |
| 271 |
|
| void |
| 272 |
511 |
| mh_hist_find_bin_numbers(mh_histogram_t *hist, double coord[], unsigned int bin[]) |
| 273 |
|
| { |
| 274 |
511 |
| const unsigned int ndim = MH_HIST_NDIM(hist); |
| 275 |
|
| unsigned int i; |
| 276 |
511 |
| mh_axis_t **axises = hist->axises; |
| 277 |
2040 |
▶ | for (i = 0; i < ndim; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ndim; ++i) { |
1529 |
511 |
2040 |
|
| 278 |
1529 |
| bin[i] = mh_axis_find_bin(axises[i], coord[i]); |
| 279 |
|
| } |
| 280 |
511 |
| } |
| 281 |
|
| |
| 282 |
|
| |
| 283 |
|
| unsigned int |
| 284 |
487 |
| mh_hist_find_bin(mh_histogram_t *hist, double coord[]) |
| 285 |
|
| { |
| 286 |
487 |
| mh_hist_find_bin_numbers(hist, coord, hist->bin_buffer); |
| 287 |
487 |
| return mh_hist_flat_bin_number(hist, hist->bin_buffer); |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
|
| |
| 291 |
|
| unsigned int |
| 292 |
0 |
| mh_hist_find_bin_buf(mh_histogram_t *hist, double coord[], unsigned int bin_number_buffer[]) |
| 293 |
|
| { |
| 294 |
0 |
| mh_hist_find_bin_numbers(hist, coord, bin_number_buffer); |
| 295 |
0 |
| return mh_hist_flat_bin_number(hist, bin_number_buffer); |
| 296 |
|
| } |
| 297 |
|
| |
| 298 |
|
| |
| 299 |
|
| void |
| 300 |
2503 |
| mh_hist_flat_bin_number_to_dim_bins(mh_histogram_t *hist, |
| 301 |
|
| unsigned int flat_bin, |
| 302 |
|
| unsigned int dim_bins[]) |
| 303 |
|
| { |
| 304 |
2503 |
| const unsigned short ndim = MH_HIST_NDIM(hist); |
| 305 |
2503 |
▶ | if (ndim == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ndim == 1) |
5 |
2498 |
2503 |
|
| 306 |
5 |
| dim_bins[0] = flat_bin; |
| 307 |
|
| else { |
| 308 |
|
| register int i, nbins; |
| 309 |
2498 |
| register mh_axis_t **axises = hist->axises; |
| 310 |
|
| |
| 311 |
9942 |
▶ | for (i = 0; i < ndim; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ndim; ++i) { |
7444 |
2498 |
9942 |
|
| 312 |
7444 |
| nbins = MH_AXIS_NBINS(axises[i])+2; |
| 313 |
7444 |
| dim_bins[i] = flat_bin % nbins; |
| 314 |
7444 |
| flat_bin = (flat_bin - dim_bins[i]) / nbins; |
| 315 |
|
| } |
| 316 |
|
| } |
| 317 |
2503 |
| } |
| 318 |
|
| |
| 319 |
|
| |
| 320 |
|
| unsigned int |
| 321 |
145 |
| mh_hist_fill(mh_histogram_t *hist, double x[]) |
| 322 |
|
| { |
| 323 |
145 |
| const unsigned int flat_bin = mh_hist_find_bin(hist, x); |
| 324 |
145 |
▶ | if (flat_bin >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) |
145 |
0 |
145 |
|
| 325 |
|
| return UINT_MAX; |
| 326 |
145 |
| hist->data[flat_bin] += 1; |
| 327 |
145 |
| hist->total += 1; |
| 328 |
145 |
| hist->nfills++; |
| 329 |
145 |
| return flat_bin; |
| 330 |
|
| } |
| 331 |
|
| |
| 332 |
|
| |
| 333 |
|
| unsigned int |
| 334 |
144 |
| mh_hist_fill_bin(mh_histogram_t *hist, unsigned int dim_bins[]) |
| 335 |
|
| { |
| 336 |
144 |
| const unsigned int flat_bin = mh_hist_flat_bin_number(hist, dim_bins); |
| 337 |
144 |
▶ | if (flat_bin >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) |
144 |
0 |
144 |
|
| 338 |
|
| return UINT_MAX; |
| 339 |
144 |
| hist->data[flat_bin] += 1; |
| 340 |
144 |
| hist->total += 1; |
| 341 |
144 |
| hist->nfills++; |
| 342 |
144 |
| return flat_bin; |
| 343 |
|
| } |
| 344 |
|
| |
| 345 |
|
| |
| 346 |
|
| unsigned int |
| 347 |
342 |
| mh_hist_fill_w(mh_histogram_t *hist, double x[], double weight) |
| 348 |
|
| { |
| 349 |
342 |
| const unsigned int flat_bin = mh_hist_find_bin(hist, x); |
| 350 |
342 |
▶ | if (flat_bin >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) |
342 |
0 |
342 |
|
| 351 |
|
| return UINT_MAX; |
| 352 |
342 |
| hist->data[flat_bin] += weight; |
| 353 |
342 |
| hist->total += weight; |
| 354 |
342 |
| hist->nfills++; |
| 355 |
342 |
| return flat_bin; |
| 356 |
|
| } |
| 357 |
|
| |
| 358 |
|
| |
| 359 |
|
| unsigned int |
| 360 |
360 |
| mh_hist_fill_bin_w(mh_histogram_t *hist, unsigned int dim_bins[], double weight) |
| 361 |
|
| { |
| 362 |
360 |
| const unsigned int flat_bin = mh_hist_flat_bin_number(hist, dim_bins); |
| 363 |
360 |
▶ | if (flat_bin >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) |
360 |
0 |
360 |
|
| 364 |
|
| return UINT_MAX; |
| 365 |
360 |
| hist->data[flat_bin] += weight; |
| 366 |
360 |
| hist->total += weight; |
| 367 |
360 |
| hist->nfills++; |
| 368 |
360 |
| return flat_bin; |
| 369 |
|
| } |
| 370 |
|
| |
| 371 |
|
| |
| 372 |
|
| void |
| 373 |
0 |
| mh_hist_fill_n(mh_histogram_t *hist, unsigned int n, double **xs) |
| 374 |
|
| { |
| 375 |
|
| register unsigned int flat_bin; |
| 376 |
|
| register unsigned int i; |
| 377 |
0 |
▶ | for (i = 0; i < n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ++i) { |
0 |
0 |
0 |
|
| 378 |
0 |
| flat_bin = mh_hist_find_bin(hist, xs[i]); |
| 379 |
0 |
▶ | if (flat_bin >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) |
0 |
0 |
0 |
|
| 380 |
0 |
| continue; |
| 381 |
0 |
| hist->data[flat_bin] += 1; |
| 382 |
|
| } |
| 383 |
0 |
| hist->nfills += n; |
| 384 |
0 |
| hist->total += n; |
| 385 |
0 |
| } |
| 386 |
|
| |
| 387 |
|
| |
| 388 |
|
| void |
| 389 |
0 |
| mh_hist_fill_bin_n(mh_histogram_t *hist, unsigned int n, unsigned int **dim_bins) |
| 390 |
|
| { |
| 391 |
|
| register unsigned int flat_bin; |
| 392 |
|
| register unsigned int i; |
| 393 |
0 |
▶ | for (i = 0; i < n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ++i) { |
0 |
0 |
0 |
|
| 394 |
0 |
| flat_bin = mh_hist_flat_bin_number(hist, dim_bins[i]); |
| 395 |
0 |
▶ | if (flat_bin >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) |
0 |
0 |
0 |
|
| 396 |
0 |
| continue; |
| 397 |
0 |
| hist->data[flat_bin] += 1; |
| 398 |
|
| } |
| 399 |
0 |
| hist->nfills += n; |
| 400 |
0 |
| hist->total += n; |
| 401 |
0 |
| } |
| 402 |
|
| |
| 403 |
|
| |
| 404 |
|
| void |
| 405 |
0 |
| mh_hist_fill_nw(mh_histogram_t *hist, unsigned int n, double **xs, double weights[]) |
| 406 |
|
| { |
| 407 |
|
| register unsigned int flat_bin; |
| 408 |
|
| register unsigned int i; |
| 409 |
|
| double w; |
| 410 |
0 |
▶ | for (i = 0; i < n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ++i) { |
0 |
0 |
0 |
|
| 411 |
0 |
| w = weights[i]; |
| 412 |
0 |
| flat_bin = mh_hist_find_bin(hist, xs[i]); |
| 413 |
0 |
▶ | if (flat_bin >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) |
0 |
0 |
0 |
|
| 414 |
0 |
| continue; |
| 415 |
0 |
| hist->data[flat_bin] += w; |
| 416 |
0 |
| hist->nfills += w; |
| 417 |
0 |
| hist->total += w; |
| 418 |
|
| } |
| 419 |
0 |
| } |
| 420 |
|
| |
| 421 |
|
| |
| 422 |
|
| void |
| 423 |
0 |
| mh_hist_fill_bin_nw(mh_histogram_t *hist, unsigned int n, unsigned int **dim_bins, double weights[]) |
| 424 |
|
| { |
| 425 |
|
| register unsigned int flat_bin; |
| 426 |
|
| register unsigned int i; |
| 427 |
|
| double w; |
| 428 |
0 |
▶ | for (i = 0; i < n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ++i) { |
0 |
0 |
0 |
|
| 429 |
0 |
| w = weights[i]; |
| 430 |
0 |
| flat_bin = mh_hist_flat_bin_number(hist, dim_bins[i]); |
| 431 |
0 |
▶ | if (flat_bin >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) |
0 |
0 |
0 |
|
| 432 |
0 |
| continue; |
| 433 |
0 |
| hist->data[flat_bin] += w; |
| 434 |
0 |
| hist->nfills += w; |
| 435 |
0 |
| hist->total += w; |
| 436 |
|
| } |
| 437 |
0 |
| } |
| 438 |
|
| |
| 439 |
|
| |
| 440 |
|
| int |
| 441 |
59 |
| mh_hist_set_bin_content(mh_histogram_t *hist, unsigned int dim_bins[], double content) |
| 442 |
|
| { |
| 443 |
59 |
| const unsigned int flat_bin = mh_hist_flat_bin_number(hist, dim_bins); |
| 444 |
|
| double old; |
| 445 |
59 |
▶ | if (flat_bin >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) |
59 |
0 |
59 |
|
| 446 |
|
| return -1; |
| 447 |
59 |
| old = hist->data[flat_bin]; |
| 448 |
59 |
| hist->data[flat_bin] = content; |
| 449 |
59 |
| hist->total += content - old; |
| 450 |
59 |
| return 0; |
| 451 |
|
| } |
| 452 |
|
| |
| 453 |
|
| |
| 454 |
|
| int |
| 455 |
1059 |
| mh_hist_get_bin_content(mh_histogram_t *hist, unsigned int dim_bins[], double *content) |
| 456 |
|
| { |
| 457 |
1059 |
| const unsigned int flat_bin = mh_hist_flat_bin_number(hist, dim_bins); |
| 458 |
1059 |
▶ | if (flat_bin >= hist->nbins_total) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) { |
1059 |
0 |
1059 |
|
| 459 |
|
| content = NULL; |
| 460 |
|
| return -1; |
| 461 |
|
| } |
| 462 |
|
| else { |
| 463 |
1059 |
| *content = hist->data[flat_bin]; |
| 464 |
1059 |
| return 0; |
| 465 |
|
| } |
| 466 |
|
| } |
| 467 |
|
| |
| 468 |
|
| |
| 469 |
|
| mh_histogram_t * |
| 470 |
1 |
| mh_hist_contract_dimension(mh_histogram_t *hist, unsigned int contracted_dimension) |
| 471 |
|
| { |
| 472 |
|
| mh_axis_t **axises; |
| 473 |
|
| mh_axis_t **new_hist_axises; |
| 474 |
|
| mh_histogram_t *outhist; |
| 475 |
|
| unsigned int i, j, linear_nbins, ilinear, flat_bin; |
| 476 |
|
| unsigned int *dimension_map; |
| 477 |
|
| unsigned int *dim_bin_buffer; |
| 478 |
|
| unsigned int *reduced_dim_bin_buffer; |
| 479 |
1 |
| unsigned int ndims = MH_HIST_NDIM(hist); |
| 480 |
|
| |
| 481 |
1 |
▶ | if (ndims == 1 || contracted_dimension >= ndims) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ndims == 1 || contracted_dimension >= ndims) |
0 |
1 |
1 |
|
| 482 |
|
| return NULL; |
| 483 |
|
| |
| 484 |
1 |
| axises = hist->axises; |
| 485 |
|
| |
| 486 |
|
| /* Mapping from reduced dimension number to original |
| 487 |
|
| * dimension number, so from destination to source. */ |
| 488 |
1 |
| dimension_map = malloc(sizeof(unsigned int) * (ndims-1)); |
| 489 |
|
| /* Setup array of cloned axises for the new histogram. */ |
| 490 |
1 |
| new_hist_axises = malloc(sizeof(mh_axis_t *) * (ndims-1)); |
| 491 |
|
| j = 0; |
| 492 |
4 |
▶ | for (i = 0; i < ndims; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ndims; ++i) { |
3 |
1 |
4 |
|
| 493 |
3 |
▶ | if (i == contracted_dimension) { /* FIXME there must be a better way */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == contracted_dimension) { /* FIXME there must be a better way */ |
1 |
2 |
3 |
|
| 494 |
|
| j = 1; |
| 495 |
1 |
| continue; |
| 496 |
|
| } |
| 497 |
2 |
| dimension_map[i-j] = i; |
| 498 |
2 |
| new_hist_axises[i-j] = mh_axis_clone(axises[i]); |
| 499 |
2 |
▶ | if (new_hist_axises[i-j] == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_hist_axises[i-j] == NULL) { |
0 |
2 |
2 |
|
| 500 |
|
| ndims = i-j; /* abuse for emergency cleanup */ |
| 501 |
0 |
▶ | for (i = 0; i < ndims; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ndims; ++i) |
0 |
0 |
0 |
|
| 502 |
0 |
| free(new_hist_axises[i]); |
| 503 |
0 |
| free(new_hist_axises); |
| 504 |
0 |
| free(dimension_map); |
| 505 |
0 |
| return NULL; |
| 506 |
|
| } |
| 507 |
|
| } |
| 508 |
|
| |
| 509 |
|
| /* Create output N-1 dimensional histogram. */ |
| 510 |
1 |
| outhist = mh_hist_create(ndims-1, new_hist_axises); |
| 511 |
1 |
| free(new_hist_axises); |
| 512 |
|
| |
| 513 |
1 |
| dim_bin_buffer = malloc(ndims * sizeof(unsigned int)); |
| 514 |
1 |
| reduced_dim_bin_buffer = malloc((ndims-1) * sizeof(unsigned int)); |
| 515 |
|
| |
| 516 |
|
| /* - Iterate over all bins in the source histogram. |
| 517 |
|
| * - Find the vector of bin indexes in each dimension. |
| 518 |
|
| * - Copy the bin indexes over to the N-1 dimensional vector. |
| 519 |
|
| * - Use that vector to write the original bin's content to the |
| 520 |
|
| * right bin in the output histogram. |
| 521 |
|
| * |
| 522 |
|
| * This isn't hugely efficient but nicely abstracts away the problem |
| 523 |
|
| * with N/N-1 dimensionality by having the dimension mapping in a data |
| 524 |
|
| * structure (dimension_map) and simply skipping a dimension to contract. |
| 525 |
|
| */ |
| 526 |
|
| /* TODO allow skipping of overflow/underflow in contraction somehow? |
| 527 |
|
| * TODO generic mechanism for contracting only a range of bins? |
| 528 |
|
| */ |
| 529 |
1 |
| linear_nbins = hist->nbins_total; |
| 530 |
2449 |
▶ | for (ilinear = 0; ilinear < linear_nbins; ++ilinear) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ilinear = 0; ilinear < linear_nbins; ++ilinear) { |
2448 |
1 |
2449 |
|
| 531 |
|
| /* Get the [ix, iy, iz, ...] N-dim bin numbers from the linear bin. */ |
| 532 |
2448 |
| mh_hist_flat_bin_number_to_dim_bins(hist, ilinear, dim_bin_buffer); |
| 533 |
|
| |
| 534 |
|
| /* Copy all dimension indexes but the one we're contracting. */ |
| 535 |
7344 |
▶ | for (i = 0; i < ndims-1; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ndims-1; ++i) |
4896 |
2448 |
7344 |
|
| 536 |
4896 |
| reduced_dim_bin_buffer[i] = dim_bin_buffer[ dimension_map[i] ]; |
| 537 |
|
| |
| 538 |
|
| /* unrolled fill_w without updating total and nfills */ |
| 539 |
2448 |
| flat_bin = mh_hist_flat_bin_number(outhist, reduced_dim_bin_buffer); |
| 540 |
|
| /* direct access to hist->data since we're iterating in linearized bins already */ |
| 541 |
2448 |
| outhist->data[flat_bin] += hist->data[ilinear]; |
| 542 |
|
| } |
| 543 |
|
| |
| 544 |
1 |
| free(dim_bin_buffer); |
| 545 |
1 |
| free(reduced_dim_bin_buffer); |
| 546 |
|
| |
| 547 |
|
| /* fix the number of fills and total*/ |
| 548 |
1 |
| outhist->nfills = hist->nfills; |
| 549 |
1 |
| outhist->total = hist->total; |
| 550 |
|
| |
| 551 |
1 |
| return outhist; |
| 552 |
|
| } |
| 553 |
|
| |
| 554 |
|
| |
| 555 |
|
| int |
| 556 |
4 |
| mh_hist_data_equal_eps(mh_histogram_t *left, mh_histogram_t *right, double epsilon) |
| 557 |
|
| { |
| 558 |
4 |
| const unsigned int total_nbins_left = left->nbins_total; |
| 559 |
4 |
| const unsigned int total_nbins_right = right->nbins_total; |
| 560 |
|
| unsigned int i; |
| 561 |
4 |
| double *data_left = left->data; |
| 562 |
4 |
| double *data_right = right->data; |
| 563 |
|
| |
| 564 |
4 |
▶ | if (total_nbins_left != total_nbins_right) |
Branch: 1 branch
| Branch | True | False | Total |
| if (total_nbins_left != total_nbins_right) |
4 |
0 |
4 |
|
| 565 |
|
| return 0; |
| 566 |
|
| |
| 567 |
63 |
▶ | for (i = 0; i < total_nbins_left; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < total_nbins_left; ++i) { |
60 |
3 |
63 |
|
| 568 |
60 |
▶ | if ( data_left[i] + epsilon < data_right[i] |
Branch: 1 branch
| Branch | True | False | Total |
| if ( data_left[i] + epsilon < data_right[i] |
60 |
0 |
60 |
|
| 569 |
60 |
▶ | || data_left[i] - epsilon > data_right[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| || data_left[i] - epsilon > data_right[i]) { |
59 |
1 |
60 |
|
| 570 |
|
| /* printf("NOT EQUAL: at %u: left=%.10f right=%.10f\n", i, data_left[i], data_right[i]); */ |
| 571 |
|
| return 0; |
| 572 |
|
| } |
| 573 |
|
| } |
| 574 |
|
| |
| 575 |
|
| return 1; |
| 576 |
|
| } |
| 577 |
|
| |
| 578 |
|
| int |
| 579 |
4 |
| mh_hist_data_equal(mh_histogram_t *left, mh_histogram_t *right) |
| 580 |
|
| { |
| 581 |
4 |
| return mh_hist_data_equal_eps(left, right, DBL_EPSILON); |
| 582 |
|
| } |
| 583 |
|
| |
| 584 |
|
| |
| 585 |
|
| int |
| 586 |
3 |
| mh_hist_cumulate(mh_histogram_t *hist, unsigned int cumulation_dimension) |
| 587 |
|
| { |
| 588 |
3 |
| const unsigned int ndims = MH_HIST_NDIM(hist); |
| 589 |
|
| unsigned int ilinear; |
| 590 |
3 |
| const unsigned int nlinearbins = hist->nbins_total; |
| 591 |
|
| unsigned int *bin_buffer; |
| 592 |
|
| |
| 593 |
3 |
▶ | if (cumulation_dimension >= ndims) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cumulation_dimension >= ndims) |
3 |
0 |
3 |
|
| 594 |
|
| return -1; |
| 595 |
|
| |
| 596 |
|
| /* In a single dimension, the content of the i-th bin of the cumulative |
| 597 |
|
| * histogram is the content of the i-1-th bin of the cumulative histogram |
| 598 |
|
| * PLUS the content of the i-th bin of the ORIGINAL histogram. |
| 599 |
|
| * |
| 600 |
|
| * So if we have an N-dimensional histogram, we simply apply that same |
| 601 |
|
| * bit of logic to each bin in the dimension to cumulate. |
| 602 |
|
| * Since there's not an easy facility yet to iterate over a single dimension, |
| 603 |
|
| * we use the fact that due to the way we store data, the bin numbers increase |
| 604 |
|
| * in such a way along each dimension as we iterate over the flattened |
| 605 |
|
| * representation in memory, that C[i-1] has always been calculated. |
| 606 |
|
| * This means that when we visit any given bin, we know we've visited all |
| 607 |
|
| * other bins that precede the current bin in any dimension. In other words, |
| 608 |
|
| * we can use the C[i] = C[i-1] + H[i] relation. |
| 609 |
|
| * With this property and doing the same in-place, we get: H[i] += H[i-1] |
| 610 |
|
| */ |
| 611 |
3 |
| bin_buffer = hist->bin_buffer; |
| 612 |
58 |
▶ | for (ilinear = 0; ilinear < nlinearbins; ++ilinear) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ilinear = 0; ilinear < nlinearbins; ++ilinear) { |
55 |
3 |
58 |
|
| 613 |
55 |
| mh_hist_flat_bin_number_to_dim_bins(hist, ilinear, bin_buffer); |
| 614 |
55 |
▶ | if (bin_buffer[cumulation_dimension] > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bin_buffer[cumulation_dimension] > 0) { |
44 |
11 |
55 |
|
| 615 |
|
| /* printf("%u = %f, ", ilinear, hist->data[ilinear]); */ |
| 616 |
44 |
| bin_buffer[cumulation_dimension]--; /* one step back in the cumulation dimension */ |
| 617 |
44 |
| hist->data[ilinear] += hist->data[ mh_hist_flat_bin_number(hist, bin_buffer) ]; |
| 618 |
|
| /* printf("after = %f (prevbin: %i, %f)\n", hist->data[ilinear], mh_hist_flat_bin_number(hist, bin_buffer), hist->data[mh_hist_flat_bin_number(hist, bin_buffer)]); */ |
| 619 |
|
| } |
| 620 |
|
| } |
| 621 |
|
| |
| 622 |
|
| return 0; |
| 623 |
|
| } |
| 624 |
|
| |
| 625 |
|
| |
| 626 |
|
| void |
| 627 |
0 |
| mh_hist_debug_bin_iter_print(mh_histogram_t *hist) |
| 628 |
|
| { |
| 629 |
|
| unsigned int i, j; |
| 630 |
0 |
| const unsigned int ndim = MH_HIST_NDIM(hist); |
| 631 |
0 |
| const unsigned int n = hist->nbins_total; |
| 632 |
|
| |
| 633 |
0 |
▶ | for (i = 0; i < n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ++i) { |
0 |
0 |
0 |
|
| 634 |
0 |
| mh_hist_flat_bin_number_to_dim_bins(hist, i, MH_HIST_ARG_BIN_BUFFER(hist)); |
| 635 |
0 |
| printf("[%u", MH_HIST_ARG_BIN_BUFFER(hist)[0]); |
| 636 |
0 |
▶ | for(j = 1; j < ndim; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(j = 1; j < ndim; ++j) { |
0 |
0 |
0 |
|
| 637 |
0 |
| printf(",%u", MH_HIST_ARG_BIN_BUFFER(hist)[j]); |
| 638 |
|
| } |
| 639 |
|
| printf("]\n"); |
| 640 |
|
| } |
| 641 |
0 |
| } |
| 642 |
|
| |
| 643 |
|
| void |
| 644 |
0 |
| mh_hist_debug_dump_data(mh_histogram_t *hist) |
| 645 |
|
| { |
| 646 |
|
| unsigned int i, j; |
| 647 |
0 |
| unsigned int ndims = MH_HIST_NDIM(hist); |
| 648 |
0 |
| unsigned int n = hist->nbins_total; |
| 649 |
0 |
▶ | for (i = 0; i < n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ++i) { |
0 |
0 |
0 |
|
| 650 |
0 |
| mh_hist_flat_bin_number_to_dim_bins(hist, i, MH_HIST_ARG_BIN_BUFFER(hist)); |
| 651 |
0 |
▶ | for (j = 0; j < ndims; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < ndims; ++j) { |
0 |
0 |
0 |
|
| 652 |
0 |
| printf("%u ", (MH_HIST_ARG_BIN_BUFFER(hist))[j]); |
| 653 |
|
| } |
| 654 |
0 |
| printf("(%u) => %.10f\n", i, hist->data[i]); |
| 655 |
|
| } |
| 656 |
0 |
| } |
| 657 |
|
| |
| 658 |
|
| int |
| 659 |
0 |
| mh_hist_is_overflow_bin(mh_histogram_t *hist, unsigned int dim_bins[]) |
| 660 |
|
| { |
| 661 |
0 |
| const unsigned int flat_bin = mh_hist_flat_bin_number(hist, dim_bins); |
| 662 |
0 |
▶ | if (flat_bin >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flat_bin >= hist->nbins_total) |
0 |
0 |
0 |
|
| 663 |
|
| return 0; |
| 664 |
0 |
| return MH_BITFIELD_GET(hist->overflow_bin_bitfield, flat_bin); |
| 665 |
|
| } |
| 666 |
|
| |
| 667 |
|
| int |
| 668 |
0 |
| mh_hist_is_overflow_bin_linear(mh_histogram_t *hist, unsigned int linear_bin_num) |
| 669 |
|
| { |
| 670 |
0 |
▶ | if (linear_bin_num >= hist->nbins_total) |
Branch: 1 branch
| Branch | True | False | Total |
| if (linear_bin_num >= hist->nbins_total) |
0 |
0 |
0 |
|
| 671 |
|
| return 0; |
| 672 |
0 |
| return MH_BITFIELD_GET(hist->overflow_bin_bitfield, linear_bin_num); |
| 673 |
|
| } |
| 674 |
|
| |