Project
Loading...
Searching...
No Matches
standalone-cluster-dump-entropy-analysed.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <memory.h>
18#include <math.h>
19#include <queue>
20#include <map>
21#include <iterator>
22#include <algorithm>
23#include <iostream>
24
25const int32_t sort_method = 1; // 0 No sorting, 1 sort after pad, 2 sort after time, 3/4 mixed methods favoring pad / time
26const int32_t sector_diff = 1;
27const int32_t row_diff = 1;
28const int32_t pad_diff = 1;
29const int32_t time_diff = 1;
30const int32_t res_diff = 0;
31const int32_t approximate_qtot = 0;
32const int32_t combine_maxtot = 1;
33const int32_t combine_sigmapadtime = 1;
34const int32_t track_based = 1;
35const int32_t track_avgtot = track_based && 0;
36const int32_t track_avgmax = track_based && 0;
37const int32_t track_diffqtot = track_based && 0;
38const int32_t track_diffqmax = track_based && 0;
39const int32_t track_separate_q = track_based && 1;
40const int32_t track_diffsigma = track_based && 0;
41const int32_t track_separate_sigma = track_based && 1;
42const int32_t truncate_bits = 1;
43const int32_t separate_sectors = 0;
44const int32_t separate_patches = 0;
45const int32_t separate_sides = 0;
46const int32_t full_row_numbers = 1;
47const int32_t distinguish_rows = 0;
48const int32_t optimized_negative_values = 1;
49
50const int32_t print_clusters = 0;
51
52const char* file = "clusters-pbpb.dump";
53const int32_t max_clusters = 2000000;
54
55const int32_t truncate_sigma = 3;
56const int32_t truncate_charge = 4;
57
58const int32_t sort_pad_mixed_bins = 100;
59const int32_t sort_time_mixed_bins = 400;
60
61#define EVENT 0
62#define SECTOR 1
63#define PATCH 2
64#define ROW 3
65#define PAD 4
66#define TIME 5
67#define SIGMA_PAD 6
68#define SIGMA_TIME 7
69#define QMAX 8
70#define QTOT 9
71#define FLAG_PADTIME 10
72#define CLUSTER_ID 11
73#define RES_PAD 12
74#define RES_TIME 13
75#define AVG_TOT 14
76#define AVG_MAX 15
77#define QMAX_QTOT 16
78#define SIGMA_PAD_TIME 17
79#define DIFF_SIGMA_PAD 18
80#define DIFF_SIGMA_TIME 19
81#define DIFF_SIGMA_PAD_TIME 20
82#define AVG_TOT_MAX 21
83#define ROW_TRACK_FIRST 22
84#define ROW_TRACK 23
85
86#define PAD_80 24
87#define PAD_92 25
88#define PAD_104 26
89#define PAD_116 27
90#define PAD_128 28
91#define PAD_140 29
92
93const int32_t rr = optimized_negative_values && 0 ? 13 : 14; // We can make them all 14 for convenience, the encoding will handle it
94
95const uint32_t field_bits[] = {0, 6, 0, 8, 14, 15, 8, 8, 10, 16, 2, 0, 14, 15, 16, 10, 26, 16, 8, 8, 16, 26, 8, 8, rr, rr, rr, rr, rr, 14};
96const uint32_t significant_bits[] = {0, 6, 0, 8, 14, 15, truncate_sigma, truncate_sigma, truncate_charge, truncate_charge, 2, 0, 14, 15, truncate_charge, truncate_charge, 26, 16, truncate_sigma, truncate_sigma, 16, 26, 8, 8, rr, rr, rr, rr, rr, 14};
97const int32_t nFields = sizeof(field_bits) / sizeof(field_bits[0]);
98const char* field_names[] = {"event", "sector", "patch", "row", "pad", "time", "sigmaPad", "sigmaTime", "qmax", "qtot", "flagPadTime", "trackID", "resTrackPad",
99 "resTrackTime", "trackQTot", "trackQMax", "qmaxtot", "sigmapadtime", "diffsigmapad", "diffsigmatime", "diffsigmapadtime", "tracktotmax", "trackfirstrow", "trackrow", "pad_80", "pad_92",
100 "pad_104", "pad_116", "pad_128", "pad_140"};
101
103 struct
104 {
106 int32_t trackID;
108 };
109 uint32_t vals[16];
110};
111
112int32_t fgRows[6][2] = {{0, 30}, {30, 62}, {63, 90}, {90, 116}, {117, 139}, {139, 158}};
113int32_t fgNRows[6] = {31, 33, 28, 27, 23, 20};
114
115int32_t fgNPads[159] = {68, 68, 68, 68, 70, 70, 70, 72, 72, 72, 74, 74, 74, 76, 76, 76, 78, 78, 78, 80, 80, 80, 82, 82, 82, 84, 84, 84, 86, 86, 86, 88, 88, 88, 90, 90, 90, 92, 92, 92, 94, 94, 94, 96, 96, 96, 98, 98, 98, 100, 100, 100, 102,
116 102, 102, 104, 104, 104, 106, 106, 106, 108, 108, 74, 76, 76, 76, 76, 78, 78, 78, 80, 80, 80, 80, 82, 82, 82, 84, 84, 84, 86, 86, 86, 86, 88, 88, 88, 90, 90, 90, 90, 92, 92, 92, 94, 94, 94, 96, 96, 96, 96, 98, 98, 98, 100,
117 100, 100, 100, 102, 102, 102, 104, 104, 104, 106, 106, 106, 106, 108, 108, 108, 110, 110, 110, 110, 112, 112, 114, 114, 114, 116, 116, 118, 118, 120, 120, 122, 122, 122, 124, 124, 126, 126, 128, 128, 130, 130, 130, 132, 132, 134, 134, 136, 136, 138, 138, 138, 140};
118
119int32_t fgNPadsMod[159] = {80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104,
120 104, 104, 104, 104, 104, 116, 116, 116, 116, 116, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104,
121 104, 104, 104, 104, 104, 104, 104, 104, 104, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 128, 128, 128, 128, 128, 128, 128, 128, 128, 126, 126, 128, 128, 140, 140, 140, 140, 140, 134, 134, 140, 140, 140, 140, 140, 140};
122
123// ---------------------------------- HUFFMAN TREE
124
125typedef std::vector<bool> HuffCode;
126typedef std::map<uint32_t, HuffCode> HuffCodeMap;
127
128class INode
129{
130 public:
131 const double f;
132
133 virtual ~INode() {}
134
135 protected:
136 INode(double f) : f(f) {}
137};
138
139class InternalNode : public INode
140{
141 public:
142 INode* const left;
143 INode* const right;
144
145 InternalNode(INode* c0, INode* c1) : INode(c0->f + c1->f), left(c0), right(c1) {}
147 {
148 delete left;
149 delete right;
150 }
151};
152
153class LeafNode : public INode
154{
155 public:
156 const uint32_t c;
157
158 LeafNode(double f, uint32_t c) : INode(f), c(c) {}
159};
160
161struct NodeCmp {
162 bool operator()(const INode* lhs, const INode* rhs) const { return lhs->f > rhs->f; }
163};
164
165INode* BuildTree(const double* frequencies, uint32_t UniqueSymbols)
166{
167 std::priority_queue<INode*, std::vector<INode*>, NodeCmp> trees;
168
169 for (int32_t i = 0; i < UniqueSymbols; i++) {
170 if (frequencies[i] != 0) {
171 trees.push(new LeafNode(frequencies[i], i));
172 }
173 }
174 while (trees.size() > 1) {
175 INode* childR = trees.top();
176 trees.pop();
177
178 INode* childL = trees.top();
179 trees.pop();
180
181 INode* parent = new InternalNode(childR, childL);
182 trees.push(parent);
183 }
184 return trees.top();
185}
186
187void GenerateCodes(const INode* node, const HuffCode& prefix, HuffCodeMap& outCodes)
188{
189 if (const LeafNode* lf = dynamic_cast<const LeafNode*>(node)) {
190 outCodes[lf->c] = prefix;
191 } else if (const InternalNode* in = dynamic_cast<const InternalNode*>(node)) {
192 HuffCode leftPrefix = prefix;
193 leftPrefix.push_back(false);
194 GenerateCodes(in->left, leftPrefix, outCodes);
195
196 HuffCode rightPrefix = prefix;
197 rightPrefix.push_back(true);
198 GenerateCodes(in->right, rightPrefix, outCodes);
199 }
200}
201
202//--------------------------------------------- END HUFFMAN
203
204bool clustercompare_padtime(cluster_struct a, cluster_struct b) { return (a.pad < b.pad || (a.pad == b.pad && a.time < b.time)); }
205
206bool clustercompare_timepad(cluster_struct a, cluster_struct b) { return (a.time < b.time || (a.time == b.time && a.pad < b.pad)); }
207
209
211
212bool clustercompare_inevent(cluster_struct a, cluster_struct b) { return (a.sector < b.sector || (a.sector == b.sector && a.patch < b.patch) || (a.sector == b.sector && a.patch == b.patch && a.row < b.row)); }
213
214void do_diff(uint32_t& val, int32_t& last, uint32_t bits, uint32_t maxval = 0)
215{
216 int32_t tmp = val;
217 val -= last;
218 if (maxval && optimized_negative_values) {
219 while ((signed)val < 0) {
220 val += maxval;
221 }
222 } else {
223 val &= (1 << bits) - 1;
224 }
225 last = tmp;
226}
227
228uint32_t truncate(int32_t j, uint32_t val)
229{
231 int32_t ldz = sizeof(uint32_t) * 8 - __builtin_clz(val);
232 if (ldz > significant_bits[j]) {
233 val &= ((1 << ldz) - 1) ^ ((1 << (ldz - significant_bits[j])) - 1);
234 }
235 }
236 return (val);
237}
238
239int32_t main(int argc, char** argv)
240{
241 FILE* fp;
242
244 printf("Cannot use truncate bits with differential qmax / qtot");
245 return (1);
246 }
248 printf("Cannot use truncate bits with differential sigma");
249 return (1);
250 }
251
252 if (!(fp = fopen(file, "rb"))) {
253 printf("Error opening file\n");
254 return (1);
255 }
256
257 fseek(fp, 0, SEEK_END);
258 size_t nFileSize = ftell(fp);
259 fseek(fp, 0, SEEK_SET);
260
261 size_t nClusters = nFileSize / sizeof(cluster_struct);
264 }
265
267 if (clusters == NULL) {
268 printf("Memory allocation error\n");
269 return (1);
270 }
271
272 fprintf(stderr, "Reading %d clusters...", (int32_t)nClusters);
273 fread(clusters, sizeof(cluster_struct), nClusters, fp);
274
275 fprintf(stderr, "Done\nSorting clusters...");
276
277 if (sort_method) {
278 int32_t starti = 0;
279 if (!track_based) {
280 fprintf(stderr, " (removing track ordering)...");
281 int32_t last_event = 0;
282 for (int32_t i = 0; i <= nClusters; i++) {
283 int32_t event = (i == nClusters ? -1 : clusters[i].event);
284 if (last_event != event) {
285 if (i - 1 > starti) {
286 std::sort(clusters + starti, clusters + i - 1, clustercompare_inevent);
287 }
288 starti = i;
289 }
290 last_event = event;
291 }
292 }
293
294 starti = 0;
295 int32_t startrow = -1;
296 for (int32_t i = 0; i <= nClusters; i++) {
297 int32_t currow;
298 if (i == nClusters) {
299 currow = -1;
300 } else if (track_based && clusters[i].trackID != -1) {
301 currow = -2;
302 } else {
303 currow = clusters[i].row;
304 }
305 if (currow != startrow && startrow != -2) {
306 if (i - 1 > starti) {
307 if (sort_method == 1) {
308 std::sort(clusters + starti, clusters + i - 1, clustercompare_padtime);
309 } else if (sort_method == 2) {
310 std::sort(clusters + starti, clusters + i - 1, clustercompare_timepad);
311 } else if (sort_method == 3) {
312 std::sort(clusters + starti, clusters + i - 1, clustercompare_padtime_mixed);
313 } else if (sort_method == 4) {
314 std::sort(clusters + starti, clusters + i - 1, clustercompare_timepad_mixed);
315 }
316 }
317 starti = i;
318 startrow = currow;
319 }
320 }
321 }
322 fprintf(stderr, "Done\n");
323
324 fclose(fp);
325
326 int64_t* histograms[nFields];
327 double* probabilities[nFields];
328 int64_t counts[nFields];
329 int32_t used[nFields];
330 for (int32_t i = SECTOR; i < nFields; i++) {
331 if (i == CLUSTER_ID) {
332 continue;
333 }
334 histograms[i] = new int64_t[1 << field_bits[i]];
335 probabilities[i] = new double[1 << field_bits[i]];
336 }
337
338 double rawtotalbytes = 0;
339 double entrototalbytes = 0;
340 for (int32_t isector = 0; isector < 36; isector++) {
341 for (int32_t ipatch = 0; ipatch < 6; ipatch++) {
342 if (separate_sectors) {
343 printf("SECTOR %d ", isector);
344 }
345 if (separate_patches) {
346 printf("PATCH %d", ipatch);
347 }
349 printf("\n");
350 }
351 for (int32_t i = SECTOR; i < nFields; i++) {
352 if (i == CLUSTER_ID || i == PATCH) {
353 continue;
354 }
355 memset(histograms[i], 0, sizeof(int64_t) * (1 << field_bits[i]));
356 counts[i] = 0;
357 used[i] = 0;
358 }
359
360 size_t nClustersUsed = 0;
361
362 int32_t lastRow = 0, lastPad = 0, lastTime = 0, lastSector = 0, lastResPad = 0, lastResTime = 0, lastQTot = 0, lastQMax = 0, lastSigmaPad = 0, lastSigmaTime = 0, lastTrack = -1, lastEvent = 0;
363
364 for (size_t i = 0; i < nClusters; i++) {
365 const cluster_struct& cluster_org = clusters[i];
366 cluster_struct cluster = clusters[i];
367 if (cluster.pad >= 32768) {
368 printf("%d\n", cluster.pad);
369 }
370
371 if ((separate_sectors && cluster.sector != isector) || (separate_patches && cluster.patch != ipatch)) {
372 continue;
373 }
374 if (separate_sides && !(cluster.sector < 18 ^ isector < 18)) {
375 continue;
376 }
377
378 bool newTrack = lastTrack != cluster.trackID;
379 uint32_t dSigmaPad, dSigmaTime;
380
381 if (cluster.event != lastEvent) {
382 lastRow = lastPad = lastTime = lastSector = 0;
383 lastTrack = -1;
384 }
385
386 if (full_row_numbers) {
387 cluster.row += fgRows[cluster.patch][0];
388 }
389
390 if ((sector_diff || res_diff || track_diffqtot || track_diffqmax) && cluster.trackID != -1 && track_based) {
391 if (lastTrack != cluster.trackID) {
392 lastSector = lastResPad = lastResTime = lastQTot = lastQMax = lastSigmaPad = lastSigmaTime = 0;
393 }
394
395 if (sector_diff) {
396 do_diff(cluster.sector, lastSector, field_bits[SECTOR]);
397 }
398
399 if (res_diff) {
400 do_diff(cluster.resPad, lastResPad, field_bits[RES_PAD]);
401 do_diff(cluster.resTime, lastResTime, field_bits[RES_TIME]);
402 }
403
404 if (track_diffqtot) {
405 cluster.avgtot = cluster.qtot;
406 do_diff(cluster.avgtot, lastQTot, field_bits[QTOT]);
407 }
408 if (track_diffqmax) {
409 cluster.avgmax = cluster.qmax;
410 do_diff(cluster.avgmax, lastQMax, field_bits[QMAX]);
411 }
412 if (track_diffsigma) {
413 dSigmaPad = cluster.sigmaPad;
414 dSigmaTime = cluster.sigmaTime;
415 do_diff(dSigmaPad, lastSigmaPad, field_bits[SIGMA_PAD]);
416 do_diff(dSigmaTime, lastSigmaTime, field_bits[SIGMA_TIME]);
417 } else if (track_separate_sigma) {
418 dSigmaPad = truncate(SIGMA_PAD, cluster.sigmaPad);
419 dSigmaTime = truncate(SIGMA_TIME, cluster.sigmaTime);
420 }
421 }
422
423 if (cluster.row != lastRow) {
424 lastPad = lastTime = 0;
425 }
426 if (row_diff) {
427 do_diff(cluster.row, lastRow, field_bits[ROW]);
428 } else {
429 lastRow = cluster.row;
430 }
431
432 if (pad_diff && (cluster.trackID == -1 || !track_based)) {
433 do_diff(cluster.pad, lastPad, field_bits[PAD], (distinguish_rows ? fgNPadsMod[cluster_org.row + fgRows[cluster.patch][0]] : 140) * 60);
434 }
435 if (time_diff && (cluster.trackID == -1 || !track_based)) {
436 do_diff(cluster.time, lastTime, field_bits[TIME], 1024 * 25);
437 }
438
439 if (approximate_qtot && (!track_based || cluster.trackID == -1 || (track_avgtot == 0 && track_diffqtot == 0))) {
440 cluster.qtot -= cluster.sigmaPad * cluster.qmax / 3;
441 if (cluster.qtot < 0) {
442 cluster.qtot = -truncate(QTOT, -cluster.qtot);
443 } else {
444 cluster.qtot = truncate(QTOT, cluster.qtot);
445 }
446 cluster.qtot &= (1 << field_bits[QTOT]) - 1;
447 }
448
449 if (track_avgtot && cluster.trackID != -1) {
450 int32_t tmp = truncate(QTOT, cluster.qtot) - truncate(QTOT, cluster.avgtot);
451 if (newTrack) {
452 cluster.qtot = truncate(QTOT, cluster.avgtot);
453 }
454 cluster.avgtot = tmp & ((1 << field_bits[QTOT]) - 1);
455 }
456 if (track_avgmax && cluster.trackID != -1) {
457 int32_t tmp = cluster.qmax - cluster.avgmax;
458 if (newTrack) {
459 cluster.qmax = cluster.avgmax;
460 }
461 cluster.avgmax = tmp & ((1 << field_bits[QMAX]) - 1);
462 }
463
464 // Copy qmax / qtot to combined track avg... slot, to use for combine_maxtot
465 if ((((combine_maxtot && (track_avgtot || track_diffqtot)) || track_separate_q) && track_avgmax == 0 && track_diffqmax == 0) && cluster.trackID != -1) {
466 cluster.avgmax = cluster.qmax;
467 }
468 if ((((combine_maxtot && (track_avgmax || track_diffqmax)) || track_separate_q) && track_avgtot == 0 && track_diffqtot == 0) && cluster.trackID != -1) {
469 cluster.avgtot = cluster.qtot;
470 }
471
472 for (int32_t j = 0; j < sizeof(cluster_struct) / sizeof(uint32_t); j++) {
473 if (approximate_qtot && (j == QTOT || j == AVG_TOT)) {
474 continue;
475 }
476 if (track_avgtot && (j == QTOT || j == AVG_TOT)) {
477 continue;
478 }
479 cluster.vals[j] = truncate(j, cluster.vals[j]);
480 }
481
482 lastEvent = cluster.event;
483 lastTrack = cluster.trackID;
484
485 if (print_clusters > 0 || (print_clusters < 0 && i < -print_clusters)) {
486 printf("Event %u Track %d Sector %u Patch %u Row %u Pad %u Time %u sigmaPad %u sigmaTime %u qTot %u qMax %u Flag %u resPad %u resTime %u avgTot %u avgMax %u\n", cluster.event, cluster.trackID, cluster.sector, cluster.patch, cluster.row, cluster.pad, cluster.time, cluster.sigmaPad,
487 cluster.sigmaTime, cluster.qtot, cluster.qmax, cluster.splitPadTime, cluster.resPad, cluster.resTime, cluster.avgtot, cluster.avgmax);
488 }
489
490 for (int32_t j = SECTOR; j < nFields; j++) {
491 bool forceStore = false;
492 if (j == CLUSTER_ID || j == PATCH) {
493 continue;
494 }
495
496 if (j == SECTOR && (track_based == 0 || cluster.trackID == -1)) {
497 continue;
498 }
499
500 if (track_based && cluster.trackID != -1 && !newTrack) {
501 if (j == PAD || j == TIME || (j >= PAD_80 && j <= PAD_140)) {
502 continue;
503 }
504 if (j == RES_PAD || j == RES_TIME) {
505 cluster.vals[j] &= (1 << field_bits[j]) - 1;
506 forceStore = true;
507 }
508 }
509
510 if ((track_avgtot || track_diffqtot || track_separate_q) && cluster.trackID != -1) {
511 if (j == QTOT && (!newTrack || (track_avgtot == 0 && track_diffqtot == 0))) {
512 continue;
513 }
514 if (j == AVG_TOT && (track_diffqtot == 0 || !newTrack)) {
515 forceStore = true;
516 }
517 }
518 if ((track_avgmax || track_diffqmax || track_separate_q) && cluster.trackID != -1) {
519 if (j == QMAX && (!newTrack || (track_avgmax == 0 && track_diffqmax == 0))) {
520 continue;
521 }
522 if (j == AVG_MAX && (track_diffqmax == 0 || !newTrack)) {
523 forceStore = true;
524 }
525 }
526
527 if ((track_diffsigma || track_separate_sigma) && cluster.trackID != -1) {
528 if (j == SIGMA_PAD || j == SIGMA_TIME) {
529 continue;
530 }
531 if (j == DIFF_SIGMA_PAD) {
532 histograms[j][dSigmaPad]++;
533 counts[j]++;
534 }
535 if (j == DIFF_SIGMA_TIME) {
536 histograms[j][dSigmaTime]++;
537 counts[j]++;
538 }
539 }
540
541 if (track_based && row_diff && cluster.trackID != -1) {
542 if (j == ROW) {
543 continue;
544 }
545 int32_t myj = newTrack ? ROW_TRACK_FIRST : ROW_TRACK;
546 if (j == myj) {
547 histograms[myj][cluster.vals[ROW]]++;
548 counts[myj]++;
549 }
550 }
551
552 if (j <= FLAG_PADTIME || forceStore) {
553 if (cluster.vals[j] >= (1 << field_bits[j])) {
554 printf("Cluster value %d/%s out of bit range %d > %d\n", j, field_names[j], cluster.vals[j], (1 << field_bits[j]));
555 } else {
556 histograms[j][cluster.vals[j]]++;
557 counts[j]++;
558 }
559 } else if (j == QMAX_QTOT && (!track_based || cluster.trackID == -1 || (((track_avgmax == 0 && track_avgtot == 0 && track_diffqmax == 0 && track_diffqtot == 0) || newTrack) && track_separate_q == 0))) {
560 int32_t val = (cluster.qtot << field_bits[QMAX]) | cluster.qmax;
561 histograms[j][val]++;
562 counts[j]++;
563 } else if (((track_avgmax || track_avgtot || track_diffqmax || track_diffqtot) && !newTrack || track_separate_q) && cluster.trackID != -1 && j == AVG_TOT_MAX) {
564 int32_t val = (cluster.avgtot << field_bits[QMAX]) | cluster.avgmax;
565 histograms[j][val]++;
566 counts[j]++;
567 } else if (j == SIGMA_PAD_TIME && (!track_based || cluster.trackID == -1 || (track_diffsigma == 0 && track_separate_sigma == 0))) {
568 int32_t val = (cluster.sigmaTime << field_bits[SIGMA_PAD]) | cluster.sigmaPad;
569 histograms[j][val]++;
570 counts[j]++;
571 } else if ((track_diffsigma || track_separate_sigma) && cluster.trackID != -1 && j == DIFF_SIGMA_PAD_TIME) {
572 int32_t val = (dSigmaPad << field_bits[SIGMA_PAD]) | dSigmaTime;
573 histograms[j][val]++;
574 counts[j]++;
575 } else if (distinguish_rows && j >= PAD_80 && j <= PAD_140) {
576 int32_t myj = fgNPads[cluster_org.row + fgRows[cluster.patch][0]];
577 myj = (myj - (80 - 11)) / 12;
578 myj += PAD_80;
579 if (myj == j) {
580 if (cluster.pad >= (1 << field_bits[j])) {
581 printf("Cluster value %d/%s out of bit range %d > %d\n", j, field_names[j], cluster.vals[j], (1 << field_bits[j]));
582 } else {
583 histograms[j][cluster.pad]++;
584 counts[j]++;
585 }
586 }
587 }
588 }
589 nClustersUsed++;
590 }
591
592 printf("Clusters in block: %ld / %ld\n", nClustersUsed, nClusters);
593
594 double log2 = log(2.);
595 double entropies[nFields];
596 double huffmanSizes[nFields];
597 for (int32_t i = SECTOR; i < nFields; i++) {
598 if (i == CLUSTER_ID || i == PATCH) {
599 continue;
600 }
601 double entropy = 0.;
602 double huffmanSize = 0;
603
604 if (counts[i]) {
605 for (int32_t j = 0; j < (1 << field_bits[i]); j++) {
606 // printf("Field %d/%s Value %d Entries %ld\n", i, field_names[i], j, histograms[i][j]);
607
608 probabilities[i][j] = (double)histograms[i][j] / (double)counts[i];
609 if (probabilities[i][j]) {
610 double I = -log(probabilities[i][j]) / log2;
611 double H = I * probabilities[i][j];
612 // printf("Field %d/%s Value %d I prob %f I %f H %f\n", i, field_names[i], probabilities[i][j], I, H);
613
614 entropy += H;
615 }
616 }
617
618 INode* root = BuildTree(probabilities[i], 1 << field_bits[i]);
619
620 HuffCodeMap codes;
621 GenerateCodes(root, HuffCode(), codes);
622 delete root;
623
624 for (HuffCodeMap::const_iterator it = codes.begin(); it != codes.end(); it++) {
625 huffmanSize += it->second.size() * probabilities[i][it->first];
626 }
627 }
628 entropies[i] = entropy;
629 huffmanSizes[i] = huffmanSize;
630 }
631
632 int32_t rawBits = 0;
633 double entroTotal = 0., huffmanTotal = 0.;
634 for (int32_t i = SECTOR; i < nFields; i++) {
635 if (i == CLUSTER_ID || i == PATCH) {
636 continue;
637 }
638
639 if (i <= FLAG_PADTIME) {
640 rawBits += field_bits[i];
641 }
642
643 if (combine_maxtot && (i == QMAX || i == QTOT)) {
644 continue;
645 }
646 if (combine_sigmapadtime && (i == SIGMA_PAD || i == SIGMA_TIME)) {
647 continue;
648 }
650 continue;
651 }
652
653 if (distinguish_rows && i == PAD) {
654 continue;
655 }
656
660 (distinguish_rows && i >= PAD_80 && i <= PAD_140)) {
661 entroTotal += entropies[i] * counts[i];
662 huffmanTotal += huffmanSizes[i] * counts[i];
663 used[i] = 1;
664 }
665 }
666 for (int32_t i = SECTOR; i < nFields; i++) {
667 if (field_bits[i] == 0) {
668 continue;
669 }
670 if (counts[i] == 0) {
671 continue;
672 }
673 printf("Field %2d/%16s (count %10ld / used %1d) rawBits %2d huffman %9.6f entropy %9.6f\n", i, field_names[i], counts[i], used[i], field_bits[i], huffmanSizes[i], entropies[i]);
674 }
675 rawBits = 79; // Override incorrect calculation: Row is only 6 bit in raw format, and sector is not needed!
676 printf("Raw Bits: %d - Total Size %f MB Clusters %d\n", rawBits, (double)rawBits * (double)nClustersUsed / 8. / 1.e6, nClustersUsed);
677 printf("Huffman Bits: %f - Total Size %f MB\n", huffmanTotal / (double)nClustersUsed, huffmanTotal / 8. / 1.e6);
678 printf("Entropy Bits: %f - Total Size %f MB\n", entroTotal / (double)nClustersUsed, entroTotal / 8. / 1.e6);
679 printf("Maximum Compression Ratio: %f (Huffman %f)\n", (double)rawBits * (double)nClustersUsed / entroTotal, (double)rawBits * (double)nClustersUsed / huffmanTotal);
680 entrototalbytes += entroTotal;
681 rawtotalbytes += (double)rawBits * (double)nClustersUsed;
682
683 if (separate_sides && !separate_sectors && isector == 0) {
684 isector = 17;
685 } else if (!separate_sectors) {
686 isector = 9999999;
687 }
688
689 if (!separate_patches) {
690 ipatch = 9999999;
691 }
692 }
693 }
694
696 printf("Total Compression: %f\n", rawtotalbytes / entrototalbytes);
697 }
698
699 printf("Exiting\n");
700 for (int32_t i = SECTOR; i < nFields; i++) {
701 if (i == CLUSTER_ID || i == PATCH) {
702 continue;
703 }
704 delete[] histograms[i];
705 delete[] probabilities[i];
706 }
707 delete[] clusters;
708 return (0);
709}
int32_t i
bool const GPUTPCGMMerger::trackCluster * c1
uint32_t j
Definition RawData.h:0
int nClusters
struct _cl_event * event
Definition glcorearb.h:2982
GLdouble GLdouble right
Definition glcorearb.h:4077
GLdouble f
Definition glcorearb.h:310
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
Definition glcorearb.h:4150
GLuint GLfloat * val
Definition glcorearb.h:1582
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
const int32_t track_separate_sigma
const int32_t sort_time_mixed_bins
const uint32_t field_bits[]
void GenerateCodes(const INode *node, const HuffCode &prefix, HuffCodeMap &outCodes)
const int32_t combine_sigmapadtime
const int32_t optimized_negative_values
bool clustercompare_timepad(cluster_struct a, cluster_struct b)
void do_diff(uint32_t &val, int32_t &last, uint32_t bits, uint32_t maxval=0)
const int32_t sort_pad_mixed_bins
bool clustercompare_padtime_mixed(cluster_struct a, cluster_struct b)
uint32_t truncate(int32_t j, uint32_t val)
bool clustercompare_padtime(cluster_struct a, cluster_struct b)
std::vector< bool > HuffCode
INode * BuildTree(const double *frequencies, uint32_t UniqueSymbols)
std::map< uint32_t, HuffCode > HuffCodeMap
const uint32_t significant_bits[]
bool clustercompare_inevent(cluster_struct a, cluster_struct b)
bool clustercompare_timepad_mixed(cluster_struct a, cluster_struct b)
bool operator()(const INode *lhs, const INode *rhs) const
#define main
std::vector< Cluster > clusters