Пример #1
0
def define_event_loop(options, n_evt):
    """
    Produce an iterator over the event numbers.

    Parameters
    ----------
    options : dictionary
        Contains the job parameters.
    n_evt : int
        Number of events in the input file.

    Returns
    ------
    gen : generator
        A generator producing the event numbers as configured in the job.
    """
    nevt = options.get("NEVENTS", 0)
    max_evt = n_evt if options["RUN_ALL"] or nevt > n_evt else nevt
    start = options["SKIP"]
    print_mod = options.get("PRINT_MOD", max(1, (max_evt - start) // 20))

    for i in range(start, max_evt):
        if not i % print_mod:
            logger.info("Event # {}".format(i))
        yield i
Пример #2
0
def ISIDORA(argv=sys.argv):
    CFP = configure(argv)

    if CFP["INFO"]:

        print("""
        ISIDORA:
        1. Reads an Nh5 file produced by DIOMIRA, which stores the
            raw waveforms (RWF) for the PMTs and SiPMs waveforms, as well as
            data on geometry, sensors and MC. The RDWF of the PMTs
            show negative swing due to the HPF of the EP FEE electronics

        2. Performs DBLR on the PMT RWF and produces corrected waveforms (CWF).

        3. Adds the CWF and ancilliary info to the DST

        4. Computes the energy of the PMTs per each event and writes to DST

        """)

    N_BASELINE = CFP["N_BASELINE"]
    THR_TRIGGER = CFP["THR_TRIGGER"]
    ACUM_DISCHARGE_LENGTH = CFP["ACUM_DISCHARGE_LENGTH"]
    ACUM_TAU = CFP["ACUM_TAU"]
    ACUM_COMPRESS = CFP["ACUM_COMPRESS"]

    # open the input file in mode append
    with tb.open_file(CFP["FILE_IN"], "a") as h5in:
        # access the PMT raw data in file
        pmtrd_ = h5in.root.RD.pmtrwf  # PMT raw data must exist
        NEVENTS_DST, NPMT, PMTWL = pmtrd_.shape

        logger.info("nof PMTs = {} WF side = {} ".format(NPMT, PMTWL))
        logger.info("nof events in input DST = {} ".format(NEVENTS_DST))

        # create an extensible array to store the CWF waveforms
        # if it exists remove and create again
        if "/RD/pmtcwf" in h5in:
            h5in.remove_node("/RD", "pmtcwf")

        pmtcwf = h5in.create_earray(h5in.root.RD,
                                    "pmtcwf",
                                    atom=tb.Int16Atom(),
                                    shape=(0, NPMT, PMTWL),
                                    expectedrows=NEVENTS_DST)
        # if "/RD/pmtacum" in h5in:
        #     h5in.remove_node("/RD", "pmtacum")
        #
        # pmtacum = h5in.create_earray(h5in.root.RD, "pmtacum",
        #                              atom=tb.Int16Atom(),
        #                              shape=(0, NPMT, PMTWL),
        #                              expectedrows=NEVENTS_DST)

        if "/Deconvolution" not in h5in:
            h5in.create_group(h5in.root, "Deconvolution")
        if "/Deconvolution/Parameters" in h5in:
            h5in.remove_node("/Deconvolution", "Parameters")
        deconv_table = h5in.create_table(h5in.root.Deconvolution, "Parameters",
                                         DECONV_PARAM,
                                         "Deconvolution parameters",
                                         tbl.filters("NOCOMPR"))
        tbl.store_deconv_table(deconv_table, CFP)

        # LOOP
        t0 = time()
        for i in define_event_loop(CFP, NEVENTS_DST):
            signal_r, acum, baseline, baseline_end, \
              noise_rms = DBLR(pmtrd_[i],
                               n_baseline=N_BASELINE,
                               thr_trigger=THR_TRIGGER,
                               acum_discharge_length=ACUM_DISCHARGE_LENGTH,
                               acum_tau=ACUM_TAU,
                               acum_compress=ACUM_COMPRESS)

            # append to pmtcwf
            pmtcwf.append(signal_r.reshape(1, NPMT, PMTWL))
            # append to pmtacum
            #pmtacum.append(acum.reshape(1, NPMT, PMTWL))

        t1 = time()
        dt = t1 - t0
        pmtcwf.flush()
        #pmtacum.flush()

        print("ISIDORA has run over {} events in {} seconds".format(i + 1, dt))
    print("Leaving ISIDORA. Safe travels!")
Пример #3
0
def ANASTASIA(argv=sys.argv):
    """
    ANASTASIA driver
    """
    CFP = configure(argv)

    if CFP["INFO"]:
        print(__doc__)

    # Increate thresholds by 1% for safety
    PMT_NOISE_CUT_RAW = CFP["PMT_NOISE_CUT_RAW"] * 1.01
    PMT_NOISE_CUT_BLR = CFP["PMT_NOISE_CUT_BLR"] * 1.01
    SIPM_ZS_METHOD = CFP["SIPM_ZS_METHOD"]
    SIPM_NOISE_CUT = CFP["SIPM_NOISE_CUT"]

    with tb.open_file(CFP["FILE_IN"], "r+") as h5in:
        pmtblr = h5in.root.RD.pmtblr
        pmtcwf = h5in.root.RD.pmtcwf
        sipmrwf = h5in.root.RD.sipmrwf
        sipmdf = DB.DataSiPM()

        NEVT, NPMT, PMTWL = pmtcwf.shape
        NEVT, NSIPM, SIPMWL = sipmrwf.shape

        logger.info("# events in DST = {}".format(NEVT))
        logger.info("#PMTs = {} #SiPMs = {}".format(NPMT, NSIPM))
        logger.info("PMT WFL = {} SiPM WFL = {}".format(PMTWL, SIPMWL))

        # Create instance of the noise sampler and compute noise thresholds
        sipms_noise_sampler_ = SiPMsNoiseSampler(SIPMWL)

        if SIPM_ZS_METHOD == "FRACTION":
            sipms_thresholds_ = sipms_noise_sampler_.ComputeThresholds(
                SIPM_NOISE_CUT, sipmdf['adc_to_pes'])
        else:
            sipms_thresholds_ = np.ones(NSIPM) * SIPM_NOISE_CUT

        if "/ZS" not in h5in:
            h5in.create_group(h5in.root, "ZS")
        if "/ZS/PMT" in h5in:
            h5in.remove_node("/ZS", "PMT")
        if "/ZS/BLR" in h5in:
            h5in.remove_node("/ZS", "BLR")
        if "/ZS/SiPM" in h5in:
            h5in.remove_node("/ZS", "SiPM")

        # Notice the Int16, not Float32! bad for compression
        pmt_zs_ = h5in.create_earray(h5in.root.ZS,
                                     "PMT",
                                     atom=tb.Int16Atom(),
                                     shape=(0, NPMT, PMTWL),
                                     expectedrows=NEVT)

        blr_zs_ = h5in.create_earray(h5in.root.ZS,
                                     "BLR",
                                     atom=tb.Int16Atom(),
                                     shape=(0, NPMT, PMTWL),
                                     expectedrows=NEVT)

        sipm_zs_ = h5in.create_earray(h5in.root.ZS,
                                      "SiPM",
                                      atom=tb.Int16Atom(),
                                      shape=(0, NSIPM, SIPMWL),
                                      expectedrows=NEVT)

        t0 = time()
        for i in define_event_loop(CFP, NEVT):
            pmtzs = wfm.noise_suppression(pmtcwf[i], PMT_NOISE_CUT_RAW)
            blrzs = wfm.subtract_baseline(FE.CEILING - pmtblr[i])
            blrzs = wfm.noise_suppression(blrzs, PMT_NOISE_CUT_BLR)

            pmt_zs_.append(pmtzs[np.newaxis])
            blr_zs_.append(blrzs[np.newaxis])

            sipmzs = sipmrwf[i]
            if "/MC" not in h5in:
                sipmzs = wfm.subtract_baseline(sipmzs, 200)
            sipmzs = wfm.noise_suppression(sipmzs, sipms_thresholds_)
            sipm_zs_.append(sipmzs[np.newaxis])

        t1 = time()
        dt = t1 - t0

        print("ANASTASIA has run over {} events in {} seconds".format(
            i + 1, dt))
    print("Leaving ANASTASIA. Safe travels!")
Пример #4
0
def DIOMIRA(argv=sys.argv):
    """
    Diomira driver
    """
    CFP = configure(argv)

    if CFP["INFO"]:
        print("""
        DIOMIRA:
         1. Reads a MCRD file produced by art/centella, which stores MCRD
        waveforms for PMTs (bins of 1 ns) and SiPMs (bins of 1 mus)
        2. Simulates the response of the energy plane and outputs both RWF
        and TWF
        3. Simulates the response of the tracking plane in the SiPMs and
        outputs SiPM RWF
        4. Add a table describing the FEE parameters used for simulation
        5. Copies the tables on geometry, detector data and MC
        """)

    # open the input file
    with tables.open_file(CFP["FILE_IN"], "r") as h5in:
        # access the PMT raw data in file
        pmtrd_ = h5in.root.pmtrd
        sipmrd_ = h5in.root.sipmrd

        NEVENTS_DST, NPMT, PMTWL = pmtrd_.shape
        PMTWL_FEE = int(PMTWL / FE.t_sample)
        NEVENTS_DST, NSIPM, SIPMWL = sipmrd_.shape

        logger.info("nof PMTs = {} nof  SiPMs = {} "
                    "nof events in input DST = {} ".format(
                        NPMT, NSIPM, NEVENTS_DST))
        logger.info("lof PMT WF = {} lof SiPM WF (MC) = {} "
                    "lof PMT WF (FEE) = {}".format(PMTWL, SIPMWL, PMTWL_FEE))

        # access the geometry and the sensors metadata info
        mctrk_t = h5in.root.MC.MCTracks
        sipmdf = DB.DataSiPM()

        # Create instance of the noise sampler
        noise_sampler_ = SiPMsNoiseSampler(SIPMWL, True)
        sipms_thresholds_ = CFP["NOISE_CUT"] * np.array(sipmdf["adc_to_pes"])

        COMPRESSION = CFP["COMPRESSION"]
        # open the output file
        with tables.open_file(CFP["FILE_OUT"],
                              "w",
                              filters=tbl.filters(COMPRESSION)) as h5out:

            # create a group to store MC data
            mcgroup = h5out.create_group(h5out.root, "MC")
            # copy the mctrk table
            mctrk_t.copy(newparent=mcgroup)

            # create a table to store Energy plane FEE, hang it from MC group
            fee_table = h5out.create_table(mcgroup, "FEE", FEE,
                                           "EP-FEE parameters",
                                           tbl.filters("NOCOMPR"))

            # create a group to store True waveform data
            twfgroup = h5out.create_group(h5out.root, "TWF")
            # create a table to store true waveform (zs, rebinned)
            pmt_twf_table = h5out.create_table(twfgroup, "PMT", SENSOR_WF,
                                               "Store for PMTs TWF",
                                               tbl.filters(COMPRESSION))

            sipm_twf_table = h5out.create_table(twfgroup, "SiPM", SENSOR_WF,
                                                "Store for SiPM TWF",
                                                tbl.filters(COMPRESSION))

            # and index in event column
            pmt_twf_table.cols.event.create_index()
            sipm_twf_table.cols.event.create_index()

            # fill FEE table
            tbl.store_FEE_table(fee_table)

            # create a group to store RawData
            h5out.create_group(h5out.root, "RD")

            # create an extensible array to store the RWF waveforms
            pmtrwf = h5out.create_earray(h5out.root.RD,
                                         "pmtrwf",
                                         atom=tables.Int16Atom(),
                                         shape=(0, NPMT, PMTWL_FEE),
                                         expectedrows=NEVENTS_DST)

            pmtblr = h5out.create_earray(h5out.root.RD,
                                         "pmtblr",
                                         atom=tables.Int16Atom(),
                                         shape=(0, NPMT, PMTWL_FEE),
                                         expectedrows=NEVENTS_DST)

            sipmrwf = h5out.create_earray(h5out.root.RD,
                                          "sipmrwf",
                                          atom=tables.Int16Atom(),
                                          shape=(0, NSIPM, SIPMWL),
                                          expectedrows=NEVENTS_DST)
            # LOOP
            t0 = time()
            for i in define_event_loop(CFP, NEVENTS_DST):
                # supress zeros in MCRD and rebin the ZS function in 1 mus bins
                rebin = int(units.mus / units.ns)

                trueSiPM = wfm.zero_suppression(sipmrd_[i], 0.)

                # dict_map applies a function to the dictionary values
                truePMT = cf.dict_map(
                    lambda df: wfm.rebin_df(df, rebin),
                    wfm.zero_suppression(pmtrd_[i],
                                         0.,
                                         to_mus=int(units.ns / units.ms)))

                # store in table
                tbl.store_wf_table(i, pmt_twf_table, truePMT)
                tbl.store_wf_table(i, sipm_twf_table, trueSiPM)

                # simulate PMT response and return an array with RWF;BLR
                # convert to float, append to EVector

                dataPMT, blrPMT = simulate_pmt_response(i, pmtrd_)
                pmtrwf.append(dataPMT.astype(int).reshape(1, NPMT, PMTWL_FEE))
                pmtblr.append(blrPMT.astype(int).reshape(1, NPMT, PMTWL_FEE))

                # simulate SiPM response and return an array with RWF
                # convert to float, zero suppress and dump to table
                dataSiPM = simulate_sipm_response(i, sipmrd_, noise_sampler_)
                dataSiPM = wfm.to_adc(dataSiPM, sipmdf)
                dataSiPM = wfm.noise_suppression(dataSiPM, sipms_thresholds_)

                sipmrwf.append(dataSiPM.astype(int).reshape(1, NSIPM, SIPMWL))

            pmtrwf.flush()
            sipmrwf.flush()
            pmtblr.flush()

            t1 = time()
            dt = t1 - t0
            print("DIOMIRA has run over {} events in {} seconds".format(
                i + 1, dt))
    print("Leaving Diomira. Safe travels!")
Пример #5
0
def DOROTHEA(argv=sys.argv):
    """
    DOROTHEA driver
    """
    CFP = configure(argv)

    if CFP["INFO"]:
        print(__doc__)

    FILE_IN = CFP["FILE_IN"]
    FILE_OUT = CFP["FILE_OUT"]
    COMPRESSION = CFP["COMPRESSION"]
    NEVENTS = CFP["NEVENTS"]

    logger.info("Debug level = {}".format(CFP["VERBOSITY"]))
    logger.info("Input file = {}".format(FILE_IN))
    logger.info("Output file = {}".format(FILE_OUT))
    logger.info("# events requested = {}".format(NEVENTS))
    logger.info("Compression library/level = {}".format(COMPRESSION))

    # open the input file
    with tb.open_file(FILE_IN, "r") as h5in:
        # access the PMT ZS data in file
        pmtzs_ = h5in.root.ZS.PMT
        blrzs_ = h5in.root.ZS.BLR
        sipmzs_ = h5in.root.ZS.SiPM

        NEVT, NPMT, PMTWL = pmtzs_.shape
        NEVT, NSIPM, SIPMWL = sipmzs_.shape

        logger.info("# events in DST: {}".format(NEVT))
        logger.info("# PMTs = {}, # SiPMs = {} ".format(NPMT, NSIPM))
        logger.info("PMT WFL = {}, SiPM WFL = {}".format(PMTWL, SIPMWL))

        pmtdf = DB.DataPMT()
        sipmdf = DB.DataSiPM()

        pmt_to_pes = abs(1.0 / pmtdf.adc_to_pes.reshape(NPMT, 1))
        sipm_to_pes = abs(1.0 / sipmdf.adc_to_pes.reshape(NSIPM, 1))

        # open the output file
        with tb.open_file(FILE_OUT, "w",
                          filters=tbl.filters(COMPRESSION)) as h5out:

            # create groups and copy MC data to the new file
            if "/MC" in h5in:
                mcgroup = h5out.create_group(h5out.root, "MC")
                twfgroup = h5out.create_group(h5out.root, "TWF")

                h5in.root.MC.MCTracks.copy(newparent=mcgroup)
                h5in.root.MC.FEE.copy(newparent=mcgroup)
                h5in.root.TWF.PMT.copy(newparent=twfgroup)
                h5in.root.TWF.SiPM.copy(newparent=twfgroup)

            pmapsgroup = h5out.create_group(h5out.root, "PMAPS")

            # create a table to store pmaps (rebined, linked, zs wfs)
            pmaps_ = h5out.create_table(pmapsgroup, "PMaps", PMAP,
                                        "Store for PMaps",
                                        tbl.filters(COMPRESSION))

            pmaps_blr_ = h5out.create_table(pmapsgroup, "PMapsBLR", PMAP,
                                            "Store for PMaps made with BLR",
                                            tbl.filters(COMPRESSION))

            # add index in event column
            pmaps_.cols.event.create_index()
            pmaps_blr_.cols.event.create_index()

            # LOOP
            t0 = time()
            for i in define_event_loop(CFP, NEVT):
                pmtwf = np.sum(pmtzs_[i] * pmt_to_pes, axis=0)
                blrwf = np.sum(blrzs_[i] * pmt_to_pes, axis=0)
                sipmwfs = sipmzs_[i] * sipm_to_pes

                pmap = build_pmap(pmtwf, sipmwfs)
                classify_peaks(pmap, **CFP)
                tbl.store_pmap(pmap, pmaps_, i)

                pmap_blr = build_pmap(blrwf, sipmwfs)
                classify_peaks(pmap_blr, **CFP)
                tbl.store_pmap(pmap_blr, pmaps_blr_, i)

            t1 = time()
            dt = t1 - t0
            print("DOROTHEA has run over {} events in {} seconds".format(
                i + 1, dt))
    print("Leaving DOROTHEA. Safe travels!")