Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooJSONFactoryWSTool.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Carsten D. Burgard, DESY/ATLAS, Dec 2021
5 *
6 * Copyright (c) 2022, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#ifndef RooFitHS3_RooJSONFactoryWSTool_h
14#define RooFitHS3_RooJSONFactoryWSTool_h
15
17
18#include <RooArgList.h>
19#include <RooArgSet.h>
20#include <RooGlobalFunc.h>
21#include <RooWorkspace.h>
22
23#include <map>
24#include <stdexcept>
25#include <set>
26#include <vector>
27
29
30namespace RooFit {
31namespace HS3 {
32struct Distribution;
33struct HistFactoryDist;
34struct ParameterPoint;
35struct ParameterValue;
36struct ToolExportState;
37struct ToolImportState;
38template <class Tag>
39struct Ref;
42 std::vector<DistributionRef> channelConstraints);
43} // namespace HS3
44} // namespace RooFit
45namespace RooStats {
46class ModelConfig;
47}
48class RooRealVar;
49class RooAbsCategory;
50class RooCategory;
51class RooSimultaneous;
52
54public:
55 /// Named objects are always stored in lists, as the HS3 standard requires. Kept for backwards compatibility.
56 static constexpr bool useListsInsteadOfDicts = true;
57
58 struct Config {
60 bool allowSanitizeNames = true;
61 /// Identify constant factors of HistFactory samples that are used like the inverse bin width of HistFactory
62 /// before ROOT 6.26 by their structure, and don't write them, since `histfactory_dist` implies them. Turn this
63 /// off if a workspace has such factors that are not meant as bin widths: they are then written as normfactors.
65 /// HS3 requires the components of a combined distribution to have distinct variates, e.g. "mass_SR" and
66 /// "mass_CR" instead of one "mass". If enabled, the observables of every combined data set are renamed on
67 /// export to "obs_<key>" (the labels of the key joined by "_"), or "obs_<key>_<observable>" if a component
68 /// has several observables; the components of the matching RooSimultaneous are written with the renamed
69 /// observables (functions depending on them get the suffix "_<key>"). This allows exporting workspaces whose
70 /// components share an observable. Off by default: without it, such workspaces can't be exported.
72 /// HS3 requires the terms of a likelihood and the components of a combined distribution to have distinct
73 /// variates, e.g. "mass_SR" and "mass_CR" for the observables of two channels instead of one "mass". By
74 /// default, the export fails otherwise. If true, the document is written anyway, with a warning for every
75 /// shared variate. It is not valid HS3 and can't be read back before the variates are renamed, e.g. by hand in
76 /// the JSON.
78 // Write array distributions (HS3 `array_dist`). If false, no `array_dist` is written: HistFactory constraints
79 // are named individually, and a RooProdPdf tagged as array distribution is written as `product_dist`.
80 bool exportArrayDist = true;
81 // How the constraint terms of HistFactory models enter the likelihood: listed in "aux_distributions", with
82 // the observed values as constant parameters (false), or listed in "distributions", each paired with a data set
83 // holding the observed values of its observables (true).
85 };
86
87 static Config &config();
88
89 static RooWorkspace sanitizeWS(const RooWorkspace &ws);
90 static RooWorkspace cleanWS(const RooWorkspace &ws, bool onlyModelConfig = false);
91
92 struct CombinedData {
93 std::string name;
94 std::vector<std::string> columns;
95 };
96
98
100
101 static std::string name(const RooFit::Detail::JSONNode &n);
102 static bool isValidName(const std::string &str);
103 static bool testValidName(const std::string &str, bool forcError);
104 static std::string sanitizeName(const std::string str);
106
108 static RooFit::Detail::JSONNode const *findNamedChild(RooFit::Detail::JSONNode const &node, std::string const &name);
109
110 static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax = -1);
111
112 /// The object `objname` from the workspace, imported from the document first if needed. Throws a
113 /// DependencyMissingError if neither has it.
114 template <class T>
115 T *request(const std::string &objname, const std::string &requestAuthor)
116 {
117 if (T *out = tryRequest<T>(objname)) {
118 return out;
119 }
120 throw DependencyMissingError(requestAuthor, objname, T::Class()->GetName());
121 }
122
123 /// Like request(), but returns nullptr if neither the workspace nor the document has the object.
124 template <class T>
125 T *tryRequest(const std::string &objname)
126 {
127 return requestImpl<T>(objname);
128 }
129
130 template <class T>
131 T *requestArg(const RooFit::Detail::JSONNode &node, const std::string &key)
132 {
133 std::string requestAuthor(RooJSONFactoryWSTool::name(node));
134 if (!node.has_child(key)) {
135 RooJSONFactoryWSTool::error("no \"" + key + "\" given in \"" + requestAuthor + "\"");
136 }
137 return request<T>(node[key].val(), requestAuthor);
138 }
139
140 template <class T, class Coll_t>
141 Coll_t requestCollection(const RooFit::Detail::JSONNode &node, const std::string &seqName)
142 {
143 std::string requestAuthor(RooJSONFactoryWSTool::name(node));
144 if (!node.has_child(seqName)) {
145 RooJSONFactoryWSTool::error("no \"" + seqName + "\" given in \"" + requestAuthor + "\"");
146 }
147 if (!node[seqName].is_seq()) {
148 RooJSONFactoryWSTool::error("\"" + seqName + "\" in \"" + requestAuthor + "\" is not a sequence");
149 }
150
151 Coll_t out;
152 for (const auto &elem : node[seqName].children()) {
153 out.add(*request<T>(elem.val(), requestAuthor));
154 }
155 return out;
156 }
157
158 template <class T>
159 RooArgSet requestArgSet(const RooFit::Detail::JSONNode &node, const std::string &seqName)
160 {
161 return requestCollection<T, RooArgSet>(node, seqName);
162 }
163
164 template <class T>
165 RooArgList requestArgList(const RooFit::Detail::JSONNode &node, const std::string &seqName)
166 {
167 return requestCollection<T, RooArgList>(node, seqName);
168 }
169
171
172 template <class Obj_t>
173 Obj_t &wsImport(Obj_t const &obj)
174 {
176 return *static_cast<Obj_t *>(_workspace.obj(obj.GetName()));
177 }
178
179 template <class Obj_t, typename... Args_t>
180 Obj_t &wsEmplace(RooStringView name, Args_t &&...args)
181 {
182 return wsImport(Obj_t(name.c_str(), name.c_str(), std::forward<Args_t>(args)...));
183 }
184
185 [[noreturn]] static void error(const char *s);
186 [[noreturn]] inline static void error(const std::string &s) { error(s.c_str()); }
187 static std::ostream &warning(const std::string &s);
188
189 static RooArgSet readAxes(const RooFit::Detail::JSONNode &node);
190 static std::unique_ptr<RooDataHist>
191 readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp, RooArgSet const &vars);
192
193 bool importJSON(std::string const &filename);
194 bool importJSON(std::istream &os);
195 bool exportJSON(std::string const &fileName);
196 bool exportJSON(std::ostream &os);
197
198 std::string exportJSONtoString();
199 bool importJSONfromString(const std::string &s);
200 void importJSONElement(const std::string &name, const std::string &jsonString);
202
203 void importFunction(const RooFit::Detail::JSONNode &p, bool importAllDependants);
204 void importFunction(const std::string &jsonString, bool importAllDependants);
205
206 static std::unique_ptr<RooFit::Detail::JSONTree> createNewJSONTree();
207
209
210 // error handling helpers
211 class DependencyMissingError : public std::runtime_error {
212 std::string _parent, _child, _class;
213
214 public:
215 DependencyMissingError(const std::string &p, const std::string &c, const std::string &classname)
216 : std::runtime_error("object '" + p + "' is missing dependency '" + c + "' of type '" + classname + "'"),
217 _parent(p),
218 _child(c),
220 {
221 }
222 const std::string &parent() const { return _parent; }
223 const std::string &child() const { return _child; }
224 const std::string &classname() const { return _class; }
225 };
226
227 template <typename... Keys_t>
229 {
230 return node.get("misc", "ROOT_internal", keys...);
231 }
232
233 static void exportAxis(RooFit::Detail::JSONNode &obsNode, RooRealVar const &var);
234 static void exportCategoricalAxis(RooFit::Detail::JSONNode &obsNode, RooCategory const &cat);
235
236 static void
237 exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
238
239 static void exportArray(std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
240
241 void queueExport(RooAbsArg const &arg) { _serversToExport.push_back(&arg); }
243 {
244 _serversToExport.push_back(arg);
245 _serversToDelete.push_back(arg);
246 }
247
248 std::string exportTransformed(const RooAbsReal *original, const std::string &suffix, const std::string &formula);
249 std::string exportAsArrayComponent(RooAbsPdf const &pdf, std::string const &arrayName);
250 /// The name of the top-level object whose export is in progress (the root of the current export recursion).
251 std::string const &exportRootName() const;
252 void recordConstraintTerms(std::string const &pdfName, std::vector<std::string> const &terms);
253 /// Record that `observable` is the observable of the constraint term `pdf` (the side that is not the constrained
254 /// parameter). A Gaussian whose mean it is gets written with x and mean swapped, so that the observable is x.
255 void setConstraintObservable(RooAbsPdf const &pdf, std::string const &observable);
256 std::string const *constraintObservable(std::string const &pdfName) const;
257
258 void setAttribute(const std::string &obj, const std::string &attrib);
259 bool hasAttribute(const std::string &obj, const std::string &attrib);
260 std::string getStringAttribute(const std::string &obj, const std::string &attrib);
261 void setStringAttribute(const std::string &obj, const std::string &attrib, const std::string &value);
262
263 /// The "hs3_version" of the document that is currently being imported, or an empty string outside of an import.
264 std::string const &hs3VersionInput() const { return _hs3VersionInput; }
265
266private:
267 template <class T>
268 T *requestImpl(const std::string &objname);
269 void exportObject(RooAbsArg const &func);
270
271 // To export multiple objects sorted alphabetically
272 template <class T>
273 void exportObjects(T const &args)
274 {
275 RooArgSet argSet;
276 for (RooAbsArg const *arg : args) {
277 argSet.add(*arg);
278 }
279 argSet.sort();
280 for (RooAbsArg *arg : argSet) {
281 exportObject(*arg);
282 }
283 }
284 void exportArrayDists();
285 bool exportConstraintData(std::string const &term, RooArgSet const *globalObservables);
286
287 void exportSimultaneous(RooSimultaneous const &simPdf);
288 void recordCategory(RooAbsCategory const &cat);
289 void exportData(RooAbsData const &data);
291
293 void importDistribution(RooFit::HS3::Distribution const &distribution);
294
296 void warnIfFreeWithoutDomain(RooRealVar const &var);
298
299 void exportVariable(const RooAbsArg *v, RooFit::HS3::ParameterPoint &point, bool storeConstant);
300 void exportVariables(const RooArgSet &allElems, RooFit::HS3::ParameterPoint &point, bool storeConstant);
301
303
305 const std::vector<RooJSONFactoryWSTool::CombinedData> &combined,
306 const std::vector<RooAbsData *> &single);
307
308 void exportSingleModelConfig(RooStats::ModelConfig const &mc, std::string const &analysisName,
309 std::string const *dataName);
310
312 std::vector<RooFit::HS3::DistributionRef> channelConstraints);
313
314 // member variables
315 std::string _hs3VersionInput;
317
318 // objects to represent intermediate information
319 std::unique_ptr<RooFit::HS3::ToolImportState> _import;
320 std::unique_ptr<RooFit::HS3::ToolExportState> _export;
321 std::vector<RooAbsArg const *> _serversToExport;
322 std::vector<RooAbsArg const *> _serversToDelete;
323 // Renaming of the observables of combined data sets, see Config::renameCombinedObservables: for each data set and
324 // each selection label, the new name of each observable; for each RooSimultaneous and selection label, the
325 // component with renamed observables to export instead of the original one. The objects are owned here.
326 std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> _combinedObservableRenames;
327 std::map<std::string, std::map<std::string, RooAbsPdf const *>> _renamedComponents;
328 std::vector<std::unique_ptr<RooAbsArg>> _renamedObjects;
330
331 // The objects that are currently being imported, innermost last, to detect circular dependencies.
332 std::vector<std::string> _importStack;
333};
334#endif
#define c(i)
Definition RSha256.hxx:101
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:142
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
A space to attach TBranches.
Abstract container object that can hold multiple RooAbsArg objects.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
void sort(bool reverse=false)
Sort collection using std::sort and name comparison.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:55
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Object to represent discrete states.
Definition RooCategory.h:28
JSONNode & get(std::string const &key)
bool has_child(std::string const &) const
DependencyMissingError(const std::string &p, const std::string &c, const std::string &classname)
When using RooFit, statistical models can be conveniently handled and stored as a RooWorkspace.
static constexpr bool useListsInsteadOfDicts
Named objects are always stored in lists, as the HS3 standard requires. Kept for backwards compatibil...
static void rebuildModelConfigInWorkspace(RooStats::ModelConfig *mc, RooWorkspace &ws)
std::string getStringAttribute(const std::string &obj, const std::string &attrib)
static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax=-1)
T * requestArg(const RooFit::Detail::JSONNode &node, const std::string &key)
RooJSONFactoryWSTool(RooWorkspace &ws)
void recordConstraintTerms(std::string const &pdfName, std::vector< std::string > const &terms)
T * request(const std::string &objname, const std::string &requestAuthor)
The object objname from the workspace, imported from the document first if needed.
void exportSimultaneous(RooSimultaneous const &simPdf)
Export a RooSimultaneous as a "combined_dist".
void exportData(RooAbsData const &data)
bool hasAttribute(const std::string &obj, const std::string &attrib)
static void exportCategoricalAxis(RooFit::Detail::JSONNode &obsNode, RooCategory const &cat)
Export the name and binning of a RooRealVar to a JSONNode.
bool importJSON(std::string const &filename)
Imports a JSON file from the given filename to the workspace.
std::string const & exportRootName() const
The name of the top-level object whose export is in progress (the root of the current export recursio...
Coll_t requestCollection(const RooFit::Detail::JSONNode &node, const std::string &seqName)
T * tryRequest(const std::string &objname)
Like request(), but returns nullptr if neither the workspace nor the document has the object.
std::map< std::string, std::map< std::string, std::map< std::string, std::string > > > _combinedObservableRenames
Obj_t & wsImport(Obj_t const &obj)
std::vector< std::string > _importStack
static std::unique_ptr< RooDataHist > readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp, RooArgSet const &vars)
Read binned data from the JSONNode and create a RooDataHist object.
static RooFit::Detail::JSONNode & appendNamedChild(RooFit::Detail::JSONNode &node, std::string const &name)
void importDistribution(RooFit::HS3::Distribution const &distribution)
static RooFit::Detail::JSONNode & getRooFitInternal(RooFit::Detail::JSONNode &node, Keys_t const &...keys)
static void exportArray(std::size_t n, double const *contents, RooFit::Detail::JSONNode &output)
Export an array of doubles to a JSONNode.
static bool testValidName(const std::string &str, bool forcError)
void recordCategory(RooAbsCategory const &cat)
void exportObjects(T const &args)
static void exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output)
Export histogram data to a JSONNode.
void exportModelConfig(RooStats::ModelConfig const &mc, const std::vector< RooJSONFactoryWSTool::CombinedData > &combined, const std::vector< RooAbsData * > &single)
std::vector< RooAbsArg const * > _serversToDelete
void queueExportTemporary(RooAbsArg *arg)
static std::unique_ptr< RooFit::Detail::JSONTree > createNewJSONTree()
Create a new JSON tree with version information.
void queueExport(RooAbsArg const &arg)
std::map< std::string, std::map< std::string, RooAbsPdf const * > > _renamedComponents
std::string const & hs3VersionInput() const
The "hs3_version" of the document that is currently being imported, or an empty string outside of an ...
std::unique_ptr< RooFit::HS3::ToolExportState > _export
RooArgSet requestArgSet(const RooFit::Detail::JSONNode &node, const std::string &seqName)
RooJSONFactoryWSTool::CombinedData exportCombinedData(RooAbsData const &data)
Export a data set with category observables as a "combined" data set.
std::string exportJSONtoString()
Export the workspace to a JSON string.
RooArgList requestArgList(const RooFit::Detail::JSONNode &node, const std::string &seqName)
static RooWorkspace cleanWS(const RooWorkspace &ws, bool onlyModelConfig=false)
void exportVariable(const RooAbsArg *v, RooFit::HS3::ParameterPoint &point, bool storeConstant)
std::string exportTransformed(const RooAbsReal *original, const std::string &suffix, const std::string &formula)
static bool isValidName(const std::string &str)
Check if a string is a valid name.
void importVariable(RooFit::HS3::ParameterValue const &p)
T * requestImpl(const std::string &objname)
std::vector< std::unique_ptr< RooAbsArg > > _renamedObjects
void importDependants(const RooFit::Detail::JSONNode &n)
static void exportAxis(RooFit::Detail::JSONNode &obsNode, RooRealVar const &var)
void importJSONElement(const std::string &name, const std::string &jsonString)
static RooWorkspace sanitizeWS(const RooWorkspace &ws)
static void error(const char *s)
Writes an error message to the RooFit message service and throws a runtime_error.
void setConstraintObservable(RooAbsPdf const &pdf, std::string const &observable)
Record that observable is the observable of the constraint term pdf (the side that is not the constra...
void setAttribute(const std::string &obj, const std::string &attrib)
void exportObject(RooAbsArg const &func)
void exportSingleModelConfig(RooStats::ModelConfig const &mc, std::string const &analysisName, std::string const *dataName)
void importFunction(const RooFit::Detail::JSONNode &p, bool importAllDependants)
Import a function from the JSONNode into the workspace.
bool importJSONfromString(const std::string &s)
Import the workspace from a JSON string.
std::unique_ptr< RooFit::HS3::ToolImportState > _import
static RooFit::Detail::JSONNode & makeVariablesNode(RooFit::Detail::JSONNode &rootNode)
Obj_t & wsEmplace(RooStringView name, Args_t &&...args)
static std::string sanitizeName(const std::string str)
Cleans up names to the HS3 standard.
static void error(const std::string &s)
void exportVariables(const RooArgSet &allElems, RooFit::HS3::ParameterPoint &point, bool storeConstant)
static std::string name(const RooFit::Detail::JSONNode &n)
void exportAllObjects(RooFit::Detail::JSONNode &n)
bool exportJSON(std::string const &fileName)
Export the workspace to JSON format and write to the specified file.
static RooFit::Detail::JSONNode const * findNamedChild(RooFit::Detail::JSONNode const &node, std::string const &name)
void importAllNodes(RooFit::Detail::JSONNode &n)
bool exportConstraintData(std::string const &term, RooArgSet const *globalObservables)
std::string exportAsArrayComponent(RooAbsPdf const &pdf, std::string const &arrayName)
void warnIfFreeWithoutDomain(RooRealVar const &var)
std::string const * constraintObservable(std::string const &pdfName) const
void setStringAttribute(const std::string &obj, const std::string &attrib, const std::string &value)
std::vector< RooAbsArg const * > _serversToExport
static std::ostream & warning(const std::string &s)
Writes a warning message to the RooFit message service.
static RooArgSet readAxes(const RooFit::Detail::JSONNode &node)
Read axes from the JSONNode and create a RooArgSet representing them.
void importVariableElement(const RooFit::Detail::JSONNode &n)
Variable that can be changed from the outside.
Definition RooRealVar.h:37
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
< A class that holds configuration information for a model using a workspace as a store
Definition ModelConfig.h:34
The RooStringView is a wrapper around a C-style string that can also be constructed from a std::strin...
Persistable container for RooFit projects.
TObject * obj(RooStringView name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name)
bool import(const RooAbsArg &arg, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}, const RooCmdArg &arg9={})
Import a RooAbsArg object, e.g.
RooCmdArg RecycleConflictNodes(bool flag=true)
RooCmdArg Silence(bool flag=true)
const Int_t n
Definition legend1.C:16
Types that represent an HS3 document.
void setExportedObject(RooJSONFactoryWSTool &tool, HistFactoryDist dist, std::vector< DistributionRef > channelConstraints)
Called by the exporter of a HistFactory distribution, which is then written by the tool.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
Namespace for the RooStats classes.
Definition CodegenImpl.h:67
A name that says what it refers to, so that for example a DataRef can't be used as a DomainRef.
Definition HS3Document.h:63
The document that the tool is exporting.
What the tool needs to know about the document that it is importing.
bool allowExportSharedVariates
HS3 requires the terms of a likelihood and the components of a combined distribution to have distinct...
bool renameCombinedObservables
HS3 requires the components of a combined distribution to have distinct variates, e....
bool identifyLegacyBinWidthFactors
Identify constant factors of HistFactory samples that are used like the inverse bin width of HistFact...