How to use uproot - 10 common examples

To help you get started, we’ve selected a few uproot examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scikit-hep / uproot / tests / test_compression.py View on Github external
def test_compression_content1(self):
        for name, array in uproot.open("tests/samples/Zmumu-uncompressed.root")["events"].arrays(["Type", "Event", "E1", "px1", "Q1", "M"]).items():
            array = array.tolist()
            assert uproot.open("tests/samples/Zmumu-zlib.root")["events"].array(name).tolist() == array
            assert uproot.open("tests/samples/Zmumu-lzma.root")["events"].array(name).tolist() == array
            assert uproot.open("tests/samples/Zmumu-lz4.root")["events"].array(name).tolist() == array
            assert uproot.open("tests/samples/Zmumu-zstd.root")["events"].array(name).tolist() == array
github FAST-HEP / fast-carpenter / tests / selection / test_filters.py View on Github external
def test_selection_2_weights_data(config_2, filename):
    selection = filters.build_selection("test_selection_1",
                                        config_2, weights=["EventWeight"])
    infile = uproot.open(filename)["events"]
    mask = selection(infile, is_mc=False)
    assert np.count_nonzero(mask) == 1486

    header = selection.results_header()
    assert len(header) == 2
    assert len(header[0]) == 9

    result = selection.results()

    assert len(result) == 4
    assert result[0][header[0].index("depth")] == 0
    assert result[0][header[0].index("cut")] == "Any"
    assert result[0][header[0].index("passed_incl")] == 1486
    assert result[0][header[0].index("passed_incl") + 1] == 1486
github scikit-hep / uproot / tests / test_tree.py View on Github external
def test_tree_arrays(self):
        file = uproot.open("tests/samples/simple.root")

        tree = file["tree"]
        arrays = tree.arrays()
        assert arrays[b"one"].tolist() == [1, 2, 3, 4]
        assert arrays[b"two"].tolist() == numpy.array([1.1, 2.2, 3.3, 4.4], dtype=numpy.float32).tolist()
        assert arrays[b"three"].tolist() == [b"uno", b"dos", b"tres", b"quatro"]

        # get arrays again
        arrays = tree.arrays()
        assert arrays[b"one"].tolist() == [1, 2, 3, 4]
        assert arrays[b"two"].tolist() == numpy.array([1.1, 2.2, 3.3, 4.4], dtype=numpy.float32).tolist()
        assert arrays[b"three"].tolist() == [b"uno", b"dos", b"tres", b"quatro"]

        # get tree again
        tree = file["tree"]
        arrays = tree.arrays()
github scikit-hep / uproot / tests / test_compression.py View on Github external
def test_compression_identity(self):
        assert uproot.open("tests/samples/Zmumu-zlib.root").compression.algoname == "zlib"
        assert uproot.open("tests/samples/Zmumu-zlib.root").compression.level == 4

        assert uproot.open("tests/samples/Zmumu-lzma.root").compression.algoname == "lzma"
        assert uproot.open("tests/samples/Zmumu-lzma.root").compression.level == 4

        assert uproot.open("tests/samples/Zmumu-lz4.root").compression.algoname == "lz4"
        assert uproot.open("tests/samples/Zmumu-lz4.root").compression.level == 4

        assert uproot.open("tests/samples/Zmumu-zstd.root").compression.algoname == "zstd"
        assert uproot.open("tests/samples/Zmumu-zstd.root").compression.level == 5

        assert uproot.open("tests/samples/Zmumu-uncompressed.root").compression.level == 0

        assert uproot.open("tests/samples/HZZ-zlib.root").compression.algoname == "zlib"
        assert uproot.open("tests/samples/HZZ-zlib.root").compression.level == 4

        assert uproot.open("tests/samples/HZZ-lzma.root").compression.algoname == "lzma"
        assert uproot.open("tests/samples/HZZ-lzma.root").compression.level == 4

        assert uproot.open("tests/samples/HZZ-lz4.root").compression.algoname == "lz4"
        assert uproot.open("tests/samples/HZZ-lz4.root").compression.level == 4

        assert uproot.open("tests/samples/HZZ-zstd.root").compression.algoname == "zstd"
        assert uproot.open("tests/samples/HZZ-zstd.root").compression.level == 5

        assert uproot.open("tests/samples/HZZ-uncompressed.root").compression.level == 0
github scikit-hep / uproot / tests / test_tree.py View on Github external
def test_double32(self):
        t = uproot.open("tests/samples/demo-double32.root")["T"]
        fD64 = t.array("fD64")
        fF32 = t.array("fF32")
        fI32 = t.array("fI32")
        fI30 = t.array("fI30")
        fI28 = t.array("fI28")
        ratio_fF32 = fF32 / fD64
        ratio_fI32 = fI32 / fD64
        ratio_fI30 = fI30 / fD64
        ratio_fI28 = fI28 / fD64
        assert ratio_fF32.min() > 0.9999 and ratio_fF32.max() < 1.0001
        assert ratio_fI32.min() > 0.9999 and ratio_fI32.max() < 1.0001
        assert ratio_fI30.min() > 0.9999 and ratio_fI30.max() < 1.0001
        assert ratio_fI28.min() > 0.9999 and ratio_fI28.max() < 1.0001
github zfit / phasespace / tests / helpers / rapidsim.py View on Github external
def get_tree(file_name, top_particle, particles):
    """Load a RapidSim tree."""
    rapidsim_file = uproot.open(file_name)
    tree = rapidsim_file['DecayTree']
    return {particle: np.stack([1000.0*tree.array('{}_{}_TRUE'.format(particle, coord))
                                for coord in ('PX', 'PY', 'PZ', 'E')])
            for particle in particles}
github scikit-hep / uproot / tests / test_issues.py View on Github external
def test_issue55(self):
        withoffsets = uproot.open(
            "tests/samples/small-dy-withoffsets.root")["tree"]
        nooffsets = uproot.open(
            "tests/samples/small-dy-nooffsets.root")["tree"]
        assert numpy.array_equal(withoffsets.array("nJet"),
                                 nooffsets.array("nJet"))
        assert numpy.array_equal(withoffsets.array("nMuon"),
                                 nooffsets.array("nMuon"))

        def equal(left, right):
            if len(left) != len(right):
                return False
            for x, y in zip(left, right):
                if not numpy.array_equal(x, y):
                    return False
            return True
github scikit-hep / uproot / tests / test_issues.py View on Github external
def test_issue76(self):
        t = uproot.open("tests/samples/issue76.root")["Events"]
        assert list(t.array("rootStrings")[0]) == [b"2", b"4"]
        x, y = t.array("rootStrings")[0]
        assert isinstance(x, uproot.rootio.TString)
github scikit-hep / uproot / tests / test_write.py View on Github external
def test_th1(tmp_path):
    filename = join(str(tmp_path), "example.root")
    testfile = join(str(tmp_path), "test.root")

    f = ROOT.TFile.Open(testfile, "RECREATE")
    h = ROOT.TH1F("hvar", "title", 5, 1, 10)
    h.Sumw2()
    h.Fill(1.0, 3)
    h.Fill(2.0, 4)
    h.Write()
    f.Close()

    t = uproot.open(testfile)
    hist = t["hvar"]
    with uproot.recreate(filename, compression=None) as f:
        f["test"] = hist

    f = ROOT.TFile.Open(filename)
    h = f.Get("test")
    sums = [0.0, 25.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    bincontents = [7.0, 0.0, 0.0, 0.0, 0.0]
    assert list(h.GetSumw2()) == sums
    count = 0
    for x in range(1, 6):
        assert h.GetBinContent(x) == bincontents[count]
        count += 1
    assert h.GetNbinsX() == 5
    assert h.GetMean() == 1.5714285714285714
    assert h.GetRMS() == 0.4948716593053938
github scikit-hep / uproot / tests / test_write.py View on Github external
def test_issue340(tmp_path):
    filename = join(str(tmp_path), "example.root")

    a = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
    with uproot.recreate(filename) as f:
        f["t"] = uproot.newtree({"normal": numpy.float64})
        f["t"].extend({"normal": a})

    t = uproot.open(filename)["t"]
    for i in range(10):
        assert t["normal"].basket(0)[i] == a[i]