[RF] Introduce a first-class `RooBinIndex` for reusable bin-index computation
维护者通常 1 天内回复
@guitargeek 已经在做这个了。
开始于 2026年9月8日。
评估
这个 Issue 还没有评估数据。
描述
Explain what you would like to see improved and how.
Related PRs: #23298, #15543, #11171, #12955
Motivation
While working on #23298, the question came up whether the computation of a histogram bin index should be represented by a standalone RooFit object, tentatively called RooBinIndex.
The immediate motivation is RooBinWidthFunction: after #23298 it depends directly on its observables, but consequently needs to determine the current multi-dimensional bin itself in both scalar and batch evaluation. This logic is not specific to bin widths, however. Similar coordinate-to-bin-index computations already exist in several RooFit and HistFactory classes.
A first-class RooBinIndex could provide a single graph node representing
observables -> flattened bin index
and allow multiple histogram-like objects with the same binning to share that result.
This issue is intended to collect the relevant use cases and design questions. It could either serve as the starting point for a refactor now, or remain as follow-up work after merging #23298 independently.
Existing implementations / potential consumers
The following classes currently contain or rely on closely related bin-index logic:
RooBinWidthFunction
The implementation in #23298 computes the current bin directly from its observable list, including multi-dimensional index flattening and separate scalar/batch implementations.
This is the immediate use case that prompted the discussion.
RooHistFunc
RooHistFunc currently has
Int_t getBin() const;
std::vector<Int_t> getBins(RooFit::EvalContext &ctx) const;
which effectively implement a private version of the proposed abstraction.
Its evaluation code additionally has separate scalar, 1D batch, and ND batch paths for mapping function observables onto the underlying histogram bins.
RooHistPdf
RooHistPdf performs essentially the same mapping from PDF observables to the bins of its RooDataHist.
For zero-order histogram evaluation this could naturally become
RooBinIndex -> weight[index]
Interpolation would still require the actual coordinates and neighbouring bin geometry, so RooBinIndex would only represent the central-bin lookup.
RooParamHistFunc
This is perhaps the simplest potential consumer. Its scalar evaluation currently amounts to
idx = dataHist.getIndex(observables);
return parameters[idx];
while code generation independently asks RooDataHist to generate the corresponding tree-index expression.
This could naturally become a parameter lookup driven by a RooBinIndex.
HistFactory ParamHistFunc
The HistFactory implementation contains several versions of the same operation:
- scalar evaluation through
RooDataHist::getIndex(); - vectorized evaluation using
RooAbsBinning::binNumbers()and explicit index multipliers; - code generation through
RooDataHist::calculateTreeIndexForCodeSquash().
This is a strong candidate for consolidation.
PiecewiseInterpolation
#15543 already introduced an optimization based on the fact that the nominal and variation RooHistFuncs in HistFactory use the same bin structure.
The generated code computes the bin index only once and then uses it to address the nominal, low, and high arrays.
A first-class RooBinIndex could make this sharing explicit in the RooFit computation graph instead of rediscovering it in code generation.
Conceptually, a HistFactory channel could then look like
+-> nominal histogram
+-> systematic variations
observables -> BinIndex -+-> MC-stat parameters
+-> bin-width correction
+-> other bin-wise quantities
RooDataHist
RooDataHist::calcTreeIndex() is currently the main general implementation of multi-dimensional bin-index calculation, and calculateTreeIndexForCodeSquash() implements the corresponding generated expression.
It probably should share an implementation with RooBinIndex, but RooDataHist itself should not necessarily depend on a graph node: it also needs to calculate indices for arbitrary coordinate collections outside a computation graph.
A possible layering would therefore be
common bin-index implementation
/ \
RooBinIndex RooDataHist
graph node storage API
Secondary candidates
RooBinSamplingPdf also has duplicated scalar/batch bin lookup: scalar evaluation uses getBin(), while batch evaluation independently determines the bin from the boundaries.
RooBinningCategory is conceptually very close to a one-dimensional RooBinIndex: it maps a real l-value through a named binning onto a category whose index corresponds to the bin number. It probably should not disappear, since the category labels are useful, but the two implementations might eventually share their underlying bin-lookup machinery.
Other uses of binNumber() such as in RooFFTConvPdf, RooPlot, or RooMomentMorphFuncND appear to be local geometry/cache algorithms rather than reusable graph-level bin-index computations, so they probably do not need to participate in this refactor.
Design questions
A reusable RooBinIndex turns out to require a few architectural decisions.
1. Which binning defines the index?
For RooBinWidthFunction after #23298, the live observables and their current default binnings are authoritative.
For RooHistFunc and RooHistPdf, however, the values can come from one set of observables while the relevant bin geometry comes from the corresponding histogram observables / RooDataHist.
Therefore an API that only stores
RooBinIndex(name, observables);
and always uses observables[i].getBinning() may be too restrictive.
The value source and the binning source may need to be representable separately.
2. What is the multi-dimensional flattening convention?
RooDataHist and HistFactory do not universally use the same ordering.
RooDataHist indexing makes the last listed dimension the fastest-running one.
The legacy HistFactory ParamHistFunc, on the other hand, stores parameters in TH1 ordering, where the first/x dimension is fastest, and contains explicit conversion logic between the two conventions.
A standalone RooBinIndex should have a clearly defined canonical ordering. Consumers with legacy storage conventions could then transform their arrays explicitly if necessary.
3. How are categories handled?
For categories, the histogram-bin number is the ordinal state number, which is not necessarily identical to the category's numeric state index.
Scalar evaluation can use RooAbsCategoryLValue::getBin(), but batch evaluation sees the numeric category values and therefore needs a proper value-to-ordinal mapping.
This needs to be handled centrally if RooBinIndex is intended to support mixed real/category histograms.
4. Dynamic and named binnings
The implementation should define how it behaves when
- a binning is replaced;
- the range or number of bins changes;
- a named rather than default binning is used;
- a parameterized/transformed binning changes dynamically.
This is already relevant in #23298 because RooBinWidthFunction needs to detect binning changes that do not necessarily propagate through the usual dirty-state mechanism.
5. Evaluation backends
If introduced as a RooFit graph node, RooBinIndex should ideally have consistent support for
- scalar evaluation;
EvalContextbatch evaluation;- code generation / code squashing.
Otherwise it risks simply moving the current duplication to a different layer.
Possible scope
There seem to be two reasonable levels of implementation.
Minimal extraction
Introduce RooBinIndex primarily for #23298 and the immediately duplicated helpers:
- new
RooBinIndex; - use it in
RooBinWidthFunction; - remove or replace
RooHistFunc::getBin()/getBins()where appropriate.
This would be a relatively small follow-up and would not require restructuring histogram evaluation more generally.
Canonical bin-index node
Make RooBinIndex the common graph-level representation of coordinate-to-bin mapping and adapt at least:
RooBinWidthFunction;RooHistFunc;RooHistPdf;RooParamHistFunc;- HistFactory
ParamHistFunc; PiecewiseInterpolation;- the relevant
RooDataHisthelper implementation.
A more exhaustive cleanup could additionally include RooBinSamplingPdf and share implementation with RooBinningCategory.
This larger version would also make it possible for HistFactory models to explicitly share one bin-index node between several bin-wise functions.
Relation to #23298
I don't think this necessarily needs to block #23298.
The RooBinWidthFunction changes in that PR make the duplicated bin-index computation particularly visible, but a genuinely reusable RooBinIndex requires decisions about binning ownership, index ordering, categories, dynamic binnings and evaluation backends that go beyond the scope of the original PR.
One reasonable path would therefore be to merge #23298 as-is and use this issue as the basis for a later refactor.
Alternatively, if there is agreement on the desired RooBinIndex abstraction, #23298 could serve as the first consumer of it.
ROOT version
latest master
Installation method
built from source
Operating system
linux
Additional context
One of the core motivations for this is to make the computational graph more aligned with the client/server proxy links which form the basis of HS3 export.
- 主要语言
- C++
- 星标
- 3.3k
- 派生
- 1.6k
- 平均合并
- 2 天 17 小时
- 30 天内合并 PR
- 188
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
root-project/root 的其他 Issue
-
thisroot.sh: wrong ROOTSYS in zsh when the shell's command line has a newline可能已有人在做 @dpiparo 于 5 天前认领。 未关闭bug in:Build System
难度 2/5 1-3 小时 新手友好度 86/100
root-project/root#23746 · 已指派 1 人 ·
维护者通常 1 天内回复
-
[tmva][sofie] Inputs of unsupported type (e.g. float16) give code that does not compile instead of an error可能已有人在做 @lmoneta 于 15 天前认领。 未关闭AI-Assisted bug in:SOFIE
难度 2/5 1-3 小时 新手友好度 65/100
root-project/root#23655 · 已指派 1 人 ·
维护者通常 1 天内回复
-
[RDataFrame] Code that transforms `#var` alias to `R_rdf_sizeof_var` does not respect string literals可能已有人在做 @vepadulano 于 2 天前认领。 未关闭bug in:RDataFrame
难度 3/5 1-2 天 新手友好度 71/100
root-project/root#23790 · 已指派 1 人 ·
维护者通常 1 天内回复
-
TDataMember::GetOffset interfers to TDataMember::GetOffsetCint可能已有人在做 @pcanal 于 2 天前认领。 未关闭bug in:Core Libraries
难度 4/5 3-5 天 新手友好度 48/100
root-project/root#23777 · 已指派 1 人 ·
维护者通常 1 天内回复
-
`TChain::GetEntries` internal caching ineffective?可能已有人在做 @pcanal 于 8 天前认领。 未关闭improvement in:TTree
root-project/root#23738 · 6 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
查看 root-project/root 的全部 Issue
相似的 Issue
-
Remove source files from packaging for deb, zip, etc可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100
OpenSpace/OpenSpace#4353 · 1 条评论 ·
维护者通常 5 天内回复
-
难度 2/5 1-3 小时 新手友好度 86/100
justcallmekoko/ESP32Marauder#1656 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
-
macOS: a double-click on a notarized altairsim is refused, and the docs give only the offline case可能已有人在做 @anhaoXie 今天认领。 未关闭documentation
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
Decompiler: convertCharRef() in xml.y overflows its int4 accumulator on long character references可能已有人在做 @kaayzouee 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
NationalSecurityAgency/ghidra#10009 ·
维护者通常 2 天内回复