Monte Carlo reweighting toolkit with multiple backends (hep_ml, XGBoost, neural networks, folding, and binning).
Project description
mcreweight
mcreweight is a Python package for Monte Carlo event reweighting to match data distributions in multiplicity and kinematic variables. It provides multiple reweighting backends, including hep_ml-based gradient boosting, XGBoost, neural-network approaches, folding variants, and bin-based reweighting, with optional Optuna hyperparameter tuning and integrated plotting/validation utilities.
[!WARNING]
Binsreweighting is a useful low-dimensional baseline, but it becomes unstable quickly as the number of training variables grows. Prefer it for one or two dimensions, use extra care around three or four, and avoid relying on it as the main method in higher-dimensional problems.
Documentation
Project documentation is currently intended to be available at:
- CERN Pages:
http://mcreweight.docs.cern.ch - Read the Docs:
https://mcreweight.readthedocs.io/once the project is imported and built there
Setup
Run in a lb-conda environment, as
lb-conda mcreweight
Installation
If you don't run in a lb-conda environment, consider installing the python package from PyPI or cloning it from GitLab.
From PyPI
pip install mcreweight
From GitLab
Requires pixi.
git clone https://gitlab.cern.ch/lhcb-dpa/tools/mcreweight.git
cd mcreweight
To run the CLI tools you can prefix them with pixi run, i.e.
pixi run run-reweight --help
pixi run apply-weights --help
To run the verification checks used in CI:
pixi run -e lint quality
pixi run test
pixi run -e docs build-docs
To auto-format the code before rerunning the checks:
pixi run -e lint black .
pixi run -e lint quality
This repository currently uses these Pixi environments:
default: package runtime and CLI usagelint:blackandrufftest:pytestdocs: Sphinx documentation build
Useful Pixi commands:
pixi run -e lint ruff check .
pixi run -e lint black --check .
pixi run -e docs sphinx-build -b html -n -W --keep-going docs docs/_build/html
pixi run -e test pytest tests/test_cli.py::test_run_reweight -v
pixi run -e test pytest tests/test_cli.py::test_apply_weights -v
The pixi.lock file pins all dependencies for reproducibility. To update them, run pixi update and commit the updated lock file.
Usage
Both CLIs support two usage modes:
- Config-driven mode (recommended): pass
--config <file.yaml> - Direct CLI mode: pass all options on the command line
Use --dry-run to validate the merged configuration without running.
Both CLIs treat weightsdir and plotdir as root directories:
run-reweightwrites to<weightsdir>/<sample>/and<plotdir>/<sample>/apply-weightsreads trained artifacts from<weightsdir>/<training-sample>/apply-weightswrites normalized weights to<weightsdir>/<application-sample>/and plots to<plotdir>/<application-sample>/
To run reweighting do it via configuration file:
run-reweight --config <path_to_config.yaml>
or passing all options to the command line:
run-reweight --path-data <path_to_data.root> \
--path-mc <path_to_mc.root> \
--training-vars <variable_list> \
--monitoring-vars <monitoring_variable_list> \
--sample <sample> \
--n_trials <optuna_tests> \
--test_size <test_sample_size> \
--weightsdir <weights_directory>
To apply saved weights to an MC sample with configuration file:
apply-weights --config <path_to_config.yaml>
or passing all options to the command line:
apply-weights --path-mc <path_to_mc.root> \
--vars <variable_list> \
--training-sample <training_sample> \
--application-sample <application_sample> \
--method <method_for_reweighter> \
--monitoring-vars <monitoring_variable_list> \
--output-path <output_file.root> \
--weightsdir <weights_directory>
Options
For the reweighting (run-reweight):
General:
--config: YAML configuration file--dry-run: Validate config and print effective settings only--verbosity: Logging level (1,2,3,4), default1
Inputs:
--path-mc: MC ROOT file path(s)--tree-mc: MC tree name (default:DecayTree)--mcweights-name: MC input weight branch--mc-label: MC sample label (default:MC)--path-data: Data ROOT file path(s)--tree-data: Data tree name (default:DecayTree)--sweights-name: Data sWeights branch (default:sweight_sig)--data-label: Data sample label (default:Data)--path-xlabels: YAML mapping for axis labels (default:utils/default_xlabels.yaml)
Variables:
--training-vars: Reweighting training variables--monitoring-vars: Variables used for monitoring plots
Reweighting:
--sample: Sample tag (default:bd_jpsikst_ee)--methods: Methods list (defaults from YAML or internal defaults)--transform: Feature transform (quantile,yeo-johnson,signed-log,scaler)--n_trials: Optuna trials (default:10)--test_size: Test split fraction (default:0.3)--n_folds: Folding count (default:10)--n_bins: Bin count forBinsmethod (default:10)--n_neighs: Neighbors forBinsmethod (default:3)--reweight-validation-fraction: Validation split used by iterative ONNX reweighters--reweight-early-stopping-rounds: Early-stopping patience for iterative ONNX reweighters--reweight-metric-every: Evaluate ONNX validation metric every N stages--folding-aggregation: Folding aggregation (weighted_geometric,geometric,median)--shap: Enable SHAP computation
Output:
--weightsdir: Root directory for training artifacts;<sample>/is created automatically. If omitted,MCREWEIGHTS_DATA_ROOTis used--plotdir: Root directory for plots;<sample>/is created automatically (default:plots)
Additional options can be found by running:
run-reweight --help
For the application of the weights (apply-weights):
General:
--config: YAML configuration file--dry-run: Validate config and print effective settings only--verbosity: Logging level (1,2,3,4), default1
Inputs:
--path-mc: MC file path(s)--tree-mc: MC tree name (default:DecayTree)--mcweights-name: Existing MC input weight branch--path-data: Optional data file path(s), for comparison plots--tree-data: Data tree name (default:DecayTree)--sweights-name: Data sWeights branch (default:sweight_sig)--path-xlabels: YAML mapping for axis labels
Variables:
--vars: Application variables--training-vars: Variables used when training the saved model--monitoring-vars: Variables used for monitoring plots
Reweighting:
--method: Method (GB,XGB,NN,Folding,XGBFolding,NNFolding,Bins), defaultXGB--training-sample: Training sample tag (default:bd_jpsikst_ee)--application-sample: Application sample tag (default:bd_jpsikst_ee)--weightsdir: Root directory for trained artifacts;<training-sample>/is read and<application-sample>/is written automatically. If omitted,MCREWEIGHTS_DATA_ROOTis used--plotdir: Root directory for plots;<application-sample>/is created automatically (default:plots)
Output:
--output-path: Output ROOT file path--output-ntuple: Output ntuple type (default:TTree)--output-tree: Output tree name (default:DecayTree)--weights-name: Name of produced weights branch (default:weights)
Additional options can be found by running:
apply-weights --help
Example
Reweighting:
pixi run run-reweight \
--config tests_run/run_reweighting_config.yaml \
--verbosity 2
Application of the weights:
pixi run apply-weights \
--config tests_run/apply_weights_config.yaml \
--method XGB \
--output-path test_applied_weights.root
Documentation build:
pixi run -e docs build-docs
The generated HTML documentation is written to docs/_build/html/.
Contact
For questions, please contact the repository maintainer.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcreweight-1.0.3.tar.gz.
File metadata
- Download URL: mcreweight-1.0.3.tar.gz
- Upload date:
- Size: 37.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c128685be0952c226c5250964dbfb1a7e4bc7b3528c7d1fd01df5793ede1250
|
|
| MD5 |
c86eb1581fe7a8d55a7287dd98870330
|
|
| BLAKE2b-256 |
b39b1777bb4662dc8b122827108c5a80bc7c20ee59e16de48be93b527bbcade0
|
File details
Details for the file mcreweight-1.0.3-py3-none-any.whl.
File metadata
- Download URL: mcreweight-1.0.3-py3-none-any.whl
- Upload date:
- Size: 81.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94ec5a5434a52ba5e3568374e1391af0508236b4d40d372cab8acda4bf22b14e
|
|
| MD5 |
df0ffe69c1cc2d0d6f839ef4b44f2f0a
|
|
| BLAKE2b-256 |
7282a08db8a5c736ce6134d7d1caa99d16444d25583ec3b1c3addcea4a7c19f4
|