-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzboson.py
More file actions
357 lines (305 loc) · 11.1 KB
/
Copy pathzboson.py
File metadata and controls
357 lines (305 loc) · 11.1 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""
--------------------TITLE--------------------
Measuring Gamma width of Z boson
---------------------------------------------
This python code takes data files reads, filters and merges them.
Then 2D fitting is carried out using chi squared minimisation.
Mesh arrays are created to calculate the uncertainties on m_zz and gamma_zz values.
Then code plots graphs of fitting and data points. Chi-square contour elipses are plotted as well
01/05/2025
"""
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fmin
GAMMA_EE = 83.91 * (10**-3) # partial width in GeV
H_REDUCED = 6.582 * (10**-24) # reduced h value in GeV/s
M_Z_START = 90 #starting value of m_Z in GeV/c^2
GAMMA_Z_START = 3 #starting value of gamma_z in GeV
def is_float(parameter):
"""
Function checks if a parameter value is a real number.
Parameters
----------
parameter : STRING
parameter that is to be checked.
Returns
-------
bool
if the parameter is real number returns True.
"""
try:
float(parameter)
return True
except ValueError:
return False
def validation(parameter):
"""
Function validates data by checking if data is a real number.
Parameters
----------
parameter : STRING
Takes a parameter to check.
Returns
-------
bool
if parameter value is real number and not a NaN.
"""
return bool(is_float(parameter) and not math.isnan(float(parameter)))
def read_file(file_name):
"""
Function reads, validates and filters data from text.
Parameters
----------
file_name : STRING
name of the file.
Returns
-------
ARRAY OF FLOAT
Array of energy values from file.
ARRAY OF FLOAT
Array of cross section values from file.
ARRAY OF FLOAT
Array of uncertainties in cross section from file.
"""
#Reads the file
try:
with open(file_name, 'r') as open_file:
data_array = np.empty((0, 3))
for line in open_file:
split_up = line.split(',')
if validation(split_up[0]) and validation(split_up[1]) and validation(split_up[2]):
if float(split_up[2]) != 0:
e_value = float(split_up[0])
sigma_value = float(split_up[1])
uncertainty_value = float(split_up[2])
if np.abs(sigma_value - model(e_value, 91.2, 2.5)) < 3 * uncertainty_value:
temp = np.array([e_value, sigma_value, uncertainty_value])
data_array = np.vstack((data_array, temp))
open_file.close()
return data_array[:, 0], data_array[:, 1], data_array[:, 2]
#Stops the program if file is not found
except FileNotFoundError:
print("File not found")
def merge_data():
"""
Function merges the data from two files and sorts it by energy values.
Returns
-------
merged_e : ARRAY OF FLOAT
Array of combined energy values from files.
merged_sigma : ARRAY OF FLOAT
Array of combined cross section values from files.
merged_uncertainty : ARRAY OF FLOAT
Array of combined uncertainties values from files.
"""
e_1, sigma_1, uncertainty_1 = read_file("z_boson_data_1.csv")
e_2, sigma_2, uncertainty_2 = read_file("z_boson_data_2.csv")
j = 0
merged_e = np.empty(0)
merged_sigma = np.empty(0)
merged_uncertainty = np.empty(0)
for i, value in enumerate(e_1):
while value >= e_2[j] and j < (len(e_2) - 1):
merged_e = np.append(merged_e, e_2[j])
merged_sigma = np.append(merged_sigma, sigma_2[j])
merged_uncertainty = np.append(merged_uncertainty, uncertainty_2[j])
j += 1
merged_e = np.append(merged_e, value)
merged_sigma = np.append(merged_sigma, sigma_1[i])
merged_uncertainty = np.append(merged_uncertainty, uncertainty_1[i])
return merged_e, merged_sigma, merged_uncertainty
def model(e_value, m_z, gamma_z):
"""
Function calculates the cross section value
from theory formula with given
m_z, gamma_z and energy values.
Parameters
----------
e_value : FLOAT
energy value.
m_z : FLOAT
mass of z bozon.
gamma_z : FLOAT
partial width of z bozon.
Returns
-------
FLOAT
Returns the cross section value calculated from theory.
"""
factor = (12 * np.pi / m_z**2) * 0.3894 * (10**6)
numerator = (e_value * GAMMA_EE)**2
denominator = (e_value**2 - m_z**2)**2 + (m_z * gamma_z)**2
return factor * (numerator / denominator)
def chi_square(parameters, E, sigma, uncertainty):
"""
Function calculates the chi squared
values with given m_z, gamma_z.
Parameters
----------
parameters : FLOAT
fitting paramteres (m_z and gamma_z).
E : ARRAY OF FLOAT
Energy values.
sigma : ARRAY OF FLOAT
Cross section values.
uncertainty : ARRAY OF FLOAT
Uncertainty values.
Returns
-------
FLOAT
Chi squared value calculated with given parameters.
"""
m_z, gamma_z = parameters
return np.sum(((sigma - model(E, m_z, gamma_z)) / uncertainty)**2)
def min_chi_square(E, sigma, uncertainty):
"""
Function calculates minimal chi square
values and corresponding m_z, gamma_z values.
Parameters
----------
E : ARRAY OF FLOAT
Energy values.
sigma : ARRAY OF FLOAT
Cross section values.
uncertainty : ARRAY OF FLOAT
Uncertainty values.
Returns
-------
optimal_params : ARRAY OF FLOAT
List of optimal m_z, gamma_z
min_chi : FLOAT
minimum chi squared values.
"""
initial_values = [M_Z_START, GAMMA_Z_START]
optimal_params, min_chi, *_ = fmin(lambda p: chi_square(p, E, sigma, uncertainty),
initial_values, full_output=True, disp=False)
return optimal_params, min_chi
def mesh_arrays(optimal_values, E, sigma, uncertainty):
"""
Function creates mesh arrays for m_z, gamma_z and chi square.
Parameters
----------
optimal_values : ARRAY OF FLOAT
List of optimal m_z, gamma_z
E : ARRAY OF FLOAT
Energy values.
sigma : ARRAY OF FLOAT
Cross section values.
uncertainty : ARRAY OF FLOAT
Uncertainty values.
Returns
-------
m_mesh : 2D ARRAY OF FLOAT
mesh array of m_z values linearly spaced.
gamma_mesh : 2D ARRAY OF FLOAT
mesh array of gamma_z values linearly spaced.
chi_square_mesh : 2D ARRAY OF FLOAT
mesh array of chi square values coresponding to m_z and gamma_z values.
"""
m_array = np.linspace(optimal_values[0] - 0.05, optimal_values[0] + 0.05, 400)
gamma_array = np.linspace(optimal_values[1] - 0.05, optimal_values[1] + 0.05, 400)
m_mesh, gamma_mesh = np.meshgrid(m_array, gamma_array)
chi_square_vectorized = np.vectorize(lambda m, g: chi_square([m, g], E, sigma, uncertainty))
chi_square_mesh = chi_square_vectorized(m_mesh, gamma_mesh)
return m_mesh, gamma_mesh, chi_square_mesh
def output_data(optimal_values, min_chi_square_value, E):
"""
Function prints m_z value in GeV/c^2,
gamma_z value in GeV,
Reduced chi squared value,
Tau_z value in s.
Parameters
----------
optimal_values : ARRAY OF FLOAT
List of optimal m_z, gamma_z
min_chi_square_value : FLOAT
minimum chi squared.
E : ARRAY OF FLOAT
Energy values.
Returns
-------
None.
"""
reduced_chi_square = min_chi_square_value / (len(E) - 2)
tau_z = H_REDUCED / optimal_values[1]
print(f"m_z = {optimal_values[0]:.2f} GeV/c^2")
print(f"Gamma_z = {optimal_values[1]:.3f} GeV")
print(f"Reduced chi-squared = {reduced_chi_square:.3f}")
print(f"Tau_z = {tau_z:.2e} s")
def plot_data(optimal_values, min_chi_square_value, E, sigma, uncertainty):
"""
Function plots data and fitted plot,
plots chi square contour elipses.
Parameters
----------
optimal_values : ARRAY OF FLOAT
List of optimal m_z, gamma_z
min_chi_square_value : FLOAT
minimum chi squared.
E : ARRAY OF FLOAT
Energy values.
sigma : ARRAY OF FLOAT
Cross section values.
uncertainty : ARRAY OF FLOAT
Uncertainty values.
Returns
-------
None.
"""
m_mesh, gamma_mesh, chi_mesh = mesh_arrays(optimal_values, E, sigma, uncertainty)
x_array = np.linspace(85, 95, 200)
plt.figure()
plt.errorbar(E, sigma, yerr=uncertainty, fmt='o', markersize=3, color='purple', label="Data")
plt.plot(x_array, model(x_array, *optimal_values), color='orange', label="Fit")
plt.xlabel("E / GeV")
plt.ylabel("sigma / nb")
plt.title("Data and line of best fit")
plt.legend()
plt.grid()
plt.savefig("C:/Labs/assignment_2/results/data_line_of_best_fit.png", dpi=300, bbox_inches='tight')
plt.show()
plt.figure()
plt.contour(m_mesh, gamma_mesh, chi_mesh, levels=[min_chi_square_value + 1, min_chi_square_value + 2.3,
min_chi_square_value + 5.99])
plt.xlabel("m_z / GeV/c^2")
plt.ylabel("Gamma_z / GeV")
plt.title("Chi-square Contour Ellipses")
plt.grid()
plt.savefig("C:/Labs/assignment_2/results/chi_square_contour_ellipses.png", dpi=300, bbox_inches='tight')
plt.show()
def uncertainties_func(optimal_values, min_chi_square_value, E, sigma, uncertainty):
"""
Function calculates the uncertainties for m_z and gamma_z.
And prints them.
Parameters
----------
optimal_values : ARRAY OF FLOAT
List of optimal m_z, gamma_z
min_chi_square_value : FLOAT
minimum chi squared.
E : ARRAY OF FLOAT
Energy values.
sigma : ARRAY OF FLOAT
Cross section values.
uncertainty : ARRAY OF FLOAT
Uncertainty values.
Returns
-------
None.
"""
m_mesh, gamma_mesh, chi_mesh = mesh_arrays(optimal_values, E, sigma, uncertainty)
tolerance = 0.01
contour_value = np.abs(chi_mesh - (min_chi_square_value + 1)) < tolerance
m_contour = m_mesh[contour_value]
gamma_contour = gamma_mesh[contour_value]
m_uncertainty = (np.max(m_contour) - np.min(m_contour)) / 2
gamma_uncertainty = (np.max(gamma_contour) - np.min(gamma_contour)) / 2
print(f"Uncertainty in m_z: {m_uncertainty:.4f} GeV/c^2")
print(f"Uncertainty in gamma_z: {gamma_uncertainty:.4f} GeV")
if __name__ == "__main__":
E, sigma, uncertainty = merge_data()
optimal_values, min_chi_sq_val = min_chi_square(E, sigma, uncertainty)
output_data(optimal_values, min_chi_sq_val, E)
plot_data(optimal_values, min_chi_sq_val, E, sigma, uncertainty)
uncertainties_func(optimal_values, min_chi_sq_val, E, sigma, uncertainty)