Event.cpp 6.94 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
/*
 * 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 "Event.h"

#include <iomanip>
#include <cmath>
#include <string.h>
#include <sstream>

using namespace std;

namespace lstdpp128 {

time_type Event::time() const {
	return (((int64_t)rollover << 32) | timestamp);
}

void Event::setTime(time_type value) {

	timestamp = (value & 0xFFFFFFFF);
	rollover = ((value >> 32) & 0xFFFF);
}

std::ostream& operator<< (std::ostream& os, Event const & e) {
legoc's avatar
legoc committed
41
42
	return os << dec << setw(20) << e.time() << setw(12) << e.energy << setw(8) << (int)e.crate << ", " << (int)e.board << setw(8) << e.channel
			<< setw(8) << e.data << setw(20) << e.rollover << setw(20) << e.timestamp;
legoc's avatar
legoc committed
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
}

std::string printBinary(int32_t * buffer) {

	ostringstream os;

	os << hex << setw(8) << setfill('0') << (uint32_t)(*(buffer))
											<< " " << setw(8) << setfill('0') << (uint32_t)(*(buffer + 1))
											<< " " << setw(8) << setfill('0') << (uint32_t)(*(buffer + 2))
											<< " " << setw(8) << setfill('0') << (uint32_t)(*(buffer + 3))
											<< setw(0) << setfill(' ') << dec;

	return os.str();
}

int32_t EventBlock::maxId = 0;

EventBlock * EventBlock::split() {

	time_type midTime = (minTime() + maxTime()) / 2;

	return split(midTime);
}

EventBlock * EventBlock::split(time_type midTime) {

	EventBlock * upperBlock = new EventBlock();

	Event event;
	event.setTime(midTime);

	multiset<Event, CompareEvents>::const_iterator e = events.upper_bound(event);

	upperBlock->events = multiset<Event, CompareEvents>(e, events.end());
	events = multiset<Event, CompareEvents>(events.begin(), e);

	return upperBlock;
}

std::ostream& operator<< (std::ostream& os, EventBlock const & b) {
	if (b.events.empty()) {
		return os << b.id << " : empty";
	}
	return os << b.id << " : " << b.minTime() << " - " << b.maxTime() << " (" << b.events.size() << ")";
}

bool readEvent(Event & event, int32_t * buffer) {

	uint8_t crate = (uint8_t)((*buffer >> 28) & 0xF);
	uint8_t board = (uint8_t)((*buffer >> 22) & 0x3F);

	if (crate >= listModeContext.crateBoard.nbCrates) {
		// No output because the log file can be very large
		return false;
	}

	if (board >= listModeContext.crateBoard.crates[event.crate].nbBoards) {
		// No output because the log file can be very large
		return false;
	}

	event.crate = crate;
	event.board = board;
	event.channel = (uint8_t)((*buffer >> 16) & 0x3F);
	event.rollover = (uint16_t)(*buffer & 0xFFFF);

	BoardType boardType = event.boardType();

111
112
	int numberOfBitsOfTimestamp = 32;

legoc's avatar
legoc committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	if (boardType == CFD_BOARD_TYPE) {
		event.timestamp = (uint32_t)(*(buffer + 1) & 0xFFFFFFFF);
		event.energy = (uint16_t)(*(buffer + 2) & 0xFFF);

		// Extra data if zero crossing
		int32_t temp = (*(buffer + 3) & 0xFFFFFFFF);
		memcpy(reinterpret_cast<void *>(&event.data), reinterpret_cast<const void *>(&temp), 4);

	} else if (boardType == V1751_BOARD_TYPE) {
		event.timestamp = (uint32_t)(*(buffer + 1) & 0xFFFFFFFF);
		event.energy = (uint16_t)((*(buffer + 2) >> 16) & 0x7FFF);

		// Extra data
		event.data = (uint16_t)((*(buffer + 2)) & 0x7FFF);

128
129
130
131
	} else if (boardType == V1724_BOARD_TYPE) {
		// The timestamp is on 30bits
		numberOfBitsOfTimestamp = 30;
		event.timestamp = (uint32_t)(*(buffer + 1) & 0x3FFFFFFF);
legoc's avatar
format    
legoc committed
132

133
134
135
		event.energy = (uint16_t)(*(buffer + 2) & 0x7FFF);

	} else if (boardType == V1730_BOARD_TYPE) {
136
		event.timestamp = (uint32_t)(*(buffer + 1) & 0xFFFFFFFF);
legoc's avatar
legoc committed
137
138
139
140
141
		event.energy = (uint16_t)(*(buffer + 2) & 0x7FFF);
	}

	// Use printBinary(buffer) to debug if necessary.

142
143
144
	// Calculate the absolute time.
	time_type absoluteTime = (((int64_t)event.rollover << numberOfBitsOfTimestamp) | event.timestamp);

145
146
147
148
	// In version == 1, the event time in the file is relative so that the time resolution is used to convert to absolute time.
	// In version >= 2, the event time in the file is already absolute.
	if (listModeContext.version == 1) {
		// Set the absolute time. Here the event is still in relative time.
149
		absoluteTime = absoluteTime * event.timeResolution();
150
151
152
153
154
155
156

		// We can set the absolute time that changes the rollover and timestamp.
		event.setTime(absoluteTime);

		if (absoluteTime != event.time()) {
			cerr << "Problem while converting relative time to absolute time" << endl;
		}
legoc's avatar
legoc committed
157
	}
158
159
160
161
	else {
		// We can set the absolute time that changes the rollover and timestamp.
		event.setTime(absoluteTime);
	}
162

legoc's avatar
legoc committed
163
164
165
166
167
168
169
	return true;
}

void writeEvent(Event const & event, int32_t * buffer) {

	Event relativeTimeEvent(event);

170
171
	// See comment on versions in readEvent.
	if (listModeContext.version == 1) {
legoc's avatar
legoc committed
172

173
174
175
176
177
178
		// We reset the event time to the relative time because we do not want to change the header of the lsto.
		time_type relativeTime = event.time() / event.timeResolution();

		// We can set the realtive time that changes the rollover and timestamp.
		relativeTimeEvent.setTime(relativeTime);
	}
legoc's avatar
legoc committed
179
180
181
182
183
184
185
186
187

	*buffer = 0;
	*buffer |= ((relativeTimeEvent.crate & 0xF) << 28);
	*buffer |= ((relativeTimeEvent.board & 0x3F) << 22);
	*buffer |= ((relativeTimeEvent.channel & 0x3F) << 16);

	BoardType boardType = relativeTimeEvent.boardType();

	if (boardType == CFD_BOARD_TYPE) {
188
189
190

		*buffer |= (relativeTimeEvent.rollover & 0xFFFF);

legoc's avatar
legoc committed
191
192
193
194
195
		*(buffer + 1) = relativeTimeEvent.timestamp;
		*(buffer + 2) = (relativeTimeEvent.energy & 0xFFF);
		memcpy(buffer + 3, reinterpret_cast<const void *>(&relativeTimeEvent.data), 4);

	} else if (boardType == V1751_BOARD_TYPE) {
196
197
198

		*buffer |= (relativeTimeEvent.rollover & 0xFFFF);

legoc's avatar
legoc committed
199
200
201
202
203
		// There is one bit that is not used: first bit of data (the buffer written may be different from the buffer read).
		*(buffer + 1) = relativeTimeEvent.timestamp;
		*(buffer + 2) = ((relativeTimeEvent.energy << 16)) | (event.data & 0x7FFF);
		*(buffer + 3) = 0;

204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
	} else if (boardType == V1724_BOARD_TYPE) {

		// The card timestamp is on 30bits with 2 bits of zero.
		// The rollover is calculated by taking the 2 bits of the actual timestamp (>> 30 for 32 bits = 2 bits).
		int32_t rollover = ((relativeTimeEvent.rollover << 2) | (relativeTimeEvent.timestamp >> 30));
		*buffer |= (rollover & 0xFFFF);

		// Taking the 30 bits and setting 0 to the 2 first bits.
		*(buffer + 1) = (relativeTimeEvent.timestamp & 0x3FFFFFFF);
		*(buffer + 2) = (relativeTimeEvent.energy & 0x7FFF);
		*(buffer + 3) = 0;

	} else if (boardType == V1730_BOARD_TYPE) {

		*buffer |= (relativeTimeEvent.rollover & 0xFFFF);

220
		*(buffer + 1) = (relativeTimeEvent.timestamp & 0xFFFFFFFF);
legoc's avatar
legoc committed
221
222
223
224
225
226
227
228
		*(buffer + 2) = (relativeTimeEvent.energy & 0x7FFF);
		*(buffer + 3) = 0;
	}

	// Use printBinary(buffer) to debug if necessary.
}

}