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::ostream &log(int level);
52
53 static std::string name(const RooFit::Detail::JSONNode &n);
54
56 static RooFit::Detail::JSONNode const *findNamedChild(RooFit::Detail::JSONNode const &node, std::string const &name);
57
58 static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll);
59
60 template <class T>
61 T *request(const std::string &objname, const std::string &requestAuthor)
62 {
63 if (T *out = requestImpl<T>(objname)) {
64 return out;
65 }
66 throw DependencyMissingError(requestAuthor, objname, T::Class()->GetName());
67 }
68
69 template <class T>
70 T *requestArg(const RooFit::Detail::JSONNode &node, const std::string &key)
71 {
72 std::string requestAuthor(RooJSONFactoryWSTool::name(node));
73 if (!node.has_child(key)) {
74 RooJSONFactoryWSTool::error("no \"" + key + "\" given in \"" + requestAuthor + "\"");
75 }
76 return request<T>(node[key].val(), requestAuthor);
77 }
78
79 template <class T, class Coll_t>
80 Coll_t requestCollection(const RooFit::Detail::JSONNode &node, const std::string &seqName)
81 {
82 std::string requestAuthor(RooJSONFactoryWSTool::name(node));
83 if (!node.has_child(seqName)) {
84 RooJSONFactoryWSTool::error("no \"" + seqName + "\" given in \"" + requestAuthor + "\"");
85 }
86 if (!node[seqName].is_seq()) {
87 RooJSONFactoryWSTool::error("\"" + seqName + "\" in \"" + requestAuthor + "\" is not a sequence");
88 }
89
90 Coll_t out;
91 for (const auto &elem : node[seqName].children()) {
92 out.add(*request<T>(elem.val(), requestAuthor));
93 }
94 return out;
95 }
96
97 template <class T>
98 RooArgSet requestArgSet(const RooFit::Detail::JSONNode &node, const std::string &seqName)
99 {
100 return requestCollection<T, RooArgSet>(node, seqName);
101 }
102
103 template <class T>
104 RooArgList requestArgList(const RooFit::Detail::JSONNode &node, const std::string &seqName)
105 {
106 return requestCollection<T, RooArgList>(node, seqName);
107 }
108
110
111 template <class Obj_t>
112 Obj_t &wsImport(Obj_t const &obj)
113 {
115 return *static_cast<Obj_t *>(_workspace.obj(obj.GetName()));
116 }
117
118 template <class Obj_t, typename... Args_t>
119 Obj_t &wsEmplace(RooStringView name, Args_t &&...args)
120 {
121 return wsImport(Obj_t(name, name, std::forward<Args_t>(args)...));
122 }
123
124 static void error(const char *s);
125 inline static void error(const std::string &s) { error(s.c_str()); }
126
127 static std::unique_ptr<RooDataHist> readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp);
128 static std::unique_ptr<RooDataHist>
129 readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp, RooArgList const &varlist);
130
131 bool importJSON(std::string const &filename);
132 bool importYML(std::string const &filename);
133 bool importJSON(std::istream &os);
134 bool importYML(std::istream &os);
135 bool exportJSON(std::string const &fileName);
136 bool exportYML(std::string const &fileName);
137 bool exportJSON(std::ostream &os);
138 bool exportYML(std::ostream &os);
139
140 std::string exportJSONtoString();
141 std::string exportYMLtoString();
142 bool importJSONfromString(const std::string &s);
143 bool importYMLfromString(const std::string &s);
144 void importJSONElement(const std::string &name, const std::string &jsonString);
146
147 void importFunction(const RooFit::Detail::JSONNode &n, bool importAllDependants);
148 void importFunction(const std::string &jsonString, bool importAllDependants);
149
150 static std::unique_ptr<RooFit::Detail::JSONTree> createNewJSONTree();
151
153
154 // error handling helpers
155 class DependencyMissingError : public std::exception {
157
158 public:
159 DependencyMissingError(const std::string &p, const std::string &c, const std::string &classname)
161 {
162 _message = "object '" + _parent + "' is missing dependency '" + _child + "' of type '" + _class + "'";
163 };
164 const std::string &parent() const { return _parent; }
165 const std::string &child() const { return _child; }
166 const std::string &classname() const { return _class; }
167 const char *what() const noexcept override { return _message.c_str(); }
168 };
169
170 template <typename... Keys_t>
172 {
173 return node.get("misc", "ROOT_internal", keys...);
174 }
175
176 static void
177 exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
178
179 static void exportArray(std::size_t n, double const *contents, RooFit::Detail::JSONNode &output);
180
181 static void exportCategory(RooAbsCategory const &cat, RooFit::Detail::JSONNode &node);
182
183 void queueExport(RooAbsArg const &arg) { _serversToExport.push_back(&arg); }
184
185private:
186 template <class T>
187 T *requestImpl(const std::string &objname);
188
189 void exportObject(RooAbsArg const &func, std::set<std::string> &exportedObjectNames);
190
191 void exportData(RooAbsData const &data);
193
195
198
200 void exportVariables(const RooArgSet &allElems, RooFit::Detail::JSONNode &n);
201
203
205 const std::vector<RooJSONFactoryWSTool::CombinedData> &d);
206
208 std::string const &analysisName,
209 std::map<std::string, std::string> const *dataComponents);
210
211 // member variables
217
218 // objects to represent intermediate information
219 std::unique_ptr<RooFit::JSONIO::Detail::Domains> _domains;
220 std::vector<RooAbsArg const *> _serversToExport;
221};
222#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 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
char name[80]
Definition TGX11.cxx:110
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
A space to attach TBranches.
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.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)
static constexpr bool useListsInsteadOfDicts
bool importYML(std::string const &filename)
static void exportCategory(RooAbsCategory const &cat, RooFit::Detail::JSONNode &node)
T * requestArg(const RooFit::Detail::JSONNode &node, const std::string &key)
void importVariable(const RooFit::Detail::JSONNode &n)
T * request(const std::string &objname, const std::string &requestAuthor)
void exportData(RooAbsData const &data)
static std::unique_ptr< RooDataHist > readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp)
void exportVariables(const RooArgSet &allElems, RooFit::Detail::JSONNode &n)
bool importJSON(std::string const &filename)
static std::ostream & log(int level)
Coll_t requestCollection(const RooFit::Detail::JSONNode &node, const std::string &seqName)
Obj_t & wsImport(Obj_t const &obj)
static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll)
static RooFit::Detail::JSONNode & appendNamedChild(RooFit::Detail::JSONNode &node, std::string const &name)
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)
bool importYMLfromString(const std::string &s)
RooFit::Detail::JSONNode * _rootnodeOutput
static void exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output)
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)
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)
RooArgList requestArgList(const RooFit::Detail::JSONNode &node, const std::string &seqName)
const RooFit::Detail::JSONNode * _attributesNode
T * requestImpl(const std::string &objname)
void importDependants(const RooFit::Detail::JSONNode &n)
void importJSONElement(const std::string &name, const std::string &jsonString)
static void error(const char *s)
void exportModelConfig(RooFit::Detail::JSONNode &rootnode, RooStats::ModelConfig const &mc, const std::vector< RooJSONFactoryWSTool::CombinedData > &d)
bool exportYML(std::string const &fileName)
bool importJSONfromString(const std::string &s)
RooFit::Detail::JSONNode * _varsNode
void exportObject(RooAbsArg const &func, std::set< std::string > &exportedObjectNames)
static RooFit::Detail::JSONNode & makeVariablesNode(RooFit::Detail::JSONNode &rootNode)
Obj_t & wsEmplace(RooStringView name, Args_t &&...args)
void importAllNodes(const RooFit::Detail::JSONNode &n)
static void error(const std::string &s)
static std::string name(const RooFit::Detail::JSONNode &n)
void exportAllObjects(RooFit::Detail::JSONNode &n)
bool exportJSON(std::string const &fileName)
static RooFit::Detail::JSONNode const * findNamedChild(RooFit::Detail::JSONNode const &node, std::string const &name)
std::vector< RooAbsArg const * > _serversToExport
std::unique_ptr< RooFit::JSONIO::Detail::Domains > _domains
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-syle string that can also be constructed from a std::string...
The RooWorkspace is a 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=RooCmdArg(), const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg(), const RooCmdArg &arg9=RooCmdArg())
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 Common.h:18
Namespace for the RooStats classes.
Definition Asimov.h:19
std::map< std::string, std::string > components
static void output()