diff options
author | tlatorre <tlatorre@uchicago.edu> | 2018-08-14 09:53:09 -0500 |
---|---|---|
committer | tlatorre <tlatorre@uchicago.edu> | 2018-08-14 09:53:09 -0500 |
commit | 0b7f199c0d93074484ea580504485a32dc29f5e2 (patch) | |
tree | e167b6d102b87b7a5eca4558e7f39265d5edc502 /zdab_utils.c | |
parent | 636595905c9f63e6bfcb6d331312090ac2075377 (diff) | |
download | sddm-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 'zdab_utils.c')
-rw-r--r-- | zdab_utils.c | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/zdab_utils.c b/zdab_utils.c new file mode 100644 index 0000000..d946eef --- /dev/null +++ b/zdab_utils.c @@ -0,0 +1,199 @@ +#include "Record_Info.h" +#include <stdint.h> +#include "zdab_utils.h" +#include "pack2b.h" +#include <stdlib.h> /* for size_t */ +#include <stdio.h> /* for fprintf() */ + +int isOrphan(aPmtEventRecord *pmtRecord) +{ + /* Returns non-zero if the specified event is an orphan. */ + int i; + + uint32_t *mtc_data = (uint32_t *) &pmtRecord->TriggerCardData; + + for (i = 0; i < 6; ++i) { + if (*mtc_data != 0) return 0; + ++mtc_data; + } + + return 1; +} + +// PH 04/23/98 +// Swap 4-byte integer/floats between native and external format +void swap_int32(int32_t *val_pt, int count) +{ + int32_t *last = val_pt + count; + while (val_pt < last) { + *val_pt = ((*val_pt << 24) & 0xff000000) | + ((*val_pt << 8) & 0x00ff0000) | + ((*val_pt >> 8) & 0x0000ff00) | + ((*val_pt >> 24) & 0x000000ff); + ++val_pt; + } + return; +} + +// Swap 2-byte integers between native and external format +void swap_int16(int16_t *val_pt, int count) +{ + char tmp; + int i; + for (i=0; i<count; ++i) { + tmp = ((char *) val_pt)[0]; + ((char *) val_pt)[0] = ((char *) val_pt)[1]; + ((char *) val_pt)[1] = tmp; + ++val_pt; + } + return; +} + +void unpack_ev(uint32_t *data, EVBank *b) +{ + unpack((uint8_t *) data, "l",&b->run); + unpack((uint8_t *) (data+1), "l",&b->evn); + unpack((uint8_t *) (data+2), "l",&b->dtp); + unpack((uint8_t *) (data+3), "l",&b->jdy); + unpack((uint8_t *) (data+4), "l",&b->ut1); + unpack((uint8_t *) (data+5), "l",&b->ut2); + unpack((uint8_t *) (data+6), "l",&b->dte); + unpack((uint8_t *) (data+7), "l",&b->hmsc); + unpack((uint8_t *) (data+8), "l",&b->gtr1); + unpack((uint8_t *) (data+9), "l",&b->gtr2); + unpack((uint8_t *) (data+10),"l",&b->npm); + unpack((uint8_t *) (data+11),"l",&b->nph); + unpack((uint8_t *) (data+12),"l",&b->sub_run); + unpack((uint8_t *) (data+13),"l",&b->mc_pck); + unpack((uint8_t *) (data+14),"l",&b->rec); + unpack((uint8_t *) (data+15),"l",&b->vpck); + unpack((uint8_t *) (data+16),"l",&b->gtr_id); + unpack((uint8_t *) (data+17),"l",&b->trg_type); + unpack((uint8_t *) (data+18),"l",&b->peak); + unpack((uint8_t *) (data+19),"l",&b->diff); + unpack((uint8_t *) (data+20),"l",&b->integral); + unpack((uint8_t *) (data+21),"l",&b->err); + unpack((uint8_t *) (data+22),"l",&b->data_set); + unpack((uint8_t *) (data+22),"lll",&b->spare1[0], + &b->spare1[1], + &b->spare1[2]); + unpack((uint8_t *) (data+26),"l",&b->ncd_status); + unpack((uint8_t *) (data+27),"l",&b->num_muxg); + unpack((uint8_t *) (data+29),"l",&b->num_mux); + unpack((uint8_t *) (data+29),"l",&b->num_scope); + unpack((uint8_t *) (data+30),"lllll",&b->spare2[0], + &b->spare2[1], + &b->spare2[2], + &b->spare2[3], + &b->spare2[4]); + unpack((uint8_t *) (data+35),"l",&b->ncd_clk_up); + unpack((uint8_t *) (data+36),"l",&b->ncd_clk_lw); + unpack((uint8_t *) (data+37),"l",&b->ncd_reg); + unpack((uint8_t *) (data+38),"l",&b->ncd_gtid); + unpack((uint8_t *) (data+39),"l",&b->ncd_sync); + unpack((uint8_t *) (data+40),"l",&b->spare3[0], + &b->spare3[1], + &b->spare3[2], + &b->spare3[3], + &b->spare3[4], + &b->spare3[5], + &b->spare3[6], + &b->spare3[7], + &b->spare3[8], + &b->spare3[9]); +} + +void unpack_pmt(uint32_t *data, PMTBank *b) +{ + unpack((uint8_t *) data,"l",&b->pn); + unpack((uint8_t *) (data+1),"l",&b->pf); + unpack((uint8_t *) (data+2),"f",&b->pt); + unpack((uint8_t *) (data+3),"f",&b->phl); + unpack((uint8_t *) (data+4),"f",&b->phs); + unpack((uint8_t *) (data+5),"f",&b->plx); + unpack((uint8_t *) (data+6),"f",&b->pt0); + unpack((uint8_t *) (data+7),"l",&b->pif); + unpack((uint8_t *) (data+8),"f",&b->pit); + unpack((uint8_t *) (data+9),"f",&b->pihl); + unpack((uint8_t *) (data+10),"f",&b->pihs); + unpack((uint8_t *) (data+11),"f",&b->pilx); + unpack((uint8_t *) (data+12),"f",&b->pit0); + unpack((uint8_t *) (data+13),"l",&b->cell); + unpack((uint8_t *) (data+14),"l",&b->pin); + unpack((uint8_t *) (data+15),"l",&b->tslh); + unpack((uint8_t *) (data+16),"l",&b->hca); + unpack((uint8_t *) (data+17),"l",&b->eca_val); + unpack((uint8_t *) (data+18),"l",&b->pca_val); + unpack((uint8_t *) (data+19),"l",&b->anxx); + unpack((uint8_t *) (data+20),"l",&b->ept); + unpack((uint8_t *) (data+21),"l",&b->ehl); + unpack((uint8_t *) (data+22),"l",&b->ehs); + unpack((uint8_t *) (data+23),"l",&b->elx); + unpack((uint8_t *) (data+24),"l",&b->pt1); + unpack((uint8_t *) (data+25),"l",&b->ptm); + unpack((uint8_t *) (data+26),"l",&b->ptms); + unpack((uint8_t *) (data+27),"l",&b->qm); + unpack((uint8_t *) (data+28),"l",&b->qms); + unpack((uint8_t *) (data+29),"l",&b->qrc); +} + +int swap_PmtRecord(aPmtEventRecord *aPmtRecord, size_t size) +{ + /* Swap a Pmt Event Record. This function swaps both the Pmt event record + * and the PMT hits and sub fields. Returns -1 if the PMT record has too + * many hits. */ + SWAP_INT32(aPmtRecord, sizeof(aPmtEventRecord)/sizeof(uint32_t)); + + int npmt = aPmtRecord->NPmtHit; + + if (npmt > MAX_NHIT) { + fprintf(stderr, "Read error: Bad ZDAB -- %d pmt hit!", npmt); + return -1; + } else { + if (size < sizeof(aPmtEventRecord) + 3*npmt*4) { + fprintf(stderr, "swap_PmtRecord: size of record is %zu bytes, but there are %i PMT hits", size, npmt); + return -1; + } + // swap the hit data + SWAP_INT32(aPmtRecord + 1, 3*npmt); + // swap the sub-fields + uint32_t *sub_header = &aPmtRecord->CalPckType; + while (*sub_header & SUB_NOT_LAST) { + if (size < (sub_header - (uint32_t *) aPmtRecord)*4 + (*sub_header & SUB_LENGTH_MASK)*4 + 4) { + fprintf(stderr, "swap_PmtRecord: size of record is %zu bytes, " + "but sub-field requires %lu bytes", + size, + (sub_header - (uint32_t *) aPmtRecord)*4 + (*sub_header & SUB_LENGTH_MASK)*4 + 4); + return -1; + } + sub_header += (*sub_header & SUB_LENGTH_MASK); + SWAP_INT32(sub_header, 1); // swap the sub-field header + // get number of data words (-1 because we don't want to include header size) + uint32_t data_words = (*sub_header & SUB_LENGTH_MASK) - 1; + if (size < (sub_header - (uint32_t *) aPmtRecord)*4 + (*sub_header & SUB_LENGTH_MASK)*4) { + fprintf(stderr, "swap_PmtRecord: size of record is %zu bytes, " + "but sub-field requires %lu bytes", + size, + (sub_header - (uint32_t *) aPmtRecord)*4 + (*sub_header & SUB_LENGTH_MASK)*4); + return -1; + } + SWAP_INT32(sub_header+1, data_words); + } + } + + return 0; +} + +void swap_TrigRecord(struct TriggerInfo *aTrigRecord) +{ + /* Byte swap a Trigger Record. */ + SWAP_INT32(aTrigRecord, sizeof(struct TriggerInfo)/sizeof(uint32_t)); +} + +void swap_RunRecord(struct RunRecord *aRunRecord) +{ + /* Byte swap a Run Record. */ + SWAP_INT32(aRunRecord, sizeof(struct RunRecord)/sizeof(uint32_t)); +} + + |