Skip to content

Commit

Permalink
feat: use uproot4 for file writing (#256)
Browse files Browse the repository at this point in the history
* use uproot>=4.1 for file writing instead of uproot3
* drop uproot3 dependency from contrib setup extra
  • Loading branch information
alexander-held authored Aug 27, 2021
1 parent 3959aee commit 5ed199a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 47 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

extras_require = {"contrib": ["uproot3", "uproot>=4.0"]}
extras_require = {"contrib": ["uproot>=4.1"]} # uproot 4.1 for file writing
extras_require["test"] = sorted(
set(
extras_require["contrib"]
Expand Down
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import pytest
import uproot3 as uproot
import uproot


class Utils:
@staticmethod
def create_ntuple(fname, treename, varname, var_array, weightname, weight_array):
with uproot.recreate(fname) as f:
f[treename] = uproot.newtree({varname: "float64", weightname: "float64"})
f[treename].extend({varname: var_array, weightname: weight_array})
f[treename] = {varname: var_array, weightname: weight_array}


@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions tests/contrib/test_histogram_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def test_from_uproot(tmp_path, utils):
fnames = [tmp_path / "test.root"]
treename = "tree"
varname = "var"
var_array = [1.1, 2.3, 3.0, 3.2]
var_array = np.asarray([1.1, 2.3, 3.0, 3.2])
weightname_write = "weight"
weight_array = [1.0, 1.0, 2.0, 1.0]
weight_array = np.asarray([1.0, 1.0, 2.0, 1.0])
bins = np.asarray([1, 2, 3, 4])
# create something to read
utils.create_ntuple(
Expand Down
60 changes: 19 additions & 41 deletions util/create_ntuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import matplotlib.pyplot as plt
import numpy as np
import uproot3 as uproot
import uproot


def toy_distribution(noncentral, multiplier, offset, num_events):
Expand Down Expand Up @@ -60,73 +60,51 @@ def create_file(file_name, distributions, weights, labels, extra_weights=None):
for i, label in enumerate(labels):
lep_charge = create_lepton_charge(n_events)
if label == "background":
f[label] = uproot.newtree(
{
"jet_pt": "float64",
"weight": "float64",
"lep_charge": "int",
"weight_up": "float64",
"weight_down": "float64",
}
)
f[label].extend(
{
"jet_pt": distributions[i],
"weight": weights[i],
"lep_charge": lep_charge,
"weight_up": extra_weights[0],
"weight_down": extra_weights[1],
}
)
f[label] = {
"jet_pt": distributions[i],
"weight": weights[i],
"lep_charge": lep_charge,
"weight_up": extra_weights[0],
"weight_down": extra_weights[1],
}
else:
f[label] = uproot.newtree(
{"jet_pt": "float64", "weight": "float64", "lep_charge": "int"}
)
f[label].extend(
{
"jet_pt": distributions[i],
"weight": weights[i],
"lep_charge": lep_charge,
}
)
f[label] = {
"jet_pt": distributions[i],
"weight": weights[i],
"lep_charge": lep_charge,
}


def create_file_pseudodata(file_name, pseudodata):
n_events = len(pseudodata)
with uproot.recreate(file_name) as f:
# write pseudodata
lep_charge = create_lepton_charge(n_events)
f["pseudodata"] = uproot.newtree({"jet_pt": "float64", "lep_charge": "int"})
f["pseudodata"].extend({"jet_pt": pseudodata, "lep_charge": lep_charge})
f["pseudodata"] = {"jet_pt": pseudodata, "lep_charge": lep_charge}


def read_file(file_name):
distributions = []
weights = []
labels = []
with uproot.open(file_name) as f:
all_trees = f.allkeys(
filterclass=lambda cls: issubclass(cls, uproot.tree.TTreeMethods)
)
all_trees = f.keys(filter_classname="TTree", recursive=True)
for tree in all_trees:
distributions.append(f[tree].array("jet_pt"))
weights.append(f[tree].array("weight"))
labels.append(tree)
distributions.append(f[tree]["jet_pt"].array(library="np"))
weights.append(f[tree]["weight"].array(library="np"))
labels.append(f[tree].name)
return distributions, weights, labels


def read_file_pseudodata(file_name):
with uproot.open(file_name) as f:
distribution = f["pseudodata"].array("jet_pt")
distribution = f["pseudodata"]["jet_pt"].array(library="np")
return distribution


def plot_distributions(data, weights, labels, pseudodata, bins):
bin_width_str = str(int(bins[1] - bins[0]))

# labels = [l.split('\'')[1] for l in labels]
yield_each = [str(round(np.sum(w), 1)) for w in weights]
labels = [label.decode().split(";")[0] for label in labels]

# plot normalized distributions
for i in reversed(range(len(data))):
Expand Down

0 comments on commit 5ed199a

Please # to comment.