data.h 4.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * internal data representation
 * @author Tobias Weber <tweber@ill.fr>
 * @date 1-June-2018
 * @license see 'LICENSE' file
 */

#ifndef __DATAREP_H__
#define __DATAREP_H__

#include <vector>
#include <string>
Tobias WEBER's avatar
Tobias WEBER committed
13
#include <tuple>
14
15
16
17

using t_real_dat = double;


Tobias WEBER's avatar
Tobias WEBER committed
18
19
20
class Data;
class Dataset;

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

/**
 * data set (e.g. of one polarisation channel)
 */
class Data
{
private:
	// counts
	// can have multiple detectors and monitors
	std::vector<std::vector<t_real_dat>> m_counts;
	std::vector<std::vector<t_real_dat>> m_counts_err;

	// monitors
	std::vector<std::vector<t_real_dat>> m_monitors;
	std::vector<std::vector<t_real_dat>> m_monitors_err;

	// x axes
	std::vector<std::vector<t_real_dat>> m_x;
	std::vector<std::string> m_x_names;

Tobias WEBER's avatar
Tobias WEBER committed
41

42
public:
Tobias WEBER's avatar
Tobias WEBER committed
43
44
	std::size_t GetNumCounters() const { return m_counts.size(); }
	std::size_t GetNumMonitors() const { return m_monitors.size(); }
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
	std::size_t GetNumAxes() const { return m_x.size(); }


	// counters
	const std::vector<t_real_dat>& GetCounter(std::size_t i) const { return m_counts[i]; }
	const std::vector<t_real_dat>& GetCounterErrors(std::size_t i) const { return m_counts_err[i]; }
	void AddCounter(const std::vector<t_real_dat> &dat, const std::vector<t_real_dat> &err)
	{
		m_counts.push_back(dat);
		m_counts_err.push_back(err);
	}
	void AddCounter(std::vector<t_real_dat> &&dat, std::vector<t_real_dat> &&err)
	{
		m_counts.push_back(std::move(dat));
		m_counts_err.push_back(std::move(err));
	}


	// monitors
	const std::vector<t_real_dat>& GetMonitor(std::size_t i) const { return m_monitors[i]; }
	const std::vector<t_real_dat>& GetMonitorErrors(std::size_t i) const { return m_monitors_err[i]; }
	void AddMonitor(const std::vector<t_real_dat> &dat, const std::vector<t_real_dat> &err)
	{
		m_monitors.push_back(dat);
		m_monitors_err.push_back(err);
	}
	void AddMonitor(std::vector<t_real_dat> &&dat, std::vector<t_real_dat> &&err)
	{
		m_monitors.push_back(std::move(dat));
		m_monitors_err.push_back(std::move(err));
	}


	// x axes
	const std::vector<t_real_dat>& GetAxis(std::size_t i) const { return m_x[i]; }
	const std::string& GetAxisName(std::size_t i) const { return m_x_names[i]; }
Tobias WEBER's avatar
Tobias WEBER committed
81
82
	void SetAxisNames(const std::vector<std::string> &names) { m_x_names = names; }
	void SetAxisNames(std::vector<std::string> &&names) { m_x_names = std::move(names); }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	void AddAxis(const std::vector<t_real_dat> &dat, const std::string &name="")
	{
		m_x.push_back(dat);

		if(name != "")
			m_x_names.push_back(name);
		else
			m_x_names.push_back("ax" + std::to_string(GetNumAxes()));
	}
	void AddAxis(std::vector<t_real_dat> &&dat, const std::string &name="")
	{
		m_x.emplace_back(std::move(dat));

		if(name != "")
			m_x_names.push_back(name);
		else
			m_x_names.push_back("ax" + std::to_string(GetNumAxes()));
	}
Tobias WEBER's avatar
Tobias WEBER committed
101

Tobias WEBER's avatar
Tobias WEBER committed
102
103
104
public:
	Data norm(std::size_t mon = 0) const;

Tobias WEBER's avatar
Tobias WEBER committed
105
106
107

	// binary operators
	friend Data operator +(const Data& dat1, const Data& dat2);
108
109
	friend Data operator +(const Data& dat, t_real_dat d);
	friend Data operator +(t_real_dat d, const Data& dat);
Tobias WEBER's avatar
Tobias WEBER committed
110
	friend Data operator -(const Data& dat1, const Data& dat2);
111
	friend Data operator -(const Data& dat, t_real_dat d);
Tobias WEBER's avatar
Tobias WEBER committed
112
113
114
115
116
117
118
	friend Data operator *(const Data& dat1, t_real_dat d);
	friend Data operator *(t_real_dat d, const Data& dat1);
	friend Data operator /(const Data& dat1, t_real_dat d);

	// unary operators
	friend const Data& operator +(const Data& dat);
	friend Data operator -(const Data& dat);
Tobias WEBER's avatar
Tobias WEBER committed
119

Tobias WEBER's avatar
Tobias WEBER committed
120

Tobias WEBER's avatar
Tobias WEBER committed
121
122
123
	// different ways of uniting data containers
	static Data add_pointwise(const Data& dat1, const Data& dat2);
	static Data append(const Data& dat1, const Data& dat2);
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
};



/**
 * collection of individual data (i.e. polarisation channels)
 */
class Dataset
{
private:
	std::vector<Data> m_data;

public:
	std::size_t GetNumChannels() const { return m_data.size(); }
	const Data& GetChannel(std::size_t channel) const { return m_data[channel]; }
	void AddChannel(const Data &data) { m_data.push_back(data); }
	void AddChannel(Data &&data) { m_data.emplace_back(std::move(data)); }
Tobias WEBER's avatar
Tobias WEBER committed
141

Tobias WEBER's avatar
Tobias WEBER committed
142
143
144
public:
	Dataset norm(std::size_t mon = 0) const;

145
146
147
	// export to gnuplot
	bool SaveGpl(const std::string& file) const;

Tobias WEBER's avatar
Tobias WEBER committed
148

149
150
	// binary operators
	friend Dataset operator +(const Dataset& dat1, const Dataset& dat2);
151
152
	friend Dataset operator +(const Dataset& dat, t_real_dat d);
	friend Dataset operator +(t_real_dat d, const Dataset& dat);
153
	friend Dataset operator -(const Dataset& dat1, const Dataset& dat2);
154
	friend Dataset operator -(const Dataset& dat, t_real_dat d);
155
156
157
158
159
160
161
162
	friend Dataset operator *(const Dataset& dat1, t_real_dat d);
	friend Dataset operator *(t_real_dat d, const Dataset& dat1);
	friend Dataset operator /(const Dataset& dat1, t_real_dat d);

	// unary operators
	friend const Dataset& operator +(const Dataset& dat);
	friend Dataset operator -(const Dataset& dat);

Tobias WEBER's avatar
Tobias WEBER committed
163

164
165
166
	// different ways of uniting data sets
	static Dataset add_pointwise(const Dataset& dat1, const Dataset& dat2);
	static Dataset append(const Dataset& dat1, const Dataset& dat2);
167

Tobias WEBER's avatar
Tobias WEBER committed
168
	static std::tuple<bool, Dataset> convert_instr_file(const char* pcFile);
169
170
171
172
};


#endif