When using RooFit, statistical models can be conveniently handled and stored as a RooWorkspace.
However, for the sake of interoperability with other statistical frameworks, and also ease of manipulation, it may be useful to store statistical models in text form.
The RooJSONFactoryWSTool is a helper class to achieve exactly this, exporting to and importing from JSON.
In order to import a workspace from a JSON file, you can do
Similarly, in order to export a workspace to a JSON file, you can do
Analogously, in C++, you can do
and
For more details, consult the tutorial rf515_hfJSON.
The RooJSONFactoryWSTool only knows about a limited set of classes for import and export. If import or export of a class you're interested in fails, you might need to add your own importer or exporter. Please consult the relevant section in the RooFit Developer Documentation to learn how to do that (Implement JSON I/O for a RooFit class with RooFit HS3).
You can always get a list of all the available importers and exporters by calling the following functions:
Definition at line 53 of file RooJSONFactoryWSTool.h.
Classes | |
| struct | Config |
| class | DependencyMissingError |
Public Member Functions | |
| RooJSONFactoryWSTool (RooWorkspace &ws) | |
| ~RooJSONFactoryWSTool () | |
| std::vector< RooFit::HS3::DistributionRef > const * | channelConstraints (std::string const &dist) const |
The constraint terms that the document that is currently being imported adds to the HistFactory distribution dist, besides those that its modifiers name, or nullptr if there are none. | |
| std::string const * | constraintObservable (std::string const &pdfName) const |
| std::string | exportAsArrayComponent (RooAbsPdf const &pdf, std::string const &arrayName) |
| bool | exportJSON (std::ostream &os) |
| Export the workspace as an HS3 document, and write it as JSON to the stream. Returns true; errors are exceptions. | |
| bool | exportJSON (std::string const &fileName) |
| Export the workspace as an HS3 document, and write it to a JSON file. Returns true; errors are exceptions. | |
| std::string | exportJSONtoString () |
| Export the workspace as an HS3 document, and return it as a JSON string. | |
| std::string const & | exportRootName () const |
| The name of the top-level object whose export is in progress (the root of the current export recursion). | |
| std::string | exportTransformed (const RooAbsReal *original, const std::string &suffix, const std::string &formula) |
| std::string | getStringAttribute (const std::string &obj, const std::string &attrib) |
| bool | hasAttribute (const std::string &obj, const std::string &attrib) |
| RooFit::HS3::Version | hs3Version () const |
| The HS3 version of the document that is currently being imported. | |
| void | importFunction (const RooFit::Detail::JSONNode &p, bool importAllDependants) |
Import the function or distribution p into the workspace, with the importers that are registered for its type. | |
| void | importFunction (const std::string &jsonString, bool importAllDependants) |
| Import a function or distribution that is given as a JSON string, see the overload for a JSONNode. | |
| bool | importJSON (std::istream &is) |
| Import an HS3 document that is read as JSON from the stream into the workspace. Returns true; errors are exceptions. | |
| bool | importJSON (std::string const &filename) |
| Import the HS3 document of a JSON file into the workspace. Returns true; errors are exceptions. | |
| void | importJSONElement (const std::string &name, const std::string &jsonString) |
| bool | importJSONfromString (const std::string &s) |
| Import an HS3 document that is given as a JSON string into the workspace. Returns true; errors are exceptions. | |
| void | importVariableElement (const RooFit::Detail::JSONNode &elementNode) |
| bool | isConstraintReadAsChannel (RooAbsPdf const &pdf) const |
Whether the constraint term pdf contributes to the likelihoods of the document that is currently being imported as a component of a combined distribution with data, by itself or as a component of an array distribution. | |
| void | queueExport (RooAbsArg const &arg) |
| void | queueExportTemporary (RooAbsArg *arg) |
| void | recordConstraintTerms (std::string const &pdfName, std::vector< std::string > const &terms) |
| template<class T > | |
| T * | request (const std::string &objname, const std::string &requestAuthor) |
The object objname from the workspace, imported from the document first if needed. | |
| template<class T > | |
| T * | requestArg (const RooFit::Detail::JSONNode &node, const std::string &key) |
| template<class T > | |
| RooArgList | requestArgList (const RooFit::Detail::JSONNode &node, const std::string &seqName) |
| template<class T > | |
| RooArgSet | requestArgSet (const RooFit::Detail::JSONNode &node, const std::string &seqName) |
| template<class T , class Coll_t > | |
| Coll_t | requestCollection (const RooFit::Detail::JSONNode &node, const std::string &seqName) |
| void | setAttribute (const std::string &obj, const std::string &attrib) |
| void | setConstraintObservable (RooAbsPdf const &pdf, std::string const &observable) |
Record that observable is the observable of the constraint term pdf (the side that is not the constrained parameter). | |
| void | setStringAttribute (const std::string &obj, const std::string &attrib, const std::string &value) |
| template<class T > | |
| T * | tryRequest (const std::string &objname) |
| Like request(), but returns nullptr if neither the workspace nor the document has the object. | |
| RooWorkspace * | workspace () |
| template<class Obj_t , typename... Args_t> | |
| Obj_t & | wsEmplace (RooStringView name, Args_t &&...args) |
| template<class Obj_t > | |
| Obj_t & | wsImport (Obj_t const &obj) |
Static Public Member Functions | |
| static RooFit::Detail::JSONNode & | appendNamedChild (RooFit::Detail::JSONNode &node, std::string const &name) |
| static RooWorkspace | cleanWS (const RooWorkspace &ws, bool onlyModelConfig=false) |
| static Config & | config () |
| static void | error (const char *s) |
| Write an error to the RooFit message service, and throw it as a std::runtime_error. | |
| static void | error (const std::string &s) |
| static void | exportArray (std::size_t n, double const *contents, RooFit::Detail::JSONNode &output) |
Write n numbers as an array, each as an integer if it has no fractional part. | |
| static void | exportHisto (RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output) |
Write the axes and the n bin contents of a histogram over the variables vars, which are RooRealVars or RooCategories, in the order of the variables. | |
| static void | fillSeq (RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax=-1) |
| static RooFit::Detail::JSONNode const * | findNamedChild (RooFit::Detail::JSONNode const &node, std::string const &name) |
| static bool | isValidName (const std::string &str) |
| Whether a string is a valid name: a letter or an underscore, followed by letters, digits or underscores. | |
| static std::string | name (const RooFit::Detail::JSONNode &n) |
| static RooArgSet | readAxes (const RooFit::Detail::JSONNode &node) |
Create the variables for the "axes" of topNode, which are owned by the returned set. | |
| static std::unique_ptr< RooDataHist > | readBinnedData (const RooFit::Detail::JSONNode &n, const std::string &namecomp, RooArgSet const &vars) |
Create a RooDataHist with the name name over the variables vars from the "contents" of n, and its "errors" if it has any. | |
| static std::string | sanitizeName (const std::string str) |
| Replace some characters that are common in the names of RooFit objects, but that the HS3 standard doesn't allow in names, if config().allowSanitizeNames is set. | |
| static RooWorkspace | sanitizeWS (const RooWorkspace &ws) |
| static bool | testValidName (const std::string &name, bool forceError) |
| static std::ostream & | warning (const std::string &s) |
| Write a warning to the RooFit message service, and return its stream. | |
Static Public Attributes | |
| static constexpr bool | useListsInsteadOfDicts = true |
| Named objects are always stored in lists, as the HS3 standard requires. Kept for backwards compatibility. | |
Private Member Functions | |
| void | exportAllObjects (RooFit::Detail::JSONNode &n) |
| void | exportArrayDists () |
| void | exportCombinedData (RooAbsData const &data) |
| Export a data set with category observables as a "combined" data set. | |
| bool | exportConstraintData (std::string const &term, RooArgSet const *globalObservables, std::string const &dataName) |
| void | exportData (RooAbsData const &data) |
| void | exportModelConfig (RooStats::ModelConfig const &mc, const std::vector< RooAbsData * > &singleDataSets) |
| void | exportObject (RooAbsArg const &func) |
| template<class T > | |
| void | exportObjects (T const &args) |
| void | exportSimultaneous (RooSimultaneous const &simPdf) |
| Export a RooSimultaneous as a "combined_dist". | |
| void | exportSingleModelConfig (RooStats::ModelConfig const &mc, std::string const &analysisName, std::string const *dataName) |
| void | exportVariable (const RooAbsArg *v, RooFit::HS3::ParameterPoint &point) |
| void | importAllNodes (RooFit::Detail::JSONNode &n) |
| void | importDependants (const RooFit::Detail::JSONNode &n) |
| void | importDistribution (RooFit::HS3::Distribution const &distribution) |
| void | importVariable (RooFit::HS3::ParameterValue const &p) |
| void | prepareCombinedObservableRenaming () |
| void | recordCategory (RooAbsCategory const &cat) |
| template<class T > | |
| T * | requestImpl (const std::string &objname) |
| template<> | |
| RooRealVar * | requestImpl (const std::string &objname) |
| template<> | |
| RooAbsPdf * | requestImpl (const std::string &objname) |
| template<> | |
| RooAbsReal * | requestImpl (const std::string &objname) |
| void | warnIfFreeWithoutDomain (RooRealVar const &var) |
Private Attributes | |
| std::map< std::string, std::map< std::string, std::map< std::string, std::string > > > | _combinedObservableRenames |
| std::unique_ptr< RooFit::HS3::ToolExportState > | _export |
| std::unique_ptr< RooFit::HS3::ToolImportState > | _import |
| std::vector< std::string > | _importStack |
| std::map< std::string, std::map< std::string, RooAbsPdf const * > > | _renamedComponents |
| std::vector< std::unique_ptr< RooAbsArg > > | _renamedObjects |
| std::vector< RooAbsArg const * > | _serversToDelete |
| std::vector< RooAbsArg const * > | _serversToExport |
| RooWorkspace & | _workspace |
Friends | |
| void | RooFit::HS3::setExportedObject (RooJSONFactoryWSTool &tool, RooFit::HS3::HistFactoryDist dist, std::vector< RooFit::HS3::DistributionRef > channelConstraints) |
#include <RooFitHS3/RooJSONFactoryWSTool.h>
| RooJSONFactoryWSTool::RooJSONFactoryWSTool | ( | RooWorkspace & | ws | ) |
Definition at line 1825 of file RooJSONFactoryWSTool.cxx.
|
default |
|
static |
Definition at line 1873 of file RooJSONFactoryWSTool.cxx.
| std::vector< HS3::DistributionRef > const * RooJSONFactoryWSTool::channelConstraints | ( | std::string const & | dist | ) | const |
The constraint terms that the document that is currently being imported adds to the HistFactory distribution dist, besides those that its modifiers name, or nullptr if there are none.
Definition at line 1840 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 3776 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 3823 of file RooJSONFactoryWSTool.cxx.
| std::string const * RooJSONFactoryWSTool::constraintObservable | ( | std::string const & | pdfName | ) | const |
Definition at line 2271 of file RooJSONFactoryWSTool.cxx.
|
static |
Write an error to the RooFit message service, and throw it as a std::runtime_error.
Definition at line 3741 of file RooJSONFactoryWSTool.cxx.
|
inlinestatic |
Definition at line 192 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 3257 of file RooJSONFactoryWSTool.cxx.
|
static |
Write n numbers as an array, each as an integer if it has no fractional part.
RooFit doesn't use this function itself, but exporters outside of ROOT do.
Definition at line 2520 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2243 of file RooJSONFactoryWSTool.cxx.
| std::string RooJSONFactoryWSTool::exportAsArrayComponent | ( | RooAbsPdf const & | pdf, |
| std::string const & | arrayName ) |
Definition at line 2214 of file RooJSONFactoryWSTool.cxx.
|
private |
Export a data set with category observables as a "combined" data set.
The categories are the columns, sorted by name, and are declared as categorical axes of the default domain. There is one sub-data set per key, i.e. per combination of states, written like any other data set and named after the data set and the labels of the key. A key is written also without entries: RooFit treats a channel without entries as present and empty. If the workspace has a RooSimultaneous with the same columns, each sub-data set only carries the observables of the corresponding component, and keys without entries for which it has no component are left out; otherwise, every sub-data set carries all observables.
Definition at line 2616 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2291 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2679 of file RooJSONFactoryWSTool.cxx.
|
static |
Write the axes and the n bin contents of a histogram over the variables vars, which are RooRealVars or RooCategories, in the order of the variables.
Definition at line 2513 of file RooJSONFactoryWSTool.cxx.
| bool RooJSONFactoryWSTool::exportJSON | ( | std::ostream & | os | ) |
Export the workspace as an HS3 document, and write it as JSON to the stream. Returns true; errors are exceptions.
Definition at line 3457 of file RooJSONFactoryWSTool.cxx.
| bool RooJSONFactoryWSTool::exportJSON | ( | std::string const & | fileName | ) |
Export the workspace as an HS3 document, and write it to a JSON file. Returns true; errors are exceptions.
Definition at line 3467 of file RooJSONFactoryWSTool.cxx.
| std::string RooJSONFactoryWSTool::exportJSONtoString | ( | ) |
Export the workspace as an HS3 document, and return it as a JSON string.
Definition at line 3449 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2870 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2098 of file RooJSONFactoryWSTool.cxx.
|
inlineprivate |
Definition at line 277 of file RooJSONFactoryWSTool.h.
| std::string const & RooJSONFactoryWSTool::exportRootName | ( | ) | const |
The name of the top-level object whose export is in progress (the root of the current export recursion).
Definition at line 2209 of file RooJSONFactoryWSTool.cxx.
|
private |
Export a RooSimultaneous as a "combined_dist".
The components are selected by a key with one label per column. The columns are the index category of the RooSimultaneous or, for a RooSuperCategory index, its input categories, sorted by name. They are declared as categorical axes of the default domain. States without a pdf are not written: HS3 does not require a component for every key.
Definition at line 2358 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2998 of file RooJSONFactoryWSTool.cxx.
| std::string RooJSONFactoryWSTool::exportTransformed | ( | const RooAbsReal * | original, |
| const std::string & | suffix, | ||
| const std::string & | formula ) |
Definition at line 2005 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 1975 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 1857 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 1880 of file RooJSONFactoryWSTool.cxx.
| std::string RooJSONFactoryWSTool::getStringAttribute | ( | const std::string & | obj, |
| const std::string & | attrib ) |
Definition at line 3491 of file RooJSONFactoryWSTool.cxx.
| bool RooJSONFactoryWSTool::hasAttribute | ( | const std::string & | obj, |
| const std::string & | attrib ) |
Definition at line 3475 of file RooJSONFactoryWSTool.cxx.
| HS3::Version RooJSONFactoryWSTool::hs3Version | ( | ) | const |
The HS3 version of the document that is currently being imported.
Outside of an import it is 0.0.0, like a version that can't be parsed.
Definition at line 1852 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 3509 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2858 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2483 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::importFunction | ( | const RooFit::Detail::JSONNode & | p, |
| bool | importAllDependants ) |
Import the function or distribution p into the workspace, with the importers that are registered for its type.
If importAllDependants is false, p is a fragment of a document, whose own "functions" and "distributions" are imported first.
Definition at line 2422 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::importFunction | ( | const std::string & | jsonString, |
| bool | importAllDependants ) |
Import a function or distribution that is given as a JSON string, see the overload for a JSONNode.
Definition at line 2506 of file RooJSONFactoryWSTool.cxx.
| bool RooJSONFactoryWSTool::importJSON | ( | std::istream & | is | ) |
Import an HS3 document that is read as JSON from the stream into the workspace. Returns true; errors are exceptions.
Definition at line 3651 of file RooJSONFactoryWSTool.cxx.
| bool RooJSONFactoryWSTool::importJSON | ( | std::string const & | filename | ) |
Import the HS3 document of a JSON file into the workspace. Returns true; errors are exceptions.
Definition at line 3659 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::importJSONElement | ( | const std::string & | name, |
| const std::string & | jsonString ) |
Definition at line 3667 of file RooJSONFactoryWSTool.cxx.
| bool RooJSONFactoryWSTool::importJSONfromString | ( | const std::string & | s | ) |
Import an HS3 document that is given as a JSON string into the workspace. Returns true; errors are exceptions.
Definition at line 3442 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2821 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::importVariableElement | ( | const RooFit::Detail::JSONNode & | elementNode | ) |
Definition at line 3702 of file RooJSONFactoryWSTool.cxx.
Whether the constraint term pdf contributes to the likelihoods of the document that is currently being imported as a component of a combined distribution with data, by itself or as a component of an array distribution.
It is then a measurement of its own, and must not be multiplied into the distributions that name it as a constraint as well.
Definition at line 1827 of file RooJSONFactoryWSTool.cxx.
|
static |
Whether a string is a valid name: a letter or an underscore, followed by letters, digits or underscores.
Only characters from the ASCII character set are allowed.
Definition at line 1894 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 1916 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2529 of file RooJSONFactoryWSTool.cxx.
|
inline |
Definition at line 234 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 235 of file RooJSONFactoryWSTool.h.
|
static |
Create the variables for the "axes" of topNode, which are owned by the returned set.
Definition at line 2786 of file RooJSONFactoryWSTool.cxx.
|
static |
Create a RooDataHist with the name name over the variables vars from the "contents" of n, and its "errors" if it has any.
Definition at line 2797 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2408 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::recordConstraintTerms | ( | std::string const & | pdfName, |
| std::vector< std::string > const & | terms ) |
Definition at line 2280 of file RooJSONFactoryWSTool.cxx.
|
inline |
The object objname from the workspace, imported from the document first if needed.
Throws a DependencyMissingError if neither has it.
Definition at line 121 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 137 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 171 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 165 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 147 of file RooJSONFactoryWSTool.h.
|
private |
|
private |
Definition at line 1924 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 1936 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 1955 of file RooJSONFactoryWSTool.cxx.
|
static |
Replace some characters that are common in the names of RooFit objects, but that the HS3 standard doesn't allow in names, if config().allowSanitizeNames is set.
Definition at line 3749 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 3830 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::setAttribute | ( | const std::string & | obj, |
| const std::string & | attrib ) |
Definition at line 3486 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::setConstraintObservable | ( | RooAbsPdf const & | pdf, |
| std::string const & | observable ) |
Record that observable is the observable of the constraint term pdf (the side that is not the constrained parameter).
A Gaussian whose mean it is gets written with x and mean swapped, so that the observable is x.
Definition at line 2266 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::setStringAttribute | ( | const std::string & | obj, |
| const std::string & | attrib, | ||
| const std::string & | value ) |
Definition at line 3502 of file RooJSONFactoryWSTool.cxx.
Definition at line 1900 of file RooJSONFactoryWSTool.cxx.
|
inline |
Like request(), but returns nullptr if neither the workspace nor the document has the object.
Definition at line 131 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 2848 of file RooJSONFactoryWSTool.cxx.
|
static |
Write a warning to the RooFit message service, and return its stream.
Definition at line 3735 of file RooJSONFactoryWSTool.cxx.
|
inline |
Definition at line 176 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 186 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 179 of file RooJSONFactoryWSTool.h.
|
friend |
|
private |
Definition at line 326 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 320 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 319 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 332 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 327 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 328 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 322 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 321 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 316 of file RooJSONFactoryWSTool.h.
|
staticconstexpr |
Named objects are always stored in lists, as the HS3 standard requires. Kept for backwards compatibility.
Definition at line 56 of file RooJSONFactoryWSTool.h.