DPPHistogram.cpp 6.03 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
/*
 * 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 "DPPHistogram.h"
#include "InstrumentAbstraction/CommandNames.h"
#include "DataProvider/ChangeAspect.h"
#include <common/base/Convert.h>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <cstdlib>
26
#include <common/base/ServerProperties.h>
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

namespace npp {

/*
 * Property names
 */
const std::string DPPHistogram::LIVEHISTO = "nomadhisto";
const char DPPHistogram::SHARED_MEMORY_NAME[] = "HistogramSharedMemory";
const char DPPHistogram::SHARED_MEMORY_DATA_NAME[] = "data";
const char DPPHistogram::SHARED_MEMORY_ADCRATES_NAME[] = "adc_rates";
const std::string DPPHistogram::HISTOGRAM = "histogram";

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

const std::string DPPHistogram::TYPE = "dpp_histogram";

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

	triggerChannel.init(this, SAVE, "trigger_channel");
	delay.init(this, SAVE, "delay");
	dT.init(this, SAVE, "dT");
	maxBlock.init(this, SAVE, "max_block");
	blockSize.init(this, SAVE, "block_size");
	test.init(this, SAVE, "test");
	testFileName.init(this, SAVE, "test_file_name");
	xData.init(this, NOSAVE, "x_data");
	one.init(this, NOSAVE, "one");
	refreshTimeS.init(this, SAVE, "refresh_time_s");

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

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

}

DPPHistogram::~DPPHistogram() {

	shared_memory_object::remove(SHARED_MEMORY_NAME);

	delete _segment;
}

void DPPHistogram::postConfiguration() {

	// ADC X data
	_xData = new int32[1];
	xData.set(_xData);
	xData.setSize(1);

	// ADC Y data
	shared_memory_object::remove(SHARED_MEMORY_NAME);
	_segment = new managed_shared_memory(create_only, SHARED_MEMORY_NAME, SEGMENT_SIZE);

	// allocating first
	_segment->construct<int32_t>(SHARED_MEMORY_DATA_NAME)[1](0);
	_ADCRates = _segment->construct<float64>(SHARED_MEMORY_ADCRATES_NAME)[128](0);

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

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

	resetData();
}

void DPPHistogram::start() {
	resetData();
	startLiveHisto();
}

void DPPHistogram::stop() {
	stopLiveHisto();
}

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

void DPPHistogram::resetData() {

	// number of elements
	int32_t size = numberOfChannels() * numberOfADCs();

	// ADC X data
	delete [] _xData;
	_xData = new int32[numberOfChannels()];

	// initializing
	for (int32 i = 0; i < numberOfChannels(); ++i) {
		_xData[i] = i;
	}

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

	// ADC Y data
	// destroying last array
	_segment->destroy<int32_t>(SHARED_MEMORY_DATA_NAME);

	// allocating new array
	int32_t * array = _segment->construct<int32_t>(SHARED_MEMORY_DATA_NAME)[size](0);

	// setting dynamic properties
	for (int32 i = 0; i < numberOfADCs(); i++) {
		histogram.set(i, (int32 *)&array[i * numberOfChannels()]);
		histogram.setSize(i, numberOfChannels());
	}

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

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

void DPPHistogram::startLiveHisto() {

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

	_liveRunning = true;

	_liveCoincidenceThread = new thread(bind(&DPPHistogram::executeLiveHisto, this));
	_liveRefreshThread = new thread(bind(&DPPHistogram::executeRefreshHisto, this));
	sendProgressEvent(100);
}

void DPPHistogram::stopLiveHisto() {

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

	_liveRunning = false;

	if (_liveCoincidenceThread != 0) {

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

		::system(command.c_str());

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

		cout << "terminated " << LIVEHISTO << endl;
	}

	if (_liveRefreshThread != 0) {

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

void DPPHistogram::executeLiveHisto() {

	string command;

	command += LIVEHISTO + " ";

	if (!test()) {

		size_t size = fileName().size();

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

	command += to<string>(triggerChannel()) + " ";

	// set in 1e-8s
	command += to<string>(delay() / 10) + " ";
	command += to<string>(dT() / 10) + " ";

	command += "$HOME/.nomadserver/crateboardhistogram.txt ";

	command += to<string>(maxBlock()) + " ";
	command += to<string>(blockSize()) + " ";

	cout << command << endl;

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

void DPPHistogram::executeRefreshHisto() {

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

		for (int32 i = 0; i < numberOfADCs(); ++i) {
			histogram.sendEvent(i);
		}

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

}