Event.cpp 5.56 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
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
}

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

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

	} else if ((boardType == V1724_BOARD_TYPE) || (boardType == V1730_BOARD_TYPE)) {
		event.timestamp = (uint32_t)(*(buffer + 1) & 0x3FFFFFFF);
		event.energy = (uint16_t)(*(buffer + 2) & 0x7FFF);
	}

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

	// Set the absolute time. Here the event is still in relative time.
	time_type absoluteTime = event.time() * event.timeResolution();

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

	return true;
}

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

	Event relativeTimeEvent(event);

	// 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 absolute time that changes the rollover and timestamp.
	relativeTimeEvent.setTime(relativeTime);

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

	BoardType boardType = relativeTimeEvent.boardType();

	if (boardType == CFD_BOARD_TYPE) {
		*(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) {
		// 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;

	} else if ((boardType == V1724_BOARD_TYPE) || (boardType == V1730_BOARD_TYPE)) {
		*(buffer + 1) = (relativeTimeEvent.timestamp & 0x3FFFFFFF);
		*(buffer + 2) = (relativeTimeEvent.energy & 0x7FFF);
		*(buffer + 3) = 0;
	}

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

}