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 <unordered_map>
27
28namespace RooFit {
29namespace JSONIO {
30namespace Detail {
31class Domains;
32}
33} // namespace JSONIO
34} // namespace RooFit
35namespace RooStats {
36class ModelConfig;
37}
38class RooRealVar;
39
41public:
42 static constexpr bool useListsInsteadOfDicts = true;
43
49
50 static Config &config();
51
52 static RooWorkspace sanitizeWS(const RooWorkspace &ws);
53 static RooWorkspace cleanWS(const RooWorkspace &ws, bool onlyModelConfig = false);
54
55 struct CombinedData {
56 std::string name;
57 std::map<std::string, std::string> components;
58 };
59
61
63
64 static std::string name(const RooFit::Detail::JSONNode &n);
65 static bool isValidName(const std::string &str);
66 static bool testValidName(const std::string &str, bool forcError);
67 static std::string sanitizeName(const std::string str);
69
71 static RooFit::Detail::JSONNode const *findNamedChild(RooFit::Detail::JSONNode const &node, std::string const &name);
72
73 static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax = -1);
74
75 template <class T>
76 T *request(const std::string &objname, const std::string &requestAuthor)
77 {
78 if (T *out = requestImpl<T>(objname)) {
79 return out;
80 }
81 throw DependencyMissingError(requestAuthor, objname, T::Class()->GetName());
82 }
83
84 template <class T>
85 T *requestArg(const RooFit::Detail::JSONNode &node, const std::string &key)
86 {
88 if (!node.has_child(key)) {
89 RooJSONFactoryWSTool::error("no \"" + key + "\" given in \"" + requestAuthor + "\"");
90 }
91 return request<T>(node[key].val(), requestAuthor);
92 }
93
94 template <class T, class Coll_t>
96 {
98 if (!node.has_child(seqName)) {
99 RooJSONFactoryWSTool::error("no \"" + seqName + "\" given in \"" + requestAuthor + "\"");
100 }
101 if (!node[seqName].is_seq()) {
102 RooJSONFactoryWSTool::error("\"" + seqName + "\" in \"" + requestAuthor + "\" is not a sequence");
103 }
104
105 Coll_t out;
106 for (const auto &elem : node[seqName].children()) {
107 out.add(*request<T>(elem.val(), requestAuthor));
108 }
109 return out;
110 }
111
112 template <class T>
114 {
116 }
117
118 template <class T>
120 {
122 }
123
125
126 template <class Obj_t>
127 Obj_t &wsImport(Obj_t const &obj)
128 {
130 return *static_cast<Obj_t *>(_workspace.obj(obj.GetName()));
131 }
132
133 template <class Obj_t, typename... Args_t>
135 {
136 return wsImport(Obj_t(name.c_str(), name.c_str(), std::forward<Args_t>(args)...));
137 }
138
139 [[noreturn]] static void error(const char *s);
140 [[noreturn]] inline static void error(const std::string &s) { error(s.c_str()); }
141 static std::ostream &warning(const std::string &s);
142
143 static RooArgSet readAxes(const RooFit::Detail::JSONNode &node);
144 static std::unique_ptr<RooDataHist>
145 readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp, RooArgSet const &vars);
146
147 bool importJSON(std::string const &filename);
148 bool importYML(std::string const &filename);
149 bool importJSON(std::istream &os);
150 bool importYML(std::istream &os);
151 bool exportJSON(std::string const &fileName);
152 bool exportYML(std::string const &fileName);
153 bool exportJSON(std::ostream &os);
154 bool exportYML(std::ostream &os);
155
156 std::string exportJSONtoString();
157 std::string exportYMLtoString();
158 bool importJSONfromString(const std::string &s);
159 bool importYMLfromString(const std::string &s);
160 void importJSONElement(const std::string &name, const std::string &jsonString);
162
164 void importFunction(const std::string &jsonString, bool importAllDependants);
165
166 static std::unique_ptr<RooFit::Detail::JSONTree> createNewJSONTree();
167
169
170 // error handling helpers
171 class DependencyMissingError : public std::exception {
173
174 public:
175 DependencyMissingError(const std::string &p, const std::string &c, const std::string &classname)
177 {
178 _message = "object '" + _parent + "' is missing dependency '" + _child + "' of type '" + _class + "'";
179 };
180 const std::string &parent() const { return _parent; }
181 const std::string &child() const { return _child; }
182 const std::string &classname() const { return _class; }
183 const char *what() const noexcept override { return _message.c_str(); }
184 };
185
186 template <typename... Keys_t>
188 {
189 return node.get("misc", "ROOT_internal", keys...);
190 }
191
192 static void exportAxis(RooFit::Detail::JSONNode &obsNode, RooRealVar const &var);
193
194 static void
195 exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
196
197 static void exportArray(std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
198
200
201 void queueExport(RooAbsArg const &arg) { _serversToExport.push_back(&arg); }
203 {
204 _serversToExport.push_back(arg);
205 _serversToDelete.push_back(arg);
206 }
207
208 std::string exportTransformed(const RooAbsReal *original, const std::string &suffix, const std::string &formula);
209
210 void setAttribute(const std::string &obj, const std::string &attrib);
211 bool hasAttribute(const std::string &obj, const std::string &attrib);
212 std::string getStringAttribute(const std::string &obj, const std::string &attrib);
213 void setStringAttribute(const std::string &obj, const std::string &attrib, const std::string &value);
214
215private:
216 template <class T>
217 T *requestImpl(const std::string &objname);
218 void exportObject(RooAbsArg const &func, std::set<std::string> &exportedObjectNames);
219
220 // To export multiple objects sorted alphabetically
221 template <class T>
222 void exportObjects(T const &args, std::set<std::string> &exportedObjectNames)
223 {
224 RooArgSet argSet;
225 for (RooAbsArg const *arg : args) {
226 argSet.add(*arg);
227 }
228 argSet.sort();
229 for (RooAbsArg *arg : argSet) {
231 }
232 }
233
234 void exportData(RooAbsData const &data);
236
238
241
244
246
248 const std::vector<RooJSONFactoryWSTool::CombinedData> &combined,
249 const std::vector<RooAbsData *> &single);
250
252 std::string const &analysisName,
253 std::map<std::string, std::string> const *dataComponents);
254
255 // member variables
261
262 // objects to represent intermediate information
263 std::unique_ptr<RooFit::JSONIO::Detail::Domains> _domains;
264 std::vector<RooAbsArg const *> _serversToExport;
265 std::vector<RooAbsArg const *> _serversToDelete;
266
267 // Name-keyed indices over the top-level "functions" and "distributions"
268 // sequences of the input JSON. Built once at the start of importAllNodes()
269 // so that requestImpl() lookups become O(1) instead of an O(N) scan over
270 // every sibling node.
271 std::unordered_map<std::string, RooFit::Detail::JSONNode const *> _functionsByName;
272 std::unordered_map<std::string, RooFit::Detail::JSONNode const *> _distributionsByName;
273};
274#endif
#define c(i)
Definition RSha256.hxx:101
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:145
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:76
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:56
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
JSONNode & get(std::string const &key)
virtual children_view children()
virtual bool has_child(std::string const &) const =0
DependencyMissingError(const std::string &p, const std::string &c, const std::string &classname)
const char * what() const noexcept override
When using RooFit, statistical models can be conveniently handled and stored as a RooWorkspace.
static constexpr bool useListsInsteadOfDicts
static void rebuildModelConfigInWorkspace(RooStats::ModelConfig *mc, RooWorkspace &ws)
std::string getStringAttribute(const std::string &obj, const std::string &attrib)
bool importYML(std::string const &filename)
Imports a YML file from the given filename to the workspace.
static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax=-1)
void exportObjects(T const &args, std::set< std::string > &exportedObjectNames)
void exportCategory(RooAbsCategory const &cat, RooFit::Detail::JSONNode &node)
T * requestArg(const RooFit::Detail::JSONNode &node, const std::string &key)
RooJSONFactoryWSTool(RooWorkspace &ws)
T * request(const std::string &objname, const std::string &requestAuthor)
void exportData(RooAbsData const &data)
Export data from the workspace to a JSONNode.
void exportModelConfig(RooFit::Detail::JSONNode &rootnode, RooStats::ModelConfig const &mc, const std::vector< RooJSONFactoryWSTool::CombinedData > &combined, const std::vector< RooAbsData * > &single)
bool hasAttribute(const std::string &obj, const std::string &attrib)
bool importJSON(std::string const &filename)
Imports a JSON file from the given filename to the workspace.
Coll_t requestCollection(const RooFit::Detail::JSONNode &node, const std::string &seqName)
void exportVariables(const RooArgSet &allElems, RooFit::Detail::JSONNode &n, bool storeConstant, bool storeBins)
Export variables from the workspace to a JSONNode.
Obj_t & wsImport(Obj_t const &obj)
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)
std::string exportYMLtoString()
Export the workspace to a YML string.
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.
bool importYMLfromString(const std::string &s)
Import the workspace from a YML string.
static bool testValidName(const std::string &str, bool forcError)
RooFit::Detail::JSONNode * _rootnodeOutput
static void exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output)
Export histogram data to a JSONNode.
std::vector< RooAbsArg const * > _serversToDelete
std::unordered_map< std::string, RooFit::Detail::JSONNode const * > _functionsByName
void exportSingleModelConfig(RooFit::Detail::JSONNode &rootnode, RooStats::ModelConfig const &mc, std::string const &analysisName, std::map< std::string, std::string > const *dataComponents)
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)
RooArgSet requestArgSet(const RooFit::Detail::JSONNode &node, const std::string &seqName)
const RooFit::Detail::JSONNode * _rootnodeInput
RooJSONFactoryWSTool::CombinedData exportCombinedData(RooAbsData const &data)
Export combined data from the workspace to a custom struct.
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)
std::string exportTransformed(const RooAbsReal *original, const std::string &suffix, const std::string &formula)
const RooFit::Detail::JSONNode * _attributesNode
static bool isValidName(const std::string &str)
Check if a string is a valid name.
T * requestImpl(const std::string &objname)
void importDependants(const RooFit::Detail::JSONNode &n)
Import all dependants (servers) of a node into the workspace.
static void exportAxis(RooFit::Detail::JSONNode &obsNode, RooRealVar const &var)
Export the name and binning of a RooRealVar to a JSONNode.
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 setAttribute(const std::string &obj, const std::string &attrib)
void importVariable(const RooFit::Detail::JSONNode &p)
Import a variable from the JSONNode into the workspace.
bool exportYML(std::string const &fileName)
Export the workspace to YML format and write to the specified file.
void exportVariable(const RooAbsArg *v, RooFit::Detail::JSONNode &n, bool storeConstant, bool storeBins)
Export a variable from the workspace to a JSONNode.
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.
RooFit::Detail::JSONNode * _varsNode
void exportObject(RooAbsArg const &func, std::set< std::string > &exportedObjectNames)
Export an object from the workspace to a JSONNode.
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.
void importAllNodes(const RooFit::Detail::JSONNode &n)
Imports all nodes of the JSON data and adds them to the workspace.
static void error(const std::string &s)
static std::string name(const RooFit::Detail::JSONNode &n)
void exportAllObjects(RooFit::Detail::JSONNode &n)
Export all objects in the workspace to a JSONNode.
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)
std::unordered_map< std::string, RooFit::Detail::JSONNode const * > _distributionsByName
void setStringAttribute(const std::string &obj, const std::string &attrib, const std::string &value)
std::vector< RooAbsArg const * > _serversToExport
std::unique_ptr< RooFit::JSONIO::Detail::Domains > _domains
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
< 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
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:72
Namespace for the RooStats classes.
Definition CodegenImpl.h:66
std::map< std::string, std::string > components
static void output()