aboutsummaryrefslogtreecommitdiff
path: root/src/likelihood.h
blob: ddd8397bece72c052212c05b7f0a46df49ee4720 (plain)
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
/* Copyright (c) 2019, Anthony Latorre <tlatorre at uchicago>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.

 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.

 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef LIKELIHOOD_H
#define LIKELIHOOD_H

#include "event.h"
#include <stddef.h> /* for size_t */
#include <gsl/gsl_interp.h>
#include <gsl/gsl_spline.h>

/* Probability that a channel is miscalibrated and/or not working even though
 * it's marked as OK in DQXX, etc. */
#define P_MISCALIBRATION 1e-4

#define EPSILON 1e-10

#define PSUP_REFLECTION_TIME 80.0

/* Minimum number of points in the track integral. */
#define MIN_NPOINTS 10

/* To avoid having to allocate new arrays every time we evaluate the likelihood
 * function, we just allocate static arrays with `MAX_POINTS` elements. */
#define MAX_NPOINTS 1000

/* Maximum number of PE to consider if the PMT was hit. */
#define MAX_PE 10000
/* Maximum number of PE to consider if the PMT wasn't hit.
 *
 * Note: This must be less than MAX_PE. */
#define MAX_PE_NO_HIT 10

/* To speed things up we quit calculating the probability of a hit when the
 * ratio between the current probability and the maximum probability is less
 * than 10**(-MIN_RATIO). So if MIN_RATIO is -10, that means that we ignore
 * probabilities which are 10 million times less likely than the most probable
 * value for n. */
#define MIN_RATIO -10
/* Same as above but for the "fast" likelihood calculation. */
#define MIN_RATIO_FAST -2

/* The fraction of reflected light which is detected. */
#define CHARGE_FRACTION 0.4

/* Dark rate of the PMTs (Hz).
 *
 * From pmt_response.dat in SNOMAN. */
#define DARK_RATE 1000.0
/* Single PE transit time spread (ns).
 *
 * From pmt_response.dat in SNOMAN. */
#define PMT_TTS 1.61
/* Event window (ns) */
#define GTVALID 400.0
#define BETA_MIN 0.8

/* Maximum number of vertices to fit. */
#define MAX_VERTICES 10

typedef struct vertex {
    int id;
    double T0;
    double pos[3];
    double dir[3];
    double t0;
    double z1[10];
    double z2[10];
    size_t n;
} vertex;

typedef struct particle {
    int id;
    double mass;
    double range;
    double *x;
    double *T;
    double *cos_theta;
    double *cdf_shower;
    /* Spline for looking up cdf -> cos(theta). */
    gsl_spline *spline_shower;
    gsl_interp_accel *acc_shower;
    double *cdf_delta;
    double *x_shower;
    double *gamma_pdf;
    double xlo_shower;
    double xhi_shower;
    double pos_a;
    double pos_b;
    gsl_spline *spline_delta;
    gsl_interp_accel *acc_delta;
    size_t n;
    double a;
    double b;
    double shower_photons;
    double delta_ray_a;
    double delta_ray_b;
    double delta_ray_photons;
} particle;

particle *particle_init(int id, double T0, size_t n);
double particle_get_energy(double x, particle *p);
void particle_free(particle *p);
double time_pdf(double t, double mu_noise, double mu_indirect, double *mu, size_t n, double *ts, double tmean, double *ts_sigma);
double time_cdf(double t, double mu_noise, double mu_indirect, double *mu, size_t n, double *ts, double tmean, double *ts_sigma);
double nll_best(event *ev);
double nll(event *ev, vertex *v, size_t n, double dx, int ns, const int fast, int charge_only, int hit_only);

#endif