aboutsummaryrefslogtreecommitdiff
path: root/src/fit.c
blob: 80b4b3f69bbbe87b03bfee7798540156ebdce484 (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
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
#include "likelihood.h"
#include <stdio.h>
#include "zebra.h"
#include "Record_Info.h"
#include "event.h"
#include "zdab_utils.h"
#include "scattering.h"
#include "pmt.h"
#include "sno_charge.h"
#include "db.h"
#include "dqxx.h"
#include <nlopt.h>
#include <math.h> /* for sin(), cos(), etc. */

#define EV_RECORD    0x45562020 // 'EV  '  (as written to ZDAB file)

double nll(unsigned int n, const double *x, double *grad, void *params)
{
    double T, theta, phi, t0;
    double pos[3], dir[3];
    static size_t iter;
    double fval;

    T = x[0];

    pos[0] = x[1];
    pos[1] = x[2];
    pos[2] = x[3];

    theta = x[4];
    phi = x[5];
    dir[0] = sin(theta)*cos(phi);
    dir[1] = sin(theta)*sin(phi);
    dir[2] = cos(theta);

    t0 = x[6];

    fval = nll_muon((event *) params, T, pos, dir, t0);

    printf("%5zu %10.2f %7.2f %7.2f %7.2f %4.2f %4.2f %5.2f f() = %7.3e\n",
           iter++,
           x[0],
           x[1],
           x[2],
           x[3],
           x[4],
           x[5],
           x[6],
           fval);

    return fval;
}

int main(int argc, char **argv)
{
    int i;
    zebraFile *f;
    bank b;
    int rv;
    PMTBank bpmt;
    EVBank bev;
    event ev = {0};
    int crate, card, channel;
    int id;
    double x[7];
    double ss[7];
    double opt_f;

    if (argc < 2) {
        fprintf(stderr, "usage: fit [filename]\n");
        return 1;
    }

    f = zebra_open(argv[1]);

    if (!f) {
        fprintf(stderr, "%s\n", zebra_err);
        return 1;
    }

    load_pmt_info();

    for (i = 0; i < 10000; i++) {
        ev.pmt_hits[i].hit = 0;
        ev.pmt_hits[i].flags = 0;
    }

    ev.run = -1;

    nlopt_opt opt = nlopt_create(NLOPT_LN_SBPLX, 7);
    nlopt_set_min_objective(opt,nll,&ev);

    init_interpolation();
    init_charge();
    dict *db = db_init();

    if (load_file(db, "DQXX_0000010000.dat")) {
        fprintf(stderr, "failed to load DQXX_0000010000.dat: %s\n", db_err);
        exit(1);
    }

    if (dqxx_init(db, &ev)) {
        fprintf(stderr, "failed to initialize DQXX bank: %s\n", dqxx_err);
        exit(1);
    }

    while (1) {
        rv = next_bank(f, &b);

        if (rv == -1) {
            fprintf(stderr, "error getting bank: %s\n", zebra_err);
            goto err;
        } else if (rv == 1) {
            /* EOF */
            break;
        }

        if (b.name == PMT_RECORD) {
            unpack_pmt(b.data, &bpmt);
            card = bpmt.pin/1024;
            crate = (bpmt.pin % 1024)/32;
            channel = bpmt.pin % 32;
            id = crate*512 + card*32 + channel;
            ev.pmt_hits[id].hit = 1;
            ev.pmt_hits[id].t = bpmt.pt;
            ev.pmt_hits[id].qhl = bpmt.phl;
            ev.pmt_hits[id].qhs = bpmt.phs;
            ev.pmt_hits[id].qlx = bpmt.plx;
            //ev.pmt_hits[id].flags |= bpmt.pn & KPF_DIS;
        } else if (b.name == EV_RECORD) {
            unpack_ev(b.data, &bev);
            if (ev.run != -1) {
                if (ev.run != bev.run || ev.gtid != bev.gtr_id) {
                    /* New event, so we need to fit the old event. */
                    x[0] = 500.0;
                    x[1] = 100.0;
                    x[2] = 100.0;
                    x[3] = 100.0;
                    x[4] = 1.57;
                    x[5] = 0.0;
                    x[6] = 120.0;

                    ss[0] = 10.0;
                    ss[1] = 10.0;
                    ss[2] = 10.0;
                    ss[3] = 10.0;
                    ss[4] = 0.1;
                    ss[5] = 0.1;
                    ss[6] = 1.0;

                    nlopt_set_initial_step(opt, ss);

                    nlopt_optimize(opt,x,&opt_f);
                }
            }
            ev.run = bev.run;
            ev.gtid = bev.gtr_id;
        }
    }

    free_interpolation();

    db_free(db);

    nlopt_destroy(opt);

    zebra_close(f);

    return 0;

err:

    zebra_close(f);
    return 1;
}