Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Chi2 between pdf and RooDataHist with empty bins

Aperta
#21,696 3 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@guitargeek ci sta già lavorando.

Dal 26/3/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

improvement in:RooFit
Explain what you would like to see improved and how.

To calculate chi2 between a pdf and a RooDataHist, we can use RooAbsReal::createChi2(). However, it fails if the RooDataHist has bins with zero error (i.e. empty bins). The expected behavior should be that empty bins are skipped, just like when computing the chi2 between a TF1 and a TH1.

Reproducer:

from array import array
import numpy as np

import ROOT

bin_edge = [
    0., 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5,
    0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.,
]
bin_content = [
    0, 0, 0, 0, 0, 1, 3, 16, 17, 14, 18, 18, 7, 5, 1, 0, 0, 0, 0, 0
]

h = ROOT.TH1D(
    "h_gauss_w", "h_gauss_w",
    len(bin_edge) - 1,
    array("d", bin_edge),
)
h.Sumw2()
rng = np.random.default_rng(1)
for i, count in enumerate(bin_content):
    center = 0.5 * (bin_edge[i] + bin_edge[i + 1])
    for _ in range(int(count)):
        wt = float(rng.uniform(0.5, 2.0))
        h.Fill(center, wt)

f = ROOT.TF1("f_gauss", "gaus", 0.0, 1.0)
h.Fit(f, 'QS')

# prints 6.73
print(f"Chi2 from TH1::Chisquare = {h.Chisquare(f)}")

data = ROOT.RooDataHist("dh", "dh", ROOT.RooArgList(x), h)

params = f.GetParameters()
fitted_mu, fitted_sigma = params[1], params[2]

x   = ROOT.RooRealVar("X", "x", 0.0, 1.0)
x.setBins(len(bin_edge) - 1)
mu  = ROOT.RooRealVar("mu",    "mu",    fitted_mu,  0.0, 1.0)
sig = ROOT.RooRealVar("sigma", "sigma", fitted_sigma,  0.01, 0.5)
pdf = ROOT.RooGaussian("gauss", "gauss", x, mu, sig)

bin_width = 0.05
# make sure the normalization matches between the function and the pdf
norm_val = f.Integral(bin_edge[0], bin_edge[-1]) / bin_width
norm_var = ROOT.RooRealVar('norm', 'norm', norm_val)
extended_pdf = ROOT.RooAddPdf('pdf_ext', 'pdf_ext', ROOT.RooArgList(pdf), ROOT.RooArgList(norm_var))

chi2_obj = extended_pdf.createChi2(
    data,
    ROOT.RooFit.DataError(ROOT.RooAbsData.SumW2),
    ROOT.RooFit.Extended(True)
)

chi2_obj.getVal()

# prints 0
# and [#0] ERROR:Eval -- RooChi2Var::RooChi2Var(pdf_ext) INFINITY ERROR: bin 0 has zero error
print(f"Chi2 from RooAbsReal::createChi2 = {chi2_obj.getVal()}")
ROOT version

6.38.00

Installation method

conda

Operating system

Linux

Additional context

No response

Lingua principale
C++
Stelle
3.3k
Fork
1.6k
Merge medio
2g 16h
PR unite (30g)
194

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di root-project/root

Tutte le issue di root-project/root

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.