-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulation_algorithms.cpp
More file actions
390 lines (358 loc) · 16.6 KB
/
simulation_algorithms.cpp
File metadata and controls
390 lines (358 loc) · 16.6 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <array>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <future>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <random>
#include <string>
#include <thread>
#include <valarray>
#include <vector>
#include "algebra_types.h"
#include "algebra_utils.h"
#include "detector_setup.h"
#include "geometry_types.h"
#include "histogram.h"
#include "sampled_distribution.h"
#include "simulation_algorithms.h"
#include "utilities.h"
constexpr double MEAN_ELOSS { 0.16 }; ///!< mean energy loss in MeV/mm
constexpr double ELOSS_WIDTH { 0.2 * MEAN_ELOSS }; ///!< width of e loss distribution
/** @brief The cumulative distribution function (CDF) for the cos^2(x) PDF distribution
* This CDF is used for the calculation of the Probability Density Function (PDF)
* of the cos^2(x) distribution for generating random values following the angular distribution
* of the muon tracks
*/
auto cos2cdf = [](double x, const std::vector<double>& params = std::vector<double>{}) {
//return cdf to following pdf: cos^2(x)
return (2 / pi()) * (x / 2. + sin(2. * x) / 4.) + 0.5; //from Wolfram Alpha
};
//erfc(e^(-(x - μ)/(2 σ))/sqrt(2))≈erfc(exp(-(0.5 (x - μ))/σ))/sqrt(2))
auto moyal_cdf = [](double x, const std::vector<double>& params) {
assert(params.size() == 2);
//return cdf to following pdf: Moayal distribution
constexpr double c_sqrt2 { std::sqrt(2.) };
return std::erfc(std::exp(-0.5*(x-params.at(0))/params.at(1))/c_sqrt2);
};
void theta_scan(const DetectorSetup& setup, std::mt19937& gen, std::size_t nr_events, double theta_min, double theta_max, std::size_t nr_bins, std::vector<Histogram>* histos)
{
/*
if (setup.ref_detector() == setup.detectors().end()) {
std::cerr << "no reference detector defined in DetectorSetup!\n";
return;
}
*/
Histogram acc_hist("acceptance_scan_theta",
nr_bins,
0., theta_max);
const auto bounds { setup.ref_detector().bounding_box() };
std::uniform_real_distribution<> distro_x {
bounds.first[0],
bounds.second[0],
};
std::uniform_real_distribution<> distro_y {
bounds.first[1],
bounds.second[1],
};
std::uniform_real_distribution<> distro_z {
bounds.first[2],
bounds.second[2]
};
std::uniform_real_distribution<> distro_phi(-pi(), pi());
const double theta_step { (theta_max - theta_min) / (nr_bins - 1) };
std::cout << "theta step: " << theta_step << " rad = " << toDeg(theta_step) << " deg\n";
std::cout << "#theta acceptance acceptance_error\n";
double theta { theta_min };
for (size_t bin { 0 }; bin < nr_bins; ++bin) {
std::size_t mc_events { 0 };
std::size_t detector_events { 0 };
for (std::size_t n = 0; n < nr_events; ++n) {
double phi { distro_phi(gen) };
Line line { Line::generate(
{ distro_x(gen), distro_y(gen), distro_z(gen) }, theta, phi) };
bool coincidence { false };
LineSegment refdet_path { setup.ref_detector().intersection(line) };
if (refdet_path.length() > DEFAULT_EPSILON) {
mc_events++;
coincidence = true;
}
for (auto detector { setup.detectors().cbegin() };
detector != setup.detectors().end();
++detector) {
if (detector == setup.detectors().cbegin())
continue;
LineSegment det_path { detector->intersection(line) };
if (det_path.length() < DEFAULT_EPSILON) {
coincidence = false;
}
}
if (coincidence) {
//std::cout << n << " " << std::setw(2) << toDeg(theta) << " " << toDeg(phi) << " " << det1_path.length() << " " << det2_path.length() << "\n";
detector_events++;
}
}
std::cout << toDeg(theta) << " " << static_cast<double>(detector_events) / mc_events << " " << std::sqrt(detector_events) / mc_events << "\n";
// acc_hist.fill(theta, detector_events);
acc_hist.fill(theta, static_cast<double>(detector_events) / mc_events);
std::cout << std::flush;
theta += theta_step;
}
if (histos != nullptr)
histos->push_back(acc_hist);
}
double simulate_geometric_aperture(const DetectorSetup& setup, std::mt19937& gen, std::size_t nr_events, double theta)
{
/*
if (setup.ref_detector() == setup.detectors().end()) {
std::cerr << "no reference detector defined in DetectorSetup!\n";
return {};
}
*/
auto bounds { setup.ref_detector().bounding_box() };
Point dimensions { bounds.second - bounds.first };
std::cout << "detector bounds: min=" << bounds.first << " max=" << bounds.second << "\n";
std::cout << "detector dimensions=" << dimensions << "\n";
//bounds.first -= dimensions * 5;
//bounds.second += dimensions * 5;
std::cout << "simulation bounds: min=" << bounds.first << " max=" << bounds.second << "\n";
std::uniform_real_distribution<> distro_x {
bounds.first[0],
bounds.second[0]
};
std::uniform_real_distribution<> distro_y {
bounds.first[1],
bounds.second[1]
};
const double simulation_plane_z_pos { bounds.first[2] };
std::uniform_real_distribution<> distro_z {
bounds.first[2],
bounds.second[2]
};
std::uniform_real_distribution<> distro_phi(-pi(), pi());
std::size_t mc_events { 0 };
std::size_t detector_events { 0 };
for (std::size_t n = 0; n < nr_events; ++n) {
const double phi { (inEpsilon(theta)) ? 0. : distro_phi(gen) };
Line line { Line::generate({ distro_x(gen), distro_y(gen), distro_z(gen) }, theta, phi) };
bool coincidence { true };
LineSegment refdet_path { setup.ref_detector().intersection(line) };
mc_events++;
for (auto detector { setup.detectors().cbegin() };
detector != setup.detectors().end();
++detector) {
LineSegment det_path { detector->intersection(line) };
if (det_path.length() < DEFAULT_EPSILON) {
coincidence = false;
}
}
if (coincidence) {
/*
std::cout << "coincidence detected n="<< n << " " << std::setw(2) << toDeg(theta) << " " << toDeg(phi) << "\n";
*/
detector_events++;
}
}
double acceptance { static_cast<double>(detector_events) / mc_events };
std::cout << "events simulated:" << mc_events << " events detected:" << detector_events << " acceptance:" << static_cast<double>(detector_events) / mc_events << " acceptance error: " << std::sqrt(detector_events) / mc_events << "\n";
dimensions = { (bounds.second - bounds.first) };
const double simulation_area { 1e-6 * dimensions[0] * dimensions[1] };
double effective_area { acceptance * simulation_area };
std::cout << "effective area: " << effective_area << " +-" << std::sqrt(detector_events) / mc_events * simulation_area << " m^2\n";
return effective_area;
}
template <int PHI_BINS = 256, int THETA_BINS = 256>
std::array<std::array<double, THETA_BINS>, PHI_BINS> theta_phi_scan(const DetectorSetup& setup, std::mt19937& gen, std::size_t nr_events, double theta_min, double theta_max, double phi_min, double phi_max)
{
std::array<std::array<double, THETA_BINS>, PHI_BINS> phi_theta_acceptance {};
/*
if (setup.ref_detector() == setup.detectors().end()) {
std::cerr << "no reference detector defined in DetectorSetup!\n";
return phi_theta_acceptance;
}
*/
const auto bounds { setup.ref_detector().bounding_box() };
std::uniform_real_distribution<> distro_x {
bounds.first[0],
bounds.second[0],
};
std::uniform_real_distribution<> distro_y {
bounds.first[1],
bounds.second[1],
};
std::uniform_real_distribution<> distro_z {
bounds.first[2],
bounds.second[2]
};
const double phi_step { (phi_max - phi_min) / (PHI_BINS - 1) };
const double theta_step { (theta_max - theta_min) / (THETA_BINS - 1) };
std::cout << "#phi theta acceptance acceptance_error\n";
for (std::size_t phi_bin { 0 }; phi_bin < PHI_BINS; phi_bin++) {
double phi { phi_min + phi_bin * phi_step };
for (std::size_t theta_bin { 0 }; theta_bin < THETA_BINS; theta_bin++) {
double theta { theta_min + theta_bin * theta_step };
std::size_t mc_events { 0 };
std::size_t detector_events { 0 };
for (std::size_t n = 0; n < nr_events; ++n) {
Line line { Line::generate(
{ distro_x(gen), distro_y(gen), distro_z(gen) }, theta, phi) };
bool coincidence { false };
LineSegment refdet_path { setup.ref_detector().intersection(line) };
if (refdet_path.length() > DEFAULT_EPSILON) {
mc_events++;
coincidence = true;
}
for (auto detector { setup.detectors().cbegin() };
detector != setup.detectors().end();
++detector) {
if (detector == setup.detectors().cbegin())
continue;
LineSegment det_path { detector->intersection(line) };
if (det_path.length() < DEFAULT_EPSILON) {
coincidence = false;
}
}
if (coincidence) {
//std::cout << n << " " << std::setw(2) << toDeg(theta) << " " << toDeg(phi) << " " << det1_path.length() << " " << det2_path.length() << "\n";
detector_events++;
}
}
double acceptance { static_cast<double>(detector_events) / mc_events };
std::cout << toDeg(phi) << " " << toDeg(theta) << " " << acceptance << " " << std::sqrt(detector_events) / mc_events << "\n";
phi_theta_acceptance[phi_bin][theta_bin] = acceptance;
std::cout << std::flush;
}
}
return phi_theta_acceptance;
}
DataItem<double> cosmic_simulation(const DetectorSetup& setup, std::mt19937& gen, std::size_t nr_events, std::vector<Histogram>* histos, std::size_t nr_bins, double theta_max, int coinc_level)
{
DataItem<double> data_item {};
/*
if (setup.ref_detector() == setup.detectors().end()) {
std::cerr << "no reference detector defined in DetectorSetup!\n";
return DataItem<double> {};
}
*/
const std::size_t n_detectors { setup.detectors().size() };
std::map<std::size_t, double> pathlength_values {};
std::vector<Histogram> pathlength_histos {};
std::vector<Histogram> eloss_histos {};
std::size_t det_index { 0 };
for ( auto det : setup.detectors() ) {
auto bounds { det.bounding_box() };
double max_pl = norm(bounds.second - bounds.first);
//std::cout << "det " << std::to_string(det_index+1) << ": max dim=" << max_pl << std::endl;
Histogram pl_hist("pathlength_det"+std::to_string(det_index+1), 500U, 0., max_pl*0.1);
Histogram eloss_hist("eloss_det"+std::to_string(det_index+1), 500U, 0., MEAN_ELOSS*max_pl*0.1);
pathlength_histos.emplace_back( std::move(pl_hist) );
eloss_histos.emplace_back( std::move(eloss_hist) );
det_index++;
}
if (coinc_level < 0)
coinc_level = setup.detectors().size();
Histogram theta_hist("theta_distribution", nr_bins, 0., theta_max);
Histogram phi_hist("phi_distribution", nr_bins, -pi(), pi());
Histogram theta_acc_hist("accepted_theta_distribution", nr_bins, 0., theta_max);
Histogram phi_acc_hist("accepted_phi_distribution", nr_bins, -pi(), pi());
const auto bounds { setup.ref_detector().bounding_box() };
std::uniform_real_distribution<> distro_x {
bounds.first[0],
bounds.second[0],
};
// std::cout << "x-bounds: min=" << bounds.first[0] << " max=" << bounds.second[0] << "\n";
std::uniform_real_distribution<> distro_y {
bounds.first[1],
bounds.second[1],
};
// std::cout << "y-bounds: min=" << bounds.first[1] << " max=" << bounds.second[1] << "\n";
std::uniform_real_distribution<> distro_z {
bounds.first[2],
bounds.second[2]
};
// std::cout << "z-bounds: min=" << bounds.first[2] << " max=" << bounds.second[2] << "\n";
std::uniform_real_distribution<> distro_phi(-pi(), pi());
SampledDistribution distro_theta(cos2cdf, 0., pi() / 2., std::vector<double>{});
SampledDistribution distro_eloss(moyal_cdf, 0., 20., { MEAN_ELOSS, ELOSS_WIDTH });
std::size_t mc_events { 0 };
std::size_t detector_events { 0 };
for (std::size_t n = 0; n < nr_events; ++n) {
const double theta { distro_theta(gen) };
const double phi { distro_phi(gen) };
double eloss { distro_eloss(gen) };
//std::cout << "eloss=" << eloss << std::endl;
Line line {
Line::generate({ distro_x(gen), distro_y(gen), distro_z(gen) }, theta, phi)
};
theta_hist.fill(theta);
phi_hist.fill(phi);
unsigned int coincidence { 0 };
LineSegment refdet_path { setup.ref_detector().intersection(line) };
if (refdet_path.length() > 0.) {
pathlength_values[0] = refdet_path.length();
mc_events++;
if (mc_events % 100'000 == 0)
std::cout << mc_events / 1000UL << "k MC events\n";
coincidence++;
} else continue;
det_index = 0;
for (auto detector { setup.detectors().cbegin() };
detector != setup.detectors().end();
++detector, ++det_index) {
if (detector == setup.detectors().cbegin())
continue;
LineSegment det_path { detector->intersection(line) };
if (det_path.length() > 0.) {
pathlength_values[det_index] = det_path.length();
coincidence++;
}
}
if (coincidence >= coinc_level) {
//std::cout << n << " " << std::setw(2) << toDeg(theta) << " " << toDeg(phi) << " " << det1_path.length() << " " << det2_path.length() << "\n";
theta_acc_hist.fill(theta);
phi_acc_hist.fill(phi);
for ( auto [ detindex, pathlength_value ] : pathlength_values ) {
pathlength_histos.at(detindex).fill(pathlength_value);
eloss_histos.at(detindex).fill(pathlength_value * eloss);
}
detector_events++;
}
}
std::cout << "MC events: " << mc_events << " detected events: " << detector_events << " acceptance: " << static_cast<double>(detector_events) / mc_events << " err(acceptance): " << std::sqrt(detector_events) / mc_events << "\n";
if (histos != nullptr) {
histos->push_back(std::move(theta_hist));
histos->push_back(std::move(phi_hist));
histos->push_back(std::move(theta_acc_hist));
histos->push_back(std::move(phi_acc_hist));
if ( !pathlength_histos.empty() ) {
histos->insert(histos->end(), pathlength_histos.begin(), pathlength_histos.end());
}
if ( !eloss_histos.empty() ) {
histos->insert(histos->end(), eloss_histos.begin(), eloss_histos.end());
}
}
return { static_cast<double>(detector_events) / mc_events, std::sqrt(detector_events) / mc_events };
}
MeasurementVector<double, double> cosmic_simulation_detector_sweep(const DetectorSetup& setup, std::mt19937& gen, std::size_t nr_events, const Vector& detector_rotation_axis, double detector_min_angle, double detector_max_angle, std::size_t nr_angles, int coinc_level)
{
MeasurementVector<double, double> data_series {};
//std::cout<<"rot matrix of orig. setup:\n"<<setup.ref_detector()->get_rotation_matrix();
auto rotated_setup { setup };
//std::cout<<"rot matrix of copied setup:\n"<<rotated_setup.ref_detector()->get_rotation_matrix();
const double dtheta { (detector_max_angle - detector_min_angle) / std::max<std::size_t>(1, (nr_angles - 1)) };
std::cout << "min angle=" << detector_min_angle << ", dtheta=" << dtheta << "\n";
double angle { detector_min_angle };
rotated_setup.rotate(detector_rotation_axis, detector_min_angle);
for (std::size_t i = 0; i < nr_angles; ++i) {
std::cout << "current angle=" << toDeg(angle) << "deg\n";
DataItem<double> item { cosmic_simulation(rotated_setup, gen, nr_events, nullptr, 90, toRad(90.), coinc_level) };
data_series.emplace_back(DataItem<double>({ angle, dtheta }), std::move(item));
angle += dtheta;
rotated_setup.rotate(detector_rotation_axis, dtheta);
// std::cout<<"rot matrix of rotated setup:\n"<<rotated_setup.ref_detector()->get_rotation_matrix();
}
return data_series;
}