Event.h 3.58 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
/*
 * 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.
 */

#ifndef LSTDPP_EVENT_H
#define LSTDPP_EVENT_H

#include <iostream>
#include <set>
#include "CrateBoard.h"

namespace lstdpp128 {

typedef int64_t time_type;

/**
 * Structure defining the event in lstdpp.
 */
struct Event {
	uint8_t crate;
	uint8_t board;
	uint16_t channel;
	uint16_t rollover;
	uint32_t timestamp;
	uint16_t energy;
	uint32_t data;

	enum CleanType {CLEAN, NOT_CLEAN, ANTI_COMPTON, UNDEFINED};
	CleanType status;

	Event() :
		crate(0),
		board(0),
		channel(0),
		rollover(0),
		timestamp(0),
		energy(0),
		data(0),
		status(UNDEFINED) {}

	/**
	 * Returns the board type of the event in the current list mode context.
	 */
	inline BoardType boardType() const {
		return listModeContext.crateBoard.crates[crate].boards[board].boardType;
	}

	/**
	 * Returns the time resolution of the event in the current list mode context.
	 */
	inline uint32_t timeResolution() const {
		return BoardTimeResolution[listModeContext.crateBoard.crates[crate].boards[board].boardType];
	}

	/**
	 * Gets the absolute time.
	 */
	time_type time() const;

	/**
	 * Sets the absolute time. The value is converted into rollover and timestamp.
	 */
	void setTime(time_type value);

	/**
	 * Function provided for convenience to get the data as an int32 value.
	 */
	inline int32_t int32Data() const {
		return *reinterpret_cast<const int32_t *>(&data);
	}

	/**
	 * Function provided for convenience to get the data as a float value.
	 */
	inline float floatData() const {
		return *reinterpret_cast<const float *>(&data);
	}
};

/**
 * This function is only for CFD_BOARD_TYPE events.
 */
inline float zeroCrossing(Event const &event) {
	return event.floatData();
}

/**
 * This function is only for V1751_BOARD_TYPE events.
 */
inline uint16_t qLong(Event const & event) {
	return event.energy;
}

/**
 * This function is only for V1751_BOARD_TYPE events.
 */
inline uint16_t qShort(Event const & event) {
	return event.data;
}

/**
 * This function is only for V1751_BOARD_TYPE events.
 */
inline float qRatio(Event const & event) {
	if (qLong(event) == 0) {
		return 0.0;
	}
	return (float)(qLong(event) - qShort(event)) / (float)qLong(event);
}

/**
 * Comparator to sort events with time.
 */
struct CompareEvents {

	inline bool operator()(Event const & a, Event const & b) {
		return a.time() < b.time();
	}
};

std::ostream& operator<< (std::ostream& os, Event const & e);

std::string printBinary(int32_t * buffer);

bool readEvent(Event & event, int32_t * buffer);
void writeEvent(Event const & event, int32_t * buffer);

struct EventBlock {

	static int32_t maxId;
	int32_t id;

	std::multiset<Event, CompareEvents> events;

	EventBlock() {
		id = maxId;
		maxId++;
	}

	inline time_type minTime() const {
		return events.begin()->time();
	}

	inline time_type maxTime() const {
		std::multiset<Event, CompareEvents>::const_reverse_iterator e = events.rend();
		++e;
		return e->time();
	}

	EventBlock * split();
	EventBlock * split(time_type midTime);
};

std::ostream& operator<< (std::ostream& os, EventBlock const & b);

}

#endif