Exemple #1
0
def _main():
    args = _parse_cli_args()
    check_input(args.file)
    outfile = args.out
    if not outfile:
        outfile = os.path.splitext(args.file)[0] + ".h5fit"
    check_output(outfile, args.yes)

    histogram = Histogram.from_h5hist(args.file)
    histogram = histogram.cropped_to_adc_cuts()

    popts, pstds = bisection_fit(histogram.pcx, histogram.counts, n=2)

    copyfile(args.file, outfile)
    with h5py.File(outfile, "a") as f:
        fit = f.require_group("Fit")
        f.attrs["basefile"] = os.path.basename(args.file)
        fit.create_dataset("p", data=popts)
        fit.create_dataset("perr", data=pstds)

    sys.exit(0)
Exemple #2
0
def _main():
    args = _parse_cli_args()
    check_input(args.file)
    outfile = args.out
    if not outfile:
        outfile = os.path.splitext(args.file)[0] + ".h5hist"
    check_output(outfile, args.yes)

    if not args.ychannel:
        hist, ex = hist1d_from_mpa_data(args.file,
                                        args.xchannel,
                                        nxbins=args.nx)
        kind = "1D"
    else:
        hist, ex, ey = hist2d_from_mpa_data(args.file,
                                            args.xchannel,
                                            args.ychannel,
                                            nxbins=args.nx,
                                            nybins=args.ny)
        kind = "2D"

    with h5py.File(args.file, "r") as f:
        datafile = f.attrs.get("datafile", None)
    if not datafile:
        datafile = os.path.basename(args.file)

    with h5py.File(outfile, "w") as f:
        f.attrs["datafile"] = datafile
        f.attrs["basefile"] = os.path.basename(args.file)
        f.attrs["kind"] = kind
        f.attrs["xchannel"] = args.xchannel
        f.create_dataset("EX", data=ex)
        f.create_dataset("HIST", data=hist)
        if kind == "2D":
            f.attrs["orientation"] = "x = dim0/rows, y = dim1/cols"
            f.attrs["ychannel"] = args.ychannel
            f.create_dataset("EY", data=ey)

    sys.exit(0)
Exemple #3
0
def _main():
    parser = argparse.ArgumentParser(
        description="Convert an MPA-3 list file into HDF5 format.",
        parents=[default_argparser]
    )
    args = parser.parse_args()
    fin = check_input(args.file)
    fout = args.out
    if not fout:
        fout = fin.replace(".lst", ".h5")
    fout = check_output(fout, args.yes)
    convert(fin, fout)
    sys.exit(0)
def _main():
    args = _parse_cli_args()
    check_input(args.file)
    save_as = {
        ".pdf": args.pdf,
        ".pgf": args.pgf,
        ".png": args.png,
        ".eps": args.eps,
    }
    if args.out:
        out_name, ext = os.path.splitext(args.out)
        if ext:
            save_as[ext] = True
    else:
        out_name, _ = os.path.splitext(args.file)

    if not any(save_as.values()):
        save_as[".pdf"] = True
    for ext, do_save in save_as.items():
        if do_save:
            check_output(out_name + ext, args.yes)

    with h5py.File(args.file, "r") as f:
        popts = f["Fit"]["p"][:]
        pstds = f["Fit"]["perr"][:]

    histogram = Histogram.from_h5hist(args.file)
    histogram = histogram.cropped_to_adc_cuts()

    synth_histogram = make_synth_histogram(histogram.pcx, histogram, popts,
                                           pstds)
    with plt.rc_context(rc={'lines.markersize': 1}):
        _ = fit_overview_plot(histogram, synth_histogram, popts, pstds)

    for ext, do_save in save_as.items():
        if do_save:
            plt.savefig(out_name + ext)

    sys.exit(0)
Exemple #5
0
def _main():
    args = _parse_cli_args()
    check_input(args.file)
    save_as = {
        ".pdf": args.pdf,
        ".pgf": args.pgf,
        ".png": args.png,
        ".eps": args.eps,
    }
    if args.out:
        out_name, ext = os.path.splitext(args.out)
        if ext:
            save_as[ext] = True
    else:
        out_name, _ = os.path.splitext(args.file)

    if not any(save_as.values()):
        save_as[".pdf"] = True
    for ext, do_save in save_as.items():
        if do_save:
            check_output(out_name + ext, args.yes)

    # with h5py.File(args.file, "r") as f:
    #     kind = f.attrs["kind"]
    #     if kind == "1D":
    #         _fig = hist1d_from_h5hist(f, scaling=args.linear)
    #     elif kind == "2D":
    #         _fig = hist2d_from_h5hist(f, scaling=args.linear)
    hist = Histogram.from_h5hist(args.file, metafile=args.meta)
    fig = hist.plot()

    for ext, do_save in save_as.items():
        if do_save:
            plt.savefig(out_name + ext)

    sys.exit(0)
Exemple #6
0
from _common import (default_argparser, read_orchestration_csv, check_input,
                     check_output)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description=
        "Extract metadata from orchestration file and save as pickle object.",
        parents=[default_argparser],
    )
    parser.add_argument("lst",
                        type=str,
                        help="Filename corresponding to lst file")

    args = parser.parse_args()

    if not args.out:
        out = args.lst[:-4] + ".meta"
    else:
        out = args.out

    check_input(args.file)
    check_output(out, yes=args.yes)

    df = read_orchestration_csv(args.file)
    df = df.set_index("FILE")

    with open(out, "wb") as f:
        pickle.dump(dict(df.loc[args.lst]), f)

    sys.exit(0)
Exemple #7
0
def _main():
    args = _parse_cli_args()
    check_input(args.file)
    outfile = args.out
    if not outfile:
        outfile = os.path.splitext(args.file)[0] + ".h5roi"
    check_output(outfile, args.yes)

    kind_of_roi = [args.strip, args.rect, args.poly]
    assert sum(bool(a) for a in kind_of_roi) == 1, "Choose exactly one type of ROI!"

    if args.poly:
        vx = np.array(args.poly[::2])
        vy = np.array(args.poly[1::2])
        _check_roi_2d_poly = _make_check_roi_2d_poly(vx, vy)

    with h5py.File(args.file, "r") as fin, h5py.File(outfile, "w") as fout:
        datafile = fin.attrs.get("datafile", None)
        if not datafile:
            datafile = os.path.basename(args.file)
        fout["datafile"] = datafile

        eve_in = fin["EVENTS"]
        xchan = eve_in[args.xchannel]
        ychan = eve_in[args.ychannel] if args.ychannel else None

        fin.copy("CFG", fout)
        eve_out = fout.create_group("EVENTS")
        roiinf = fout.create_group("ROI")
        for name, vec in eve_in.items():
            stor = eve_out.create_dataset(name, (2,), maxshape=vec.shape, dtype=vec.dtype)

        n = eve_in["TIME"].len()
        chunk_size = TYPICAL_DASK_CHUNK
        stor_pos = 0
        for k in tqdm(range(n//chunk_size+1), desc="Processing"):
            if (k+1)*chunk_size + 1 < n:
                slc = np.s_[k*chunk_size:(k+1)*chunk_size + 1]
            else:
                slc = np.s_[k*chunk_size:]

            xarr = xchan[slc]
            if ychan:
                yarr = ychan[slc]

            if args.strip:
                in_roi = _check_roi_1d(xarr, min(args.strip), max(args.strip))
                kind = "1D"
                roiargs = str(args.strip)

            if args.rect:
                in_roi = _check_roi_2d_rect(
                    xarr, args.rect[0], args.rect[1], yarr, args.rect[2], args.rect[3]
                )
                kind = "2DRect"
                roiargs = str(args.rect)

            if args.poly:
                in_roi = _check_roi_2d_poly(xarr, yarr)
                kind = "2DPoly"
                roiargs = str(args.rect)

            for name, vec in eve_in.items():
                stor = eve_out[name]
                filt = vec[slc][in_roi]
                stor.resize(stor_pos+filt.size, axis=0)
                stor[stor_pos:stor_pos+filt.size] = filt
            stor_pos += filt.size


        roiinf.attrs["kind"] = kind
        roiinf.attrs["roiargs"] = roiargs
        roiinf.attrs["xchannel"] = args.xchannel
        roiinf.attrs["basefile"] = os.path.basename(args.file)
        if args.ychannel:
            roiinf.attrs["ychannel"] = args.ychannel


    sys.exit(0)