Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
JSONIOUtils.h
Go to the documentation of this file.
1#ifndef JSONIOUtils_h
2#define JSONIOUtils_h
3
4#include <map>
5#include <memory>
6#include <optional>
7#include <string>
8#include <string_view>
9#include <vector>
11
12class RooAbsArg;
13class RooAbsBinning;
15class RooAbsPdf;
16class RooAbsReal;
17class RooArgList;
18class ParamHistFunc;
19
20// A RooProdPdf with this attribute represents an HS3 `array_dist`: a product of one-dimensional distributions of one
21// type, each over its own observable. The factors are the components, imported as individual pdfs named after their
22// observable.
23inline constexpr const char *arrayDistTag = "hs3_array_dist";
24
25class RooAbsBinning;
26class RooAbsCategory;
28class RooAbsReal;
29class RooArgList;
30class RooArgSet;
31class RooCategory;
32class RooDataHist;
33class RooWorkspace;
34class ParamHistFunc;
35
36// True if `arg` is a RooConstVar whose name is a plain number (i.e. a literal constant).
37bool isLiteralConstVar(RooAbsArg const &arg);
38
39// The HS3 type and the observable of `pdf` as a component of an array distribution (RooPoisson, RooGaussian or
40// RooLognormal in the standard parametrization, over a named observable), or std::nullopt if `pdf` cannot be a
41// component.
42// If `observable` names the mean of a RooGaussian, the Gaussian is written with x and mean swapped (see
43// RooJSONFactoryWSTool::setConstraintObservable()).
45 std::string type;
46 std::string observable;
47};
48std::optional<ArrayComponentInfo> arrayComponentInfo(RooAbsPdf const &pdf, std::string const *observable = nullptr);
49
50// Write the array distribution `name` with the given components into `elem`. `observableOf` gives the observable
51// recorded for a component by name, if any.
52void writeArrayDist(RooFit::Detail::JSONNode &elem, std::string const &name,
53 std::vector<RooAbsPdf const *> const &components,
54 std::map<std::string, std::string> const &observableOf);
55
57 bool valid = false;
58 double value = 0.0;
59};
60
61ToNumberOutput toNumber(const std::string &str);
63bool startsWith(std::string_view str, std::string_view prefix);
64bool endsWith(std::string_view str, std::string_view suffix);
65std::string removeSuffix(std::string_view str, std::string_view suffix);
67
68// The key of a fixed domain axis (HS3 #131): the parameter only takes this value, and RooFit reads it as a
69// RooConstVar.
70inline constexpr const char *fixedValueKey = "value";
71bool parseHS3Version(std::string const &version, int &major, int &minor);
72bool isHS3VersionBefore(std::string const &version, int major, int minor);
73
74// A RooHistFunc or RooHistPdf is evaluated with its own observables (`funcObs`), which are mapped by position onto
75// the variables of its histogram (`histObs`) and need not have the same names, e.g. after a RooCustomizer replaced
76// them. The variables of the histogram `dh`, in the order of the histogram, each named after the observable that is
77// mapped onto it, so that the axes are written with the names of the observables. A variable onto which no variable
78// or category is mapped keeps its name.
79std::unique_ptr<RooArgSet>
80histogramVariablesNamedAfterObservables(RooDataHist const &dh, RooArgSet const &histObs, RooArgSet const &funcObs);
81
82// ParamHistFunc orders its parameters like the bins of a TH1 (first variable fastest), while HS3 lays out per-bin
83// arrays with the last axis varying fastest, like RooDataHist. These helpers convert at the HS3 boundary.
84std::vector<RooAbsReal *> paramHistFuncParamsInBinOrder(ParamHistFunc const &func);
85RooArgList toParamHistFuncOrder(RooArgList const &paramsInBinOrder, RooAbsCollection const &vars);
86// Documents before HS3 0.3 wrote the parameters of multi-dimensional ParamHistFuncs in TH1 order. No such documents
87// are known; they are rejected instead of being guessed.
88void checkParamHistFuncOrderIsDefined(std::string const &hs3Version, RooAbsCollection const &vars,
89 std::string const &context);
90
91// Selection keys (HS3 "combined" data sets and "combined_dist"): a key is one label per column, and the columns are
92// categories declared by categorical axes of the domains. In RooFit, the columns are the index category of a
93// RooSimultaneous (a RooCategory), or the input categories of a RooSuperCategory index, or the category observables
94// of a combined data set. They are sorted by name, so that a distribution and a data set agree on the order of the
95// columns without looking at each other.
96std::vector<RooCategory const *> selectionColumns(RooAbsCategory const &indexCat);
97std::vector<RooCategory const *> selectionColumns(RooAbsCollection const &vars);
98// All keys of the columns, in the order of the columns and of their states, with the first column varying slowest.
99std::vector<std::vector<std::string>> selectionKeys(std::vector<RooCategory const *> const &columns);
100// The state label of a RooCategory (one column) or of a RooSuperCategory over the columns in that order.
101std::string selectionLabel(std::vector<std::string> const &key);
102// The state label of `indexCat` for a key with the labels in the order of `columns`: the label itself for a
103// RooCategory, or "{a;b}" with the labels in the order of the inputs for a RooSuperCategory.
104std::string selectionLabel(RooAbsCategory const &indexCat, std::vector<RooCategory const *> const &columns,
105 std::vector<std::string> const &key);
106// A key as written in the document, for messages: ["a", "b"].
107std::string selectionKeyToString(std::vector<std::string> const &key);
108// Read the columns of a combined distribution or data set from the list `field` of `node`: each entry must be the
109// name of a category in the workspace, i.e. declared by a categorical axis of a domain.
110std::vector<RooCategory *> readSelectionColumns(RooWorkspace const &ws, RooFit::Detail::JSONNode const &node,
111 const char *field, std::string const &context);
112// Read and validate the "key" of a component: one label per column, each one of the categories of its column.
113std::vector<std::string> readSelectionKey(RooFit::Detail::JSONNode const &component,
114 std::vector<RooCategory *> const &columns, std::string const &context);
115
116#endif
void writeArrayDist(RooFit::Detail::JSONNode &elem, std::string const &name, std::vector< RooAbsPdf const * > const &components, std::map< std::string, std::string > const &observableOf)
ToNumberOutput toNumber(const std::string &str)
std::vector< std::vector< std::string > > selectionKeys(std::vector< RooCategory const * > const &columns)
bool isHS3VersionBefore(std::string const &version, int major, int minor)
std::optional< ArrayComponentInfo > arrayComponentInfo(RooAbsPdf const &pdf, std::string const *observable=nullptr)
std::vector< RooCategory * > readSelectionColumns(RooWorkspace const &ws, RooFit::Detail::JSONNode const &node, const char *field, std::string const &context)
std::vector< RooCategory const * > selectionColumns(RooAbsCategory const &indexCat)
std::vector< RooAbsReal * > paramHistFuncParamsInBinOrder(ParamHistFunc const &func)
std::vector< std::string > readSelectionKey(RooFit::Detail::JSONNode const &component, std::vector< RooCategory * > const &columns, std::string const &context)
constexpr const char * fixedValueKey
Definition JSONIOUtils.h:70
RooArgList toParamHistFuncOrder(RooArgList const &paramsInBinOrder, RooAbsCollection const &vars)
std::string selectionKeyToString(std::vector< std::string > const &key)
constexpr const char * arrayDistTag
Definition JSONIOUtils.h:23
bool startsWith(std::string_view str, std::string_view prefix)
bool endsWith(std::string_view str, std::string_view suffix)
std::string selectionLabel(std::vector< std::string > const &key)
bool isLiteralConstVar(RooAbsArg const &arg)
std::string removeSuffix(std::string_view str, std::string_view suffix)
std::unique_ptr< RooArgSet > histogramVariablesNamedAfterObservables(RooDataHist const &dh, RooArgSet const &histObs, RooArgSet const &funcObs)
bool parseHS3Version(std::string const &version, int &major, int &minor)
void writeAxisBinning(RooFit::Detail::JSONNode &node, const RooAbsBinning &binning)
void writeReference(RooFit::Detail::JSONNode &node, RooAbsArg const &arg)
void checkParamHistFuncOrderIsDefined(std::string const &hs3Version, RooAbsCollection const &vars, std::string const &context)
char name[80]
Definition TGX11.cxx:142
A class which maps the current values of a RooRealVar (or a set of RooRealVars) to one of a number of...
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
Abstract base class for RooRealVar binning definitions.
A space to attach TBranches.
Abstract container object that can hold multiple RooAbsArg objects.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
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:24
Object to represent discrete states.
Definition RooCategory.h:28
Container class to hold N-dimensional binned data.
Definition RooDataHist.h:40
Persistable container for RooFit projects.
std::string observable
Definition JSONIOUtils.h:46
std::string type
Definition JSONIOUtils.h:45