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
27namespace RooFit {
28namespace JSONIO {
29namespace Detail {
30class Domains;
31}
32} // namespace JSONIO
33} // namespace RooFit
34namespace RooStats {
35class ModelConfig;
36}
37
39public:
40 static constexpr bool useListsInsteadOfDicts = true;
42 static bool allowSanitizeNames;
43 static RooWorkspace sanitizeWS(const RooWorkspace &ws);
44 static RooWorkspace cleanWS(const RooWorkspace &ws, bool onlyModelConfig = false);
45
46 struct CombinedData {
47 std::string name;
48 std::map<std::string, std::string> components;
49 };
50
52
54
55 static std::string name(const RooFit::Detail::JSONNode &n);
56 static bool isValidName(const std::string &str);
57 static bool testValidName(const std::string &str, bool forcError);
58 static std::string sanitizeName(const std::string str);
60
62 static RooFit::Detail::JSONNode const *findNamedChild(RooFit::Detail::JSONNode const &node, std::string const &name);
63
64 static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax = -1);
65 static void fillSeqSanitizedName(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax = -1);
66
67 template <class T>
68 T *request(const std::string &objname, const std::string &requestAuthor)
69 {
70 if (T *out = requestImpl<T>(objname)) {
71 return out;
72 }
73 throw DependencyMissingError(requestAuthor, objname, T::Class()->GetName());
74 }
75
76 template <class T>
77 T *requestArg(const RooFit::Detail::JSONNode &node, const std::string &key)
78 {
80 if (!node.has_child(key)) {
81 RooJSONFactoryWSTool::error("no \"" + key + "\" given in \"" + requestAuthor + "\"");
82 }
83 return request<T>(node[key].val(), requestAuthor);
84 }
85
86 template <class T, class Coll_t>
88 {
90 if (!node.has_child(seqName)) {
91 RooJSONFactoryWSTool::error("no \"" + seqName + "\" given in \"" + requestAuthor + "\"");
92 }
93 if (!node[seqName].is_seq()) {
94 RooJSONFactoryWSTool::error("\"" + seqName + "\" in \"" + requestAuthor + "\" is not a sequence");
95 }
96
97 Coll_t out;
98 for (const auto &elem : node[seqName].children()) {
99 out.add(*request<T>(elem.val(), requestAuthor));
100 }
101 return out;
102 }
103
104 template <class T>
106 {
108 }
109
110 template <class T>
112 {
114 }
115
117
118 template <class Obj_t>
119 Obj_t &wsImport(Obj_t const &obj)
120 {
122 return *static_cast<Obj_t *>(_workspace.obj(obj.GetName()));
123 }
124
125 template <class Obj_t, typename... Args_t>
127 {
128 return wsImport(Obj_t(name.c_str(), name.c_str(), std::forward<Args_t>(args)...));
129 }
130
131 [[noreturn]] static void error(const char *s);
132 [[noreturn]] inline static void error(const std::string &s) { error(s.c_str()); }
133 static std::ostream &warning(const std::string &s);
134
135 static RooArgSet readAxes(const RooFit::Detail::JSONNode &node);
136 static std::unique_ptr<RooDataHist>
137 readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp, RooArgSet const &vars);
138
139 bool importJSON(std::string const &filename);
140 bool importYML(std::string const &filename);
141 bool importJSON(std::istream &os);
142 bool importYML(std::istream &os);
143 bool exportJSON(std::string const &fileName);
144 bool exportYML(std::string const &fileName);
145 bool exportJSON(std::ostream &os);
146 bool exportYML(std::ostream &os);
147
148 std::string exportJSONtoString();
149 std::string exportYMLtoString();
150 bool importJSONfromString(const std::string &s);
151 bool importYMLfromString(const std::string &s);
152 void importJSONElement(const std::string &name, const std::string &jsonString);
154
156 void importFunction(const std::string &jsonString, bool importAllDependants);
157
158 static std::unique_ptr<RooFit::Detail::JSONTree> createNewJSONTree();
159
161
162 // error handling helpers
163 class DependencyMissingError : public std::exception {
165
166 public:
167 DependencyMissingError(const std::string &p, const std::string &c, const std::string &classname)
169 {
170 _message = "object '" + _parent + "' is missing dependency '" + _child + "' of type '" + _class + "'";
171 };
172 const std::string &parent() const { return _parent; }
173 const std::string &child() const { return _child; }
174 const std::string &classname() const { return _class; }
175 const char *what() const noexcept override { return _message.c_str(); }
176 };
177
178 template <typename... Keys_t>
180 {
181 return node.get("misc", "ROOT_internal", keys...);
182 }
183
184 static void
185 exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
186
187 static void exportArray(std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
188
190
191 void queueExport(RooAbsArg const &arg) { _serversToExport.push_back(&arg); }
193 {
194 _serversToExport.push_back(arg);
195 _serversToDelete.push_back(arg);
196 }
197
198 std::string exportTransformed(const RooAbsReal *original, const std::string &suffix, const std::string &formula);
199
200 void setAttribute(const std::string &obj, const std::string &attrib);
201 bool hasAttribute(const std::string &obj, const std::string &attrib);
202 std::string getStringAttribute(const std::string &obj, const std::string &attrib);
203 void setStringAttribute(const std::string &obj, const std::string &attrib, const std::string &value);
204
205private:
206 template <class T>
207 T *requestImpl(const std::string &objname);
208 void exportObject(RooAbsArg const &func, std::set<std::string> &exportedObjectNames);
209
210 // To export multiple objects sorted alphabetically
211 template <class T>
212 void exportObjects(T const &args, std::set<std::string> &exportedObjectNames)
213 {
214 RooArgSet argSet;
215 for (RooAbsArg const *arg : args) {
216 argSet.add(*arg);
217 }
218 argSet.sort();
219 for (RooAbsArg *arg : argSet) {
221 }
222 }
223
224 void exportData(RooAbsData const &data);
226
228
231
234
236
238 const std::vector<RooJSONFactoryWSTool::CombinedData> &combined,
239 const std::vector<RooAbsData *> &single);
240
242 std::string const &analysisName,
243 std::map<std::string, std::string> const *dataComponents);
244
245 // member variables
251
252 // objects to represent intermediate information
253 std::unique_ptr<RooFit::JSONIO::Detail::Domains> _domains;
254 std::vector<RooAbsArg const *> _serversToExport;
255 std::vector<RooAbsArg const *> _serversToDelete;
256};
257#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:110
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:57
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.
void importFunction(const RooFit::Detail::JSONNode &n, bool importAllDependants)
Import a function from the JSONNode into the workspace.
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)
Export a RooAbsCategory object to a JSONNode.
T * requestArg(const RooFit::Detail::JSONNode &node, const std::string &key)
RooJSONFactoryWSTool(RooWorkspace &ws)
void importVariable(const RooFit::Detail::JSONNode &n)
Import a variable from the JSONNode into the workspace.
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)
void exportVariables(const RooArgSet &allElems, RooFit::Detail::JSONNode &n)
Export variables from the workspace to a JSONNode.
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)
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
void exportSingleModelConfig(RooFit::Detail::JSONNode &rootnode, RooStats::ModelConfig const &mc, std::string const &analysisName, std::map< std::string, std::string > const *dataComponents)
static void fillSeqSanitizedName(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax=-1)
void queueExportTemporary(RooAbsArg *arg)
static std::unique_ptr< RooFit::Detail::JSONTree > createNewJSONTree()
Create a new JSON tree with version information.
void exportVariable(const RooAbsArg *v, RooFit::Detail::JSONNode &n)
Export a variable from the workspace to a JSONNode.
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.
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)
bool exportYML(std::string const &fileName)
Export the workspace to YML format and write to the specified file.
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)
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)
< 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:67
Namespace for the RooStats classes.
Definition CodegenImpl.h:61
std::map< std::string, std::string > components
static void output()