Skip to content

Commit

Permalink
feat: Add wrapper for ROOT hadd (#2785)
Browse files Browse the repository at this point in the history
<!-- Ensure that the PR title follows conventional commit style (<type>:
<description>)-->
<!-- Possible types are here:
https://github.com/commitizen/conventional-commit-types/blob/master/index.json
-->

<!-- Add a description of your PR here-->
Adds a wrapper for the ``hadd`` utility in the ROOT software package
used primarily in High Energy Physics. Since this is the first physics
wrapper, the top-level category ``phys`` is also added here.

### QC
<!-- Make sure that you can tick the boxes below. -->

* [x] I confirm that:

For all wrappers added by this PR, 

* there is a test case which covers any introduced changes,
* `input:` and `output:` file paths in the resulting rule can be changed
arbitrarily,
* either the wrapper can only use a single core, or the example rule
contains a `threads: x` statement with `x` being a reasonable default,
* rule names in the test case are in
[snake_case](https://en.wikipedia.org/wiki/Snake_case) and somehow tell
what the rule is about or match the tools purpose or name (e.g.,
`map_reads` for a step that maps reads),
* all `environment.yaml` specifications follow [the respective best
practices](https://stackoverflow.com/a/64594513/2352071),
* the `environment.yaml` pinning has been updated by running
`snakedeploy pin-conda-envs environment.yaml` on a linux machine,
* wherever possible, command line arguments are inferred and set
automatically (e.g. based on file extensions in `input:` or `output:`),
* all fields of the example rules in the `Snakefile`s and their entries
are explained via comments (`input:`/`output:`/`params:` etc.),
* `stderr` and/or `stdout` are logged correctly (`log:`), depending on
the wrapped tool,
* temporary files are either written to a unique hidden folder in the
working directory, or (better) stored where the Python function
`tempfile.gettempdir()` points to (see
[here](https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir);
this also means that using any Python `tempfile` default behavior
works),
* the `meta.yaml` contains a link to the documentation of the respective
tool or command,
* `Snakefile`s pass the linting (`snakemake --lint`),
* `Snakefile`s are formatted with
[snakefmt](https://github.com/snakemake/snakefmt),
* Python wrapper scripts are formatted with
[black](https://black.readthedocs.io).
* Conda environments use a minimal amount of channels, in recommended
ordering. E.g. for bioconda, use (conda-forge, bioconda, nodefaults, as
conda-forge should have highest priority and defaults channels are
usually not needed because most packages are in conda-forge nowadays).

---------

Co-authored-by: Filipe G. Vieira <1151762+fgvieira@users.noreply.github.com>
  • Loading branch information
GoodingJamie and fgvieira committed Apr 4, 2024
1 parent 491d5b6 commit 3e82e9c
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 0 deletions.
274 changes: 274 additions & 0 deletions phys/root/hadd/environment.linux-64.pin.txt

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions phys/root/hadd/environment.yaml
@@ -0,0 +1,4 @@
channels:
- conda-forge
dependencies:
- ROOT >6.24.0
12 changes: 12 additions & 0 deletions phys/root/hadd/meta.yaml
@@ -0,0 +1,12 @@
name: "hadd"
description: Combine ntuples using the ROOT hadd command line tool
url: https://root.cern/doc/master/hadd_8cxx.html
authors:
- Jamie Gooding
input:
- ROOT files to be combined.
output:
- >
single ROOT file containing contents of input files.
params:
- extra: extra parameters to `hadd` (see ROOT documentation, e.g. `-f[0-9]` for compression levels)
12 changes: 12 additions & 0 deletions phys/root/hadd/test/Snakefile
@@ -0,0 +1,12 @@
rule hadd:
input:
expand("ntuple{n}.root", n=range(3)),
log:
"logs/hadd/hadd.log",
params:
extra="-f5",
threads: 2
output:
"ntuple.root",
wrapper:
"master/phys/root/hadd"
Binary file added phys/root/hadd/test/ntuple0.root
Binary file not shown.
Binary file added phys/root/hadd/test/ntuple1.root
Binary file not shown.
Binary file added phys/root/hadd/test/ntuple2.root
Binary file not shown.
12 changes: 12 additions & 0 deletions phys/root/hadd/wrapper.py
@@ -0,0 +1,12 @@
__author__ = "Jamie Gooding"
__copyright__ = "Copyright 2024, Jamie Gooding"
__email__ = "jamie.gooding@cern.ch"
__license__ = "MIT"


from snakemake.shell import shell

extra = snakemake.params.get("extra", "")
log = snakemake.log_fmt_shell()

shell("hadd -j {snakemake.threads} {extra} {snakemake.output} {snakemake.input} {log}")
7 changes: 7 additions & 0 deletions test.py
Expand Up @@ -6439,3 +6439,10 @@ def test_tmb_pytmb():
"bio/tmb/pytmb",
["snakemake", "--cores", "1", "--use-conda", "-F"],
)

@skip_if_not_modified
def test_root_hadd():
run(
"phys/root/hadd",
["snakemake", "--cores", "2", "--use-conda", "-F"],
)

0 comments on commit 3e82e9c

Please sign in to comment.