DPPCoincidence.cpp 7.84 KB
Newer Older
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
/*
 * 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 "DPPCoincidence.h"
#include "InstrumentAbstraction/CommandNames.h"
#include "DataProvider/ChangeAspect.h"
#include <common/base/Convert.h>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/format.hpp>
#include <string>
#include <cstdlib>
28
#include <common/base/ServerProperties.h>
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

namespace npp {

const std::string DPPCoincidence::NOMADCOINCIDENCE = "nomadcoincidencev";
const char DPPCoincidence::SHARED_MEMORY_NAME[] = "CoincidenceSharedMemory";
const char DPPCoincidence::SHARED_MEMORY_DATA_NAME[] = "data";
const char DPPCoincidence::SHARED_MEMORY_MATRIX_NAME[] = "matrix";
const char DPPCoincidence::SHARED_MEMORY_ADCRATES_NAME[] = "adc_rates";

using namespace common;
using namespace std;
using namespace boost;
using namespace boost::interprocess;

const std::string DPPCoincidence::TYPE = "dpp_coincidence";

DPPCoincidence::DPPCoincidence(const std::string& name) :
	DPPLiveProcess(name),
	_data(0),
	_xData(0),
	_ADCRates(0),
	_segment(0),
	_liveCoincidenceThread(0),
	_liveRunning(false),
	_liveRefreshThread(0) {

	dT.init(this, SAVE, "dT");
	cleanDT.init(this, SAVE, "clean_dT");
	processBlockSize.init(this, SAVE, "process_block_size");
	test.init(this, SAVE, "test");
	testFileName.init(this, SAVE, "test_file_name");
	xData.init(this, NOSAVE, "x_data");
	coincidenceResolution.init(this, SAVE, "coincidence_resolution");
	coincidenceData.init(this, NOSAVE, "coincidence_data");
	nOrder.init(this, NOSAVE, "n_order");
	refreshTimeS.init(this, SAVE, "refresh_time_s");
	one.init(this, NOSAVE, "one");

	order.init(this, NOSAVE, "order");
	globalRate.init(this, NOSAVE, "global_rate");
	globalCleanRate.init(this, NOSAVE, "global_clean_rate");
	detectorRate.init(this, NOSAVE, "detector_rate");
	detectorCleanRate.init(this, NOSAVE, "detector_clean_rate");

	ADCId.init(this, NOSAVE, "adc_id");
	ADCRate.init(this, NOSAVE, "adc_rate");
}

DPPCoincidence::DPPCoincidence(const DPPCoincidence& controller) :
	DPPLiveProcess(controller),
	_data(0),
	_xData(0),
	_ADCRates(0),
	_segment(0),
	_liveCoincidenceThread(0),
	_liveRunning(false),
	_liveRefreshThread(0){
}

DPPCoincidence::~DPPCoincidence() {

	shared_memory_object::remove(SHARED_MEMORY_NAME);

	delete _segment;
}

void DPPCoincidence::postConfiguration() {

	int32 matrixBits = coincidenceResolution();

	// ADC X data
	int32 matrixSize = 1 << matrixBits;
	_xData = new int32[matrixSize];
	// initializing
	for (int32 i = 0; i < matrixSize; ++i) {
		_xData[i] = i;
	}

	xData.set(_xData);
	xData.setSize(matrixSize);

	// matrix data
	shared_memory_object::remove(SHARED_MEMORY_NAME);

	const int32 segmentSize = (int32) ((1 << (matrixBits * 2)) * sizeof(int32_t) + 256 * sizeof(float64) + 128 * sizeof(float64));

	_segment = new managed_shared_memory(create_only, SHARED_MEMORY_NAME, segmentSize);

	// allocating first
	int32_t * matrix = _segment->construct<int32_t>(SHARED_MEMORY_MATRIX_NAME)[matrixSize * matrixSize](0);

	// allocating first
	_data = _segment->construct<float64>(SHARED_MEMORY_DATA_NAME)[4 * 7](0);

	// order
	nOrder.set(7);

	order.resize(7);
	globalRate.resize(7);
	globalCleanRate.resize(7);
	detectorRate.resize(7);
	detectorCleanRate.resize(7);

	// properties
	for (int32 i = 0; i < 7; i++) {
		order.set(i, i + 2);
	}

	coincidenceData.set((int32 *)matrix);
	coincidenceData.setSize(matrixSize * matrixSize);

	_ADCRates = _segment->construct<float64>(SHARED_MEMORY_ADCRATES_NAME)[128](0);

	ADCId.resize(MAX_ADC);
	ADCRate.resize(MAX_ADC);

	for (int32 i = 0; i < MAX_ADC; i++) {
		ADCId.set(i, i);
	}

	resetData();
}

void DPPCoincidence::start() {
	resetData();
	startLiveProcess();
}

void DPPCoincidence::stop() {
	stopLiveProcess();
}

void DPPCoincidence::raz() {
	resetData();
}

void DPPCoincidence::resetData() {

	// resetting values
	for (int32 i = 0; i < 7; ++i) {
		globalRate.set(i, 0.0);
		globalCleanRate.set(i, 0.0);
		detectorRate.set(i, 0.0);
		detectorCleanRate.set(i, 0.0);
	}

	// setting dynamic properties
	for (int32 i = 0; i < MAX_ADC; i++) {
		ADCRate.set(i, 0.0);
	}
}

void DPPCoincidence::startLiveProcess() {

	// if stop is taking time, waiting here
	cout << "waiting for synchronization before launching " << NOMADCOINCIDENCE << endl;
	boost::mutex::scoped_lock lock(_mutex);

	_liveRunning = true;

	_liveCoincidenceThread = new thread(bind(&DPPCoincidence::executeLiveProcess, this));
	_liveRefreshThread = new thread(bind(&DPPCoincidence::executeRefreshProcess, this));
}

void DPPCoincidence::stopLiveProcess() {

	boost::mutex::scoped_lock lock(_mutex);

	_liveRunning = false;

	if (_liveCoincidenceThread != 0) {

		string command("kill -2 `pidof ");
		command += NOMADCOINCIDENCE + "`";

		::system(command.c_str());

		_liveCoincidenceThread->join();
		delete _liveCoincidenceThread;
		_liveCoincidenceThread = 0;

		cout << "terminated " << NOMADCOINCIDENCE << endl;
	}

	if (_liveRefreshThread != 0) {

		_liveRefreshThread->join();
		delete _liveRefreshThread;
		_liveRefreshThread = 0;
	}
}

void DPPCoincidence::executeLiveProcess() {

	string command;

	command += NOMADCOINCIDENCE + " ";

	if (!test()) {

		size_t size = fileName().size();

		if (size < 6) {
			string prefix(6 - size, '0');
233
			command += common::ServerProperties::getInstance()->getNomadDataPath() + prefix + fileName() + ".lst ";
234
		} else {
235
			command += common::ServerProperties::getInstance()->getNomadDataPath() + fileName() + ".lst ";
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
		}
	} else {
		command += testFileName() + " ";
	}

	command += to<string>(dT() / 10) + " ";

	command += to<string>(cleanDT() / 10) + " ";

	command += "$HOME/.nomadserver/crateboardcoincidence.txt $HOME/.nomadserver/detectoradc.txt ";

	command += to<string>(coincidenceResolution()) + " ";
	command += to<string>(processBlockSize()) + " ";

	cout << command << endl;

	::system(command.c_str());
}

void DPPCoincidence::executeRefreshProcess() {

	while (_liveRunning) {
		sleep(refreshTimeS());

		// copying values
		for (int32 i = 0; i < 7; ++i) {
			globalRate.set(i, _data[i]);
			globalCleanRate.set(i, _data[7 + i]);
			detectorRate.set(i, _data[7 * 2 + i]);
			detectorCleanRate.set(i, _data[7 * 3 + i]);
		}

		coincidenceData.sendEvent();

		// setting dynamic properties
		for (int32  i = 0; i < numberOfADCs(); i++) {
			ADCRate.set(i, _ADCRates[i]);
		}
	}

	float64 crystalRate2 = globalRate(0);
	float64 crystalCleanRate2 = globalCleanRate(0);
	float64 detectorRate2 = detectorRate(0);
	float64 detectorCleanRate2 = detectorCleanRate(0);

	float64 crystalRate3 = globalRate(1);
	float64 crystalCleanRate3 = globalCleanRate(1);
	float64 detectorRate3 = detectorRate(1);
	float64 detectorCleanRate3 = detectorCleanRate(1);

	ostringstream ss1, ss2, ss3;

	ss1 << "  Order"
			<< "        Crystal (c/s)"
			<< "  Crystal clean (c/s)"
			<< "       Detector (c/s)"
			<< " Detector clean (c/s)";

	ss2 << "      2 " << boost::format("%20.3d") % crystalRate2 << " " << boost::format("%20.3d") % crystalCleanRate2 << " " << boost::format("%20.3d") % detectorRate2 << " " << boost::format("%20.3d") % detectorCleanRate2;
	ss3 << "      3 " << boost::format("%20.3d") % crystalRate3 << " " << boost::format("%20.3d") % crystalCleanRate3 << " " << boost::format("%20.3d") % detectorRate3 << " " << boost::format("%20.3d") % detectorCleanRate3;

	log(Level::s_Info) << ss1.str() << endlog;
	log(Level::s_Info) << ss2.str() << endlog;
	log(Level::s_Info) << ss3.str() << endlog;
}

}