Plot.cpp 7.54 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
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
/*
 * 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 {
	ENERGY = 0,
	Q_SHORT = 1,
	Q_LONG = 2,
	Q_RATIO = 3,
	POSITION_X = 4,
	POSITION_Y = 5,
	A = 6,
	AplusB = 7,
	ZERO_CROSSING = 8
};

std::string dataInfoString(DataInfo dataInfo) {

	switch (dataInfo) {
	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";
	}

	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);
	}

	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);
	}

	if (dataInfos.size() == 1) {
		return dataInfos.front();
	}

	// Select the data to plot.
	cout << "Event data:" << endl;
	for (int i = 1; i <= dataInfos.size(); ++i) {
		cout << i << ": " << dataInfoString(dataInfos[i - 1]) << endl;
	}

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

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

	return dataInfos[id - 1];
}

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) {

	// 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));
				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;
	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);
}

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.
legoc's avatar
legoc committed
330
331
	//DataInfo dataInfo = selectDataInfo(boardType);
	DataInfo dataInfo = ENERGY;
legoc's avatar
legoc committed
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

	// 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
		process(reader.buffer(), size, crate, board, channel, numberOfEvents, dataInfo, eventId, rollOverToPlot, timestampToPlot);

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

	cout << "number of events " << dataToPlot.size() << endl;

	plotRollOver(rollOverToPlot);
	plotTimestamp(timestampToPlot);

	return EXIT_SUCCESS;
}