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;
41
42 struct CombinedData {
43 std::string name;
44 std::map<std::string, std::string> components;
45 };
46
48
50
51 static std::string name(const RooFit::Detail::JSONNode &n);
52
54 static RooFit::Detail::JSONNode const *findNamedChild(RooFit::Detail::JSONNode const &node, std::string const &name);
55
56 static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax = -1);
57
58 template <class T>
59 T *request(const std::string &objname, const std::string &requestAuthor)
60 {
61 if (T *out = requestImpl<T>(objname)) {
62 return out;
63 }
64 throw DependencyMissingError(requestAuthor, objname, T::Class()->GetName());
65 }
66
67 template <class T>
68 T *requestArg(const RooFit::Detail::JSONNode &node, const std::string &key)
69 {
70 std::string requestAuthor(RooJSONFactoryWSTool::name(node));
71 if (!node.has_child(key)) {
72 RooJSONFactoryWSTool::error("no \"" + key + "\" given in \"" + requestAuthor + "\"");
73 }
74 return request<T>(node[key].val(), requestAuthor);
75 }
76
77 template <class T, class Coll_t>
78 Coll_t requestCollection(const RooFit::Detail::JSONNode &node, const std::string &seqName)
79 {
80 std::string requestAuthor(RooJSONFactoryWSTool::name(node));
81 if (!node.has_child(seqName)) {
82 RooJSONFactoryWSTool::error("no \"" + seqName + "\" given in \"" + requestAuthor + "\"");
83 }
84 if (!node[seqName].is_seq()) {
85 RooJSONFactoryWSTool::error("\"" + seqName + "\" in \"" + requestAuthor + "\" is not a sequence");
86 }
87
88 Coll_t out;
89 for (const auto &elem : node[seqName].children()) {
90 out.add(*request<T>(elem.val(), requestAuthor));
91 }
92 return out;
93 }
94
95 template <class T>
96 RooArgSet requestArgSet(const RooFit::Detail::JSONNode &node, const std::string &seqName)
97 {
98 return requestCollection<T, RooArgSet>(node, seqName);
99 }
100
101 template <class T>
102 RooArgList requestArgList(const RooFit::Detail::JSONNode &node, const std::string &seqName)
103 {
104 return requestCollection<T, RooArgList>(node, seqName);
105 }
106
108
109 template <class Obj_t>
110 Obj_t &wsImport(Obj_t const &obj)
111 {
113 return *static_cast<Obj_t *>(_workspace.obj(obj.GetName()));
114 }
115
116 template <class Obj_t, typename... Args_t>
117 Obj_t &wsEmplace(RooStringView name, Args_t &&...args)
118 {
119 return wsImport(Obj_t(name.c_str(), name.c_str(), std::forward<Args_t>(args)...));
120 }
121
122 [[noreturn]] static void error(const char *s);
123 [[noreturn]] inline static void error(const std::string &s) { error(s.c_str()); }
124 static std::ostream &warning(const std::string &s);
125
126 static RooArgSet readAxes(const RooFit::Detail::JSONNode &node);
127 static std::unique_ptr<RooDataHist>
128 readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp, RooArgSet const &vars);
129
130 bool importJSON(std::string const &filename);
131 bool importYML(std::string const &filename);
132 bool importJSON(std::istream &os);
133 bool importYML(std::istream &os);
134 bool exportJSON(std::string const &fileName);
135 bool exportYML(std::string const &fileName);
136 bool exportJSON(std::ostream &os);
137 bool exportYML(std::ostream &os);
138
139 std::string exportJSONtoString();
140 std::string exportYMLtoString();
141 bool importJSONfromString(const std::string &s);
142 bool importYMLfromString(const std::string &s);
143 void importJSONElement(const std::string &name, const std::string &jsonString);
145
146 void importFunction(const RooFit::Detail::JSONNode &n, bool importAllDependants);
147 void importFunction(const std::string &jsonString, bool importAllDependants);
148
149 static std::unique_ptr<RooFit::Detail::JSONTree> createNewJSONTree();
150
152
153 // error handling helpers
154 class DependencyMissingError : public std::exception {
156
157 public:
158 DependencyMissingError(const std::string &p, const std::string &c, const std::string &classname)
160 {
161 _message = "object '" + _parent + "' is missing dependency '" + _child + "' of type '" + _class + "'";
162 };
163 const std::string &parent() const { return _parent; }
164 const std::string &child() const { return _child; }
165 const std::string &classname() const { return _class; }
166 const char *what() const noexcept override { return _message.c_str(); }
167 };
168
169 template <typename... Keys_t>
171 {
172 return node.get("misc", "ROOT_internal", keys...);
173 }
174
175 static void
176 exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
177
178 static void exportArray(std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
179
181
182 void queueExport(RooAbsArg const &arg) { _serversToExport.push_back(&arg); }
183
184 std::string exportTransformed(const RooAbsReal *original, const std::string &suffix, const std::string &formula);
185
186 void setAttribute(const std::string &obj, const std::string &attrib);
187 bool hasAttribute(const std::string &obj, const std::string &attrib);
188 std::string getStringAttribute(const std::string &obj, const std::string &attrib);
189 void setStringAttribute(const std::string &obj, const std::string &attrib, const std::string &value);
190
191private:
192 template <class T>
193 T *requestImpl(const std::string &objname);
194
195 void exportObject(RooAbsArg const &func, std::set<std::string> &exportedObjectNames);
196
197 // To export multiple objects sorted alphabetically
198 template <class T>
199 void exportObjects(T const &args, std::set<std::string> &exportedObjectNames)
200 {
201 RooArgSet argSet;
202 for (RooAbsArg const *arg : args) {
203 argSet.add(*arg);
204 }
205 argSet.sort();
206 for (RooAbsArg *arg : argSet) {
207 exportObject(*arg, exportedObjectNames);
208 }
209 }
210
211 void exportData(RooAbsData const &data);
213
215
218
220 void exportVariables(const RooArgSet &allElems, RooFit::Detail::JSONNode &n);
221
223
225 const std::vector<RooJSONFactoryWSTool::CombinedData> &d);
226
228 std::string const &analysisName,
229 std::map<std::string, std::string> const *dataComponents);
230
231 // member variables
237
238 // objects to represent intermediate information
239 std::unique_ptr<RooFit::JSONIO::Detail::Domains> _domains;
240 std::vector<RooAbsArg const *> _serversToExport;
241};
242#endif
#define d(i)
Definition RSha256.hxx:102
#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 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 data
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: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:57
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
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:55
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
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)
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.
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.
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.
void exportSingleModelConfig(RooFit::Detail::JSONNode &rootnode, RooStats::ModelConfig const &mc, std::string const &analysisName, std::map< std::string, std::string > const *dataComponents)
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)
std::string exportTransformed(const RooAbsReal *original, const std::string &suffix, const std::string &formula)
const RooFit::Detail::JSONNode * _attributesNode
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 void error(const char *s)
Writes an error message to the RooFit message service and throws a runtime_error.
void exportModelConfig(RooFit::Detail::JSONNode &rootnode, RooStats::ModelConfig const &mc, const std::vector< RooJSONFactoryWSTool::CombinedData > &d)
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)
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)
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition ModelConfig.h:35
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 JSONIO.h:26
Namespace for the RooStats classes.
Definition Asimov.h:19
std::map< std::string, std::string > components
static void output()