-
Notifications
You must be signed in to change notification settings - Fork 2
/
PCaenWindow.cxx
434 lines (400 loc) · 15.1 KB
/
PCaenWindow.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
//
// File: PCaenWindow.cxx
//
// Description: Window to display CAEN digitized trigger sums
//
// Revisions: 2012/03/06 - PH Created (borrowed heavily from PNCDScopeWindow.cxx)
//
#include <Xm/RowColumn.h>
#include <Xm/Form.h>
#include <Xm/DrawingA.h>
#include <math.h>
#include "PCaenWindow.h"
#include "PNCDScopeImage.h"
#include "ImageData.h"
#include "PSpeaker.h"
#include "PZdabFile.h"
#include "include/NcdDataTypes.h"
#include "CUtils.h"
const int kDirtyEvent = 0x02;
const int kDirtyAll = 0x04;
const int kAllCaenChannels = 0xff; // mask for all scope channels on
static MenuStruct channels_menu[] = {
{ "All Channels",0,XK_A,IDM_CAEN_ALL, NULL, 0, 0 },
{ "Channels with Data",0, XK_D,IDM_CAEN_ACTIVE, NULL, 0, 0 },
{ NULL, 0, 0, 0, NULL, 0, 0},
{ "Auto Scale", 0, XK_u,IDM_CAEN_AUTO, NULL, 0, MENU_TOGGLE },
{ "Log Color Sum", 0, XK_L,IDM_CAEN_LOG, NULL, 0, MENU_TOGGLE },
{ NULL, 0, 0, 0, NULL, 0, 0},
{ "Channel 0", 0, XK_0,IDM_CAEN_0, NULL, 0, MENU_TOGGLE | MENU_TOGGLE_ON },
{ "Channel 1", 0, XK_1,IDM_CAEN_1, NULL, 0, MENU_TOGGLE | MENU_TOGGLE_ON },
{ "Channel 2", 0, XK_2,IDM_CAEN_2, NULL, 0, MENU_TOGGLE | MENU_TOGGLE_ON },
{ "Channel 3", 0, XK_3,IDM_CAEN_3, NULL, 0, MENU_TOGGLE | MENU_TOGGLE_ON },
{ "Channel 4", 0, XK_4,IDM_CAEN_4, NULL, 0, MENU_TOGGLE | MENU_TOGGLE_ON },
{ "Channel 5", 0, XK_5,IDM_CAEN_5, NULL, 0, MENU_TOGGLE | MENU_TOGGLE_ON },
{ "Channel 6", 0, XK_6,IDM_CAEN_6, NULL, 0, MENU_TOGGLE | MENU_TOGGLE_ON },
{ "Channel 7", 0, XK_7,IDM_CAEN_7, NULL, 0, MENU_TOGGLE | MENU_TOGGLE_ON },
};
static MenuStruct caen_main_menu[] = {
{ "Display", 0, 0, 0, channels_menu, XtNumber(channels_menu), 0 },
};
//---------------------------------------------------------------------------
// PCaenWindow constructor
//
PCaenWindow::PCaenWindow(ImageData *data)
: PImageWindow(data)
{
int n;
Arg wargs[20];
data->mSpeaker->AddListener(this);
n = 0;
XtSetArg(wargs[n], XmNtitle, "CAEN Scope"); ++n;
XtSetArg(wargs[n], XmNx, 400); ++n;
XtSetArg(wargs[n], XmNy, 300); ++n;
XtSetArg(wargs[n], XmNminWidth, 100); ++n;
XtSetArg(wargs[n], XmNminHeight, 100); ++n;
SetShell(CreateShell("caenPop",data->toplevel,wargs,n));
n = 0;
XtSetArg(wargs[n], XmNwidth, 400); ++n;
XtSetArg(wargs[n], XmNheight, 600); ++n;
XtSetArg(wargs[n], XmNbackground, data->colour[BKG_COL]); ++n;
SetMainPane(XtCreateManagedWidget("caenMain", xmFormWidgetClass,GetShell(),wargs,n));
n = 0;
XtSetArg(wargs[n],XmNmarginHeight, 1); ++n;
XtSetArg(wargs[n],XmNleftAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n],XmNtopAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n],XmNrightAttachment, XmATTACH_FORM); ++n;
Widget menu = XmCreateMenuBar(GetMainPane(), "xsnoedMenu" , wargs, n);
XtManageChild(menu);
CreateMenu(menu, caen_main_menu, XtNumber(caen_main_menu), this);
// add resource labels to CAEN channels in menu
for (int i=0; i<kCaenRsrcNum; ++i) {
if (!strncmp(data->caen_lbl[i], "Channel ", 8)) continue;
char buff[256];
sprintf(buff, "Channel %d - %s", i, data->caen_lbl[i]);
GetMenu()->SetLabel(IDM_CAEN_0+i, buff);
}
GetMenu()->SetToggle(IDM_CAEN_AUTO, data->caen_auto);
GetMenu()->SetToggle(IDM_CAEN_LOG, data->caen_log);
n = 0;
XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_WIDGET); ++n;
XtSetArg(wargs[n], XmNtopWidget, menu); ++n;
XtSetArg(wargs[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n], XmNbackground, data->colour[BKG_COL]); ++n;
Widget form = XtCreateManagedWidget("caenForm", xmFormWidgetClass,GetMainPane(),wargs,n);
// get mask of channels we want to display (and be sure invalid bits aren't set)
int chan_mask = (data->caen_mask & kAllCaenChannels);
// display all channels by default if none set
if (!chan_mask) chan_mask = kAllCaenChannels;
for (int chan=0; chan<kMaxCaenChannels; ++chan) {
char buff[128];
sprintf(buff, "chan%d", chan);
n = 0;
XtSetArg(wargs[n], XmNbackground, data->colour[BKG_COL]); ++n;
XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_FORM); ++n;
XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); ++n;
mChannel[chan] = XtCreateManagedWidget(buff, xmDrawingAreaWidgetClass, form, wargs, n);
// create histogram
mHist[chan] = new PNCDScopeImage(this, mChannel[chan], 0);
mHist[chan]->AllowLabel(FALSE);
mHist[chan]->SetScaleLimits(0,15000,10);
mHist[chan]->SetScaleMin(0);
mHist[chan]->SetScaleMax(1000);
mHist[chan]->SetCalcObj(this);
if (chan < kCaenRsrcNum) {
mHist[chan]->SetLabel(data->caen_lbl[chan]);
mHist[chan]->SetYMin(data->caen_min[chan]);
mHist[chan]->SetYMax(data->caen_max[chan]);
} else {
sprintf(buff, "Channel %d", chan);
mHist[chan]->SetLabel(buff);
mHist[chan]->SetYMin(0);
mHist[chan]->SetYMax(4096);
}
mHist[chan]->SetStyle(kHistStyleSteps);
mHist[chan]->SetFixedBins();
mHist[chan]->SetPlotCol(SCOPE1_COL);
mHist[chan]->SetOverlayCol(SCOPE0_COL);
}
// set current channel mask to an invalid entry (with all channels on)
// to force our channels to be arranged initially
data->caen_mask = -1;
// arrange our channel histogram widgets in the window
SetChannels(chan_mask);
SetDirty(kDirtyAll);
}
PCaenWindow::~PCaenWindow()
{
for (int i=0; i<kMaxCaenChannels; ++i) {
delete mHist[i];
mHist[i] = NULL;
}
SetImage(NULL); // make sure base class doesn't re-delete our image
}
void PCaenWindow::Listen(int message, void *message_data)
{
switch (message) {
case kMessageNewEvent:
case kMessageEventCleared:
SetDirty(kDirtyEvent);
break;
case kMessageColoursChanged:
SetDirty(kDirtyAll);
break;
}
}
void PCaenWindow::DoMenuCommand(int anID)
{
ImageData *data = GetData();
switch (anID) {
case IDM_CAEN_ALL:
SetChannels(kAllCaenChannels);
SetDirty(kDirtyAll);
break;
case IDM_CAEN_ACTIVE:
SetChannels(GetActiveChannels());
SetDirty(kDirtyAll);
break;
case IDM_CAEN_AUTO:
data->caen_auto ^= 1;
if (data->caen_auto) SetDirty(kDirtyAll);
break;
case IDM_CAEN_LOG:
data->caen_log ^= 1;
SetDirty(kDirtyAll);
break;
case IDM_CAEN_0:
case IDM_CAEN_1:
case IDM_CAEN_2:
case IDM_CAEN_3:
case IDM_CAEN_4:
case IDM_CAEN_5:
case IDM_CAEN_6:
case IDM_CAEN_7:
SetChannels(data->caen_mask ^ (1 << (anID - IDM_CAEN_0)));
SetDirty(kDirtyAll);
break;
}
}
void PCaenWindow::DoneGrab(PNCDScopeImage *)
{
// update current Y scale settings
ImageData *data = GetData();
for (int chan=0; chan<kMaxCaenChannels && chan<kCaenRsrcNum; ++chan) {
data->caen_min[chan] = mHist[chan]->GetYMin();
data->caen_max[chan] = mHist[chan]->GetYMax();
}
}
// get mask for active scope channels
int PCaenWindow::GetActiveChannels()
{
int chan_mask = 0;
// set our channel(s) to be displayed
for (int chan=0; chan<kMaxCaenChannels; ++chan) {
// get bitmask of channels with data
if (mHist[chan]->GetDataPt() || mHist[chan]->GetOverlayPt()) {
chan_mask |= (1 << chan);
}
}
return(chan_mask);
}
// set our displayed channels
void PCaenWindow::SetChannels(int chan_mask)
{
int n, chan;
Arg wargs[20];
ImageData *data = GetData();
if (chan_mask && chan_mask != data->caen_mask) {
// count the number of channels displayed
int chan_count = 0;
for (chan=0; chan<kMaxCaenChannels; ++chan) {
if (chan_mask & (1 << chan)) {
++chan_count;
}
}
// make necessary attachments and remap displayed channels
float height = 100.0 / chan_count;
int count = 0;
for (chan=0; chan<kMaxCaenChannels; ++chan) {
// toggle on/off our channel displays
GetMenu()->SetToggle(IDM_CAEN_0 + chan, chan_mask & (1 << chan));
if (chan_mask & (1 << chan)) {
n = 0;
if (count) {
XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_POSITION); ++n;
XtSetArg(wargs[n], XmNtopPosition, int(count*height+0.5)); ++n;
} else {
XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_WIDGET); ++n;
XtSetArg(wargs[n], XmNtopWidget, GetMenu()->GetWidget()); ++n;
}
if (count < chan_count-1) {
XtSetArg(wargs[n], XmNbottomAttachment, XmATTACH_POSITION); ++n;
XtSetArg(wargs[n], XmNbottomPosition, int((count+1)*height+0.5)); ++n;
} else {
XtSetArg(wargs[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
}
XtSetValues(mChannel[chan], wargs, n);
++count;
}
}
// map necessary channels
for (chan=0; chan<kMaxCaenChannels; ++chan) {
// only map/unmap channels that have changed
if ((chan_mask ^ data->caen_mask) & (1 << chan)) {
// must be careful not to map a widget before it is realized
if (XtIsRealized(mChannel[chan])) {
if (chan_mask & (1 << chan)) {
// show this channel
XtSetMappedWhenManaged(mChannel[chan], True);
} else {
// hide this channel
XtSetMappedWhenManaged(mChannel[chan], False);
}
} else {
n = 0;
if (chan_mask & (1 << chan)) {
XtSetArg(wargs[n], XmNmappedWhenManaged, TRUE); ++n;
} else {
XtSetArg(wargs[n], XmNmappedWhenManaged, FALSE); ++n;
}
XtSetValues(mChannel[chan], wargs, n);
}
}
}
// set our canvas to the first displayed channel (for Print Image...)
for (chan=0; chan<kMaxCaenChannels; ++chan) {
if (chan_mask & (1 << chan)) {
SetImage(mHist[chan]);
break;
}
}
data->caen_mask = chan_mask;
}
}
// calculate range for 2D histogram (return 1 if OK)
int PCaenWindow::GetRange(PHistImage *hist, int noffset, int nbin, int *min, int *max)
{
ImageData *data = GetData();
if (!data->sum) return 0;
for (int chan=0; chan<kMaxCaenChannels; ++chan) {
if (hist != mHist[chan]) continue;
u_int32 *src = data->sum_caen[chan];
if (!src) break;
long caen_size = data->sum_caen_samples[chan];
if (nbin + noffset < 0) nbin = -noffset;
if (nbin + noffset > caen_size) nbin = caen_size - noffset;
for (int i=0; i<nbin; ++i) {
u_int32 *pt = src + (i + noffset) * 4096;
for (int j=0; j<4096; ++j) {
if (pt[j]) {
if (*min > j) *min = j;
break;
}
}
for (int j=4095; j>=0; --j) {
if (pt[j]) {
if (*max < j) *max = j;
break;
}
}
}
return 1;
}
return 0;
}
// recalculate 2D histogram
void PCaenWindow::DoCalc(PHistImage *hist)
{
for (int chan=0; chan<kMaxCaenChannels; ++chan) {
if (hist != mHist[chan]) continue;
ImageData *data = GetData();
u_int32 *src = data->sum_caen[chan];
if (!src) break;
long caen_size = data->sum_caen_samples[chan];
hist->CreateData(caen_size, 1); // make 2D data
unsigned long *dst = (unsigned long *)hist->GetDataPt();
if (!dst) break;
long numPix = hist->GetNumPix();
if (!numPix) continue;
unsigned long max = 0;
memset(dst, 0, numPix * caen_size * sizeof(unsigned long));
for (int i=0; i<caen_size; ++i) {
for (int j=0; j<4096; ++j) {
u_int32 val = src[i * 4096 + j];
if (!val) continue;
int pix = hist->GetPix(j);
if (pix <= 0) {
dst[i * numPix] += val;
} else if (pix >= numPix) {
dst[i * numPix + numPix - 1] += val;
} else {
unsigned long tmp = (dst[i * numPix + pix] += val);
if (max < tmp) max = tmp;
}
}
}
mMaxVal = max;
hist->SetNumTraces(data->sum_caen_count[chan]);
hist->SetScaleLimits(0,caen_size,10);
break;
}
}
// UpdateSelf
void PCaenWindow::UpdateSelf()
{
ImageData *data = GetData();
#ifdef PRINT_DRAWS
Printf("-updateCaenWindow\n");
#endif
int chan;
// load new event if necessary
if (IsDirty() & (kDirtyEvent | kDirtyAll)) {
// delete old data if it exists
for (chan=0; chan<kMaxCaenChannels; ++chan) {
if (mHist[chan]->GetDataPt() || mHist[chan]->GetOverlayPt()) {
mHist[chan]->CreateData(0);
mHist[chan]->SetAutoScale(data->caen_auto);
mHist[chan]->SetDirty();
} else if (IsDirty() & kDirtyAll) {
mHist[chan]->SetDirty();
}
mHist[chan]->SetStyle(data->sum ? kHistStyle2D : kHistStyleSteps);
mHist[chan]->SetLogScale(data->caen_log);
}
long caen_size = data->caen_size;
if (caen_size || data->sum) {
for (chan=0; chan<kMaxCaenChannels; ++chan) {
if (data->sum) caen_size = data->sum_caen_samples[chan];
PNCDScopeImage *hist = mHist[chan];
if (hist->GetScaleMax() > caen_size) {
hist->SetScaleMax(caen_size);
}
if (data->sum_caen[chan]) {
hist->SetDirty(kDirtyHistCalc); // histogram requires calculating
} else {
u_int16 *caen = data->caen_data[chan];
if (!caen) continue;
hist->CreateData(caen_size);
long *pt = hist->GetDataPt();
if (pt) {
hist->SetCalibrated(0, 0); // not displaying antilogged data
for (int i=0; i<caen_size; ++i) {
pt[i] = caen[i];
}
hist->SetScaleLimits(0,caen_size,10);
hist->SetDirty();
}
}
}
}
}
// Update necessary histograms
for (chan=0; chan<kMaxCaenChannels; ++chan) {
// update this channel if required and visible
if (mHist[chan]->IsDirty() && (data->caen_mask & (1<<chan))) {
mHist[chan]->Draw();
}
}
}