/* Copyright (c) 2019, Anthony Latorre * * 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 . */ #include #include "zebra.h" #include "event.h" #include "zdab_utils.h" #include "pmt.h" #include "db.h" #include "dqxx.h" #include /* for PRIu32 macro */ #include /* for memcpy() */ #include /* for errno */ #include "release.h" #include "dc.h" #define EV_RECORD 0x45562020 // 'EV ' #define MCTK_RECORD 0x4d43544b // 'MCTK' #define MCVX_RECORD 0x4d435658 // 'MCVX' char *GitSHA1(void); char *GitDirty(void); void usage(void) { fprintf(stderr,"Usage: ./zdab-cat [options] FILENAME\n"); fprintf(stderr," -o output file (default: stdout)\n"); fprintf(stderr," --skip-second-event only fit the first event after a MAST bank\n"); fprintf(stderr," -h display this help message\n"); exit(1); } int main(int argc, char **argv) { int i; zebraFile *f; zebraBank bmast, bmc, bmcgn, mctk, b; int rv; EVBank bev; FTPVBank bftpv; FTXKBank bftxk; RSPBank bftxr; MCTKBank bmctk; MCVXBank bmcvx; event ev = {0}; char *filename = NULL; char *output = NULL; FILE *fout = stdout; int skip_second_event = 0; size_t nhit; int last_run; char dqxx_file[256]; for (i = 1; i < argc; i++) { if (strlen(argv[i]) >= 2 && !strncmp(argv[i], "--", 2)) { if (!strcmp(argv[i]+2,"skip-second-event")) { skip_second_event = 1; continue; } } else if (argv[i][0] == '-') { switch (argv[i][1]) { case 'o': output = argv[++i]; break; case 'h': usage(); default: fprintf(stderr, "unrecognized option '%s'\n", argv[i]); exit(1); } } else { filename = argv[i]; } } if (!filename) usage(); f = zebra_open(filename); if (!f) { fprintf(stderr, "%s\n", zebra_err); return 1; } if (output) { fout = fopen(output, "w"); if (!fout) { fprintf(stderr, "failed to open '%s': %s\n", output, strerror(errno)); return 1; } } if (fout) { fprintf(fout, "git_sha1: %s\n", GitSHA1()); fprintf(fout, "git_dirty: %s\n", GitDirty()); } if (load_pmt_info()) { zebra_close(f); if (output) fclose(fout); return 1; } for (i = 0; i < MAX_PMTS; i++) { ev.pmt_hits[i].hit = 0; ev.pmt_hits[i].flags = 0; } ev.run = -1; dict *db = db_init(); last_run = 10000; if (load_file(db, "DQXX_0000010000.dat", 0)) { fprintf(stderr, "failed to load DQXX_0000010000.dat: %s\n", db_err); goto err; } if (dqxx_init(db, &ev)) { fprintf(stderr, "failed to initialize DQXX bank: %s\n", dqxx_err); goto err; } while (1) { rv = zebra_read_next_logical_record(f); if (rv == -1) { fprintf(stderr, "error getting logical record: %s\n", zebra_err); goto err; } else if (rv == 1) { /* EOF */ break; } rv = zebra_get_bank(f, &bmast, f->first_bank); if (rv) { fprintf(stderr, "error getting MAST bank: %s\n", zebra_err); goto err; } if (bmast.links[KMAST_EV-1] == 0) { /* First logical record in SNOCR files doesn't have an EV bank. */ continue; } if (fout) fprintf(fout, "---\n"); if (bmast.links[KMAST_MC-1] == 0) goto skip_mc; rv = zebra_get_bank(f,&bmc,bmast.links[KMAST_MC-1]); if (rv) { fprintf(stderr, "error getting MC bank: %s\n", zebra_err); goto err; } if (bmast.links[KMC_MCGN-1] == 0) { fprintf(stderr, "MCGN link is zero!\n"); goto err; } rv = zebra_get_bank(f,&bmcgn,bmc.links[KMC_MCGN-1]); if (rv) { fprintf(stderr, "error getting MCGN bank: %s\n", zebra_err); goto err; } if (fout) fprintf(fout, " mcgn:\n"); while (1) { if (bmcgn.links[KMCGN_MCTK-1] == 0) { fprintf(stderr, "MCTK link is zero!\n"); goto err; } rv = zebra_get_bank(f,&mctk,bmcgn.links[KMCGN_MCTK-1]); if (rv) { fprintf(stderr, "error getting MCTK bank: %s\n", zebra_err); goto err; } if (mctk.orig == mctk.up - KMCVX_MCTK) { /* This is the first MCTK bank. */ unpack_mctk(mctk.data, &bmctk); } else { /* For some reason SNOMAN sometimes links to the second MCTK * from the MCGN bank. */ rv = zebra_get_bank(f,&b,mctk.orig); if (b.idh != MCTK_RECORD) { fprintf(stderr, "error following origin link from MCTK bank!\n"); goto err; } if (rv) { fprintf(stderr, "error getting MCTK bank: %s\n", zebra_err); goto err; } unpack_mctk(b.data, &bmctk); } if (mctk.up == 0) { fprintf(stderr, "MCVX link is zero!\n"); goto err; } rv = zebra_get_bank(f,&b,mctk.up); if (rv) { fprintf(stderr, "error getting MCVX bank: %s\n", zebra_err); goto err; } unpack_mcvx(b.data, &bmcvx); if (fout) { fprintf(fout, " -\n"); fprintf(fout, " id: %" PRIu32 "\n", bmctk.idp); fprintf(fout, " energy: %.2f\n", bmctk.ene); fprintf(fout, " posx: %.2f\n", bmcvx.x); fprintf(fout, " posy: %.2f\n", bmcvx.y); fprintf(fout, " posz: %.2f\n", bmcvx.z); fprintf(fout, " dirx: %.4f\n", bmctk.drx); fprintf(fout, " diry: %.4f\n", bmctk.dry); fprintf(fout, " dirz: %.4f\n", bmctk.drz); fprintf(fout, " time: %.4f\n", bmcvx.tim); } if (bmcgn.next) { rv = zebra_get_bank(f,&bmcgn,bmcgn.next); if (rv) { fprintf(stderr, "error getting MCGN bank: %s\n", zebra_err); goto err; } } else { break; } } skip_mc: rv = zebra_get_bank(f,&b,bmast.links[KMAST_EV-1]); if (rv) { fprintf(stderr, "error getting EV bank: %s\n", zebra_err); goto err; } /* Skip to the last event so we can traverse them in reverse order. The * reason for this is that for some reason SNOMAN puts the events in * reverse order within each logical record. */ while (b.next) { rv = zebra_get_bank(f,&b,b.next); if (rv) { fprintf(stderr, "error getting EV bank: %s\n", zebra_err); goto err; } } if (fout) fprintf(fout, " ev:\n"); while (1) { unpack_ev(b.data, &bev); ev.run = bev.run; ev.gtid = bev.gtr_id; ev.trigger_type = bev.trg_type; ev.trigger_time = bev.gtr; if (ev.run != last_run) { fprintf(stderr, "loading DQXX file for run %010i\n", ev.run); sprintf(dqxx_file, "DQXX_%010i.dat", ev.run); if (load_file(db, dqxx_file, 1)) { fprintf(stderr, "failed to load %s: %s\n", dqxx_file, db_err); goto err; } if (dqxx_init(db, &ev)) { fprintf(stderr, "failed to initialize DQXX bank: %s\n", dqxx_err); goto err; } last_run = ev.run; } rv = get_event(f,&ev,&b); nhit = get_nhit(&ev); if (fout) { fprintf(fout, " - run: %i\n", ev.run); fprintf(fout, " gtr: %.0f\n", ev.trigger_time); fprintf(fout, " nhit: %zu\n", nhit); fprintf(fout, " gtid: %i\n", ev.gtid); fprintf(fout, " trg_type: 0x%08x\n", ev.trigger_type); fprintf(fout, " dc: 0x%08x\n", get_dc_word(&ev, f, &bmast, &b)); } if (fout) { if (get_ftpv(f,&b,&bftpv)) { fprintf(stderr, "%s\n", zdab_err); } else { fprintf(fout, " ftp:\n"); fprintf(fout, " x: %.2f\n", bftpv.x); fprintf(fout, " y: %.2f\n", bftpv.y); fprintf(fout, " z: %.2f\n", bftpv.z); } } if (fout) { if (get_ftxk(f,&b,&bftxk)) { fprintf(stderr, "%s\n", zdab_err); } else { fprintf(fout, " ftk:\n"); fprintf(fout, " energy: %.2f\n", bftxk.energy); } } if (fout) { if (get_rsp(f,&b,&bftxr)) { fprintf(stderr, "%s\n", zdab_err); } else { fprintf(fout, " rsp:\n"); fprintf(fout, " energy: %.2f\n", bftxr.ene); } } /* Note the origin link for the first EV bank points back to the * structural link location in the MAST bank. These links are super * confusing! */ if ((b.orig == f->first_bank - KMAST_EV) || skip_second_event) break; rv = zebra_get_bank(f,&b,b.orig); if (rv) { fprintf(stderr, "error getting EV bank: %s\n", zebra_err); goto err; } } } db_free(db); if (fout) fclose(fout); zebra_close(f); return 0; err: db_free(db); if (fout) fclose(fout); zebra_close(f); return 1; } id='n257' href='#n257'>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
/* 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/>.
 */

#include "find_peaks.h"
#include "vector.h"
#include "event.h"
#include "pmt.h"
#include <stddef.h> /* for size_t */
#include <stdlib.h> /* for malloc() */
#include "optics.h"
#include <math.h> /* for exp() */
#include "misc.h"
#include "pmt.h"

typedef struct peak {
    size_t i;
    size_t j;
    double value;
} peak;

/* Compare two different peaks.
 *
 * Note: We return 1 if peak b is greater than peak b and -1 if peak a is
 * greater than peak b. This is backwards from what you would normally expect,
 * but it's because we want to keep the peaks sorted in *descending* order. */
static int peak_compare(const void *a, const void *b)
{
    const peak *pa = (peak *) a;
    const peak *pb = (peak *) b;

    if (pa->value > pb->value)
        return -1;
    else if (pa->value < pb->value)
        return 1;

    return 0;
}

void find_peaks_array(double *x, size_t n, size_t m, size_t *imax, size_t *jmax, size_t *npeaks, size_t max_peaks, double threshold)
{
    /* Find a maximum of `max_peaks` in the 2D array `x` indexed as:
     *
     *     x[i,j] = x[i*m + j]
     *
     *  and store the indices in the two arrays `imax` and `jmax`.
     *
     *  Peaks are defined as any array element which is greater than all 8 of
     *  it's neighbors. Peaks are also required to be at least max*threshold
     *  times as high as the highest peak.
     *
     *  The returned peaks will always be the *highest* peaks and they will be
     *  returned in sorted order from highest to lowest. */
    size_t i, j;
    double max;
    peak *p;

    if (n*m == 0) {
        *npeaks = 0;
        return;
    }

    p = malloc(sizeof(peak)*(max_peaks+1));

    /* First, find the highest value in the array (which is guaranteed to be a peak). */
    max = x[0];
    p[0].i = 0;
    p[0].j = 0;
    p[0].value = max;
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            if (x[i*m + j] > max) {
                max = x[i*m + j];
                p[0].i = i;
                p[0].j = j;
                p[0].value = max;
            }
        }
    }

    *npeaks = 1;

    if (*npeaks >= max_peaks) goto end;

    /* Now we look for other peaks which are at least max*threshold high. */
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            if (x[i*m + j] <= max*threshold) continue;

            if (i == p[0].i && j == p[0].j) continue;

            /* Check to see if it is actually a peak. */
            if (x[i*m + (j+1) % m] < x[i*m + j]               && /* 0 +1 */
                x[i*m + (j+m-1) % m] < x[i*m + j]             && /* 0 -1 */
                x[((i+1) % n)*m + j] < x[i*m + j]             && /* +1 0 */
                x[((i+1) % n)*m + (j+1) % m] < x[i*m + j]     && /* +1 +1 */
                x[((i+1) % n)*m + (j+m-1) % m] < x[i*m + j]   && /* +1 -1 */
                x[((i+n-1) % n)*m + j] < x[i*m + j]           && /* -1 0 */
                x[((i+n-1) % n)*m + (j+1) % m] < x[i*m + j]   && /* -1 +1 */
                x[((i+n-1) % n)*m + (j+m-1) % m] < x[i*m + j]) { /* -1 -1 */
                p[*npeaks].i = i;
                p[*npeaks].j = j;
                p[*npeaks].value = x[i*m+j];
                *npeaks += 1;

                qsort(p,*npeaks,sizeof(peak),peak_compare);

                if (*npeaks >= max_peaks) *npeaks = max_peaks;
            }
        }
    }

end:
    for (i = 0; i < *npeaks; i++) {
        imax[i] = p[i].i;
        jmax[i] = p[i].j;
    }

    free(p);

    return;
}

double get_qhs_avg(event *ev, double *pos, double *dir)
{
    /* Returns the average QHS value for all PMTs within the Cerenkov cone of a
     * particle at position `pos` travelling in direction `dir`. */
    size_t i;
    double qhs_sum, n_d2o;
    double pmt_dir[3];
    size_t n;

    n_d2o = get_index_snoman_d2o(400.0);

    n = 0;
    qhs_sum = 0.0;
    for (i = 0; i < MAX_PMTS; i++) {
        if (ev->pmt_hits[i].flags || pmts[i].pmt_type != PMT_NORMAL || !ev->pmt_hits[i].hit) continue;
        SUB(pmt_dir,pmts[i].pos,pos);
        normalize(pmt_dir);
        if (DOT(pmt_dir,dir) > 1/n_d2o) {
            qhs_sum += ev->pmt_hits[i].q;
            n += 1;
        }
    }

    return qhs_sum/n;
}

void get_hough_transform(event *ev, double *pos, double *x, double *y, size_t n, size_t m, double *result, double *last, size_t len)
{
    /* Computes the "Hough transform" of the event `ev` and stores it in `result`. */
    size_t i, j, k;
    double dir[3], pmt_dir[3], cos_theta2;
    int skip;
    double *sin_theta, *cos_theta, *sin_phi, *cos_phi;
    double wavelength0, n_d2o;
    double qhs[MAX_PMTS], qhs_avg;

    sin_theta = malloc(sizeof(double)*n);
    cos_theta = malloc(sizeof(double)*n);
    sin_phi = malloc(sizeof(double)*n);
    cos_phi = malloc(sizeof(double)*n);

    wavelength0 = 400.0;
    n_d2o = get_index_snoman_d2o(wavelength0);

    /* Precompute sin(theta), cos(theta), sin(phi), and cos(phi) to speed
     * things up. */
    for (i = 0; i < n; i++) {
        sin_theta[i] = sin(x[i]);
        cos_theta[i] = cos(x[i]);
    }

    for (i = 0; i < m; i++) {
        sin_phi[i] = sin(y[i]);
        cos_phi[i] = cos(y[i]);
    }

    for (i = 0; i < MAX_PMTS; i++) {
        if (ev->pmt_hits[i].flags || pmts[i].pmt_type != PMT_NORMAL || !ev->pmt_hits[i].hit) continue;
        qhs[i] = ev->pmt_hits[i].q;
    }

    /* Subtract off previous rings. */
    for (i = 0; i < len; i++) {
        qhs_avg = get_qhs_avg(ev,pos,last+3*i);
        for (j = 0; j < MAX_PMTS; j++) {
            if (ev->pmt_hits[i].flags || pmts[i].pmt_type != PMT_NORMAL || !ev->pmt_hits[i].hit) continue;

            SUB(pmt_dir,pmts[j].pos,pos);

            normalize(pmt_dir);

            cos_theta2 = DOT(pmt_dir,last+3*i);

            qhs[j] -= qhs_avg*exp(-fabs(cos_theta2-1.0/n_d2o)/0.1);

            if (qhs[j] < 0.0) qhs[j] = 0.0;
        }
    }

    /* Zero out the result array. */
    for (i = 0; i < n*m; i++) result[i] = 0.0;

    for (i = 0; i < MAX_PMTS; i++) {
        if (ev->pmt_hits[i].flags || pmts[i].pmt_type != PMT_NORMAL || !ev->pmt_hits[i].hit) continue;

        SUB(pmt_dir,pmts[i].pos,pos);

        normalize(pmt_dir);

        /* Ignore PMT hits within the Cerenkov cone of previously found peaks. */
        skip = 0;
        for (j = 0; j < len; j++) {
            if (DOT(pmt_dir,last+3*j) > 1/n_d2o) {
                skip = 1;
                break;
            }
        }

        if (skip) continue;

        for (j = 0; j < n; j++) {
            for (k = 0; k < m; k++) {
                dir[0] = sin_theta[j]*cos_phi[k];
                dir[1] = sin_theta[j]*sin_phi[k];
                dir[2] = cos_theta[j];

                cos_theta2 = DOT(pmt_dir,dir);

                result[j*n + k] += qhs[i]*exp(-fabs(cos_theta2-1.0/n_d2o)/0.1);
            }
        }
    }

    free(sin_theta);
    free(cos_theta);
    free(sin_phi);
    free(cos_phi);
}

/* Finds rings in the event `ev` by looking for peaks in the Hough transform.
 * The directions of potential particle rings are stored in the arrays
 * `peak_theta` and `peak_phi`. The number of rings found are stored in the
 * variable `npeaks`.
 *
 * This function first computes the Hough transform of the event `ev` and looks
 * for the highest peak. The next peak is then found by computing the Hough
 * transform after subtracting off the previous ring and ignoring any PMTs
 * within the Cerenkov cone of the first peak. This process is repeated until
 * `max_peaks` peaks are found.
 *
 * In addition, only peaks which are `delta` radians away from other peaks are
 * returned. So, for example if three peaks are found but the first two are
 * very close to each other, this function will only return the first and the
 * third peaks. */
void find_peaks(event *ev, double *pos, size_t n, size_t m, double *peak_theta, double *peak_phi, size_t *npeaks, size_t max_peaks, double delta)
{
    size_t i, j;
    double *x, *y, *result, *dir;
    size_t *imax, *jmax;
    size_t max;
    double theta, phi;
    int unique;

    x = calloc(n,sizeof(double));
    y = calloc(m,sizeof(double));
    result = malloc(n*m*sizeof(double));
    dir = calloc(max_peaks*3,sizeof(double));

    for (i = 0; i < n; i++) {
        x[i] = i*M_PI/(n-1);
    }

    for (i = 0; i < m; i++) {
        y[i] = i*2*M_PI/(m-1);
    }

    imax = calloc(max_peaks,sizeof(size_t));
    jmax = calloc(max_peaks,sizeof(size_t));

    *npeaks = 0;

    for (i = 0; i < max_peaks; i++) {
        get_hough_transform(ev,pos,x,y,n,m,result,dir,i);
        max = argmax(result,n*m);
        theta = x[max/m];
        phi = y[max % m];
        get_dir(dir+i*3,theta,phi);

        /* Only add the latest peak to the results if it's more than `delta`
         * radians away from all other results so far. */
        unique = 1;
        for (j = 0; j < i; j++) {
            /* If the angle between the latest peak and a previous peak is
             * within `delta` radians, it's not unique. */
            if (acos(DOT(dir+i*3,dir+j*3)) < delta) unique = 0;
        }

        /* Add it to the results if it's unique. */
        if (unique) {
            peak_theta[*npeaks] = theta;
            peak_phi[*npeaks] = phi;
            *npeaks += 1;
        }
    }

    free(imax);
    free(jmax);
    free(x);
    free(y);
    free(result);
    free(dir);
}