Skip to content

Instantly share code, notes, and snippets.

@Moelf
Last active April 7, 2025 06:52
Show Gist options
  • Save Moelf/fa5c31421d3f01e01c4578632205ca18 to your computer and use it in GitHub Desktop.
Save Moelf/fa5c31421d3f01e01c4578632205ca18 to your computer and use it in GitHub Desktop.
RooFit, ExtendPdf of GenericPdf, and `SUM::` of pdfs
obs = [2.0, 4.0]
bincenters = [1.5, 2.5]
p1 = 2.0
pdf(x) = x
pdf_int = 0.5 * (3^2 - 1^2)
per_bin_term = -sum(@. obs * log(pdf(bincenters) / pdf_int))
extend_term = -(sum(obs)*log(p1) - p1)
println(per_bin_term + extend_term)
# 1.6827899396467232
import ROOT
from ROOT import RooWorkspace, RooArgList, RooFit
hist_min = ROOT.TH1F("hist_min", "hist_min", 2, 1, 3)
for i,c in enumerate([2.0, 4.0]):
hist_min.SetBinContent(i+1, c)
ws = RooWorkspace("w", True)
# x-axis spans [1,3]
ws.factory("x[1,3]")
# p1's default value is 2.0
ws.factory("p1[2.0, 0.0, 10000.0]")
ws.factory("GenericPdf::bkg('x', x)")
ws.factory("ExtendPdf::bkg_ext(bkg, p1)")
x = ws.var("x")
model = ws.pdf("bkg_ext")
dh = ROOT.RooDataHist("dh", "dh", RooArgList(x), hist_min)
nll = model.createNLL(dh)
nll.getVal()
# 1.6827899396467232
obs = [2.0, 4.0];
bincenters = [1.5, 2.5];
p1 = 2.0;
p2 = 0.5;
overall_norm = p1 + p2
bkg_pdf(x) = x;
sig_pdf(x) = x^2;
bkg_pdf_int = (3^2 - 1^2) / 2;
sig_pdf_int = (3^3 - 1^3) / 3;
bkg_frac = p1 / overall_norm
sig_frac = p2 / overall_norm
per_bin_term = -sum(@. obs *
log(
bkg_frac * bkg_pdf(bincenters) / bkg_pdf_int +
sig_frac * sig_pdf(bincenters) / sig_pdf_int
)
);
extend_term = -(sum(obs)*log(overall_norm) - overall_norm);
println(per_bin_term + extend_term)
# 0.8497340452247997
import ROOT
from ROOT import RooWorkspace, RooArgList, RooFit
# Flat PDF: value is 0.5 so it integrates to 1 over [1, 3]
hist_min = ROOT.TH1F("hist_min", "hist_min", 2, 1, 3)
for i,c in enumerate([2.0, 4.0]):
hist_min.SetBinContent(i+1, c)
ws = RooWorkspace("w", True)
ws.factory("x[1,3]")
ws.factory("p1[2.0, 0.0, 10000.0]")
ws.factory("p2[0.5, 0.0, 10000.0]")
ws.factory("GenericPdf::bkg('x', x)")
ws.factory("GenericPdf::sig('x^2', x)")
ws.factory("SUM::model(p1*bkg, p2*sig)")
x = ws.var("x")
model = ws.pdf("model")
dh = ROOT.RooDataHist("dh", "dh", RooArgList(x), hist_min)
nll = model.createNLL(dh)
print(nll.getVal())
# 0.8497340452247997
@Moelf
Copy link
Author

Moelf commented Apr 4, 2025

ExtendPdf

For main.py and main.jl (just the ExtendPdf)

if you change the observed bincounts from [2.0, 4.0] to [4.0, 8.0], the NLL becomes 1.3655798792934464

If on top of that, you also change p0 from 2.0 to 4.0, you get NLL -4.952186287425897

image

SUM:: pdfs:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment