-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLikelihood.cpp
215 lines (172 loc) · 4.33 KB
/
Likelihood.cpp
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
/*
This code is free to use, copy, distribute, and modify.
If you use this code or any modification of this code, we request that you reference both this code https://zenodo.org/record/438675 and the paper https://arxiv.org/abs/1703.09721.
*/
#include <gsl/gsl_min.h>
#include <gsl/gsl_roots.h>
#include <gsl/gsl_errno.h>
#include <cmath>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include "Likelihood.h"
#include "MWDisks.h"
#include "ICEvent.h"
#include "Coordinates.h"
#include "vMF.h"
#include "Backgrounds.h"
#include "Progress.h"
double L_gals[54];
void calc_L_gals()
{
std::cout << "Calculating L_gals..." << std::endl;
std::ofstream data("data/L_gals.txt");
int N_Repeat;
coord2D coord_gal_event, coord_gal_MW;
double L;
bool HE_Cut;
HE_Cut = false; // a cut on galactic energy at 900 TeV. Default: false
N_Repeat = 1e6;
Progress_Bar *pbar = new Progress_Bar();
pbar->update(0);
for (unsigned int i = 0; i < events.size(); i++)
{
if (events[i].E > 900 and HE_Cut)
data << i << " " << events[i].id << " " << 0 << std::endl;
else
{
coord_gal_event = eq_to_gal(events[i].coord_eq);
L = 0;
for (int j = 0; j < N_Repeat; j++)
{
coord_gal_MW = MW(true);
L += f_vMF(cos_theta(coord_gal_event, coord_gal_MW), events[i].kappa);
} // j, N_Repeat
L /= N_Repeat;
data << i << " " << events[i].id << " " << L << std::endl;
} // E < 900 TeV
pbar->update(0, events.size(), i, true);
} // i, events
delete pbar;
data.close();
std::cout << "Done." << std::endl;
}
void read_L_gals()
{
std::ifstream data("data/L_gals.txt");
std::string line;
std::stringstream ss;
int i, id;
double L;
while (getline(data, line))
{
std::stringstream ss(line);
ss >> i >> id >> L;
L_gals[id] = L;
} // while, data, line
data.close();
}
double L_gal(ICEvent event, double f_gal)
{
return f_gal * L_gals[event.id];
}
double L_exgal(double f_gal)
{
return (1 - f_gal) / (4 * M_PI);
}
double L(ICEvent event, double f_gal)
{
double l;
l = (L_gal(event, f_gal) + L_exgal(f_gal)) * L_astro(event) + L_bkg(event);
if (l == 0)
return 1e-50;
return l;
}
double logL(double f_gal)
{
double logL;
logL = 0;
for (unsigned int i = 0; i < events.size(); i++)
{
logL += log(L(events[i], f_gal));
} // i, events
return logL;
}
// for the minimizer/root finder
double sigma_minus_2logL(double f_gal, void *params)
{
double *p = (double *)params;
double sigma, logL_hfgal;
sigma = p[0];
logL_hfgal = p[1];
return -pow(sigma, 2) - 2 * (logL(f_gal) - logL_hfgal);
}
// finds the best fit f_gal
double hat_f_gal()
{
int status;
double minimum, init, F_min, F_init, F_max;
const gsl_min_fminimizer_type *T;
gsl_min_fminimizer *s;
gsl_function F;
double params[2] = {0, 0};
init = 0.1;
// check that there is a minimum near init
F_min = sigma_minus_2logL(0, params);
F_init = sigma_minus_2logL(init, params);
F_max = sigma_minus_2logL(1, params);
if (F_min < F_init and F_init < F_max) return 0;
if (F_min > F_init and F_init > F_max) return 1;
F.function = &sigma_minus_2logL;
F.params = params;
T = gsl_min_fminimizer_brent;
s = gsl_min_fminimizer_alloc(T);
gsl_min_fminimizer_set(s, &F, init, 0, 1.);
status = GSL_CONTINUE;
while (status == GSL_CONTINUE)
{
gsl_min_fminimizer_iterate(s);
status = gsl_min_test_interval(gsl_min_fminimizer_x_lower(s), gsl_min_fminimizer_x_upper(s), 1e-5, 0);
} // while continuing
minimum = gsl_min_fminimizer_x_minimum(s);
gsl_min_fminimizer_free(s);
return minimum;
}
double sigma_to_f_gal(double sigma, bool lower)
{
int status;
double f_gal, hfgal, min, max;
const gsl_root_fsolver_type *T;
gsl_root_fsolver *s;
gsl_function F;
hfgal = hat_f_gal();
double params[2] = {sigma, logL(hfgal)};
if (lower)
{
min = 0;
max = hfgal;
}
else
{
min = hfgal;
max = 1;
}
// if they have the same sign:
if (sigma_minus_2logL(min, params) * sigma_minus_2logL(max, params) > 0)
return -1;
F.function = &sigma_minus_2logL;
F.params = params;
T = gsl_root_fsolver_brent;
s = gsl_root_fsolver_alloc(T);
gsl_root_fsolver_set(s, &F, min, max);
status = GSL_CONTINUE;
while (status == GSL_CONTINUE)
{
gsl_root_fsolver_iterate(s);
status = gsl_root_test_interval(gsl_root_fsolver_x_lower(s), gsl_root_fsolver_x_upper(s), 1e-5, 0);
} // while continuing
f_gal = gsl_root_fsolver_root(s);
gsl_root_fsolver_free(s);
return f_gal;
}