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
16#include <map>
17#include <memory>
18#include <stdexcept>
19#include <string>
20#include <vector>
21
22class RooArgList;
23class RooAbsData;
24class RooArgSet;
25class RooAbsArg;
26class RooAbsReal;
27class RooAbsPdf;
28class RooDataHist;
29class RooDataSet;
30class RooRealVar;
31class RooRealVar;
32class RooWorkspace;
33
34class TH1;
35class TClass;
36
37namespace RooFit {
38namespace Experimental {
39class JSONNode;
40}
41} // namespace RooFit
42
44public:
45 struct Config {
46 static bool stripObservables;
47 };
48
49 class Importer {
50 public:
51 virtual bool importPdf(RooJSONFactoryWSTool *, const RooFit::Experimental::JSONNode &) const { return false; }
53 {
54 return false;
55 }
56 virtual ~Importer(){};
57 };
58 class Exporter {
59 public:
60 virtual std::string const &key() const = 0;
61 virtual bool autoExportDependants() const { return true; }
63 {
64 return false;
65 }
66 virtual ~Exporter(){};
67 };
68 struct ExportKeys {
69 std::string type;
70 std::map<std::string, std::string> proxies;
71 };
73 TClass const *tclass = nullptr;
74 std::vector<std::string> arguments;
75 };
76
77 typedef std::map<const std::string, std::vector<std::unique_ptr<const Importer>>> ImportMap;
78 typedef std::map<TClass const *, std::vector<std::unique_ptr<const Exporter>>> ExportMap;
79 typedef std::map<TClass const *, ExportKeys> ExportKeysMap;
80 typedef std::map<const std::string, ImportExpression> ImportExpressionMap;
81
82 // The following maps to hold the importers and exporters for runtime lookup
83 // could also be static variables directly, but to avoid the static
84 // initialization order fiasco these are functions that return static
85 // variables.
86 static ImportMap &staticImporters();
87 static ExportMap &staticExporters();
91
92 struct Var {
93 int nbins;
94 double min;
95 double max;
96 std::vector<double> bounds;
97
98 Var(int n) : nbins(n), min(0), max(n) {}
100 };
101
102 std::ostream &log(int level) const;
103
104protected:
105 struct Scope {
106 std::vector<RooAbsArg *> observables;
107 std::map<std::string, RooAbsArg *> objects;
108 };
109 mutable Scope _scope;
112
115
117
118 std::map<std::string, std::unique_ptr<RooAbsData>> loadData(const RooFit::Experimental::JSONNode &n);
119 std::unique_ptr<RooDataSet> unbinned(RooDataHist const &hist);
120 RooRealVar *getWeightVar(const char *name);
121 RooRealVar *createObservable(const std::string &name, const RooJSONFactoryWSTool::Var &var);
122
123public:
124 class MissingRootnodeError : public std::exception {
125 public:
126 virtual const char *what() const noexcept override { return "no rootnode set"; }
127 };
128
129 class DependencyMissingError : public std::exception {
131
132 public:
133 DependencyMissingError(const std::string &p, const std::string &c, const std::string &classname)
135 {
136 _message = "object '" + _parent + "' is missing dependency '" + _child + "' of type '" + _class + "'";
137 };
138 const std::string &parent() const { return _parent; }
139 const std::string &child() const { return _child; }
140 const std::string &classname() const { return _class; }
141 virtual const char *what() const noexcept override { return _message.c_str(); }
142 };
144
145 static std::string name(const RooFit::Experimental::JSONNode &n);
146
147 template <class T>
148 T *request(const std::string &objname, const std::string &requestAuthor);
149
151 RooWorkspace *workspace() { return this->_workspace; }
152
153 template <class T>
154 static bool registerImporter(const std::string &key, bool topPriority = true)
155 {
156 return registerImporter(key, std::make_unique<T>(), topPriority);
157 }
158 template <class T>
159 static bool registerExporter(const TClass *key, bool topPriority = true)
160 {
161 return registerExporter(key, std::make_unique<T>(), topPriority);
162 }
163
164 static bool registerImporter(const std::string &key, std::unique_ptr<const RooJSONFactoryWSTool::Importer> f,
165 bool topPriority = true);
166 static bool registerExporter(const TClass *key, std::unique_ptr<const RooJSONFactoryWSTool::Exporter> f,
167 bool topPriority = true);
168 static int removeImporters(const std::string &needle);
169 static int removeExporters(const std::string &needle);
170 static void printImporters();
171 static void printExporters();
172
173 static ImportMap const &importers() { return staticImporters(); }
174 static ExportMap const &exporters() { return staticExporters(); }
177 static ExportKeysMap const &exportKeys() { return staticExportKeys(); }
178
179 // error handling helpers
180 static void error(const char *s) { throw std::runtime_error(s); }
181 static void error(const std::string &s) { throw std::runtime_error(s); }
182 template <class T>
183 static std::string concat(const T *items, const std::string &sep = ",")
184 {
185 // Returns a string being the concatenation of strings in input list <items>
186 // (names of objects obtained using GetName()) separated by string <sep>.
187 bool first = true;
188 std::string text;
189
190 // iterate over strings in list
191 for (auto it : *items) {
192 if (!first) {
193 // insert separator string
194 text += sep;
195 } else {
196 first = false;
197 }
198 if (!it)
199 text += "NULL";
200 else
201 text += it->GetName();
202 }
203 return text;
204 }
205 template <class T>
206 static std::vector<std::string> names(const T *items)
207 {
208 // Returns a string being the concatenation of strings in input list <items>
209 // (names of objects obtained using GetName()) separated by string <sep>.
210 std::vector<std::string> names;
211 // iterate over strings in list
212 for (auto it : *items) {
213 if (!it)
214 names.push_back("NULL");
215 else
216 names.push_back(it->GetName());
217 }
218 return names;
219 }
220
221 static std::string genPrefix(const RooFit::Experimental::JSONNode &p, bool trailing_underscore);
223 const std::vector<std::string> &obsnames, const TH1 *errH = 0,
224 bool writeObservables = true, bool writeErrors = true);
226 static void
227 writeObservables(const TH1 &h, RooFit::Experimental::JSONNode &n, const std::vector<std::string> &varnames);
228 static std::vector<std::vector<int>> generateBinIndices(const RooArgList &vars);
229 std::unique_ptr<RooDataHist>
230 readBinnedData(const RooFit::Experimental::JSONNode &n, const std::string &namecomp, RooArgList observables);
231 static std::map<std::string, RooJSONFactoryWSTool::Var>
232 readObservables(const RooFit::Experimental::JSONNode &n, const std::string &obsnamecomp);
233 void getObservables(const RooFit::Experimental::JSONNode &n, const std::string &obsnamecomp, RooArgSet &out);
234 void setScopeObservables(const RooArgList &args);
235 RooAbsArg *getScopeObject(const std::string &name);
236 void setScopeObject(const std::string &key, RooAbsArg *obj);
237 void clearScope();
238
239 bool importJSON(std::string const &filename);
240 bool importYML(std::string const &filename);
241 bool importJSON(std::istream &os);
242 bool importYML(std::istream &os);
243 bool exportJSON(std::string const &fileName);
244 bool exportYML(std::string const &fileName);
245 bool exportJSON(std::ostream &os);
246 bool exportYML(std::ostream &os);
247
248 std::string exportJSONtoString();
249 std::string exportYMLtoString();
250 bool importJSONfromString(const std::string &s);
251 bool importYMLfromString(const std::string &s);
252
253 static void loadFactoryExpressions(const std::string &fname);
254 static void clearFactoryExpressions();
255 static void printFactoryExpressions();
256 static void loadExportKeys(const std::string &fname);
257 static void clearExportKeys();
258 static void printExportKeys();
259
261
265 void importFunction(const RooFit::Experimental::JSONNode &n, bool isPdf);
269
271
272 bool find(const RooFit::Experimental::JSONNode &n, const std::string &elem);
273 void append(RooFit::Experimental::JSONNode &n, const std::string &elem);
274
280
284};
285#endif
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
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:69
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:82
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:64
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:35
The RooDataHist is a container class to hold N-dimensional binned data.
Definition RooDataHist.h:45
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:36
DependencyMissingError(const std::string &p, const std::string &c, const std::string &classname)
virtual const char * what() const noexcept override
virtual std::string const & key() const =0
virtual bool exportObject(RooJSONFactoryWSTool *, const RooAbsArg *, RooFit::Experimental::JSONNode &) const
virtual bool autoExportDependants() const
virtual bool importFunction(RooJSONFactoryWSTool *, const RooFit::Experimental::JSONNode &) const
virtual bool importPdf(RooJSONFactoryWSTool *, const RooFit::Experimental::JSONNode &) const
virtual const char * what() const noexcept override
When using RooFit, statistical models can be conveniently handled and stored as a RooWorkspace.
void exportAttributes(const RooAbsArg *arg, RooFit::Experimental::JSONNode &n)
void exportData(RooAbsData *data, RooFit::Experimental::JSONNode &n)
void append(RooFit::Experimental::JSONNode &n, const std::string &elem)
void importVariables(const RooFit::Experimental::JSONNode &n)
std::map< TClass const *, std::vector< std::unique_ptr< const Exporter > > > ExportMap
bool importYML(std::string const &filename)
RooJSONFactoryWSTool(RooWorkspace &ws)
static void exportHistogram(const TH1 &h, RooFit::Experimental::JSONNode &n, const std::vector< std::string > &obsnames, const TH1 *errH=0, bool writeObservables=true, bool writeErrors=true)
std::map< TClass const *, ExportKeys > ExportKeysMap
T * request(const std::string &objname, const std::string &requestAuthor)
static void loadExportKeys(const std::string &fname)
void importAllNodes(const RooFit::Experimental::JSONNode &n)
void configureVariable(const RooFit::Experimental::JSONNode &p, RooRealVar &v)
bool importJSON(std::string const &filename)
void configureToplevelPdf(const RooFit::Experimental::JSONNode &n, RooAbsPdf &pdf)
void exportVariable(const RooAbsReal *v, RooFit::Experimental::JSONNode &n)
std::map< const std::string, ImportExpression > ImportExpressionMap
static void loadFactoryExpressions(const std::string &fname)
void importVariable(const RooFit::Experimental::JSONNode &n)
RooAbsArg * getScopeObject(const std::string &name)
static ImportMap const & importers()
static int removeExporters(const std::string &needle)
void setScopeObject(const std::string &key, RooAbsArg *obj)
bool find(const RooFit::Experimental::JSONNode &n, const std::string &elem)
bool importYMLfromString(const std::string &s)
const RooFit::Experimental::JSONNode * _rootnode_input
void importPdfs(const RooFit::Experimental::JSONNode &n)
const RooFit::Experimental::JSONNode & irootnode() const
static bool registerExporter(const TClass *key, bool topPriority=true)
static std::string genPrefix(const RooFit::Experimental::JSONNode &p, bool trailing_underscore)
RooRealVar * getWeightVar(const char *name)
static int removeImporters(const std::string &needle)
void exportObject(const RooAbsArg *func, RooFit::Experimental::JSONNode &n)
static ExportMap const & exporters()
static std::string concat(const T *items, const std::string &sep=",")
void exportFunctions(const RooArgSet &allElems, RooFit::Experimental::JSONNode &n)
static std::vector< std::string > names(const T *items)
std::unique_ptr< RooDataSet > unbinned(RooDataHist const &hist)
std::map< std::string, std::unique_ptr< RooAbsData > > loadData(const RooFit::Experimental::JSONNode &n)
static ImportExpressionMap const & pdfImportExpressions()
void exportAllObjects(RooFit::Experimental::JSONNode &n)
std::map< const std::string, std::vector< std::unique_ptr< const Importer > > > ImportMap
static ImportMap & staticImporters()
void exportVariables(const RooArgSet &allElems, RooFit::Experimental::JSONNode &n)
RooFit::Experimental::JSONNode * _rootnode_output
void exportDependants(const RooAbsArg *source, RooFit::Experimental::JSONNode &n)
static std::vector< std::vector< int > > generateBinIndices(const RooArgList &vars)
RooRealVar * createObservable(const std::string &name, const RooJSONFactoryWSTool::Var &var)
static std::map< std::string, RooJSONFactoryWSTool::Var > readObservables(const RooFit::Experimental::JSONNode &n, const std::string &obsnamecomp)
static ExportKeysMap const & exportKeys()
bool exportYML(std::string const &fileName)
static ImportExpressionMap & staticFunctionImportExpressions()
RooFit::Experimental::JSONNode & orootnode()
void importFunctions(const RooFit::Experimental::JSONNode &n)
static ImportExpressionMap const & functionImportExpressions()
std::unique_ptr< RooDataHist > readBinnedData(const RooFit::Experimental::JSONNode &n, const std::string &namecomp, RooArgList observables)
static void writeObservables(const TH1 &h, RooFit::Experimental::JSONNode &n, const std::vector< std::string > &varnames)
bool importJSONfromString(const std::string &s)
static ExportKeysMap & staticExportKeys()
void importDependants(const RooFit::Experimental::JSONNode &n)
static ImportExpressionMap & staticPdfImportExpressions()
static ExportMap & staticExporters()
static void error(const std::string &s)
void getObservables(const RooFit::Experimental::JSONNode &n, const std::string &obsnamecomp, RooArgSet &out)
bool exportJSON(std::string const &fileName)
static bool registerImporter(const std::string &key, bool topPriority=true)
void setScopeObservables(const RooArgList &args)
static void error(const char *s)
void importFunction(const RooFit::Experimental::JSONNode &n, bool isPdf)
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
The RooWorkspace is a persistable container for RooFit projects.
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:80
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
TText * text
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
Definition first.py:1
std::map< std::string, std::string > proxies
std::map< std::string, RooAbsArg * > objects
std::vector< RooAbsArg * > observables
std::vector< double > bounds
void ws()
Definition ws.C:66