DPPAcquisitionController.cpp 14.5 KB
Newer Older
Paolo Mutti's avatar
Paolo Mutti committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Nomad Instrument Control Software
 *
 * Copyright 2011 Institut Laue-Langevin
 *
 * Licensed under the EUPL, Version 1.1 only (the "License");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://joinup.ec.europa.eu/software/page/eupl
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */

#include "DPPAcquisitionController.h"
Paolo Mutti's avatar
Paolo Mutti committed
20
#include "controllers/common/family/Families.h"
21
#include <lstdpp128/lstdpp.h>
Paolo Mutti's avatar
Paolo Mutti committed
22
23
24

using namespace std;
using namespace acquisition;
25
using namespace lstdpp128;
Paolo Mutti's avatar
Paolo Mutti committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

namespace npp {

const string DPPAcquisitionController::TYPE = "dppacquisition_setup";

const string DPPAcquisitionController::NO_LIVE_PROCESS = "none";
const string DPPAcquisitionController::HISTOGRAM_PROCESS = "histogram";
const string DPPAcquisitionController::COINCIDENCE_PROCESS = "coincidence";

/*
 * Constructor
 */
DPPAcquisitionController::DPPAcquisitionController(const string& name) :
		TimeAcquisitionController(name) {

Paolo Mutti's avatar
Paolo Mutti committed
41
42
	setFamily(family::ACQUISITION, family::SETTING);

Paolo Mutti's avatar
Paolo Mutti committed
43
44
45
46
47
	nbAdcControllers.init(this, SAVE, "nb_adc_controllers");
	nbBits.init(this, SAVE, "nb_bits");
	gateWidth.init(this, SAVE, "gate_width");
	scopeChannel.init(this, NOSAVE, "scope_channel");
	useScope.init(this, NOSAVE, "scope_mode");
Paolo Mutti's avatar
Paolo Mutti committed
48
	totalChannels.init(this, NOSAVE, "total_channels");
Paolo Mutti's avatar
Paolo Mutti committed
49
50
51
52
53
54
55
56
57
58
59
	useScopeStr.init(this, NOSAVE, "scope_mode_str");
	usePn1Coincidence.init(this, SAVE, "pn1_coincidence");
	liveProcessType.init(this, SAVE, "live_process_type");

	boardIndex.init(this, NOSAVE, "board_index");
	channelsPerBoard.init(this, NOSAVE, "channels_per_board");

	m_AcquisitionMode = AcquisitionCommon::PULSE_PROCESSING_MODE;

	registerFunction(TYPE);

60
61
	m_histogramController.init(this, "histogram_controller");
	m_coincidenceController.init(this, "coincidence_controller");
Paolo Mutti's avatar
Paolo Mutti committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

	registerRefresher(nbAdcControllers, &DPPAcquisitionController::refreshNbAdcControllersProperty, this);
}

/*
 * Destructor
 */
DPPAcquisitionController::~DPPAcquisitionController() {

}

/*
 * postConfiguration
 */
void DPPAcquisitionController::postConfiguration() {

	TimeAcquisitionController::postConfiguration();

	// Copy local ptr for setting the write type cast
81
	dppDriver.resize(m_nRegisteredDrivers);
Paolo Mutti's avatar
Paolo Mutti committed
82
	for (int32 i = 0; i < m_nRegisteredDrivers; ++i) {
83
84
85
86
87
88
		dppDriver[i].copy(m_Drivers[i]);
		registerUpdater(dppDriver[i]->curve0, &DPPAcquisitionController::updateCurve0ArrayProperty, this, i);
		registerUpdater(dppDriver[i]->curve1, &DPPAcquisitionController::updateCurve1ArrayProperty, this, i);
		registerUpdater(dppDriver[i]->curve2, &DPPAcquisitionController::updateCurve2ArrayProperty, this, i);
		registerUpdater(dppDriver[i]->curve3, &DPPAcquisitionController::updateCurve3ArrayProperty, this, i);
		registerUpdater(dppDriver[i]->curve4, &DPPAcquisitionController::updateCurve4ArrayProperty, this, i);
Paolo Mutti's avatar
Paolo Mutti committed
89
90
91
92
93
94
	}

	registerRefresher(usePn1Coincidence, &DPPAcquisitionController::refreshUsePn1CoincidenceModeProperty, this);
	registerRefresher(nbBits, &DPPAcquisitionController::refreshNbBitsProperty, this);

	for (int32 i = 0; i < nbAdcControllers(); ++i) {
95
96
97
98
		registerUpdater(adcController[i]->dppAcqMode, &DPPAcquisitionController::updateUseScopeProperty, this, i);
		registerUpdater(adcController[i]->displayChannel, &DPPAcquisitionController::updateUseScopeProperty, this, i);
		registerUpdater(adcController[i]->channels, &DPPAcquisitionController::setChannelsPerBoard, this);
		registerUpdater(adcController[i]->recordLength, &DPPAcquisitionController::updateRecordLengthProperty, this, i);
Paolo Mutti's avatar
Paolo Mutti committed
99
100
101
102
	}

	setChannelsPerBoard();
	setScopeMode();
103
	setCrateNumber();
104
105

	registerRefresher(liveProcessType, &DPPAcquisitionController::refreshLiveProcessTypeProperty, this);
Paolo Mutti's avatar
Paolo Mutti committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
}

/*
 * refreshNDriversProperty
 */
void DPPAcquisitionController::refreshNDriversProperty(int32 aValue) throw (CannotSetValue) {

	boardIndex.resize(aValue);
	channelsPerBoard.resize(aValue);

	TimeAcquisitionController::refreshNDriversProperty(aValue);
}

/*
 * refreshNbAdcControllersProperty
 */
void DPPAcquisitionController::refreshNbAdcControllersProperty(int32 aValue) throw (CannotSetValue) {

	if (aValue == nbAdcControllers()) {
		return;
	}
127
	adcController.resize(aValue);
Paolo Mutti's avatar
Paolo Mutti committed
128
129
130

	for (int32 i = 0; i < aValue; ++i) {
		string controllerName = "adc_controller" + lexical_cast<string>(i + 1);
131
		adcController[i].init(this, controllerName);
Paolo Mutti's avatar
Paolo Mutti committed
132
133
134
135
136
	}

}

/*
137
138
139
 * updateRecordLengthProperty
 */
void DPPAcquisitionController::updateRecordLengthProperty(int32 index) {
140
	int32 rencordlength = adcController[index]->recordLength();
141
	for (int32 i = 0; i < m_nRegisteredDrivers; ++i) {
142
		dppDriver[i]->recordLength = rencordlength;
143
144
145
146
147
	}
}

/*
 * updateCurve0ArrayProperty
Paolo Mutti's avatar
Paolo Mutti committed
148
 */
149
void DPPAcquisitionController::updateCurve0ArrayProperty(int32 index) {
Paolo Mutti's avatar
Paolo Mutti committed
150
151

	for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
152
153
154
		adcController[adc]->curve0.update(dppDriver[index]->curve0());
		adcController[adc]->curve0.setSize(dppDriver[index]->curve0.getSize());
		adcController[adc]->curve0.sendEvent();
155
156
157
158
159
160
161
162
163
	}
}

/*
 * updateCurve1ArrayProperty
 */
void DPPAcquisitionController::updateCurve1ArrayProperty(int32 index) {

	for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
164
165
166
		adcController[adc]->curve1.update(dppDriver[index]->curve1());
		adcController[adc]->curve1.setSize(dppDriver[index]->curve1.getSize());
		adcController[adc]->curve1.sendEvent();
167
168
169
170
171
172
173
174
175
	}
}

/*
 * updateCurve2ArrayProperty
 */
void DPPAcquisitionController::updateCurve2ArrayProperty(int32 index) {

	for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
176
177
178
		adcController[adc]->curve2.update(dppDriver[index]->curve2());
		adcController[adc]->curve2.setSize(dppDriver[index]->curve2.getSize());
		adcController[adc]->curve2.sendEvent();
179
180
181
182
183
184
185
186
187
	}
}

/*
 * updateCurve3ArrayProperty
 */
void DPPAcquisitionController::updateCurve3ArrayProperty(int32 index) {

	for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
188
189
190
		adcController[adc]->curve3.update(dppDriver[index]->curve3());
		adcController[adc]->curve3.setSize(dppDriver[index]->curve3.getSize());
		adcController[adc]->curve3.sendEvent();
191
192
193
194
195
196
197
198
199
	}
}

/*
 * updateCurve4ArrayProperty
 */
void DPPAcquisitionController::updateCurve4ArrayProperty(int32 index) {

	for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
200
201
202
		adcController[adc]->curve4.update(dppDriver[index]->curve4());
		adcController[adc]->curve4.setSize(dppDriver[index]->curve4.getSize());
		adcController[adc]->curve4.sendEvent();
Paolo Mutti's avatar
Paolo Mutti committed
203
204
205
206
	}
}

/*
Paolo Mutti's avatar
Paolo Mutti committed
207
 * updateUseScopeProperty
Paolo Mutti's avatar
Paolo Mutti committed
208
209
210
211
 */
void DPPAcquisitionController::updateUseScopeProperty(int32 index) {

	int32 boardChannel = 0;
212
	int32 aValue = adcController[index]->dppAcqMode();
Paolo Mutti's avatar
Paolo Mutti committed
213

214
	if (aValue == acquisition::DPPAcquisitionCommon::SCOPE_MODE) {
Paolo Mutti's avatar
Paolo Mutti committed
215
216
		for (int32 i=0; i<nbAdcControllers(); ++i) {
			if (i != index) {
217
				adcController[i]->dppAcqMode = acquisition::DPPAcquisitionCommon::LISTMODE_MODE;
Paolo Mutti's avatar
Paolo Mutti committed
218
219
			}
		}
Paolo Mutti's avatar
Paolo Mutti committed
220
221
222
		useScope = 1;
		useScopeStr = "Active";
		for (int32 i=0; i<index; ++i) {
223
			boardChannel += adcController[i]->channels();
Paolo Mutti's avatar
Paolo Mutti committed
224
		}
225
		boardChannel += adcController[index]->displayChannel();
Paolo Mutti's avatar
Paolo Mutti committed
226
227
		useListMode = 0;
		usePn1Coincidence = 0;
228
229
	}
	else if (aValue == acquisition::DPPAcquisitionCommon::LISTMODE_MODE) {
Paolo Mutti's avatar
Paolo Mutti committed
230
231
232
233
		useScope = 0;
		useScopeStr = "Inactive";
	}

Paolo Mutti's avatar
Paolo Mutti committed
234
	scopeChannel = boardChannel;
235
236
237
238
239
240
241
	int32 board = adcController[index]->acqBoardIndex();
	dppDriver[board]->useScope = useScope();
	dppDriver[board]->scopeChannel = boardChannel;
	dppDriver[board]->scopeCardActivated = adcController[index]->modelName();
	dppDriver[board]->recordLength =  adcController[index]->recordLength();
	dppDriver[board]->useListMode = useListMode();
	dppDriver[board]->usePn1Coincidence = usePn1Coincidence();
Paolo Mutti's avatar
Paolo Mutti committed
242
243
244
245
246
247
248
249
}

/*
 * refreshUseListModeProperty
 */
void DPPAcquisitionController::refreshUseListModeProperty(int32 aValue) throw (CannotSetValue) {

	for (int32 board=0; board<nDrivers(); ++board) {
250
		dppDriver[board]->useListMode = aValue;
Paolo Mutti's avatar
Paolo Mutti committed
251
252
253
254
	}

	if (aValue == 1) {
		for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
255
			adcController[adc]->dppAcqMode = acquisition::DPPAcquisitionCommon::LISTMODE_MODE;
Paolo Mutti's avatar
Paolo Mutti committed
256
257
258
259
260
261
262
263
264
265
		}
	}
}

/*
 * refreshUseListModeProperty
 */
void DPPAcquisitionController::refreshUsePn1CoincidenceModeProperty(int32 aValue) throw (CannotSetValue) {

	for (int32 board=0; board<nDrivers(); ++board) {
266
267
		dppDriver[board]->usePn1Coincidence = aValue;
		dppDriver[board]->gateWidth = gateWidth();
Paolo Mutti's avatar
Paolo Mutti committed
268
269
270
271
	}

	if (aValue == 1) {
		for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
272
			adcController[adc]->dppAcqMode = acquisition::DPPAcquisitionCommon::LISTMODE_MODE;
Paolo Mutti's avatar
Paolo Mutti committed
273
274
275
276
277
278
279
280
281
282
		}
	}
}

/*
 * refreshNbBitsProperty
 */
void DPPAcquisitionController::refreshNbBitsProperty(int32 aValue) throw (CannotSetValue) {

	for (int32 i = 0; i < m_nRegisteredDrivers; ++i) {
283
		dppDriver[i]->nbBits = aValue;
Paolo Mutti's avatar
Paolo Mutti committed
284
285
286
287
288
289
290
291
	}
}

/*
 * refreshLiveProcessTypeProperty
 */
void DPPAcquisitionController::refreshLiveProcessTypeProperty(const std::string& aValue) throw (CannotSetValue) {

292
293
	m_histogramController->raz();
	m_coincidenceController->raz();
Paolo Mutti's avatar
Paolo Mutti committed
294

295
296
297
298
299
300
	if (aValue == HISTOGRAM_PROCESS) {
		m_liveProcessController = m_histogramController;
	} else if (aValue == COINCIDENCE_PROCESS) {
		m_liveProcessController = m_coincidenceController;
	} else {
		m_liveProcessController.assign(0);
Paolo Mutti's avatar
Paolo Mutti committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
	}
}

/*
 * resize
 */
void DPPAcquisitionController::resize() throw (CannotOpenFile, SlicesFileError, TooManySlices, SliceTooBig) {

	int32 resolution = (int32) pow(2.0, nbBits());
	m_Detector->setDataSize(resolution, 1);
}

/*
 * writeParams
 */
void DPPAcquisitionController::writeParams() {

	int32 totalAdc = 0;
	for (int32 i = 0; i < nDrivers(); ++i) {
		totalAdc += channelsPerBoard.get(i);
	}
Paolo Mutti's avatar
Paolo Mutti committed
322
	totalChannels = totalAdc;
Paolo Mutti's avatar
Paolo Mutti committed
323
324

	log(Level::s_Debug) << name << cursor << "Sending DPP parameters to acquisition card : listmode " << useListMode()
Paolo Mutti's avatar
Paolo Mutti committed
325
			<< " , wanted adc " << totalChannels() << " , wanted resolution " << nbBits() << endlog;
Paolo Mutti's avatar
Paolo Mutti committed
326
327
328

	Parallel::begin();
	for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
329
		adcController[adc]->clearDataParallel();
Paolo Mutti's avatar
Paolo Mutti committed
330
331
332
333
334
335
336
	}
	Parallel::end();

	if (isModeChanged() == true) {

		Parallel::begin();
		for (int32 i = 0; i < nDrivers(); ++i) {
337
338
339
340
341
342
343
344
			dppDriver[i]->wantedMode = m_AcquisitionMode;
			dppDriver[i]->nbBits = nbBits();
			dppDriver[i]->nbChannels = channelsPerBoard.get(i);
			dppDriver[i]->scopeChannel = scopeChannel();
			dppDriver[i]->gateWidth = gateWidth();
			dppDriver[i]->usePn1Coincidence = usePn1Coincidence();
			dppDriver[i]->useListMode = useListMode();
			dppDriver[i]->useScope = useScope();
Paolo Mutti's avatar
Paolo Mutti committed
345
346
347
348

			/*
			 * Execute the write param command
			 */
349
			dppDriver[i].executeParallel(AcquisitionCommon::WRITE_PARAMETERS_COMMAND);
Paolo Mutti's avatar
Paolo Mutti committed
350
351
352
353
354
355
356
		}
		Parallel::end();

		/*
		 * Execute the write param command
		 */
		for (int32 i = 0; i < nDrivers(); ++i) {
357
			dppDriver[i].execute(AcquisitionCommon::LIST_MODE_COMMAND, false, true);
Paolo Mutti's avatar
Paolo Mutti committed
358
359
		}

360
361
		// starting DPPCoincidence
		if (useListMode() && m_liveProcessController.isAssigned()) {
Paolo Mutti's avatar
Paolo Mutti committed
362

363
364
			m_liveProcessController->numberOfADCs = totalAdc;
			m_liveProcessController->numberOfChannels = (1 << nbBits());
Paolo Mutti's avatar
Paolo Mutti committed
365

366
367
368
			// set the lst context
			setContext();

369
370
			// start the process in parallel to avoid blocking
			m_liveProcessController->startParallel();
Paolo Mutti's avatar
Paolo Mutti committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387

			if (checkStatus() == true) {
				log(Level::s_Debug) << name << emptycursor << "DPP parameters accepted by acquisition card(s): " << totalAdc
						<< " x " << nbBits() << endlog;
			}
		}
	}
}

/*
 * stop
 */
void DPPAcquisitionController::stop() {

	TimeAcquisitionController::stop();

	// stopping DPPCoincidence
388
389
	if (m_liveProcessController.isAssigned()) {
		m_liveProcessController->stopCommand();
Paolo Mutti's avatar
Paolo Mutti committed
390
391
392
393
394
395
396
397
398
399
400
401
402
	}
}

/*
 * useEnergy
 */
bool DPPAcquisitionController::useEnergy() const {
	return true;
}

void DPPAcquisitionController::setScopeMode() {

	int32 scope = 0;
Paolo Mutti's avatar
Paolo Mutti committed
403
404
	int32 boardChannel = 0;
	int32 index = 0;
Paolo Mutti's avatar
Paolo Mutti committed
405
406
	for (int32 board=0; board<nDrivers(); ++board) {
		for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
407
			if (adcController[adc]->dppAcqMode() == acquisition::DPPAcquisitionCommon::SCOPE_MODE) {
Paolo Mutti's avatar
Paolo Mutti committed
408
				index = adc;
Paolo Mutti's avatar
Paolo Mutti committed
409
410
411
412
413
414
				scope = 1;
				useScopeStr = "Active";
			} else {
				useScopeStr = "Inactive";
			}
		}
Paolo Mutti's avatar
Paolo Mutti committed
415
416
		if (scope == 1) {
			for (int32 i=0; i<index; ++i) {
417
				boardChannel += adcController[i]->channels();
Paolo Mutti's avatar
Paolo Mutti committed
418
			}
419
			boardChannel += adcController[index]->displayChannel();
Paolo Mutti's avatar
Paolo Mutti committed
420
421
			scopeChannel = boardChannel;
		}
422
423
424
425
426
427
		dppDriver[board]->useScope = scope;
		dppDriver[board]->scopeChannel = boardChannel;
		dppDriver[board]->scopeCardActivated = adcController[index]->modelName();
		dppDriver[board]->recordLength =  adcController[index]->recordLength();
		dppDriver[board]->useListMode = useListMode();
		dppDriver[board]->usePn1Coincidence = usePn1Coincidence();
Paolo Mutti's avatar
Paolo Mutti committed
428
429
430
431
432
433
434
435
436
	}
	useScope = scope;
}

void DPPAcquisitionController::setChannelsPerBoard() {

	for (int32 board=0; board<nDrivers(); ++board) {
		int32 totalChannels = 0;
		for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
437
438
			if (adcController[adc]->acqBoardIndex() == board) {
				totalChannels += adcController[adc]->channels();
Paolo Mutti's avatar
Paolo Mutti committed
439
440
441
442
			}
		}
		boardIndex.set(board, board);
		channelsPerBoard.set(board, totalChannels);
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
		dppDriver[board]->nbChannels = totalChannels;
	}
}

void DPPAcquisitionController::setCrateNumber() {
	for (int32 board=0; board<nDrivers(); ++board) {
		for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
			if (adcController[adc]->acqBoardIndex() == board) {
				adcController[adc]->crateNumber = dppDriver[board]->crateNumber();
			}
		}
	}
}

/**
 * Warning: we suppose that the list mode block is ordered like the ADC controllers
 */
void DPPAcquisitionController::setContext() {

	releaseListModeContext(listModeContext);

	int32 nbCrates = 0;

	for (int32 adc = 0; adc < nbAdcControllers(); ++adc) {
		int32 crate = adcController[adc]->crateNumber();
		if (crate >= nbCrates) {
			nbCrates = crate + 1;
		}
	}

	listModeContext.crateBoard.nbCrates = nbCrates;
	listModeContext.crateBoard.crates = new Crate[nbCrates];

	for (int32 crate = 0; crate < nbCrates; ++crate) {
		int32 nbBoards = 0;

		for (int32 board = 0; board < nbAdcControllers(); ++board) {
			if (board >= nbBoards) {
				nbBoards = board + 1;
			}
		}

		listModeContext.crateBoard.crates[crate].nbBoards = nbBoards;
		listModeContext.crateBoard.crates[crate].boards = new Board[nbBoards];

		for (int32 board = 0; board < nbAdcControllers(); ++board) {
			listModeContext.crateBoard.crates[0].boards[board].boardType = (BoardType)adcController[board]->cardType();
			listModeContext.crateBoard.crates[0].boards[board].crate = crate;
			listModeContext.crateBoard.crates[0].boards[board].eventType = 0;
			listModeContext.crateBoard.crates[0].boards[board].nbChannels = adcController[board]->nbChannels();
		}
Paolo Mutti's avatar
Paolo Mutti committed
494
	}
495
496
497

	cout << "context before live process" << endl;
	cout << listModeContext << endl;
Paolo Mutti's avatar
Paolo Mutti committed
498
499
500
}

}