-
Notifications
You must be signed in to change notification settings - Fork 2
/
PEventHistogram.cxx
639 lines (591 loc) · 14.9 KB
/
PEventHistogram.cxx
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
** class PEventHistogram
**
** This is a very special PHistImage object because the histogram
** scale for this object manifests itself as the hit colour for all
** image windows. As such, some of these methods are declared static
** to allow calculation of the histogram scale even in the absence
** of an instantiation of this class.
*/
#include <math.h>
#include "PEventHistogram.h"
#include "PImageWindow.h"
#include "PResourceManager.h"
#include "PScale.h"
#include "PUtils.h"
#include "XSnoedWindow.h"
#include "xsnoed.h"
#include "menu.h"
#define MIN_HIST_RANGE 5
#define MIN_HIST_BINS 5
// values for converting calibrated histogram scales
// (approx averages from B.Frati's distributions)
#define SPE_QHS 20 // channels for Qhs <spe> centroid
#define SPE_QHL 40 // channels for Qhl <spe> centroid
#define SPE_QLX 2 // channels for Qlx <spe> centroid
float PEventHistogram::sMaxCalTime = 1000.0;
float PEventHistogram::sMaxCalCharge = 10000.0;
float PEventHistogram::sMinRangeFloat = 0.01;
int PEventHistogram::sIsAutoScale = 0;
//---------------------------------------------------------------------------------------
// PEventHistogram constructor
//
PEventHistogram::PEventHistogram(PImageWindow *owner, Widget canvas)
: PHistImage(owner,canvas)
{
ImageData *data = owner->GetData();
mNumCols = data->num_cols;
mHistCols = new int[mNumCols];
mOverlayCol = NUM_COLOURS + mNumCols;
mIsLog = data->log_scale; // restore log scale setting
if (!mHistCols) quit("No memory for allocating colour array!");
for (int i=0; i<mNumCols; ++i) {
mHistCols[i] = NUM_COLOURS + i;
}
}
PEventHistogram::~PEventHistogram()
{
if (mGrabFlag) {
ResetGrab(1);
}
}
void PEventHistogram::Listen(int message, void *dataPt)
{
switch (message) {
case kMessageNewEvent:
case kMessageEventCleared:
case kMessageColoursChanged:
case kMessageHitsChanged:
SetDirty();
break;
default:
PImageCanvas::Listen(message, dataPt);
break;
}
}
PHistImage* PEventHistogram::GetEventHistogram(ImageData *data)
{
if (data->mWindow[HIST_WINDOW]) {
return (PHistImage *)((PImageWindow *)data->mWindow[HIST_WINDOW])->GetImage();
} else {
return NULL;
}
}
/*
** Make histogram and set x and y scale ranges
*/
void PEventHistogram::MakeHistogram()
{
ImageData *data = mOwner->GetData();
int i,n,num,slab;
long max;
HitInfo *hi = data->hits.hit_info;
long bit_mask;
long nbin = data->hist_bins;
float val, first, last, range;
int incr;
mUnderscale = 0;
mOverscale = 0;
// set the X scale to integer if the data type is integer
SetIntegerXScale(isIntegerDataType(data));
/* get histogram bins */
nbin = GetBins(data,&first, &last);
range = last - first;
/*
** Calculate and draw histogram
*/
if (mHistogram && nbin!=mNumBins) {
delete [] mHistogram;
delete [] mOverlay;
mHistogram = NULL;
mOverlay = NULL;
}
if (!mHistogram || !mOverlay) {
if (mHistogram) delete [] mHistogram;
if (mOverlay) delete [] mOverlay;
// allocate histogram and overlay arrays
mHistogram = new long[nbin];
mOverlay = new long[nbin];
if (!mHistogram || !mOverlay) {
Printf("Out of memory for histogram\n");
return;
}
mNumBins = nbin; // set number of bins
}
memset(mHistogram, 0, nbin * sizeof(long));
memset(mOverlay, 0, nbin * sizeof(long));
num = data->hits.num_nodes;
max = 0;
incr = 1;
// decide for ourselves whether the hit is under/overscale
// because we may be in the process of changing the scale
bit_mask = data->bit_mask & ~(HIT_UNDERSCALE | HIT_OVERSCALE);
for (i=0; i<num; ++i, ++hi) {
if (hi->flags & bit_mask) continue; /* only consider unmasked hits */
/* calculate bin number */
val = (getHitValPad(data, hi) - first) * nbin / range;
// convert val to an integral bin number
if (val < 0) {
// ignore underscale hits if masked out
if (data->bit_mask & HIT_UNDERSCALE) continue;
n = 0;
if (!(hi->flags & HIT_DISCARDED)) mUnderscale += incr;
} else if (val >= nbin) {
// ignore overscale hits if masked out
if (data->bit_mask & HIT_OVERSCALE) continue;
n = nbin - 1;
if (!(hi->flags & HIT_DISCARDED)) mOverscale += incr;
} else {
n = (int)val;
}
if ((mHistogram[n] += incr) > max) max = mHistogram[n];
// keep track of discarded hits in each bin
if (hi->flags & HIT_DISCARDED) mOverlay[n] += incr;
}
/* calculate a nice even maximum value for the y axis */
if (!(mGrabFlag & GRAB_Y)) {
slab = 5;
while (1) {
if (max/slab < 10) break;
slab *= 2;
if (max/slab < 10) break;
slab *= 5;
}
mYMax = (max/slab + 1) * slab;
if (mYMax < 10) mYMax = 10;
mYMin = 0;
}
// save the x scale range
mXMin = first;
mXMax = last;
}
/*
** Set histogram label string
*/
void PEventHistogram::SetHistogramLabel()
{
ImageData *data = mOwner->GetData();
if (!mLabel) {
mLabel = new char[128];
if (!mLabel) return;
}
GetHistogramLabel(data, mLabel);
}
void PEventHistogram::GetHistogramLabel(ImageData *data, char *buff)
{
strcpy(buff,data->dispName);
switch (data->wDataType) {
case IDM_DELTA_T:
if (data->nrcon) strcat(buff," (ns from fit)");
else strcat(buff," (ns from center)");
break;
#ifdef OPTICAL_CAL
case IDM_NHIT:
if (data->oca) strcat(buff," Ratio");
break;
#endif
case IDM_TAC:
if (data->wCalibrated != IDM_UNCALIBRATED) strcat(buff," (ns)");
else strcat(buff," (raw)");
break;
case IDM_QHS:
case IDM_QHL:
case IDM_QLX:
case IDM_QHL_QHS:
if (data->real_cal_scale) strcat(buff," (pe)");
else if (data->wCalibrated != IDM_UNCALIBRATED) strcat(buff," (ped corr)");
else strcat(buff," (raw)");
break;
default:
break;
}
}
void PEventHistogram::DoGrab(float xmin, float xmax)
{
mXMin = xmin;
mXMax = xmax;
CheckScaleRange();
}
// called after a grab is completed
void PEventHistogram::DoneGrab()
{
ImageData *data = mOwner->GetData();
data->log_scale = mIsLog; // keep log scale setting current
// must re-calculate hit values and redraw images if colour scale changed
SetBins(data, mXMin, mXMax);
calcHitVals(data);
sendMessage(data,kMessageHitsChanged);
}
void PEventHistogram::ResetGrab(int do_update)
{
// let the base class reset the grab flag
PHistImage::ResetGrab(do_update);
if (do_update) {
// reset colour scale to defaults if this is an auto-scale
if (sIsAutoScale) {
ImageData *data = mOwner->GetData();
SetBins(data, mXMin, mXMax);
calcHitVals(data);
sendMessage(data,kMessageHitsChanged);
} else {
// otherwise just redraw the histogram
SetDirty();
}
}
}
/* get histogram bin parameters */
long PEventHistogram::GetBins(ImageData *data, float *first_pt, float *last_pt)
{
long nbin;
float first, last, range;
long factor;
PHistImage *hist = GetEventHistogram(data);
/* handle manual scales */
if (hist && (hist->GetGrabFlag() & GRAB_X)) {
*first_pt = first = hist->GetScaleMin();
*last_pt = last = hist->GetScaleMax();
nbin = hist->GetNumBins();
range = last - first;
// must recalculate number of bins for some datatypes
switch (data->wDataType) {
case IDM_NHIT:
#ifdef OPTICAL_CAL
if (data->oca) break;
#endif
nbin = (long)range;
if (nbin < data->hist_bins) {
if (nbin < 5) nbin = 5;
} else {
/* limit the total number of bins */
while (nbin > data->hist_bins) {
nbin /= 2;
}
}
break;
case IDM_DISP_CRATE:
case IDM_DISP_CARD:
case IDM_DISP_CHANNEL:
case IDM_DISP_CELL:
nbin = (long)range;
break;
}
return(nbin);
}
nbin = data->hist_bins;
sIsAutoScale = 0;
/* get histogram scales */
switch (data->wDataType) {
case IDM_TAC:
if (data->wCalibrated != IDM_UNCALIBRATED) {
first = data->cal_tac_min;
last = data->cal_tac_max;
} else {
first = data->tac_min;
last = data->tac_max;
}
break;
case IDM_QHS:
if (data->real_cal_scale) {
first = data->cal_qhs_min / SPE_QHS;
last = data->cal_qhs_max / SPE_QHS;
} else if (data->wCalibrated != IDM_UNCALIBRATED) {
first = data->cal_qhs_min;
last = data->cal_qhs_max;
} else {
first = data->qhs_min;
last = data->qhs_max;
}
break;
case IDM_QHL:
if (data->real_cal_scale) {
first = data->cal_qhl_min / SPE_QHL;
last = data->cal_qhl_max / SPE_QHL;
} else if (data->wCalibrated != IDM_UNCALIBRATED) {
first = data->cal_qhl_min;
last = data->cal_qhl_max;
} else {
first = data->qhl_min;
last = data->qhl_max;
}
break;
case IDM_QLX:
if (data->real_cal_scale) {
first = data->cal_qlx_min / SPE_QLX;
last = data->cal_qlx_max / SPE_QLX;
} else if (data->wCalibrated != IDM_UNCALIBRATED) {
first = data->cal_qlx_min;
last = data->cal_qlx_max;
} else {
first = data->qlx_min;
last = data->qlx_max;
}
break;
case IDM_QHL_QHS:
if (data->real_cal_scale) {
first = data->cal_qhl_qhs_min / SPE_QHS;
last = data->cal_qhl_qhs_max / SPE_QHS;
} else if (data->wCalibrated != IDM_UNCALIBRATED) {
first = data->cal_qhl_qhs_min;
last = data->cal_qhl_qhs_max;
} else {
first = data->qhl_qhs_min;
last = data->qhl_qhs_max;
}
break;
case IDM_NHIT:
#ifdef OPTICAL_CAL
if (data->oca) {
first = data->cal_nhit_min;
last = data->cal_nhit_max;
break;
}
#endif
first = 0;
/* set range to the max number of hits per tube */
last = data->max_sum_nhit + 1;
if (last < MIN_HIST_BINS) last = MIN_HIST_BINS;
/* limit the total number of bins */
factor = 1;
for (nbin=(int)(last-first); nbin>data->hist_bins; nbin/=2) {
factor *= 2;
}
/* adjust last bin to avoid integer binning problems */
last = first + nbin * factor;
sIsAutoScale = 1;
break;
case IDM_CMOS_RATES:
first = data->cmos_rates_min;
last = data->cmos_rates_max;
break;
case IDM_DISP_CRATE:
first = 0;
last = nbin = NUM_SNO_CRATES;
sIsAutoScale = 1;
break;
case IDM_DISP_CARD:
first = 0;
last = nbin = NUM_CRATE_CARDS;
sIsAutoScale = 1;
break;
case IDM_DISP_CHANNEL:
first = 0;
last = nbin = NUM_CARD_CHANNELS;
sIsAutoScale = 1;
break;
case IDM_DISP_CELL:
first = 0;
last = nbin = NUM_CHANNEL_CELLS;
sIsAutoScale = 1;
break;
case IDM_DELTA_T:
first = data->delta_t_min;
last = data->delta_t_max;
break;
default:
first = data->extra_min;
last = data->extra_max;
sIsAutoScale = 0;
break;
}
// update current scale limits if the event histogram
if (hist && (hist->GetScaleMin()!=first || hist->GetScaleMax()!=last)) {
hist->SetScaleMin(first);
hist->SetScaleMax(last);
hist->UpdateScaleInfo();
}
*first_pt = first;
*last_pt = last;
return(nbin);
}
/* set histogram range */
void PEventHistogram::SetBins(ImageData *data, float first, float last)
{
/* set histogram scales */
switch (data->wDataType) {
case IDM_TAC:
if (data->wCalibrated != IDM_UNCALIBRATED) {
data->cal_tac_min = first;
data->cal_tac_max = last;
} else {
data->tac_min = (int)first;
data->tac_max = (int)last;
}
break;
case IDM_QHS:
if (data->real_cal_scale) {
data->cal_qhs_min = first * SPE_QHS;
data->cal_qhs_max = last * SPE_QHS;
} else if (data->wCalibrated != IDM_UNCALIBRATED) {
data->cal_qhs_min = first;
data->cal_qhs_max = last;
} else {
data->qhs_min = (int)first;
data->qhs_max = (int)last;
}
break;
case IDM_QHL:
if (data->real_cal_scale) {
data->cal_qhl_min = first * SPE_QHL;
data->cal_qhl_max = last * SPE_QHL;
} else if (data->wCalibrated != IDM_UNCALIBRATED) {
data->cal_qhl_min = first;
data->cal_qhl_max = last;
} else {
data->qhl_min = (int)first;
data->qhl_max = (int)last;
}
break;
case IDM_QLX:
if (data->real_cal_scale) {
data->cal_qlx_min = first * SPE_QLX;
data->cal_qlx_max = last * SPE_QLX;
} else if (data->wCalibrated != IDM_UNCALIBRATED) {
data->cal_qlx_min = first;
data->cal_qlx_max = last;
} else {
data->qlx_min = (int)first;
data->qlx_max = (int)last;
}
break;
case IDM_QHL_QHS:
if (data->real_cal_scale) {
data->cal_qhl_qhs_min = first * SPE_QHS;
data->cal_qhl_qhs_max = last * SPE_QHS;
} else if (data->wCalibrated != IDM_UNCALIBRATED) {
data->cal_qhl_qhs_min = first;
data->cal_qhl_qhs_max = last;
} else {
data->qhl_qhs_min = (int)first;
data->qhl_qhs_max = (int)last;
}
break;
case IDM_DELTA_T:
data->delta_t_min = first;
data->delta_t_max = last;
break;
case IDM_NHIT:
#ifdef OPTICAL_CAL
if (data->oca) {
data->cal_nhit_min = first;
data->cal_nhit_max = last;
break;
}
#endif
// fall through!
case IDM_DISP_CRATE:
case IDM_DISP_CARD:
case IDM_DISP_CHANNEL:
case IDM_DISP_CELL:
break; // do nothing for now (autoscaling)
case IDM_CMOS_RATES:
data->cmos_rates_min = (int)first;
data->cmos_rates_max = (int)last;
break;
default:
data->extra_min = first;
data->extra_max = last;
break;
}
}
void PEventHistogram::SetScaleLimits()
{
float min, max, min_rng;
GetLimits(mOwner->GetData(), &min, &max, &min_rng);
mXMinMin = min;
mXMaxMax = max;
mXMinRng = min_rng;
}
void PEventHistogram::SetMaxCalScale(float maxT, float maxQ, float minRng)
{
sMaxCalTime = maxT;
sMaxCalCharge = maxQ;
sMinRangeFloat = minRng;
}
/* get histogram maximum range */
void PEventHistogram::GetLimits(ImageData *data,float *min_pt, float *max_pt, float *min_rng)
{
float xmin, xmax, rmin;
rmin = MIN_HIST_RANGE; // default minimum range
/* get histogram scales */
switch (data->wDataType) {
case IDM_TAC:
if (data->wCalibrated != IDM_UNCALIBRATED) {
xmin = -sMaxCalTime;
xmax = sMaxCalTime;
rmin = sMinRangeFloat;
} else {
xmin = 0;
xmax = 4096;
}
break;
case IDM_QHS:
case IDM_QHL:
case IDM_QLX:
if (data->wCalibrated != IDM_UNCALIBRATED) {
xmin = -sMaxCalCharge;
xmax = sMaxCalCharge;
rmin = sMinRangeFloat;
} else {
xmin = 0;
xmax = 4096;
}
break;
case IDM_QHL_QHS:
if (data->wCalibrated != IDM_UNCALIBRATED) {
xmin = -sMaxCalCharge;
xmax = sMaxCalCharge;
rmin = sMinRangeFloat;
} else {
xmin = -4096;
xmax = 4096;
}
break;
case IDM_NHIT:
#ifdef OPTICAL_CAL
if (data->oca) {
xmin = 0;
xmax = 100;
rmin = sMinRangeFloat;
break;
}
#endif
xmin = 0;
/* set range to the max number of hits per tube */
xmax = data->max_sum_nhit + 1;
if (xmax < 5) xmax = 5;
break;
case IDM_CMOS_RATES:
xmin = 0;
xmax = 10e6;
break;
case IDM_DISP_CRATE:
xmin = 0;
xmax = NUM_SNO_CRATES;
break;
case IDM_DISP_CARD:
xmin = 0;
xmax = NUM_CRATE_CARDS;
break;
case IDM_DISP_CHANNEL:
xmin = 0;
xmax = NUM_CARD_CHANNELS;
break;
case IDM_DISP_CELL:
xmin = 0;
xmax = NUM_CHANNEL_CELLS;
break;
case IDM_DELTA_T:
xmin = -sMaxCalTime;
xmax = sMaxCalTime;
rmin = sMinRangeFloat;
break;
default:
xmin = -1e6;
xmax = 1e6;
rmin = sMinRangeFloat;
break;
}
*min_pt = xmin;
*max_pt = xmax;
*min_rng = rmin;
}