-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOEData.java
More file actions
258 lines (208 loc) · 6.57 KB
/
POEData.java
File metadata and controls
258 lines (208 loc) · 6.57 KB
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
package sbeam2;
import java.awt.Color;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import sbeam2.gui.POEView;
public class POEData {
public int num_points;
public float min_energy, max_energy;
public int is_Visible; //CHANGE: -2 = not visible, -1 = visible, 0+ = appended to viewnumber of value
public int POE_num;
public String title;
public float[] poe_amplitudes, energy_values;
public float energy_spacing, average_energy;
public float[] extrema = new float[2];
public ArrayList<POEView> AssociatedPOEViews;
public ArrayList<CalcData> AssociatedCalcs;
public Color poe_color;
public POEData() {
// TODO Auto-generated constructor stub
num_points = 100;
min_energy = 0;
max_energy = 200;
average_energy = 0.0f;
AssociatedPOEViews = new ArrayList<POEView>();
AssociatedCalcs = new ArrayList<CalcData>();
title = null;
poe_color = new Color(0,0,0);
poe_amplitudes = null;
energy_values = null;
energy_spacing = 0;
is_Visible = -2; //CHANGE: default value
}
public void SaveAsPOEFile(BufferedWriter os) throws IOException{
int i;
os.write(title + "\n\n");
os.write(num_points + "\n");
os.write(min_energy + " " + max_energy + "\n\n");
// Output the three components of the energy distributions color
os.write((int) poe_color.getRed() + " ");
os.write((int) poe_color.getGreen() + " ");
os.write((int) poe_color.getBlue() + "\n\n");
for (i = 0; i < num_points; i++) {
os.write(poe_amplitudes[i] + " \t");
if ((((i + 1) % 5) == 0) || (i == num_points - 1))
os.write("\n");
}
//return os;
}
public void LoadFromFile(File f){
try{
Scanner is = new Scanner(f);
int i;
int red, green, blue;
String title_temp;
title_temp = is.nextLine();
is.nextLine();
title = title_temp;
num_points = is.nextInt();
is.nextLine();
min_energy = is.nextFloat();
max_energy = is.nextFloat();
is.nextLine();
is.nextLine();
red = is.nextInt();
green = is.nextInt();
blue = is.nextInt();
is.nextLine();
is.nextLine();
poe_amplitudes = new float[num_points];
energy_values = new float[num_points];
energy_spacing = (max_energy - min_energy) / (num_points - 1);
for (i = 0; i < num_points; i++) {
poe_amplitudes[i] = is.nextFloat();
if ((((i + 1) % 5) == 0) || (i == num_points - 1)) {
is.nextLine();
}
energy_values[i] = min_energy + i * energy_spacing;
}
poe_color = new Color(red, green, blue);
is.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public float maxValue(){
float maxVal = 0;
for(int i = 0; i < num_points; i++){
if(poe_amplitudes[i] > maxVal){
maxVal = poe_amplitudes[i];
}
}
return maxVal;
}
// make sure max value is 1
public void normalizePOE(float newVal) {
// trapezoid rule to get total probability
float total = 0;
for(int i = 0; i < num_points-1; i++) {
total += (poe_amplitudes[i] + poe_amplitudes[i+1]) * energy_spacing / 2.0;
}
// normalize total probability to 1
for(int i = 0; i < num_points; i++) { // normalize all points
poe_amplitudes[i] /= total;
}
// update the views
for(int i = 0; i < this.AssociatedPOEViews.size(); i++) {
this.AssociatedPOEViews.get(i).updatePOE(this);
}
}
// update data to match view after a point is dragged
public void updatePOE(float x, float newY){
int index = (int) ((x - energy_values[0]) / energy_spacing); //find which point has changed
poe_amplitudes[index] = newY; //change poe data
normalizePOE(newY);
}
public void calcTOFDelta(float x){
int index = (int) ((x - energy_values[0]) / energy_spacing); //find which point has changed
//now update dependent TOFS
for(int calcInd = 0; calcInd < AssociatedCalcs.size(); calcInd++){
CalcData calc = AssociatedCalcs.get(calcInd);
int numTOFs = calc.num_tofs;
TOFData[] tofsForCalc = calc.calculated_tofs;
//calc.calculated_tofs = tofsForCalc; //maybe change this?
calc.CalcTOFDeltaFunctions(this.POE_num, this, index);
}
}
// called when poe view is changed to update calculated tofs
public void FindNewTOFs(float new_amplitude, boolean is_endpoint){
for(int i = 0; i < AssociatedCalcs.size(); i++)
{
CalcData calc = AssociatedCalcs.get(i);
for(int j = 0; j < calc.calculated_tofs.length; j++){
calc.calculated_tofs[j].AddOnDeltaTOF(new_amplitude, is_endpoint);
}
}
}
public String out(boolean compat){
int i;
String returner = "";
returner += this.title + "\n\n";
returner += this.POE_num + "\n";
returner += this.num_points + " "+ this.min_energy + " " + this.max_energy + " ";
if(!compat) returner += this.is_Visible; //CHANGE: visibilty in save file
returner += "\n\n";
// Output the three components of the energy distributions color
returner += (int) this.poe_color.getRed() + " ";
returner += (int) this.poe_color.getGreen() + " ";
returner += (int) this.poe_color.getBlue() + "\n\n";
for(i = 0; i < this.num_points; i++) {
returner += this.poe_amplitudes[i] + " \t";
if((((i + 1) % 20) == 0) || (i == this.num_points - 1))
returner += "\n";
}
return returner;
}
public static POEData in(Scanner is) {
int i, number_of_points;
float energy_increment;
int red, green, blue;
String title_temp;
float[] amplitude;
float[] energy;
float minimum_energy, maximum_energy;
POEData poe = new POEData();
poe.title = is.nextLine();
is.nextLine();
poe.POE_num = is.nextInt();
is.nextLine();
number_of_points = is.nextInt();
minimum_energy = is.nextFloat();
maximum_energy = is.nextFloat();
// CHANGE:open feature
String temp = is.nextLine();
if (!temp.equals("")) {
poe.is_Visible = Integer.parseInt(temp.trim());
} else {
poe.is_Visible = -2;
}
// END CHANGE
is.nextLine();
poe.num_points = number_of_points;
poe.min_energy = minimum_energy;
poe.max_energy = maximum_energy;
red = is.nextInt();
green = is.nextInt();
blue = is.nextInt();
is.nextLine();
is.nextLine();
amplitude = new float[number_of_points];
energy = new float[number_of_points];
energy_increment = (maximum_energy - minimum_energy)
/ (number_of_points - 1);
for (i = 0; i < number_of_points; i++) {
amplitude[i] = is.nextFloat();
energy[i] = minimum_energy + i * energy_increment;
}
Color color = new Color(red, green, blue);
poe.poe_color = color;
poe.poe_amplitudes = amplitude;
poe.energy_values = energy;
poe.energy_spacing = energy_increment;
is.nextLine(); // Gets last '/n' in POE
return poe;
}
}