ROOT
master
Reference Guide
Loading...
Searching...
No Matches
rf515_hfJSON.py
2
## \ingroup tutorial_roofit_main
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
14
import
ROOT
15
16
# start by creating an empty workspace
17
ws =
ROOT.RooWorkspace
(
"workspace"
)
18
19
# the RooJSONFactoryWSTool is responsible for importing and exporting things to and from your workspace
20
tool =
ROOT.RooJSONFactoryWSTool
(ws)
21
22
# use it to import the information from your JSON file
23
tool.importJSON
(
ROOT.gROOT.GetTutorialDir
().Data() +
"/roofit/roofit/rf515_hfJSON.json"
)
24
ws.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
28
model = ws[
"ModelConfig"
]
29
30
# for resetting the parameters after the fit
31
params =
model.GetPdf
().getParameters(ws[
"observed"
])
32
ROOT.SetOwnership
(params,
True
)
33
params_initial =
params.snapshot
()
34
ROOT.SetOwnership
(params_initial,
True
)
35
36
# we are fitting a clone of the model now,
37
result =
model.fitTo
(ws[
"observed"
],
ROOT.RooFit.Save
(),
ROOT.RooFit.PrintLevel
(-1))
38
ROOT.SetOwnership
(result,
True
)
39
result.Print
()
40
# reset parameters, such that we are not double-fitting the model in the
41
# closure check.
42
params.assign
(params_initial)
43
44
# in the end, you can again write to json
45
# the result will be not completely identical to the JSON file you used as an input, but it will work just the same
46
tool.exportJSON
(
"myWorkspace.json"
)
47
48
# You can again import it if you want and check for closure
49
ws_2 =
ROOT.RooWorkspace
(
"workspace"
)
50
tool_2 =
ROOT.RooJSONFactoryWSTool
(ws_2)
51
tool_2.importJSON
(
"myWorkspace.json"
)
52
ws_2.Print
()
53
model_2 = ws_2[
"ModelConfig"
]
54
result =
model_2.fitTo
(ws_2[
"observed"
],
ROOT.RooFit.Save
(),
ROOT.RooFit.PrintLevel
(-1))
55
ROOT.SetOwnership
(result,
True
)
56
result.Print
()
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
tutorials
roofit
roofit
rf515_hfJSON.py
ROOT master - Reference Guide Generated on Mon Feb 17 2025 15:03:49 (GVA Time) using Doxygen 1.10.0