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 55 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 | |
| std::string const * | RooFit::HS3::importedArrayComponentForm (RooJSONFactoryWSTool &tool, std::string const &name) |
| void | RooFit::HS3::recordImportedArrayComponent (RooJSONFactoryWSTool &tool, std::string const &name, std::string form) |
| 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 2318 of file RooJSONFactoryWSTool.cxx.
|
default |
|
static |
Definition at line 2366 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 2333 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 4295 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 4342 of file RooJSONFactoryWSTool.cxx.
| std::string const * RooJSONFactoryWSTool::constraintObservable | ( | std::string const & | pdfName | ) | const |
Definition at line 2776 of file RooJSONFactoryWSTool.cxx.
|
static |
Write an error to the RooFit message service, and throw it as a std::runtime_error.
Definition at line 4260 of file RooJSONFactoryWSTool.cxx.
|
inlinestatic |
Definition at line 194 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 3776 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 3025 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2748 of file RooJSONFactoryWSTool.cxx.
| std::string RooJSONFactoryWSTool::exportAsArrayComponent | ( | RooAbsPdf const & | pdf, |
| std::string const & | arrayName ) |
Definition at line 2719 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 3121 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2796 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 3184 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 3018 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 3976 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 3986 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 3968 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 3375 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2592 of file RooJSONFactoryWSTool.cxx.
|
inlineprivate |
Definition at line 279 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 2714 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 2863 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 3503 of file RooJSONFactoryWSTool.cxx.
| std::string RooJSONFactoryWSTool::exportTransformed | ( | const RooAbsReal * | original, |
| const std::string & | suffix, | ||
| const std::string & | formula ) |
Definition at line 2498 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2468 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 2350 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 2373 of file RooJSONFactoryWSTool.cxx.
| std::string RooJSONFactoryWSTool::getStringAttribute | ( | const std::string & | obj, |
| const std::string & | attrib ) |
Definition at line 4010 of file RooJSONFactoryWSTool.cxx.
| bool RooJSONFactoryWSTool::hasAttribute | ( | const std::string & | obj, |
| const std::string & | attrib ) |
Definition at line 3994 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 2345 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 4028 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 3363 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2988 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 2927 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 3011 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 4170 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 4178 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::importJSONElement | ( | const std::string & | name, |
| const std::string & | jsonString ) |
Definition at line 4186 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 3961 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 3326 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::importVariableElement | ( | const RooFit::Detail::JSONNode & | elementNode | ) |
Definition at line 4221 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 2320 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 2387 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 2409 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 3034 of file RooJSONFactoryWSTool.cxx.
|
inline |
Definition at line 236 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 237 of file RooJSONFactoryWSTool.h.
|
static |
Create the variables for the "axes" of topNode, which are owned by the returned set.
Definition at line 3291 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 3302 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2913 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::recordConstraintTerms | ( | std::string const & | pdfName, |
| std::vector< std::string > const & | terms ) |
Definition at line 2785 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 123 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 139 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 173 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 167 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 149 of file RooJSONFactoryWSTool.h.
|
private |
|
private |
Definition at line 2417 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2429 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 2448 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 4268 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 4349 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::setAttribute | ( | const std::string & | obj, |
| const std::string & | attrib ) |
Definition at line 4005 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 2771 of file RooJSONFactoryWSTool.cxx.
| void RooJSONFactoryWSTool::setStringAttribute | ( | const std::string & | obj, |
| const std::string & | attrib, | ||
| const std::string & | value ) |
Definition at line 4021 of file RooJSONFactoryWSTool.cxx.
Definition at line 2393 of file RooJSONFactoryWSTool.cxx.
|
inline |
Like request(), but returns nullptr if neither the workspace nor the document has the object.
Definition at line 133 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 3353 of file RooJSONFactoryWSTool.cxx.
|
static |
Write a warning to the RooFit message service, and return its stream.
Definition at line 4254 of file RooJSONFactoryWSTool.cxx.
|
inline |
Definition at line 178 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 188 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 181 of file RooJSONFactoryWSTool.h.
|
friend |
|
friend |
|
friend |
|
private |
Definition at line 332 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 326 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 325 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 338 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 333 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 334 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 328 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 327 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 322 of file RooJSONFactoryWSTool.h.
|
staticconstexpr |
Named objects are always stored in lists, as the HS3 standard requires. Kept for backwards compatibility.
Definition at line 58 of file RooJSONFactoryWSTool.h.