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
|
#include "solid_angle.h"
#include <math.h>
#include <stdio.h>
#include "optics.h"
#include "muon.h"
typedef int testFunction(char *err);
/* Table of some of the tabulated values of the refractive index of water as a
* function of wavelength and temperature. In all cases, I just used the values
* for standard atmospheric pressure and assume this corresponds approximately
* to a density of 1000 kg/m^3.
*
* See Table 7 in https://aip.scitation.org/doi/pdf/10.1063/1.555859. */
struct refractive_index_results {
double p;
double T;
double wavelength;
double n;
} refractive_index_results[] = {
{1000.0, 0 , 226.50, 1.39468},
{1000.0, 10, 226.50, 1.39439},
{1000.0, 20, 226.50, 1.39353},
{1000.0, 30, 226.50, 1.39224},
{1000.0, 0 , 404.41, 1.34431},
{1000.0, 10, 404.41, 1.34404},
{1000.0, 20, 404.41, 1.34329},
{1000.0, 30, 404.41, 1.34218},
{1000.0, 0 , 589.00, 1.33447},
{1000.0, 10, 589.00, 1.33422},
{1000.0, 20, 589.00, 1.33350},
{1000.0, 30, 589.00, 1.33243},
{1000.0, 0 , 632.80, 1.33321},
{1000.0, 10, 632.80, 1.33296},
{1000.0, 20, 632.80, 1.33224},
{1000.0, 30, 632.80, 1.33118},
{1000.0, 0 , 1013.98, 1.32626},
{1000.0, 10, 1013.98, 1.32604},
{1000.0, 20, 1013.98, 1.32537},
{1000.0, 30, 1013.98, 1.32437},
{1000.0, 0 , 2325.42, 1.27663},
{1000.0, 10, 2325.42, 1.27663},
{1000.0, 20, 2325.42, 1.27627},
{1000.0, 30, 2325.42, 1.27563},
};
/* Table of the values of solid angle for various values of r0/r and L/r.
*
* See Table 1 in http://www.umich.edu/~ners312/CourseLibrary/SolidAngleOfADiskOffAxis.pdf. */
struct solid_angle_results {
double L;
double r0;
double omega;
} solid_angle_results[] = {
{0.5,0.0,3.4732594},
{0.5,0.2,3.4184435},
{0.5,0.4,3.2435434},
{0.5,0.6,2.9185178},
{0.5,0.8,2.4122535},
{0.5,1.0,1.7687239},
{0.5,1.2,1.1661307},
{0.5,1.4,0.7428889},
{0.5,1.6,0.4841273},
{0.5,1.8,0.3287007},
{0.5,2.0,0.2324189},
{1.0,0.0,1.8403024},
{1.0,0.2,1.8070933},
{1.0,0.4,1.7089486},
{1.0,0.6,1.5517370},
{1.0,0.8,1.3488367},
{1.0,1.0,1.1226876},
{1.0,1.2,0.9003572},
{1.0,1.4,0.7039130},
{1.0,1.6,0.5436956},
{1.0,1.8,0.4195415},
{1.0,2.0,0.3257993},
{1.5,0.0,1.0552591},
{1.5,0.2,1.0405177},
{1.5,0.4,0.9975504},
{1.5,0.6,0.9301028},
{1.5,0.8,0.8441578},
{1.5,1.0,0.7472299},
{1.5,1.2,0.6472056},
{1.5,1.4,0.5509617},
{1.5,1.6,0.4632819},
{1.5,1.8,0.3866757},
{1.5,2.0,0.3217142},
{2.0,0.0,0.6633335},
{2.0,0.2,0.6566352},
{2.0,0.4,0.6370508},
{2.0,0.6,0.6060694},
{2.0,0.8,0.5659755},
{2.0,1.0,0.5195359},
{2.0,1.2,0.4696858},
{2.0,1.4,0.4191714},
{2.0,1.6,0.3702014},
{2.0,1.8,0.3243908},
{2.0,2.0,0.282707}
};
int isclose(double a, double b, double rel_tol, double abs_tol)
{
/* Returns 1 if a and b are "close". This algorithm is taken from Python's
* math.isclose() function.
*
* See https://www.python.org/dev/peps/pep-0485/. */
return fabs(a-b) <= fmax(rel_tol*fmax(fabs(a),fabs(b)),abs_tol);
}
int test_muon_get_E(char *err)
{
/* A very simple test to make sure the energy as a function of distance
* along the track makes sense. Should include more detailed tests later. */
double T, E, range;
/* Assume initial kinetic energy is 1 GeV. */
T = 1000.0;
E = get_E(T,1e-9,1.0);
/* At the beginning of the track we should have roughly the same energy. */
if (!isclose(E, T, 1e-5, 0)) {
sprintf(err, "KE = %.5f, but expected %.5f", E, T);
return 1;
}
/* Assume initial kinetic energy is 1 GeV. */
T = 1000.0;
range = get_range(T,1.0);
E = get_E(T,range,1.0);
/* At the end of the track we should have roughly the same energy. */
if (!isclose(E, 0, 1e-5, 1e-5)) {
sprintf(err, "KE = %.5f, but expected %.5f", E, 0.0);
return 1;
}
return 0;
}
int test_muon_get_range(char *err)
{
/* A very simple test to make sure we read in the PDG table correctly. */
double value;
value = get_range(1.0,1.0);
if (!isclose(value, 1.863e-3, 1e-5, 0)) {
sprintf(err, "range = %.5f, but expected %.5f", value, 1.863e-3);
return 1;
}
return 0;
}
int test_muon_get_dEdx(char *err)
{
/* A very simple test to make sure we read in the PDG table correctly. */
double value;
value = get_dEdx(1.0,1.0);
if (!isclose(value, 6.097, 1e-5, 0)) {
sprintf(err, "dE/dx = %.5f, but expected %.5f", value, 6.097);
return 1;
}
return 0;
}
int test_refractive_index(char *err)
{
/* Tests the get_index() function. */
int i;
double n;
struct refractive_index_results result;
for (i = 0; i < sizeof(refractive_index_results)/sizeof(struct refractive_index_results); i++) {
result = refractive_index_results[i];
n = get_index(result.p, result.wavelength, result.T);
if (!isclose(n, result.n, 1e-2, 0)) {
sprintf(err, "n = %.5f, but expected %.5f", n, result.n);
return 1;
}
}
return 0;
}
int test_solid_angle(char *err)
{
/* Tests the get_solid_angle() function. */
int i;
double pmt[3] = {0,0,0};
double pos[3] = {0,0,1};
double n[3] = {0,0,1};
double r = 1.0;
double solid_angle;
solid_angle = get_solid_angle(pos,pmt,n,r);
if (!isclose(solid_angle, 2*M_PI*(1-1/sqrt(2)), 1e-9, 0)) {
sprintf(err, "solid angle = %.5f, but expected %.5f", solid_angle, 2*M_PI*(1-1/sqrt(2)));
return 1;
}
for (i = 0; i < sizeof(solid_angle_results)/sizeof(struct solid_angle_results); i++) {
pos[0] = solid_angle_results[i].r0*r;
pos[2] = solid_angle_results[i].L*r;
solid_angle = get_solid_angle(pos,pmt,n,r);
if (!isclose(solid_angle, solid_angle_results[i].omega, 1e-4, 0)) {
sprintf(err, "solid angle = %.5f, but expected %.5f", solid_angle, solid_angle_results[i].omega);
return 1;
}
}
return 0;
}
struct tests {
testFunction *test;
char *name;
} tests[] = {
{test_solid_angle, "test_solid_angle"},
{test_refractive_index, "test_refractive_index"},
{test_muon_get_dEdx, "test_muon_get_dEdx"},
{test_muon_get_range, "test_muon_get_range"},
{test_muon_get_E, "test_muon_get_E"}
};
int main(int argc, char **argv)
{
int i;
char err[256];
int retval = 0;
struct tests test;
for (i = 0; i < sizeof(tests)/sizeof(struct tests); i++) {
test = tests[i];
if (!test.test(err)) {
printf("[\033[92mok\033[0m] %s\n", test.name);
} else {
printf("[\033[91mfail\033[0m] %s: %s\n", test.name, err);
retval = 1;
}
}
return retval;
}
|