diff --git a/.github/workflows/pythontests.yml b/.github/workflows/pythontests.yml index 9ac6546..1df2960 100644 --- a/.github/workflows/pythontests.yml +++ b/.github/workflows/pythontests.yml @@ -40,6 +40,7 @@ jobs: wget --quiet http://scikit-hep.org/uproot3/examples/Event.root histoprint -s Event.root -f T/event/fTracks/fTracks.fYfirst -f T/event/fTracks/fTracks.fYlast[:,0] histoprint -s Event.root -f T/fTracks.fYfirst -f T/event/fTracks/fTracks.fYlast[:,0] -C 'fNtrack > 5' + histoprint -s Event.root --field-prefix T/event/fTracks/fTracks. -f fYfirst -f fYlast[:,0] histoprint -s Event.root -f htime pre-commit: diff --git a/CHANGELOG.md b/CHANGELOG.md index 0567917..c90896f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- add support for --field-prefix option at cli + ## [2.4.0] -### Addded +### Added - RichHistogram class for use with the `rich` package. - Argument to set maximum count in bins for plotting. diff --git a/README.rst b/README.rst index 8334a79..93ab3f8 100644 --- a/README.rst +++ b/README.rst @@ -179,6 +179,12 @@ It can read in files or take data directly from STDIN:: 'tree/branch[:,2]' to histogram the 3rd elements of a vector-like branch. + --field-prefix TEXT String to prepend to each argument passed to + --field option following --field-prefix E.g. + 'histoprint -f Muon_eta --field-prefix Tau_ + -f eta -f phi' ... would refer to fields + 'Muon_eta', 'Tau_eta' and 'Tau_phi'' + -C, --cut TEXT Filter the data to be plotted by a cut condition. For ROOT files, variables must be referenced by their branch name within the diff --git a/histoprint/cli.py b/histoprint/cli.py index a5e7e39..2895f40 100644 --- a/histoprint/cli.py +++ b/histoprint/cli.py @@ -75,6 +75,14 @@ "of array-like branches, e.g. use 'tree/branch[:,2]' to histogram the 3rd " "elements of a vector-like branch.", ) +@click.option( + "--field-prefix", + "field_prefix", + type=str, + multiple=False, + help="String to prepend to all values indicated with --field option, " + "ignores position where this and --field options are specified.", +) @click.option( "-C", "--cut", @@ -109,6 +117,8 @@ def histoprint(infile, **kwargs): INFILE can be '-', in which case the data is read from STDIN. """ + if (prefix := kwargs.pop("field_prefix", None)) is not None: + kwargs["fields"] = map(lambda s: prefix + s, kwargs["fields"]) # Read file into buffer for use by implementations try: diff --git a/tests/data/2D-prefixable.csv b/tests/data/2D-prefixable.csv new file mode 100644 index 0000000..dae7a37 --- /dev/null +++ b/tests/data/2D-prefixable.csv @@ -0,0 +1,10 @@ +Txx,Txy +1,4 +2,5 +2,5 +3,6 +3,nan +3,6 +4,7 +4,7 +5,8 diff --git a/tests/test_basic.py b/tests/test_basic.py index 4828212..9ce8095 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -2,9 +2,11 @@ import numpy as np import pytest +from click.testing import CliRunner from uhi.numpy_plottable import ensure_plottable_histogram import histoprint as hp +from histoprint.cli import histoprint as cli def test_hist(): @@ -181,3 +183,39 @@ def test_rich_histogram(): tab.add_row(hist, Align.center(hist), Align.right(hist)) rich.print(tab) + + +def test_cli_opt_field_prefix(): + """Test equivalence of --field-prefix cli option to explicit prepend on --field.""" + + runner = CliRunner() + + res = runner.invoke( + cli, + [ + "-s", + "tests/data/2D-prefixable.csv", + "-f", + "Txx", + "-f", + "Txy", + ], + ) + res_prefixed = runner.invoke( + cli, + [ + "-s", + "tests/data/2D-prefixable.csv", + "-f", + "x", + "-f", + "y", + "--field-prefix", + "Tx", + ], + ) + + # assert succesful equivalent output + assert res.exit_code == 0 + assert res_prefixed.exit_code == 0 + assert res.output == res_prefixed.output