aboutsummaryrefslogtreecommitdiff
path: root/test-likelihood.c
diff options
context:
space:
mode:
authortlatorre <tlatorre@uchicago.edu>2018-08-14 09:53:09 -0500
committertlatorre <tlatorre@uchicago.edu>2018-08-14 09:53:09 -0500
commit0b7f199c0d93074484ea580504485a32dc29f5e2 (patch)
treee167b6d102b87b7a5eca4558e7f39265d5edc502 /test-likelihood.c
parent636595905c9f63e6bfcb6d331312090ac2075377 (diff)
downloadsddm-0b7f199c0d93074484ea580504485a32dc29f5e2.tar.gz
sddm-0b7f199c0d93074484ea580504485a32dc29f5e2.tar.bz2
sddm-0b7f199c0d93074484ea580504485a32dc29f5e2.zip
initial commit of likelihood fit for muons
This commit contains code to fit for the energy, position, and direction of muons in the SNO detector. Currently, we read events from SNOMAN zebra files and fill an event struct containing the PMT hits and fit it with the Nelder Mead simplex algorithm from GSL. I've also added code to read in ZEBRA title bank files to read in the DQXX files for a specific run. Any problems with channels in the DQCH and DQCR banks are flagged in the event struct by masking in a bit in the flags variable and these PMT hits are not included in the likelihood calculation. The likelihood for an event is calculated by integrating along the particle track for each PMT and computing the expected number of PE. The charge likelihood is then calculated by looping over all possible number of PE and computing: P(q|n)*P(n|mu) where q is the calibrated QHS charge, n is the number of PE, and mu is the expected number of photoelectrons. The latter is calculated assuming the distribution of PE at a given PMT follows a Poisson distribution (which I think should be correct given the track, but is probably not perfect for tracks which scatter a lot). The time part of the likelihood is calculated by integrating over the track for each PMT and calculating the average time at which the PMT is hit. We then assume the PDF for the photons to arrive is approximately a delta function and compute the first order statistic for a given time to compute the probability that the first photon arrived at a given time. So far I've only tested this with single tracks but the method was designed to be easy to use when you are fitting for multiple particles.
Diffstat (limited to 'test-likelihood.c')
-rw-r--r--test-likelihood.c193
1 files changed, 193 insertions, 0 deletions
diff --git a/test-likelihood.c b/test-likelihood.c
new file mode 100644
index 0000000..f0266b9
--- /dev/null
+++ b/test-likelihood.c
@@ -0,0 +1,193 @@
+#include "muon.h"
+#include "random.h"
+#include "optics.h"
+#include "quantum_efficiency.h"
+#include <math.h>
+#include <gsl/gsl_histogram.h>
+#include "sno.h"
+#include "pdg.h"
+#include "vector.h"
+#include "solid_angle.h"
+#include <stdlib.h> /* for atoi() and strtod() */
+#include <unistd.h> /* for exit() */
+#include "scattering.h"
+#include <errno.h> /* for errno */
+#include <string.h> /* for strerror() */
+
+void simulate_cos_theta_distribution(int N, gsl_histogram *h, double T, double theta0)
+{
+ /* Simulate the cos(theta) distribution around the original track direction
+ * for a muon with kinetic energy T. The angle from the original track
+ * distribution is simulated as a gaussian distribution with standard
+ * deviation `theta0`. */
+ int i;
+ double theta, phi, wavelength, u, qe, index, cerenkov_angle, dir[3], n[3], dest[3], E, p, beta, cos_theta;
+
+ i = 0;
+ while (i < N) {
+ /* Generate a random wavelength in the range 300-600 nm from the
+ * distribution of Cerenkov light. */
+ u = genrand_real2();
+ wavelength = 300.0*600.0/(u*(300.0-600.0) + 600.0);
+
+ qe = get_quantum_efficiency(wavelength);
+
+ /* Check to see if the photon was detected. */
+ if (genrand_real2() > qe) continue;
+
+ index = get_index(HEAVY_WATER_DENSITY, wavelength, 10.0);
+
+ /* Calculate total energy */
+ E = T + MUON_MASS;
+ p = sqrt(E*E - MUON_MASS*MUON_MASS);
+ beta = p/E;
+
+ cerenkov_angle = acos(1/(index*beta));
+
+ /* Assuming the muon track is dominated by small angle scattering, the
+ * angular distribution will be a Gaussian centered around 0 with a
+ * standard deviation of `theta0`. Here, we draw a random angle from
+ * this distribution. */
+ theta = randn()*theta0;
+
+ n[0] = sin(theta);
+ n[1] = 0;
+ n[2] = cos(theta);
+
+ /* To compute the direction of the photon, we start with a vector in
+ * the x-z plane which is offset from the track direction by the
+ * Cerenkov angle and then rotate it around the track direction by a
+ * random angle `phi`. */
+ dir[0] = sin(cerenkov_angle + theta);
+ dir[1] = 0;
+ dir[2] = cos(cerenkov_angle + theta);
+
+ phi = genrand_real2()*2*M_PI;
+
+ rotate(dest,dir,n,phi);
+
+ cos_theta = dest[2];
+
+ gsl_histogram_increment(h, cos_theta);
+
+ i += 1;
+ }
+}
+
+void usage(void)
+{
+ fprintf(stderr,"Usage: ./test-likelihood [options]\n");
+ fprintf(stderr," -n number of events\n");
+ fprintf(stderr," -T kinetic energy of muon (MeV)\n");
+ fprintf(stderr," -t standard deviation of angular distribution\n");
+ fprintf(stderr," -b number of bins\n");
+ fprintf(stderr," --xmin lowest value of cos(theta)\n");
+ fprintf(stderr," --xmax highest value of cos(theta)\n");
+ fprintf(stderr," -h display this help message\n");
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ size_t i, N, bins;
+ double T, theta0;
+ double E, p, beta;
+ double xmin, xmax;
+
+ N = 100000;
+ bins = 1000;
+ T = 1000.0;
+ theta0 = 0.1;
+ xmin = -1.0;
+ xmax = 1.0;
+
+ for (i = 1; i < argc; i++) {
+ if (!strncmp(argv[i], "--", 2)) {
+ if (!strcmp(argv[i]+2,"xmin")) {
+ xmin = strtod(argv[++i],NULL);
+ continue;
+ } else if (!strcmp(argv[i]+2,"xmax")) {
+ xmax = strtod(argv[++i],NULL);
+ continue;
+ }
+ } else if (argv[i][0] == '-') {
+ switch (argv[i][1]) {
+ case 'n':
+ N = atoi(argv[++i]);
+ break;
+ case 'b':
+ bins = atoi(argv[++i]);
+ break;
+ case 'T':
+ T = strtod(argv[++i],NULL);
+ break;
+ case 't':
+ theta0 = strtod(argv[++i],NULL);
+ break;
+ case 'h':
+ usage();
+ default:
+ fprintf(stderr, "unrecognized option '%s'\n", argv[i]);
+ exit(1);
+ }
+ }
+ }
+
+ gsl_histogram *h = gsl_histogram_alloc(bins);
+ gsl_histogram_set_ranges_uniform(h,xmin,xmax);
+
+ simulate_cos_theta_distribution(N, h, T, theta0);
+
+ gsl_histogram_scale(h, 1.0/gsl_histogram_sum(h));
+
+ FILE *pipe = popen("graph -T X --bitmap-size 2000x2000 -X 'Cos(theta)' -Y Probability", "w");
+
+ if (!pipe) {
+ fprintf(stderr, "error running graph command: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ for (i = 0; i < h->n; i++) {
+ fprintf(pipe, "%g %g\n", h->range[i], h->bin[i]);
+ fprintf(pipe, "%g %g\n", h->range[i+1], h->bin[i]);
+ }
+ fprintf(pipe, "\n\n");
+
+ gsl_histogram_reset(h);
+
+ init_interpolation();
+
+ /* Calculate total energy */
+ E = T + MUON_MASS;
+ p = sqrt(E*E - MUON_MASS*MUON_MASS);
+ beta = p/E;
+
+ for (i = 0; i < bins; i++) {
+ double lo, hi;
+ gsl_histogram_get_range(h, i, &lo, &hi);
+ double cos_theta = (lo+hi)/2.0;
+ h->bin[i] = get_probability(beta, cos_theta, theta0);
+ }
+
+ free_interpolation();
+
+ printf("\n\n");
+
+ gsl_histogram_scale(h, 1.0/gsl_histogram_sum(h));
+
+ for (i = 0; i < h->n; i++) {
+ fprintf(pipe, "%g %g\n", h->range[i], h->bin[i]);
+ fprintf(pipe, "%g %g\n", h->range[i+1], h->bin[i]);
+ }
+ fprintf(pipe, "\n\n");
+
+ if (pclose(pipe)) {
+ fprintf(stderr, "error closing graph command: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ gsl_histogram_free(h);
+
+ return 0;
+}
+