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 and YML.
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.
In order to import and export YML files, ROOT
needs to be compiled with the external dependency RapidYAML, which needs to be installed on your system when building ROOT
.
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:
Alternatively, you can generate a LaTeX version of the available importers and exporters by calling
Definition at line 38 of file RooJSONFactoryWSTool.h.
Classes | |
struct | CombinedData |
class | DependencyMissingError |
Public Member Functions | |
RooJSONFactoryWSTool (RooWorkspace &ws) | |
~RooJSONFactoryWSTool () | |
void | exportCategory (RooAbsCategory const &cat, RooFit::Detail::JSONNode &node) |
Export a RooAbsCategory object to a JSONNode. | |
bool | exportJSON (std::ostream &os) |
Export the workspace to JSON format and write to the output stream. | |
bool | exportJSON (std::string const &fileName) |
Export the workspace to JSON format and write to the specified file. | |
std::string | exportJSONtoString () |
Export the workspace to a JSON string. | |
std::string | exportTransformed (const RooAbsReal *original, const std::string &suffix, const std::string &formula) |
bool | exportYML (std::ostream &os) |
Export the workspace to YML format and write to the output stream. | |
bool | exportYML (std::string const &fileName) |
Export the workspace to YML format and write to the specified file. | |
std::string | exportYMLtoString () |
Export the workspace to a YML string. | |
std::string | getStringAttribute (const std::string &obj, const std::string &attrib) |
bool | hasAttribute (const std::string &obj, const std::string &attrib) |
void | importFunction (const RooFit::Detail::JSONNode &n, bool importAllDependants) |
Import a function from the JSONNode into the workspace. | |
void | importFunction (const std::string &jsonString, bool importAllDependants) |
Import a function from a JSON string into the workspace. | |
bool | importJSON (std::istream &os) |
Imports a JSON file from the given input stream to the workspace. | |
bool | importJSON (std::string const &filename) |
Imports a JSON file from the given filename to the workspace. | |
void | importJSONElement (const std::string &name, const std::string &jsonString) |
bool | importJSONfromString (const std::string &s) |
Import the workspace from a JSON string. | |
void | importVariableElement (const RooFit::Detail::JSONNode &n) |
bool | importYML (std::istream &os) |
Imports a YML file from the given input stream to the workspace. | |
bool | importYML (std::string const &filename) |
Imports a YML file from the given filename to the workspace. | |
bool | importYMLfromString (const std::string &s) |
Import the workspace from a YML string. | |
void | queueExport (RooAbsArg const &arg) |
template<class T > | |
T * | request (const std::string &objname, const std::string &requestAuthor) |
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 | setStringAttribute (const std::string &obj, const std::string &attrib, const std::string &value) |
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 std::unique_ptr< RooFit::Detail::JSONTree > | createNewJSONTree () |
Create a new JSON tree with version information. | |
static void | error (const char *s) |
Writes an error message to the RooFit message service and throws a runtime_error. | |
static void | error (const std::string &s) |
static void | exportArray (std::size_t n, double const *contents, RooFit::Detail::JSONNode &output) |
Export an array of doubles to a JSONNode. | |
static void | exportHisto (RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output) |
Export histogram data to a JSONNode. | |
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) |
template<typename... Keys_t> | |
static RooFit::Detail::JSONNode & | getRooFitInternal (RooFit::Detail::JSONNode &node, Keys_t const &...keys) |
static RooFit::Detail::JSONNode & | makeVariablesNode (RooFit::Detail::JSONNode &rootNode) |
static std::string | name (const RooFit::Detail::JSONNode &n) |
static RooArgSet | readAxes (const RooFit::Detail::JSONNode &node) |
Read axes from the JSONNode and create a RooArgSet representing them. | |
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 std::ostream & | warning (const std::string &s) |
Writes a warning message to the RooFit message service. | |
Static Public Attributes | |
static constexpr bool | useListsInsteadOfDicts = true |
Private Member Functions | |
void | exportAllObjects (RooFit::Detail::JSONNode &n) |
Export all objects in the workspace to a JSONNode. | |
RooJSONFactoryWSTool::CombinedData | exportCombinedData (RooAbsData const &data) |
Export combined data from the workspace to a custom struct. | |
void | exportData (RooAbsData const &data) |
Export data from the workspace to a JSONNode. | |
void | exportModelConfig (RooFit::Detail::JSONNode &rootnode, RooStats::ModelConfig const &mc, const std::vector< RooJSONFactoryWSTool::CombinedData > &d) |
void | exportObject (RooAbsArg const &func, std::set< std::string > &exportedObjectNames) |
Export an object from the workspace to a JSONNode. | |
template<class T > | |
void | exportObjects (T const &args, std::set< std::string > &exportedObjectNames) |
void | exportSingleModelConfig (RooFit::Detail::JSONNode &rootnode, RooStats::ModelConfig const &mc, std::string const &analysisName, std::map< std::string, std::string > const *dataComponents) |
void | exportVariable (const RooAbsArg *v, RooFit::Detail::JSONNode &n) |
Export a variable from the workspace to a JSONNode. | |
void | exportVariables (const RooArgSet &allElems, RooFit::Detail::JSONNode &n) |
Export variables from the workspace to a JSONNode. | |
void | importAllNodes (const RooFit::Detail::JSONNode &n) |
Imports all nodes of the JSON data and adds them to the workspace. | |
void | importDependants (const RooFit::Detail::JSONNode &n) |
Import all dependants (servers) of a node into the workspace. | |
void | importVariable (const RooFit::Detail::JSONNode &n) |
Import a variable from the JSONNode into the workspace. | |
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) |
Private Attributes | |
const RooFit::Detail::JSONNode * | _attributesNode = nullptr |
std::unique_ptr< RooFit::JSONIO::Detail::Domains > | _domains |
const RooFit::Detail::JSONNode * | _rootnodeInput = nullptr |
RooFit::Detail::JSONNode * | _rootnodeOutput = nullptr |
std::vector< RooAbsArg const * > | _serversToExport |
RooFit::Detail::JSONNode * | _varsNode = nullptr |
RooWorkspace & | _workspace |
#include <RooFitHS3/RooJSONFactoryWSTool.h>
RooJSONFactoryWSTool::RooJSONFactoryWSTool | ( | RooWorkspace & | ws | ) |
Definition at line 825 of file RooJSONFactoryWSTool.cxx.
RooJSONFactoryWSTool::~RooJSONFactoryWSTool | ( | ) |
Definition at line 827 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 849 of file RooJSONFactoryWSTool.cxx.
|
static |
Create a new JSON tree with version information.
Definition at line 1951 of file RooJSONFactoryWSTool.cxx.
|
static |
Writes an error message to the RooFit message service and throws a runtime_error.
s | The error message to be logged and thrown. |
Definition at line 2323 of file RooJSONFactoryWSTool.cxx.
|
inlinestatic |
Definition at line 123 of file RooJSONFactoryWSTool.h.
|
private |
Export all objects in the workspace to a JSONNode.
This function exports all the objects in the workspace to the provided JSONNode. The objects' information is added as key-value pairs to the JSONNode.
n | The JSONNode to which the objects will be exported. |
Definition at line 1815 of file RooJSONFactoryWSTool.cxx.
|
static |
Export an array of doubles to a JSONNode.
This function exports an array of doubles, represented by the provided size and contents, to a JSONNode. The array elements are added to the JSONNode as a sequence of values.
n | The size of the array. |
contents | A pointer to the array containing the double values. |
output | The JSONNode to which the array will be exported. |
Definition at line 1331 of file RooJSONFactoryWSTool.cxx.
void RooJSONFactoryWSTool::exportCategory | ( | RooAbsCategory const & | cat, |
RooFit::Detail::JSONNode & | node | ||
) |
Export a RooAbsCategory object to a JSONNode.
This function exports a RooAbsCategory object, represented by the provided categories and indices, to a JSONNode. The category labels and corresponding indices are added to the JSONNode as key-value pairs.
cat | The RooAbsCategory object to be exported. |
node | The JSONNode to which the category data will be exported. |
Definition at line 1355 of file RooJSONFactoryWSTool.cxx.
|
private |
Export combined data from the workspace to a custom struct.
This function exports combined data from the workspace, represented by the provided RooAbsData object, to a CombinedData struct. The struct contains information such as variables, categories, and bin contents of the combined data.
data | The RooAbsData object representing the combined data to be exported. |
Definition at line 1388 of file RooJSONFactoryWSTool.cxx.
|
private |
Export data from the workspace to a JSONNode.
This function exports data represented by the provided RooAbsData object, to a JSONNode. The data's information is added as key-value pairs to the JSONNode.
data | The RooAbsData object representing the data to be exported. |
Definition at line 1463 of file RooJSONFactoryWSTool.cxx.
|
static |
Export histogram data to a JSONNode.
This function exports histogram data, represented by the provided variables and contents, to a JSONNode. The histogram's axes information and bin contents are added as key-value pairs to the JSONNode.
vars | The RooArgSet representing the variables associated with the histogram. |
n | The number of bins in the histogram. |
contents | A pointer to the array containing the bin contents of the histogram. |
output | The JSONNode to which the histogram data will be exported. |
Definition at line 1294 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::exportJSON | ( | std::ostream & | os | ) |
Export the workspace to JSON format and write to the output stream.
os | The output stream to write the JSON data to. |
Definition at line 1978 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::exportJSON | ( | std::string const & | filename | ) |
Export the workspace to JSON format and write to the specified file.
filename | The name of the JSON file to create and write the data to. |
Definition at line 1993 of file RooJSONFactoryWSTool.cxx.
std::string RooJSONFactoryWSTool::exportJSONtoString | ( | ) |
Export the workspace to a JSON string.
Definition at line 1927 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 1710 of file RooJSONFactoryWSTool.cxx.
|
private |
Export an object from the workspace to a JSONNode.
This function exports an object, represented by the provided RooAbsArg, from the workspace to a JSONNode. The object's information is added to the JSONNode as key-value pairs.
func | The RooAbsArg representing the object to be exported. |
exportedObjectNames | A set of strings containing names of previously exported objects to avoid duplicates. This set is updated with the name of the newly exported object. |
Definition at line 1021 of file RooJSONFactoryWSTool.cxx.
|
inlineprivate |
Definition at line 199 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 1732 of file RooJSONFactoryWSTool.cxx.
std::string RooJSONFactoryWSTool::exportTransformed | ( | const RooAbsReal * | original, |
const std::string & | suffix, | ||
const std::string & | formula | ||
) |
Definition at line 999 of file RooJSONFactoryWSTool.cxx.
|
private |
Export a variable from the workspace to a JSONNode.
This function exports a variable, represented by the provided RooAbsArg pointer 'v', from the workspace to a JSONNode. The variable's information is added to the JSONNode as key-value pairs.
v | The pointer to the RooAbsArg representing the variable to be exported. |
node | The JSONNode to which the variable will be exported. |
Definition at line 947 of file RooJSONFactoryWSTool.cxx.
|
private |
Export variables from the workspace to a JSONNode.
This function exports variables, represented by the provided RooArgSet, from the workspace to a JSONNode. The variables' information is added to the JSONNode as key-value pairs.
allElems | The RooArgSet representing the variables to be exported. |
n | The JSONNode to which the variables will be exported. |
Definition at line 991 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::exportYML | ( | std::ostream & | os | ) |
Export the workspace to YML format and write to the output stream.
os | The output stream to write the YML data to. |
Definition at line 2011 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::exportYML | ( | std::string const & | filename | ) |
Export the workspace to YML format and write to the specified file.
filename | The name of the YML file to create and write the data to. |
Definition at line 2026 of file RooJSONFactoryWSTool.cxx.
std::string RooJSONFactoryWSTool::exportYMLtoString | ( | ) |
Export the workspace to a YML string.
Definition at line 1939 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 829 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 859 of file RooJSONFactoryWSTool.cxx.
|
inlinestatic |
Definition at line 170 of file RooJSONFactoryWSTool.h.
std::string RooJSONFactoryWSTool::getStringAttribute | ( | const std::string & | obj, |
const std::string & | attrib | ||
) |
Definition at line 2060 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::hasAttribute | ( | const std::string & | obj, |
const std::string & | attrib | ||
) |
Definition at line 2038 of file RooJSONFactoryWSTool.cxx.
|
private |
Imports all nodes of the JSON data and adds them to the workspace.
n | The JSONNode representing the root node of the JSON data. |
Definition at line 2088 of file RooJSONFactoryWSTool.cxx.
|
private |
Import all dependants (servers) of a node into the workspace.
This function imports all the dependants (servers) of the given JSONNode into the workspace. The dependants' information is read from the JSONNode and added to the workspace.
n | The JSONNode representing the node whose dependants are to be imported. |
Definition at line 1690 of file RooJSONFactoryWSTool.cxx.
void RooJSONFactoryWSTool::importFunction | ( | const RooFit::Detail::JSONNode & | n, |
bool | importAllDependants | ||
) |
Import a function from the JSONNode into the workspace.
This function imports a function from the given JSONNode into the workspace. The function's information is read from the JSONNode and added to the workspace.
p | The JSONNode representing the function to be imported. |
importAllDependants | A boolean flag indicating whether to import all dependants (servers) of the function. |
Definition at line 1168 of file RooJSONFactoryWSTool.cxx.
void RooJSONFactoryWSTool::importFunction | ( | const std::string & | jsonString, |
bool | importAllDependants | ||
) |
Import a function from a JSON string into the workspace.
This function imports a function from the provided JSON string into the workspace. The function's information is read from the JSON string and added to the workspace.
jsonString | The JSON string containing the function information. |
importAllDependants | A boolean flag indicating whether to import all dependants (servers) of the function. |
Definition at line 1277 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::importJSON | ( | std::istream & | is | ) |
Imports a JSON file from the given input stream to the workspace.
is | The input stream containing the JSON data. |
Definition at line 2187 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::importJSON | ( | std::string const & | filename | ) |
Imports a JSON file from the given filename to the workspace.
filename | The name of the JSON file to import. |
Definition at line 2204 of file RooJSONFactoryWSTool.cxx.
void RooJSONFactoryWSTool::importJSONElement | ( | const std::string & | name, |
const std::string & | jsonString | ||
) |
Definition at line 2250 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::importJSONfromString | ( | const std::string & | s | ) |
Import the workspace from a JSON string.
s | The JSON string containing the workspace data. |
Definition at line 1904 of file RooJSONFactoryWSTool.cxx.
|
private |
Import a variable from the JSONNode into the workspace.
This function imports a variable from the given JSONNode into the workspace. The variable's information is read from the JSONNode and added to the workspace.
p | The JSONNode representing the variable to be imported. |
Definition at line 1650 of file RooJSONFactoryWSTool.cxx.
void RooJSONFactoryWSTool::importVariableElement | ( | const RooFit::Detail::JSONNode & | n | ) |
Definition at line 2268 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::importYML | ( | std::istream & | is | ) |
Imports a YML file from the given input stream to the workspace.
is | The input stream containing the YML data. |
Definition at line 2223 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::importYML | ( | std::string const & | filename | ) |
Imports a YML file from the given filename to the workspace.
filename | The name of the YML file to import. |
Definition at line 2237 of file RooJSONFactoryWSTool.cxx.
bool RooJSONFactoryWSTool::importYMLfromString | ( | const std::string & | s | ) |
Import the workspace from a YML string.
s | The YML string containing the workspace data. |
Definition at line 1916 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 881 of file RooJSONFactoryWSTool.cxx.
|
static |
Definition at line 876 of file RooJSONFactoryWSTool.cxx.
|
inline |
Definition at line 182 of file RooJSONFactoryWSTool.h.
|
static |
Read axes from the JSONNode and create a RooArgSet representing them.
This function reads axes information from the given JSONNode and creates a RooArgSet with variables representing these axes.
topNode | The JSONNode containing the axes information to be read. |
Definition at line 1557 of file RooJSONFactoryWSTool.cxx.
|
static |
Read binned data from the JSONNode and create a RooDataHist object.
This function reads binned data from the given JSONNode and creates a RooDataHist object. The binned data is associated with the specified name and variables (RooArgSet) in the workspace.
n | The JSONNode representing the binned data to be read. |
name | The name to be associated with the created RooDataHist object. |
vars | The RooArgSet representing the variables associated with the binned data. |
Definition at line 1598 of file RooJSONFactoryWSTool.cxx.
|
inline |
Definition at line 59 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 68 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 102 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 96 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 78 of file RooJSONFactoryWSTool.h.
|
private |
|
private |
Definition at line 887 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 902 of file RooJSONFactoryWSTool.cxx.
|
private |
Definition at line 917 of file RooJSONFactoryWSTool.cxx.
void RooJSONFactoryWSTool::setAttribute | ( | const std::string & | obj, |
const std::string & | attrib | ||
) |
Definition at line 2052 of file RooJSONFactoryWSTool.cxx.
void RooJSONFactoryWSTool::setStringAttribute | ( | const std::string & | obj, |
const std::string & | attrib, | ||
const std::string & | value | ||
) |
Definition at line 2073 of file RooJSONFactoryWSTool.cxx.
|
static |
Writes a warning message to the RooFit message service.
str | The warning message to be logged. |
Definition at line 2312 of file RooJSONFactoryWSTool.cxx.
|
inline |
Definition at line 107 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 117 of file RooJSONFactoryWSTool.h.
|
inline |
Definition at line 110 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 233 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 239 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 232 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 234 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 240 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 235 of file RooJSONFactoryWSTool.h.
|
private |
Definition at line 236 of file RooJSONFactoryWSTool.h.
|
staticconstexpr |
Definition at line 40 of file RooJSONFactoryWSTool.h.