Guide on how to add JSON IO for you RooFit classes.
Contains facilities to serialize and deserialize RooWorkspaces to and from JSON.
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. This library sets out to achieve exactly that, exporting to and importing from JSON.
The main class providing import from and export to JSON is the RooJSONFactoryWSTool.
One of the most challenging aspects of providing serialization and deserialization for RooFit is the fact that RooFit follows an "open-world" philosophy with respect to the functions and pdfs it can handle. Over the years, RooFit has also accumulated a significant number of different pre-implemented functions and pdfs. What is more, you can easily create your own RooFit function by inheriting from RooAbsReal or your own RooFit pdf by inheriting from RooAbsPdf. This means that feature-complete serialization and deserialization to and from JSON will probably never be fully achieved. However, this may not impede your usage of this library, as it was written in such a way as to allow users (that is, people like you) to easily add missing importers and exporters for existing RooFit classes as well as custom implementations you might be using.
In order to implement your own importer or exporter, you can inherit from the corresponding base classes RooFit::JSONIO::Importer or RooFit::JSONIO::Exporter, respectively. You can find simple examples as well as more complicated ones in ROOT.
Any importer should take the following form:
The same importArg function is used for functions (inheriting from RooAbsReal) and pdfs (inheriting from RooAbsPdf). If the importer can't handle a given JSON node, it should return false so that the next importer is tried. The functions importFunction and importPdf of older ROOT versions are still called for backwards compatibility, so existing importers that override them keep working, but new code should override importArg.
Once your importer implementation exists, you need to register it with the tool using a line like the following:
This is a shorthand for passing an instance owned by a std::unique_ptr:
As there can be several importers for the same json key, the last (boolean) argument determines whether your new importer should be added at the top of the priority list (true) or at the bottom (false). If the import fails, other importers are attempted.
The implementation of an exporter works in a very similar fashion:
Also this needs to be registered with the tool
or equivalently
If the TClass of your class is not available where you register the exporter, you can also pass the class name as a string instead of MyClass::Class(). If exportObject returns false, the next exporter is tried.
For more complicated cases where members are lists of elements, the methods is_seq(), set_seq(), is_map(), set_map() and ranged-based for-loops via children() might come in handy.
If you encounter a missing importer or exporter, please consider filing a feature request via the issue tracker.
If you don't want to wait for one of the dev's to pick up your request an process it, you can use the above instructions to write your own. If you wrote an importer or exporter for a RooFit class that is part of ROOT, please consider contributing your implementation to ROOT such that it can help other users also missing this importer or exporter. You can fork ROOT, commit your changes, and file a pull request to ROOT for your work to be included in the next release!
Classes | |
| class | RooJSONFactoryWSTool |
| When using RooFit, statistical models can be conveniently handled and stored as a RooWorkspace. More... | |