AsymBinMerger is a Python plugin/class designed to process 2D ROOT histograms (TH2F) by merging input TH2F bins into "superbins" such that no resulting output superbin has a fractional statistical uncertainty above a specified threshold, while maximizing the number of bins retained.
This is particularly useful for analyses that require high-statistics binning in 2D phase space where this process is non-trivial.
- Merges bins based on fractional statistical uncertainty (
1/√counts) - Respects 2D histogram topology using adjacent neighbor merging
- "x-biased" tie-breaking ensures deterministic, reproducible results
- Outputs:
- Superbin-to-bin map
- Diagnostic plot
ROOT.TH2Fhistogramfloat: maximum fractional statistical uncertainty (max_stat_uncert)str(optional): output directory for files and plots, default is "merged_bins/"
- Initialize each bin as its own superbin.
- Identify bad bins: superbins whose uncertainty exceeds the threshold.
- Choose the "worst" bad superbin using:
- Highest fractional uncertainty
- Superbin centroid furthest from (0,0)
- Larger
xcoordinate, then largeryif tied
- Merge the bad superbin into a neighboring superbin:
- Select the neighbor with:
- Highest fractional uncertainty
- Furthest centroid from origin (0,0), favoring larger
x, theny
- Select the neighbor with:
- Update: Recalculate list of "bad" superbins and repeat from 3 until no bad bins remain.
- Neighbors are defined as superbins that have at least one constituent that is adjacent (up/down/left/right) to at least one constituent of the candidate superbin.
- Tie-breaking rules ensure deterministic x-biased merging.
from asym_bin_merger import AsymBinMerger
import ROOT
# Open ROOT file and retrieve histogram
r_file = ROOT.TFile.Open("path/to/your/hist.root", "READ")
hist = r_file.Get("my_hist") # Ensure it's a TH2F
# Merge with max stat uncertainty of 12%
bin_merger = AsymBinMerger(hist, 0.12, "my_hist")
# Retrieve outputs
bin_merger.write_output()
bin_merger.merged_hist_to_image()Stored in the output_dir (e.g. "merged_hist"):
-
bin_map.txt: A list of lists with format:[ [(i1, j1), (i2, j2), ...], # Superbin 0 [(k1, l1), (k2, l2), ...], # Superbin 1 ... ]
-
merged_hist.png: An image containing the merged histogram with statistical uncertainty labeled for each superbin
This package uses uv for environment and dependency management.
Note:
ROOTmust be installed separately on your system (e.g., via conda or prebuilt binaries). This tool requires a working Python-compatible ROOT environment (import ROOTmust work).
numpyos(standard library)ROOT(not installable viauv)matplotlib(for merged_hist_to_image)
# Clone the repository
git clone https://github.com/your-org/asym-bin-merger.git
cd asym-bin-merger
# Create and enter a uv virtual environment
uv venv
source .venv/bin/activate
# Install Python dependencies
uv syncInitializes the merger and performs bin merging.
Returns the post-merged histogram.
Returns the bin grouping (superbin) structure.
Generates bin map and saves it in the output directory.
Generates plot and saves it in the output directory.
Q: What happens if two bins have equal uncertainty and distance from the origin? A: The algorithm prefers the bin with larger x, then larger y coordinate to ensure deterministic merging. This algorithm is therefore "x-biased".
Q: Can I use this with histograms that contain empty bins? A: Yes. Empty bins (with zero entries) are treated as having infinite uncertainty and are prioritized for early merging.
MIT License
Developed by Ethan Cannaert, Mayuri Kawale, Elias Mettner, and Ashling Quinn for use in high-statistics ROOT-based 2D analyses.