Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf515_hfJSON.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Code HistFactory Models in JSON.
5##
6## With the HS3 standard, it is possible to code RooFit-Models of any kind as JSON files.
7## In this tutorial, you can see how to code up a (simple) HistFactory-based model in JSON and import it into a RooWorkspace.
8##
9## \macro_code
10##
11## \date November 2021
12## \author Carsten Burgard
13
14import ROOT
15
16# start by creating an empty workspace
17ws = ROOT.RooWorkspace("workspace")
18
19# the RooJSONFactoryWSTool is responsible for importing and exporting things to and from your workspace
20tool = ROOT.RooJSONFactoryWSTool(ws)
21
22# use it to import the information from your JSON file
23tool.importJSON(ROOT.gROOT.GetTutorialDir().Data() + "/roofit/rf515_hfJSON.json")
24ws.Print()
25
26# now, you can easily use your workspace to run your fit (as you usually would)
27# the model config is named after your pdf, i.e. <the pdf name>_modelConfig
28model = ws["ModelConfig"]
29pdf = model.GetPdf()
30
31# we are fitting a clone of the model now, such that we are not double-fitting
32# the model in the closure check
33result = pdf.cloneTree().fitTo(ws["observed"], Save=True, GlobalObservables=model.GetGlobalObservables(), PrintLevel=-1)
34result.Print()
35
36# in the end, you can again write to json
37# the result will be not completely identical to the JSON file you used as an input, but it will work just the same
38tool.exportJSON("myWorkspace.json")
39
40# You can again import it if you want and check for closure
41ws2 = ROOT.RooWorkspace("workspace")
42tool2 = ROOT.RooJSONFactoryWSTool(ws2)
43tool2.importJSON("myWorkspace.json")
44model2 = ws2["main_modelConfig"]
45result = model.GetPdf().fitTo(ws2["observed"], Save=True, GlobalObservables=model.GetGlobalObservables(), PrintLevel=-1)
46result.Print()