Plot.cpp 8.8 KB
Newer Older
legoc's avatar
legoc committed
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
/*
 * 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://www.osor.eu/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 <iostream>
#include <stdint.h>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <boost/tuple/tuple.hpp>
#include <boost/lexical_cast.hpp>

#define GNUPLOT_ENABLE_PTY
#include "gnuplot-iostream.h"

#include "../../EventData.h"
#include "../../Reader.h"

using namespace std;
using namespace lstdpp128;

const int BUFFER_SIZE = 1 << 10;

std::vector<int32_t> split(const std::string& content) {

	vector<int32_t> result;

	int lastIndex = 0;
	int index = content.find(',');
	while (index != string::npos) {

		istringstream is(content.substr(lastIndex, index - lastIndex));
		int32_t value;
		is >> value;

		result.push_back(value);
		lastIndex = index + 1;
		index = content.find(',', lastIndex);
	}

	istringstream is(content.substr(lastIndex));
	int32_t value;
	is >> value;

	result.push_back(value);

	return result;
}

//energy, q-short, q-long, Pos. X, Pos. Y, A, A+B, 0-cross
enum DataInfo {
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	NONE 			= 0,
	ENERGY 			= 1,
	Q_SHORT 		= 2,
	Q_LONG 			= 3,
	Q_RATIO 		= 4,
	POSITION_X 		= 5,
	POSITION_Y 		= 6,
	A 				= 7,
	AplusB 			= 8,
	ZERO_CROSSING 	= 9,
	DATA			= 10,
	DATA_SIGNED		= 11,
	DATAPLUS		= 12,
	DATAPLUS_SIGNED	= 13
legoc's avatar
legoc committed
81
82
};

83
84
85
86
87
int32_t toSigned(uint32_t data) {
	return *reinterpret_cast<int32_t *>(&data);
}

std::string dataInfoToString(DataInfo dataInfo) {
legoc's avatar
legoc committed
88
89

	switch (dataInfo) {
90
91
92
	case NONE:
		return "None";

legoc's avatar
legoc committed
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
	case ENERGY:
		return "Energy";

	case Q_SHORT:
		return "QShort";

	case Q_LONG:
		return "QLong";

	case Q_RATIO:
		return "QRatio";

	case POSITION_X:
		return "PositionX";

	case POSITION_Y:
		return "PositionY";

	case A:
		return "A";

	case AplusB:
		return "A+B";

	case ZERO_CROSSING:
		return "Zero Crossing";
119
120
121
122
123
124
125
126
127
128
129
130

	case DATA:
		return "Data";

	case DATA_SIGNED:
		return "Data signed";

	case DATAPLUS:
		return "Data plus";

	case DATAPLUS_SIGNED:
		return "Data plus signed";
legoc's avatar
legoc committed
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
	}

	return 0;
}

double eventData(Event const & event, DataInfo dataInfo) {

	switch (dataInfo) {
	case ENERGY:
		return eventEnergy(event);

	case Q_SHORT:
		return eventQShort(event);

	case Q_LONG:
		return eventQLong(event);

	case Q_RATIO:
		return eventQRatio(event);

	case POSITION_X:
		return eventPositionX(event);

	case POSITION_Y:
		return eventPositionY(event);

	case A:
		return eventA(event);

	case AplusB:
		return eventAplusB(event);

	case ZERO_CROSSING:
		return eventZeroCrossing(event);
165
166
167
168
169
170
171
172
173
174
175
176

	case DATA:
		return eventData(event);

	case DATA_SIGNED:
		return toSigned(eventData(event));

	case DATAPLUS:
		return eventData(event);

	case DATAPLUS_SIGNED:
		return toSigned(eventDataPlus(event));
legoc's avatar
legoc committed
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
	}

	return 0;
}

DataInfo selectDataInfo(BoardType type) {

	vector<DataInfo> dataInfos;

	if (boardHasEnergy(type)) {
		dataInfos.push_back(ENERGY);
	}

	if (boardHasQ(type)) {
		dataInfos.push_back(Q_SHORT);
		dataInfos.push_back(Q_LONG);
		dataInfos.push_back(Q_RATIO);
	}

	if (boardHasPositionXY(type)) {
		dataInfos.push_back(POSITION_X);
		dataInfos.push_back(POSITION_Y);
	}

	if (boardHasAB(type)) {
		dataInfos.push_back(A);
		dataInfos.push_back(AplusB);
	}

	if (boardHasZeroCrossing(type)) {
		dataInfos.push_back(ZERO_CROSSING);
	}

210
211
212
213
214
215
216
217
218
	dataInfos.push_back(DATA);
	dataInfos.push_back(DATA_SIGNED);
	dataInfos.push_back(DATAPLUS);
	dataInfos.push_back(DATAPLUS_SIGNED);

	if (dataInfos.size() == 0) {
		return NONE;
	}

legoc's avatar
legoc committed
219
220
221
222
223
224
225
	if (dataInfos.size() == 1) {
		return dataInfos.front();
	}

	// Select the data to plot.
	cout << "Event data:" << endl;
	for (int i = 1; i <= dataInfos.size(); ++i) {
226
		cout << i << ": " << dataInfoToString(dataInfos[i - 1]) << endl;
legoc's avatar
legoc committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
	}

	int id = 0;
	while (true) {
		cout << "Select the data to plot:";
		cin >> id;

		if (id >= 1 && id <= dataInfos.size()) {
			break;
		}
	}

	return dataInfos[id - 1];
}

242
void process(int32_t * buffer, int32_t size, int32_t crate, int32_t board, int32_t channel, int numberOfEvents, DataInfo dataInfo, int32_t& eventId, vector<boost::tuple<double, double> >& rollOverToPlot, vector<boost::tuple<double, double> >& timestampToPlot, vector<boost::tuple<double, double> >& dataToPlot) {
legoc's avatar
legoc committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

	// reading next block
	int32_t index = 0;
	int32_t nbIndex = size;
	int32_t * bufferPtr = buffer;

	while (index < nbIndex) {
		// reading event
		Event event;
		if (readEvent(event, bufferPtr + index)) {

			bool display = ((crate == -1 || (crate == event.crate))
						&& (board == -1 || (board == event.board))
						&& (channel == -1 || (channel == event.channel)));

			if (display && ((numberOfEvents == -1) || (numberOfEvents > 0 && eventId < numberOfEvents))) {
				rollOverToPlot.push_back(boost::make_tuple(eventId, event.rollover));
				timestampToPlot.push_back(boost::make_tuple(eventId, event.timestamp));
261
262
263
264
265

				if (dataInfo != NONE) {
					dataToPlot.push_back(boost::make_tuple(eventId, eventData(event, dataInfo)));
				}

legoc's avatar
legoc committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
				eventId++;
			}

		} else {
			cout << "problem with event " << hex << "    " << setw(8) << setfill('0') << (uint32_t)(*(bufferPtr + index))
										<< " " << setw(8) << setfill('0') << (uint32_t)(*(bufferPtr + index + 1))
										<< " " << setw(8) << setfill('0') << (uint32_t)(*(bufferPtr + index + 2))
										<< " " << setw(8) << setfill('0') << (uint32_t)(*(bufferPtr + index + 3))
										<< setw(0) << setfill(' ') << dec << endl;
		}

		index += 4;
	}
}

void plotRollOver(vector<boost::tuple<double, double> > const & rollOverToPlot) {

	Gnuplot gpPlot;
284
	gpPlot << "set multiplot\n";
legoc's avatar
legoc committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
	gpPlot << "set title 'ROLL OVER'\n";
	gpPlot << "unset key\n";
	gpPlot << "plot '-' w histep\n";
	gpPlot.send(rollOverToPlot);
}

void plotTimestamp(vector<boost::tuple<double, double> > const & timestampToPlot) {

	Gnuplot gpPlot;
	gpPlot << "set title 'TIME'\n";
	gpPlot << "unset key\n";
	gpPlot << "plot '-' w histep\n";
	gpPlot.send(timestampToPlot);
}

300
301
302
303
304
305
306
307
308
void plotData(vector<boost::tuple<double, double> > const & dataToPlot) {

	Gnuplot gpPlot;
	gpPlot << "set title 'DATA'\n";
	gpPlot << "unset key\n";
	gpPlot << "plot '-' w histep\n";
	gpPlot.send(dataToPlot);
}

legoc's avatar
legoc committed
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
int main(int argc, char * argv[]) {

	if (argc < 2) {
		cerr << "usage : lstplot128 <file> <filter> [number of events]" << endl;
		cerr << "  filter is [crate,board,channel]" << endl;
		return EXIT_FAILURE;
	}

	string fileName = argv[1];

	int32_t crate = -1;
	int32_t board = -1;
	int32_t channel = -1;

	int numberOfEvents = -1;

	if (argc >= 3) {
		string valuesString(argv[2]);
		valuesString = valuesString.substr(1, valuesString.length() - 2);
		vector<int32_t> values = split(valuesString);

		cout << "filter:" << endl;

		if (values.size() > 0) {
			crate = values[0];
			cout << "crate   " << crate << endl;
		}

		if (values.size() > 1) {
			board = values[1];
			cout << "board   " << board << endl;
		}

		if (values.size() > 2) {
			channel = values[2];
			cout << "channel " << channel << endl;
		}

		cout << endl;
	}
	else {
		cerr << "please specify a filter [crate,board,channel]" << endl;
		exit(0);
	}

	if (argc >= 4) {
		istringstream is(argv[3]);
		is >> numberOfEvents;
		cout << "reading " << numberOfEvents << " events" << endl;
	}

	Reader reader(BUFFER_SIZE);

	bool success = reader.open(fileName);

	if (!success) {
		return EXIT_FAILURE;
	}

	cout << listModeContext << endl;

	// Search for the type of the board.
	BoardIterator b;
	BoardType boardType;
	bool found = false;
	while (!b.end()) {

		if (b.crate() == crate && b.board() == board) {
			boardType = (*b).boardType;
			found = true;
			break;
		}
		++b;
	}

	if (!found) {
		cerr << "please select a valid board" << endl;
		exit(0);
	}

	// Select the data to plot.
390
391
	DataInfo dataInfo = selectDataInfo(boardType);
	cout << "Data plotted is " << dataInfoToString(dataInfo) << endl;
legoc's avatar
legoc committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

	// events
	int32_t eventId = 0;
	vector<boost::tuple<double, double> > rollOverToPlot;
	vector<boost::tuple<double, double> > timestampToPlot;
	vector<boost::tuple<double, double> > dataToPlot;

	// reading
	while (true) {
		// reading next block
		int32_t size = reader.read();

		if (size == 0) {
			break;
		}

		// processing
409
		process(reader.buffer(), size, crate, board, channel, numberOfEvents, dataInfo, eventId, rollOverToPlot, timestampToPlot, dataToPlot);
legoc's avatar
legoc committed
410
411
412
413
414
415
416
417
418
419

		// Exit the loop.
		if (numberOfEvents > 0 && eventId > numberOfEvents) {
			break;
		}
	}

	plotRollOver(rollOverToPlot);
	plotTimestamp(timestampToPlot);

420
421
422
423
424
	if (dataInfo != NONE) {
		cout << "number of events " << dataToPlot.size() << endl;
		plotData(dataToPlot);
	}

legoc's avatar
legoc committed
425
426
	return EXIT_SUCCESS;
}