aboutsummaryrefslogtreecommitdiff
path: root/src/dqxx.c
blob: 7cf77f3e0546a23f0f62e1ba816c5c72a258f043 (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
/* 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 "dqxx.h"
#include "db.h"
#include <stdint.h> /* for uint32_t */
#include "event.h"
#include <stdio.h> /* for sprintf() */

char dqxx_err[256];

/* Flag PMTs which are not operational based on the DQCH and DQCR banks.
 *
 * Returns 0 on success or -1 on error. */
int dqxx_init(dict *db, event *ev)
{
    int i, j, k, id;
    uint32_t *dqch, *dqcr, dqch_word, dqcr_word;

    for (i = 0; i < 19; i++) {
        dqch = (uint32_t *) get_bank(db, "DQCH", i);
        dqcr = (uint32_t *) get_bank(db, "DQCR", i);

        if (!dqch) {
            sprintf(dqxx_err, "failed to load DQCH for crate %i\n", i);
            return -1;
        }

        if (!dqcr) {
            sprintf(dqxx_err, "failed to load DQCR for crate %i\n", i);
            return -1;
        }

        for (j = 0; j < 16; j++) {
            dqcr_word = dqcr[30+KDQCR_TABLE+j];

            for (k = 0; k < 32; k++) {
                id = i*512 + j*32 + k;
                dqch_word = dqch[30+KDQCH_TABLE+32*j+k];

                /* Clear the PMT_FLAG_DQXX bit. */
                ev->pmt_hits[id].flags &= ~PMT_FLAG_DQXX;

                if (dqch_word & KDQCH_B_PMT_CABLE)
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;
                else if (dqch_word & KDQCH_B_PMTIC_RESISTOR)
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;
                else if (dqch_word & KDQCH_B_SEQUENCER)
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;
                else if (dqch_word & KDQCH_B_750OHM)
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;
                else if (dqch_word & KDQCH_B_NOT_OP)
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;

                if (dqcr_word & KDQCR_B_CRATE)
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;
                else if (dqcr_word & KDQCR_B_MB)
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;
                else if (dqcr_word & KDQCR_B_PMTIC)
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;
                else if (dqcr_word & KDQCR_B_DAQ)
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;
                else if (dqcr_word & KDQCR_B_GT)
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;
                else if (dqcr_word & (1 << (12 + k/8)))
                    ev->pmt_hits[id].flags |= PMT_FLAG_DQXX;
            }
        }
    }

    return 0;
}