-
Notifications
You must be signed in to change notification settings - Fork 2
/
PEventHistogram.cxx
395 lines (349 loc) · 10.5 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
//==============================================================================
// File: PEventHistogram.cxx
//
// Copyright (c) 2017, Phil Harvey, Queen's University
//==============================================================================
/*
** 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 "ImageData.h"
#include "PEventHistogram.h"
#include "PImageWindow.h"
#include "PResourceManager.h"
#include "PScale.h"
#include "PUtils.h"
#include "AgedWindow.h"
#include "menu.h"
#define MIN_HIST_RANGE 5
#define MIN_HIST_BINS 5
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[0] = 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;
ClearOverlays();
mHistogram = NULL;
}
if (!mHistogram || !mOverlay[0]) {
if (mHistogram) delete [] mHistogram;
if (mOverlay[0]) delete [] mOverlay[0];
// allocate histogram and overlay arrays
mHistogram = new long[nbin];
mOverlay[0] = new long[nbin];
if (!mHistogram || !mOverlay[0]) {
Printf("Out of memory for histogram\n");
return;
}
mNumBins = nbin; // set number of bins
mNumOverlays = 1;
}
memset(mHistogram, 0, nbin * sizeof(long));
memset(mOverlay[0], 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 (isnan(val) || 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[0][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_ERROR:
strcat(buff," (mm)");
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;
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;
// (recalculate here the number of bins for some data types)
switch (data->wDataType) {
case IDM_DISP_WIRE:
case IDM_DISP_PAD:
nbin = 64;
break;
}
return(nbin);
}
nbin = data->hist_bins;
sIsAutoScale = 0;
/* get histogram scales */
switch (data->wDataType) {
case IDM_TIME:
first = data->time_min;
last = data->time_max;
break;
case IDM_HEIGHT:
first = data->height_min;
last = data->height_max;
break;
case IDM_ERROR:
first = data->error_min;
last = data->error_max;
break;
case IDM_DISP_WIRE:
first = 0;
last = NUM_AG_WIRES;
nbin = 64;
sIsAutoScale = 1;
break;
case IDM_DISP_PAD:
first = 0;
last = NUM_AG_PADS;
nbin = 64;
sIsAutoScale = 1;
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_TIME:
data->time_min = (int)first;
data->time_max = (int)last;
break;
case IDM_HEIGHT:
data->height_min = (int)first;
data->height_max = (int)last;
break;
case IDM_ERROR:
data->error_min = (int)first;
data->error_max = (int)last;
break;
case IDM_DISP_WIRE:
case IDM_DISP_PAD:
break; // do nothing for now (autoscaling)
}
}
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_TIME:
xmin = -4096;
xmax = 4096;
break;
case IDM_HEIGHT:
xmin = -4096;
xmax = 4096;
break;
case IDM_ERROR:
xmin = 0;
xmax = 100;
rmin = .1;
break;
case IDM_DISP_WIRE:
xmin = 0;
xmax = NUM_AG_WIRES;
break;
case IDM_DISP_PAD:
xmin = 0;
xmax = NUM_AG_PADS;
break;
default:
xmin = -1e6;
xmax = 1e6;
rmin = sMinRangeFloat;
break;
}
*min_pt = xmin;
*max_pt = xmax;
*min_rng = rmin;
}