Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
43 struct CombinedData {
44 std::string name;
45 std::map<std::string, std::string> components;
46 };
47
49
51
52 static std::string name(const RooFit::Detail::JSONNode &n);
53 static bool isValidName(const std::string &str);
54 static bool testValidName(const std::string &str, bool forcError);
55
57 static RooFit::Detail::JSONNode const *findNamedChild(RooFit::Detail::JSONNode const &node, std::string const &name);
58
59 static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax = -1);
60
61 template <class T>
62 T *request(const std::string &objname, const std::string &requestAuthor)
63 {
64 if (T *out = requestImpl<T>(objname)) {
65 return out;
66 }
67 throw DependencyMissingError(requestAuthor, objname, T::Class()->GetName());
68 }
69
70 template <class T>
71 T *requestArg(const RooFit::Detail::JSONNode &node, const std::string &key)
72 {
74 if (!node.has_child(key)) {
75 RooJSONFactoryWSTool::error("no \"" + key + "\" given in \"" + requestAuthor + "\"");
76 }
77 return request<T>(node[key].val(), requestAuthor);
78 }
79
80 template <class T, class Coll_t>
82 {
84 if (!node.has_child(seqName)) {
85 RooJSONFactoryWSTool::error("no \"" + seqName + "\" given in \"" + requestAuthor + "\"");
86 }
87 if (!node[seqName].is_seq()) {
88 RooJSONFactoryWSTool::error("\"" + seqName + "\" in \"" + requestAuthor + "\" is not a sequence");
89 }
90
91 Coll_t out;
92 for (const auto &elem : node[seqName].children()) {
93 out.add(*request<T>(elem.val(), requestAuthor));
94 }
95 return out;
96 }
97
98 template <class T>
100 {
102 }
103
104 template <class T>
106 {
108 }
109
111
112 template <class Obj_t>
113 Obj_t &wsImport(Obj_t const &obj)
114 {
116 return *static_cast<Obj_t *>(_workspace.obj(obj.GetName()));
117 }
118
119 template <class Obj_t, typename... Args_t>
121 {
122 return wsImport(Obj_t(name.c_str(), name.c_str(), std::forward<Args_t>(args)...));
123 }
124
125 [[noreturn]] static void error(const char *s);
126 [[noreturn]] inline static void error(const std::string &s) { error(s.c_str()); }
127 static std::ostream &warning(const std::string &s);
128
129 static RooArgSet readAxes(const RooFit::Detail::JSONNode &node);
130 static std::unique_ptr<RooDataHist>
131 readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp, RooArgSet const &vars);
132
133 bool importJSON(std::string const &filename);
134 bool importYML(std::string const &filename);
135 bool importJSON(std::istream &os);
136 bool importYML(std::istream &os);
137 bool exportJSON(std::string const &fileName);
138 bool exportYML(std::string const &fileName);
139 bool exportJSON(std::ostream &os);
140 bool exportYML(std::ostream &os);
141
142 std::string exportJSONtoString();
143 std::string exportYMLtoString();
144 bool importJSONfromString(const std::string &s);
145 bool importYMLfromString(const std::string &s);
146 void importJSONElement(const std::string &name, const std::string &jsonString);
148
150 void importFunction(const std::string &jsonString, bool importAllDependants);
151
152 static std::unique_ptr<RooFit::Detail::JSONTree> createNewJSONTree();
153
155
156 // error handling helpers
157 class DependencyMissingError : public std::exception {
159
160 public:
161 DependencyMissingError(const std::string &p, const std::string &c, const std::string &classname)
163 {
164 _message = "object '" + _parent + "' is missing dependency '" + _child + "' of type '" + _class + "'";
165 };
166 const std::string &parent() const { return _parent; }
167 const std::string &child() const { return _child; }
168 const std::string &classname() const { return _class; }
169 const char *what() const noexcept override { return _message.c_str(); }
170 };
171
172 template <typename... Keys_t>
174 {
175 return node.get("misc", "ROOT_internal", keys...);
176 }
177
178 static void
179 exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
180
181 static void exportArray(std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
182
184
185 void queueExport(RooAbsArg const &arg) { _serversToExport.push_back(&arg); }
186
187 std::string exportTransformed(const RooAbsReal *original, const std::string &suffix, const std::string &formula);
188
189 void setAttribute(const std::string &obj, const std::string &attrib);
190 bool hasAttribute(const std::string &obj, const std::string &attrib);
191 std::string getStringAttribute(const std::string &obj, const std::string &attrib);
192 void setStringAttribute(const std::string &obj, const std::string &attrib, const std::string &value);
193
194private:
195 template <class T>
196 T *requestImpl(const std::string &objname);
197
198 void exportObject(RooAbsArg const &func, std::set<std::string> &exportedObjectNames);
199
200 // To export multiple objects sorted alphabetically
201 template <class T>
202 void exportObjects(T const &args, std::set<std::string> &exportedObjectNames)
203 {
204 RooArgSet argSet;
205 for (RooAbsArg const *arg : args) {
206 argSet.add(*arg);
207 }
208 argSet.sort();
209 for (RooAbsArg *arg : argSet) {
211 }
212 }
213
214 void exportData(RooAbsData const &data);
216
218
221
224
226
228 const std::vector<RooJSONFactoryWSTool::CombinedData> &d);
229
231 std::string const &analysisName,
232 std::map<std::string, std::string> const *dataComponents);
233
234 // member variables
240
241 // objects to represent intermediate information
242 std::unique_ptr<RooFit::JSONIO::Detail::Domains> _domains;
243 std::vector<RooAbsArg const *> _serversToExport;
244};
245#endif
#define d(i)
Definition RSha256.hxx:102
#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: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: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
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.
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.
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
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 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 CodegenImpl.h:64
Namespace for the RooStats classes.
Definition CodegenImpl.h:58
std::map< std::string, std::string > components
static void output()