-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession.cs
288 lines (218 loc) · 8.26 KB
/
Session.cs
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
/*
Gamma Analyzer - Controlling application for Burn
Copyright (C) 2016 Norwegian Radiation Protection Authority
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Authors: Dag Robole,
using System;
using System.IO;
using System.Collections.Generic;
using NLua;
using Newtonsoft.Json;
namespace crash
{
// Class to store a session
public class Session
{
// Global Lua object
private static Lua LuaEngine = new Lua();
// Function used to calculate GE factor
public LuaFunction GEScriptFunc = null;
// IP Address of peer
public string IPAddress { get; set; }
// Name of session
public string Name { get; set; }
// Session database file
public string SessionFile { get; set; }
// Comment for this session
public string Comment { get; set; }
// Livetime used for this session
public float Livetime { get; set; }
// Detector definition used with this session
private Detector mDetector;
public Detector Detector
{
get { return mDetector; }
set { mDetector = value; LoadGEScriptFunc(); }
}
// Number of channels used with this session
public float NumChannels { get; private set; }
// Max number of channel counts found for this session
public float MaxChannelCount { get; private set; }
// Min number of channel counts found for this session
public float MinChannelCount { get; private set; }
// List of spectrums stored in this session
public List<Spectrum> Spectrums { get; private set; }
// Background counts to use with this session
public float[] Background = null;
// Loaded state for this session
public bool IsLoaded { get { return !String.IsNullOrEmpty(Name); } }
// Empty state for this session
public bool IsEmpty { get { return Spectrums.Count == 0; } }
public Session()
{
Spectrums = new List<Spectrum>();
Clear();
}
public Session(string ip, string sessionFile, string name, string comment, float livetime, Detector det)
{
Spectrums = new List<Spectrum>();
Clear();
IPAddress = ip;
Name = name;
SessionFile = sessionFile;
Comment = comment;
Livetime = livetime;
Detector = det;
}
public bool Add(Spectrum spec)
{
// Add a new spectrum to the list of spectrums
int idx = Array.FindLastIndex(Spectrums.ToArray(), x => x.SessionIndex < spec.SessionIndex);
Spectrums.Insert(idx + 1, spec);
NumChannels = spec.NumChannels;
// Update state
if (spec.MaxCount > MaxChannelCount)
MaxChannelCount = spec.MaxCount;
if (spec.MinCount < MinChannelCount)
MinChannelCount = spec.MinCount;
return true;
}
public void Clear()
{
// Clear this session
Name = String.Empty;
SessionFile = String.Empty;
Comment = String.Empty;
Livetime = 0;
mDetector = null;
NumChannels = 0;
MaxChannelCount = 0;
MinChannelCount = 0;
Spectrums.Clear();
GEScriptFunc = null;
Background = null;
}
private bool LoadGEScriptFunc()
{
// Initialize GE factor function if GE script exists
string geScriptFile = GAEnvironment.GEScriptPath + Path.DirectorySeparatorChar + mDetector.GEScript;
if (!File.Exists(geScriptFile))
throw new Exception("GE script does not exist: " + geScriptFile);
GEScriptFunc = null;
string script = File.ReadAllText(geScriptFile);
LuaEngine.DoString(script);
GEScriptFunc = LuaEngine["gevalue"] as LuaFunction;
return GEScriptFunc != null;
}
public bool SetBackground(List<Spectrum> specs)
{
// Set background counts for this session and adjust for livetime
if (specs.Count < 1)
{
Background = null;
return true;
}
Background = GetAdjustedCounts(specs, Livetime);
return true;
}
public bool SetBackgroundSession(Session bkg)
{
// Set background counts for this session and adjust for livetime
if (bkg == null)
{
Background = null;
return true;
}
if (IsEmpty || !IsLoaded || bkg.IsEmpty || !bkg.IsLoaded)
return false;
Background = bkg.GetAdjustedCounts(Livetime);
return true;
}
private float[] GetAdjustedCounts(List<Spectrum> specs, float targetLivetime)
{
// Adjust counts for a given livetime
if (specs.Count < 1)
return null;
float[] spec = new float[(int)NumChannels];
foreach (Spectrum s in specs)
for (int i = 0; i < s.Channels.Count; i++)
spec[i] += s.Channels[i];
float scale = targetLivetime / Livetime;
for (int i = 0; i < spec.Length; i++)
{
spec[i] /= (float)specs.Count;
spec[i] *= scale;
}
return spec;
}
private float[] GetAdjustedCounts(float targetLivetime)
{
return GetAdjustedCounts(Spectrums, targetLivetime);
}
public float GetCountInBkg(int start, int end)
{
// Accumulate counts for a given region
if (Background == null)
return 0f;
float cnt = 0f;
for (int i = start; i < end; i++)
cnt += Background[i];
return cnt;
}
public float GetMaxCountInROI(int start, int end)
{
// Find highest count for a given region
float max = -1f;
foreach (Spectrum s in Spectrums)
{
float curr = s.GetCountInROI(start, end);
if (max == -1f)
max = curr;
else if(curr > max)
max = curr;
}
return max;
}
public double GetBackgroundCountInROI(int start, int end)
{
// Find highest count for a given region in background
if (Background == null)
return 0d;
double cnt = 0d;
for (int i=start; i<end; i++)
cnt += Background[i];
return cnt;
}
}
public class APISession
{
public APISession(Session s)
{
Name = s.Name;
IPAddress = s.IPAddress;
Comment = s.Comment;
Livetime = s.Livetime;
DetectorData = JsonConvert.SerializeObject(s.Detector, Newtonsoft.Json.Formatting.None);
}
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("ip_address")]
public string IPAddress { get; set; }
[JsonProperty("comment")]
public string Comment { get; set; }
[JsonProperty("livetime")]
public double Livetime { get; set; }
[JsonProperty("detector_data")]
public string DetectorData { get; set; }
}
}