Skip to content

Commit

Permalink
[RF] Pythonize RooAbsReal::setEvalErrorLogging mode with string to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
guitargeek committed Oct 13, 2024
1 parent 2957adf commit 77da806
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RooAbsReal(object):
\endcode
"""

__cpp_name__ = 'RooAbsReal'
__cpp_name__ = "RooAbsReal"

@cpp_signature(
"RooPlot* RooAbsReal::plotOn(RooPlot* frame,"
Expand Down Expand Up @@ -131,3 +131,23 @@ def getVal(self, normalizationSet=None):
if normalizationSet:
self._getVal_normSet = normalizationSet
return self._getVal(normalizationSet) if normalizationSet else self._getVal()

def setEvalErrorLoggingMode(m):

import ROOT

if isinstance(m, str):
# Hardcode enum integer values here, because enum lookups cause
# some memory fiasco at the end.
# TODO: fix this in cppyy / PyROOT!
lut = {"PrintErrors": 0, "CollectErrors": 1, "CountErrors": 2, "Ignore": 3}
if m not in lut:
raise ValueError(
"Unsupported value passed. The value has to be the name or enum value of an RooAbsReal ErrorLogging mode: {}".format(
lut
)
)

m = lut[m]

ROOT.RooAbsReal._setEvalErrorLoggingMode(m)
4 changes: 2 additions & 2 deletions tutorials/roofit/rf615_simulation_based_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ def learned_likelihood_ratio(x, mu):
frame1 = mu_var.frame(Title="NLL of SBI vs. Morphing", Range=(1.5, 2.5))
nll_gauss.plotOn(frame1, LineColor="g", ShiftToZero=True, Name="gauss")
nllr_learned.plotOn(frame1, LineColor="r", LineStyle="--", ShiftToZero=True, Name="learned")
ROOT.RooAbsReal.setEvalErrorLoggingMode(ROOT.RooAbsReal.Ignore) # Silence some warnings
ROOT.RooAbsReal.setEvalErrorLoggingMode("Ignore") # Silence some warnings
nll_morph.plotOn(frame1, LineColor="c", ShiftToZero=True, Name="morphed")
ROOT.RooAbsReal.setEvalErrorLoggingMode(ROOT.RooAbsReal.PrintErrors)
ROOT.RooAbsReal.setEvalErrorLoggingMode("PrintErrors")

# Plot the likelihood functions
frame2 = x_var.frame(Title="Learned vs analytical likelihhood function")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ def learned_likelihood_ratio(*args):
Range=(mu_observed[0] - 1, mu_observed[0] + 1),
)
nll_gauss.plotOn(frame1, ShiftToZero=True, LineColor="g", Name="gauss")
ROOT.RooAbsReal.setEvalErrorLoggingMode(ROOT.RooAbsReal.Ignore) # Silence some warnings
ROOT.RooAbsReal.setEvalErrorLoggingMode("Ignore") # Silence some warnings
nll_morph.plotOn(frame1, ShiftToZero=True, LineColor="c", Name="morph")
ROOT.RooAbsReal.setEvalErrorLoggingMode(ROOT.RooAbsReal.PrintErrors)
ROOT.RooAbsReal.setEvalErrorLoggingMode("PrintErrors")
nllr_learned.plotOn(frame1, LineColor="r", ShiftToZero=True, LineStyle="--", Name="learned")


Expand Down

0 comments on commit 77da806

Please sign in to comment.