Logo ROOT  
Reference Guide
No Matches
rf515_hfJSON.py File Reference

Detailed Description

View in nbviewer Open in SWAN
Code HistFactory Models in JSON.

With the HS3 standard, it is possible to code RooFit-Models of any kind as JSON files. In this tutorial, you can see how to code up a (simple) HistFactory-based model in JSON and import it into a RooWorkspace.

import ROOT
# start by creating an empty workspace
ws = ROOT.RooWorkspace("workspace")
# the RooJSONFactoryWSTool is responsible for importing and exporting things to and from your workspace
tool = ROOT.RooJSONFactoryWSTool(ws)
# use it to import the information from your JSON file
tool.importJSON(ROOT.gROOT.GetTutorialDir().Data() + "/roofit/rf515_hfJSON.json")
# now, you can easily use your workspace to run your fit (as you usually would)
# the model config is named after your pdf, i.e. <the pdf name>_modelConfig
model = ws["ModelConfig"]
# for resetting the parameters after the fit
params = model.GetPdf().getParameters(ws["observed"])
params_initial = params.snapshot()
# we are fitting a clone of the model now,
result = model.fitTo(ws["observed"], ROOT.RooFit.Save(), ROOT.RooFit.PrintLevel(-1))
# reset parameters, such that we are not double-fitting the model in the
# closure check.
# in the end, you can again write to json
# the result will be not completely identical to the JSON file you used as an input, but it will work just the same
# You can again import it if you want and check for closure
ws_2 = ROOT.RooWorkspace("workspace")
tool_2 = ROOT.RooJSONFactoryWSTool(ws_2)
model_2 = ws_2["ModelConfig"]
result = model_2.fitTo(ws_2["observed"], ROOT.RooFit.Save(), ROOT.RooFit.PrintLevel(-1))
November 2021
Carsten Burgard

Definition in file rf515_hfJSON.py.