50#include <unordered_map>
126 for (
auto const &function : doc.
functions) {
127 functions.emplace(function.name, &function);
133 if (point.name ==
"default_values") {
135 for (
auto const &
p : point.parameters) {
146 std::unordered_map<std::string, HS3::Function const *>
functions;
158 std::map<std::string, std::map<std::string, HS3::DomainAxis>>
domains;
183 std::vector<DistributionRef> channelConstraints)
185 tool.
_export->typedObject = std::move(dist);
186 tool.
_export->channelConstraints = std::move(channelConstraints);
192void addUnique(std::vector<std::string> &names, std::string
const &
name)
194 if (std::find(names.begin(), names.end(),
name) == names.end()) {
195 names.push_back(
name);
199template <
class Coll_t>
200std::vector<std::string> namesOf(Coll_t
const &objects)
202 std::vector<std::string> names;
203 for (
auto const *
object : objects) {
204 names.push_back(
object->GetName());
210bool hasLabels(
RooCategory const &cat, std::vector<std::string>
const &labels)
212 std::set<std::string> states;
213 for (
auto const &state : cat) {
214 states.insert(state.first);
216 return states == std::set<std::string>(labels.begin(), labels.end());
226 for (
int i = 0; i < binning.
numBins(); ++i) {
232constexpr double inf = std::numeric_limits<double>::infinity();
233constexpr int isInfinite(
double x)
235 return (
x >= +inf) ? +1 : ((
x <= -inf) ? -1 : 0);
244 if (!isInfinite(binning.
lowBound())) {
251 if (axis.
min && axis.
max && hasBins) {
252 axis.
binning = toBinning(binning);
259void recordDomains(std::map<std::string, std::map<std::string, HS3::DomainAxis>> &domains,
RooRealVar const &var)
262 for (
auto const &
name : var.getBinningNames()) {
270void appendDomain(std::vector<HS3::Domain> &domains, std::string
const &
name,
271 std::map<std::string, HS3::DomainAxis> axes,
272 std::map<std::string, HS3::CategoricalAxis> categories = {})
274 std::map<std::string, std::variant<HS3::DomainAxis, HS3::CategoricalAxis>> sorted;
275 for (
auto &item : axes) {
276 sorted.emplace(item.first, std::move(item.second));
278 for (
auto &item : categories) {
279 sorted.emplace(item.first, std::move(item.second));
283 for (
auto &item : sorted) {
284 domain.
axes.emplace_back(std::move(item.second));
291 if (
auto const *edges = std::get_if<HS3::EdgesBinning>(&binning)) {
292 if (edges->
edges.empty()) {
296 for (
double edge : edges->edges) {
297 bins.addBoundary(edge);
300 }
else if (
int nbins = std::get<HS3::RegularBinning>(binning).nbins) {
308 if (isInfinite(*axis.
min)) {
315 if (isInfinite(*axis.
max)) {
322 applyBinning(var, *axis.
binning);
330 auto forEachAxis = [&](
auto &&fn) {
332 for (
auto const &axis : domain.axes) {
333 if (
auto const *continuous = std::get_if<HS3::DomainAxis>(&axis)) {
334 fn(domain.
name, *continuous);
342 ws.import(RooConstVar{axis.name.c_str(), axis.name.c_str(), *axis.fixedValue},
RooFit::Silence());
347 RooRealVar var{axis.name.c_str(), axis.name.c_str(), axis.min.value_or(-inf), axis.max.value_or(inf)};
349 applyBinning(var, *axis.
binning);
358 for (
auto const &axisVariant : domain.axes) {
359 auto const *axis = std::get_if<HS3::CategoricalAxis>(&axisVariant);
367 "' already exists and is not a category");
369 if (!hasLabels(*cat, axis->categories)) {
371 "' differ from the states of the existing category '" + axis->
name +
"'");
376 for (std::size_t i = 0; i < axis->categories.size(); ++i) {
377 cat.
defineType(axis->categories[i],
static_cast<int>(i));
382 forEachAxis([&](std::string
const &domain,
HS3::DomainAxis const &axis) {
385 var->
setRange(domain.c_str(), axis.
min.value_or(-inf), axis.
max.value_or(inf));
387 applyBinning(*var, *axis.
binning, domain.c_str());
399template <
class State_t>
401 std::unique_ptr<State_t> &state;
402 ~StateScope() { state.reset(); }
409 ImportGuard(std::vector<std::string> &stack, std::string
const &
name) : _stack{stack}
411 if (std::find(_stack.begin(), _stack.end(),
name) != _stack.end()) {
413 for (std::string
const &ancestor : _stack) {
414 chain += ancestor +
" -> ";
418 _stack.push_back(
name);
420 ~ImportGuard() { _stack.pop_back(); }
423 std::vector<std::string> &_stack;
431 for (
Diagnostic const &diagnostic : diagnostics) {
433 const bool needsDot = !
object.empty() && !diagnostic.path.empty() && diagnostic.path.front() !=
'[';
434 const std::string path =
object + (needsDot ?
"." :
"") + diagnostic.path;
435 const std::string
text = path.empty() ? diagnostic.message : path +
": " + diagnostic.message;
436 if (diagnostic.severity == Diagnostic::Severity::Error) {
437 errors += (errors.empty() ?
"" :
"\n") +
text;
442 if (!errors.empty()) {
453 if (
auto const *
value = std::get_if<double>(&*
p.value)) {
458 applyRange(
v, *range);
463 v.setError(
v.getVal() * *
p.relError);
465 v.setError(*
p.error);
466 v.setConstant(
p.isConst);
469std::string genPrefix(
const JSONNode &
p,
bool trailing_underscore)
474 if (
auto node =
p.find(
"namespaces")) {
475 for (
const auto &ns : node->children()) {
481 if (trailing_underscore && !prefix.empty())
489 for (
auto const &
attr : attributes.dict) {
492 for (
auto const &tag : attributes.tags) {
499void exportParameterStepWidths(
RooWorkspace const &workspace, std::vector<RooAbsPdf *>
const &pdfs,
500 std::vector<RooAbsData *>
const &
data, std::vector<HS3::StepWidth> &out)
506 set.
add(*args,
true);
509 for (
TObject *obj : workspace.allGenericObjects()) {
511 addIfPresent(candidates, mc->GetParametersOfInterest());
512 addIfPresent(candidates, mc->GetNuisanceParameters());
513 addIfPresent(excluded, mc->GetObservables());
514 addIfPresent(excluded, mc->GetGlobalObservables());
515 addIfPresent(excluded, mc->GetConditionalObservables());
521 std::unique_ptr<RooArgSet> pdfObs{pdf->getObservables(*dataset->get())};
522 observables.
add(*pdfObs,
true);
524 if (!observables.
empty()) {
526 pdf->getParameters(&observables, params);
527 candidates.
add(params,
true);
528 excluded.
add(observables,
true);
543 auto *var =
dynamic_cast<RooRealVar *
>(workspace.
arg(stepWidth.name));
547 stepWidth.name +
"'.");
557std::set<std::string> checkVariates(
HS3Document const &doc)
562 for (std::string
const &column : columns) {
564 "RooFitHS3: the axis '" + column +
"' of the components of the combined data set '" + dataset +
565 "' is declared as a bookkeeping column without statistical meaning (misc.ROOT_internal."
566 "bookkeeping_columns), so it may be shared by the components, although HS3 requires distinct axes.");
573 ". The terms of a likelihood and the components of a combined distribution or "
574 "data set must have distinct variates.");
577 ". HS3 0.3 requires distinct variates; the document is read anyway, since it "
578 "declares an older version.");
581 std::vector<std::string> notes;
584 ". The \"constraints\" of a modifier add no term to the likelihood, which has "
587 for (std::string
const ¬e : notes) {
600 std::vector<RooArgSet const *> modelConfigSets;
601 for (
TObject *obj : ws.allGenericObjects()) {
603 for (
RooArgSet const *set : {mc->GetObservables(), mc->GetParametersOfInterest(), mc->GetNuisanceParameters(),
604 mc->GetGlobalObservables(), mc->GetConditionalObservables()}) {
606 modelConfigSets.push_back(set);
611 auto usedByNothing = [&](std::string
const &
name) {
613 if (!arg || !arg->
clients().empty()) {
616 return std::none_of(modelConfigSets.begin(), modelConfigSets.end(),
617 [&](
RooArgSet const *set) { return set->find(name.c_str()) != nullptr; });
620 for (std::string
const &axis : axes) {
621 if (usedByNothing(axis)) {
629void exportAttributes(
const RooAbsArg *arg, std::map<std::string, HS3::Attributes> &attributes)
639 out = &attributes[arg->
GetName()];
643 for (
const auto &it : arg->stringAttributes()) {
645 if (it.first ==
"factory_tag" || it.first ==
"PROD_TERM_TYPE")
648 out->
dict[it.first] = it.second;
650 for (
auto const &
attr : arg->attributes()) {
676 return static_cast<RooConstVar const &
>(arg).getVal();
678 return std::string{arg.
GetName()};
704 for (
unsigned int i = 0; i < cat.
size(); ++i) {
715 if (
auto const *cat =
dynamic_cast<RooCategory const *
>(var)) {
717 data.axes.emplace_back(toCategoricalAxis(*cat));
722 data.contents.assign(contents, contents +
n);
726std::unique_ptr<RooRealVar> createObservable(
HS3::BinnedAxis const &axis)
728 auto obs = std::make_unique<RooRealVar>(axis.
name.c_str(), axis.
name.c_str(), axis.
min, axis.
max);
729 applyBinning(*obs, axis.
binning);
738RooArgSet createBinnedObservables(std::vector<std::variant<HS3::BinnedAxis, HS3::CategoricalAxis>>
const &axes,
739 RooWorkspace const &workspace, std::string
const &dataName,
740 std::vector<std::size_t> &binOrder)
743 std::vector<std::vector<std::size_t>> axisPositions;
744 bool reordered =
false;
745 for (
auto const &axisVariant : axes) {
746 if (
auto const *axis = std::get_if<HS3::BinnedAxis>(&axisVariant)) {
747 auto obs = createObservable(*axis);
748 std::vector<std::size_t> positions(obs->numBins());
749 std::iota(positions.begin(), positions.end(), 0);
750 axisPositions.push_back(std::move(positions));
754 auto const &axis = std::get<HS3::CategoricalAxis>(axisVariant);
755 auto cat = std::make_unique<RooCategory>(axis.
name.c_str(), axis.
name.c_str());
759 "' differ from those of the category '" + axis.
name +
"'");
761 for (
unsigned int i = 0; i < wsCat->size(); ++i) {
762 auto const &state = wsCat->getOrdinal(i);
766 for (std::size_t i = 0; i < axis.
categories.size(); ++i) {
770 std::vector<std::size_t> positions(axis.
categories.size());
771 for (std::size_t i = 0; i < positions.size(); ++i) {
773 positions[i] = std::distance(axis.
categories.begin(), found);
774 reordered |= positions[i] != i;
776 axisPositions.push_back(std::move(positions));
783 std::size_t
total = 1;
784 for (
auto const &positions : axisPositions) {
785 total *= positions.size();
787 binOrder.resize(
total);
788 for (std::size_t k = 0; k <
total; ++k) {
790 std::size_t rest = k;
791 std::size_t docIndex = 0;
792 std::size_t stride = 1;
793 for (std::size_t j = axisPositions.size(); j-- > 0;) {
794 const std::size_t nBins = axisPositions[j].size();
795 docIndex += axisPositions[j][rest % nBins] * stride;
799 binOrder[k] = docIndex;
805std::vector<double> permute(std::vector<double>
const &values, std::vector<std::size_t>
const &order)
807 if (order.empty() || values.size() != order.size()) {
810 std::vector<double> out(values.size());
811 for (std::size_t k = 0; k < order.size(); ++k) {
812 out[k] = values[order[k]];
827 }
else if (var.
getMin() != min || var.
getMax() != max) {
828 std::stringstream ss;
829 ss <<
"RooFitHS3: the range [" << min <<
", " << max <<
"] of the axis '" << axis.
name <<
"' of the dataset '"
830 << dataName <<
"' differs from the range [" << var.
getMin() <<
", " << var.
getMax() <<
"] of the variable.";
839 for (
auto const &axisVariant :
input.axes) {
840 auto const *axis = std::get_if<HS3::ContinuousAxis>(&axisVariant);
843 auto const &catAxis = std::get<HS3::CategoricalAxis>(axisVariant);
847 "\" could not be found in the workspace! Categorical observables are "
848 "declared by a categorical axis in a domain.");
850 if (!hasLabels(*cat, catAxis.categories)) {
852 "\" differ from the states of the category \"" + catAxis.name +
"\"");
862 applyUnbinnedAxisRange(*axis,
name, *var, ranges);
866 const std::size_t nAxes =
input.axes.size();
867 for (std::size_t i = 0; i <
input.numEntries(); ++i) {
868 for (std::size_t j = 0; j < nAxes; ++j) {
869 const double value =
input.entries[i * nAxes + j];
870 if (
auto const *catAxis = std::get_if<HS3::CategoricalAxis>(&
input.axes[j])) {
872 static_cast<RooCategory *
>(varlist.
at(j))->setLabel(catAxis->categories[
static_cast<std::size_t
>(
value)]);
888 if (
auto const *binned = std::get_if<HS3::BinnedData>(&
input.body)) {
889 if (
binned->uncertainty && !
binned->uncertainty->correlation.empty()) {
891 input.name +
"' are ignored, because RooDataHist can't store them.");
893 std::vector<std::size_t> binOrder;
895 std::vector<double>
sigma;
896 if (
binned->uncertainty) {
897 sigma = permute(
binned->uncertainty->sigma, binOrder);
902 if (
auto const *unbinned = std::get_if<HS3::UnbinnedData>(&
input.body)) {
903 if (
unbinned->hasEntriesUncertainties) {
905 "' are ignored, because RooDataSet can't store them.");
907 return createUnbinnedData(
input.name, *unbinned, workspace, ranges);
911 auto const &point = std::get<HS3::PointData>(
input.body);
912 if (point.uncertainty) {
914 "' is ignored: HS3 doesn't define how it enters a likelihood.");
918 if (point.axes.empty()) {
920 "' has no axis, and it is not paired with a distribution that has a single variate");
921 }
else if (point.axes.size() != 1) {
925 unbinned.axes.emplace_back(point.axes.front());
926 unbinned.entries.push_back(point.value);
927 return createUnbinnedData(
input.name, unbinned, workspace, ranges);
931std::string rootVersion()
933 std::string versionName =
gROOT->GetVersion();
934 std::replace(versionName.begin(), versionName.end(),
'/',
'.');
940std::unique_ptr<RooAbsData> *findDataset(std::vector<std::unique_ptr<RooAbsData>> &datasets, std::string
const &
name)
942 auto found = std::find_if(datasets.begin(), datasets.end(), [&](
auto &
d) { return d && d->GetName() == name; });
943 return found != datasets.end() ? &*found :
nullptr;
947void requirePdf(
RooAbsArg const *arg, std::string
const &
what)
949 if (!
dynamic_cast<RooAbsPdf const *
>(arg)) {
951 (arg ? std::string(
"a ") + arg->
ClassName() +
", not a pdf" :
"not imported"));
963 const std::vector<std::string> columnNames = namesOf(columns);
964 std::vector<std::string> dataColumns =
data.columns;
965 std::sort(dataColumns.begin(), dataColumns.end());
966 if (dataColumns != columnNames) {
968 "' with the combined data set '" + dataName +
"', but their columns differ");
971 std::set<std::map<std::string, std::string>> dataKeys;
972 for (
auto const &component :
data.components) {
973 std::map<std::string, std::string> key;
974 for (std::size_t i = 0; i <
data.columns.size(); ++i) {
975 key[
data.columns[i]] = component.key[i];
977 dataKeys.insert(key);
979 auto keyToString = [&](std::map<std::string, std::string>
const &key) {
980 std::vector<std::string> labels;
981 for (std::string
const &column :
data.columns) {
982 labels.push_back(key.at(column));
986 std::set<std::map<std::string, std::string>> pdfKeys;
987 for (std::vector<std::string>
const &labels :
selectionKeys(columns)) {
992 std::map<std::string, std::string> key;
993 for (std::size_t i = 0; i < columns.size(); ++i) {
994 key[columnNames[i]] = labels[i];
997 if (dataKeys.count(key) > 0) {
1002 "the combined data set '" + dataName +
"' has no sub-data set for the key " + keyToString(key) +
1003 " of the extended component '" + component->
GetName() +
"' of '" + simPdf.
GetName() +
1004 "'. RooFit cannot leave a component unobserved: it would add the expected number of events of the " +
1005 "component to the likelihood, see https://github.com/root-project/root/issues/24004");
1008 "' has no sub-data set for the key " + keyToString(key) +
" of the component '" +
1010 "', which RooFit treats as present and empty.");
1012 for (
auto const &key : dataKeys) {
1013 if (pdfKeys.count(key) == 0) {
1015 " of the combined data set '" + dataName +
"' is ignored, because '" +
1016 simPdf.
GetName() +
"' has no component for it.");
1031 for (std::string
const &
name : *mcInfo->observables) {
1033 observables.
add(*arg);
1036 "' (misc.ROOT_internal.ModelConfigs) does not exist");
1041 std::set<std::string> inDomains;
1042 for (
auto const &domainRef : analysis.domains) {
1044 for (
auto const &axis : domain->axes) {
1049 for (
auto const &poi : analysis.parametersOfInterest) {
1050 inDomains.insert(poi.name);
1053 for (std::string
const &variate :
HS3::
variatesOf(doc, likelihood)) {
1058 if (inDomains.count(variate) || arg->
isConstant()) {
1061 observables.
add(*arg);
1062 list += (list.empty() ?
"'" :
", '") + variate +
"'";
1064 if (!list.empty()) {
1065 oocoutI(
nullptr, IO) <<
"RooFitHS3: no data set of the likelihood of the analysis '" << analysis.
name
1066 <<
"' gives its observables; they are the variates of the likelihood that are neither in a "
1067 "domain of the analysis nor constant: "
1068 << list << std::endl;
1083 bool hasGlobalObservable =
false;
1085 auto *var =
dynamic_cast<RooRealVar *
>(leaf);
1091 if (std::none_of(channels.
begin(), channels.
end(),
1092 [&](
RooAbsArg const *other) { return other->dependsOn(*var); })) {
1093 hasGlobalObservable =
true;
1095 parameters.
add(*var);
1098 return hasGlobalObservable && !parameters.
empty() && channel.
dependsOn(parameters);
1103std::vector<std::string> otherLikelihoodTerms(
HS3Document const &doc, std::string
const &dist)
1105 std::vector<std::string> out;
1106 auto add = [&](std::string
const &
name) {
1108 addUnique(out,
name);
1111 for (
auto const &likelihood : doc.likelihoods) {
1112 if (std::none_of(likelihood.
terms.begin(), likelihood.
terms.end(),
1113 [&](
auto const &term) { return term.distribution.name == dist; })) {
1116 for (
auto const &term : likelihood.terms) {
1117 add(term.distribution.name);
1119 for (
auto const &aux : likelihood.auxDistributions) {
1139 for (
auto const &state : simPdf->indexCat()) {
1141 channels.
add(*channel,
true);
1151 bool isFactor =
false;
1152 std::vector<RooProdPdf *> products;
1155 if (!product || channel == constraint) {
1158 if (
product->pdfList().find(*constraint)) {
1160 }
else if (isConstraintOfChannel(*constraint, *channel, channels)) {
1161 products.push_back(product);
1167 if (isFactor || !products.empty()) {
1168 attached.
add(*constraint);
1171 constraints.
remove(attached);
1176 std::vector<std::unique_ptr<RooAbsData>> &datasets, std::set<std::string>
const &bookkeepingColumns,
1177 std::set<std::string>
const &auxiliaryMeasurements)
1179 std::string
const &analysisName = analysis.
name;
1183 std::string mcname = mcInfo && mcInfo->
mcName ? *mcInfo->
mcName : analysisName;
1184 if (workspace.
obj(mcname))
1189 mc->
SetWS(workspace);
1194 auto checkPdf = [&](
RooAbsArg const *arg, std::string
const &
name, std::string
const &
what) {
1195 requirePdf(arg,
"the " +
what +
" '" +
name +
"' of analysis '" + analysisName +
"'");
1198 for (
auto const &term : likelihood.terms) {
1199 checkPdf(workspace.
arg(term.distribution.name), term.distribution.name,
"distribution");
1205 std::vector<std::unique_ptr<RooArgSet>> trees;
1206 for (
auto const &term : likelihood.terms) {
1207 RooAbsPdf *listed = workspace.
pdf(term.distribution.name);
1208 trees.push_back(listed ? std::unique_ptr<RooArgSet>{listed->
getComponents()} :
nullptr);
1210 auto isConstraintTermOfOthers = [&](
RooAbsArg const &term, std::size_t self) {
1211 auto isPartOf = [&term](
RooArgSet const &tree) {
1212 if (tree.find(term.
GetName())) {
1216 if (
auto const *array =
dynamic_cast<RooProdPdf const *
>(&term)) {
1217 for (
RooAbsArg const *component : array->pdfList()) {
1218 if (tree.find(component->
GetName())) {
1226 for (std::size_t i = 0; i < trees.size(); ++i) {
1227 if (i != self && trees[i] && isPartOf(*trees[i])) {
1238 std::string
const modelPdfName = mcInfo && mcInfo->
pdfName ? *mcInfo->
pdfName :
"";
1239 std::unique_ptr<RooArgSet> modelTree;
1240 if (
RooAbsPdf const *modelPdf = workspace.
pdf(modelPdfName);
1241 modelPdf && std::any_of(likelihood.
terms.begin(), likelihood.
terms.end(),
1242 [&](
auto const &term) { return term.distribution.name == modelPdfName; })) {
1243 modelTree.reset(modelPdf->getComponents());
1248 auto const *dataRef = std::get_if<HS3::DataRef>(&term.data);
1249 const bool auxiliary = auxiliaryMeasurements.count(listed.
GetName()) > 0;
1250 if (!dataRef || (!auxiliary && (!modelTree || modelTree->find(listed.
GetName())))) {
1253 auto const *
data = findDataset(datasets, dataRef->name);
1254 return data && (*data)->numEntries() == 1;
1257 std::vector<HS3::LikelihoodTerm const *> nllTerms;
1260 for (std::size_t i = 0; i < likelihood.
terms.size(); ++i) {
1261 auto const &term = likelihood.
terms[i];
1262 RooAbsPdf *listed = workspace.
pdf(term.distribution.name);
1263 if (listed && !isConstraintTermOfOthers(*listed, i) && isConstraintTermOfModel(*listed, term)) {
1264 constraintsWithData.
add(*listed);
1265 }
else if (!listed || !isConstraintTermOfOthers(*listed, i)) {
1266 nllTerms.push_back(&term);
1269 auto const *dataRef = std::get_if<HS3::DataRef>(&term.data);
1270 auto *
data = dataRef ? findDataset(datasets, dataRef->name) : nullptr;
1274 if ((*data)->numEntries() != 1) {
1275 throw std::runtime_error(
"the data set '" + dataRef->name +
"' paired with the constraint term '" +
1276 term.distribution.name +
"' must have exactly one entry");
1279 auto *var = workspace.
var(obs->GetName());
1281 if (!var || !
value) {
1283 std::string(obs->GetName()) +
"' from the data set '" + dataRef->name +
1284 "': it is not a variable of the workspace");
1289 constraintObservables.
add(*var);
1296 std::set<std::string> channelConstraints;
1297 for (
auto const &item : doc.rooFit.channelConstraints) {
1298 for (
auto const &ref : item.second) {
1299 channelConstraints.insert(ref.name);
1303 for (
auto const &aux : likelihood.auxDistributions) {
1305 if (!extConstraint) {
1308 checkPdf(extConstraint, aux.name,
"auxiliary distribution");
1310 if (channelConstraints.count(aux.name) &&
1312 RooAbsArg const *dist = workspace.arg(term->distribution.name);
1313 return dist && dist->dependsOn(*extConstraint);
1317 if (isConstraintTermOfOthers(*extConstraint, likelihood.
terms.size())) {
1320 extConstraints.
add(*extConstraint);
1324 if (
auto const *dataRef = std::get_if<HS3::DataRef>(&term->
data)) {
1325 auto const *
data = findDataset(datasets, dataRef->name);
1327 throw std::runtime_error(
"dataset '" + dataRef->name +
"' cannot be found!");
1329 observables.
add(*(*data)->get(),
true);
1335 observables.
remove(*arg);
1338 if (observables.
empty()) {
1339 observablesWithoutData(doc, analysis, likelihood, mcInfo, workspace, observables);
1343 for (
auto const &term : likelihood.terms) {
1344 auto const *dataRef = std::get_if<HS3::DataRef>(&term.
data);
1346 if (!dataRef || !simPdf) {
1349 if (
auto const *combined = HS3::find<HS3::CombinedData>(doc.
data, dataRef->name)) {
1350 checkCombinedPairing(*simPdf, dataRef->name, *combined);
1354 std::string
const pdfName = mcInfo && mcInfo->
pdfName ? *mcInfo->
pdfName :
"simPdf";
1360 if (nllTerms.size() == 1) {
1362 pdf = workspace.
pdf(nllTerms[0]->distribution.name);
1365 std::string simPdfName = analysisName +
"_simPdf";
1366 std::string indexCatName = analysisName +
"_categoryIndex";
1367 RooCategory indexCat{indexCatName.c_str(), indexCatName.c_str()};
1368 std::map<std::string, RooAbsPdf *> pdfMap;
1369 for (std::size_t i = 0; i < nllTerms.size(); ++i) {
1370 std::string
const &
name = nllTerms[i]->distribution.name;
1371 indexCat.defineType(
name, i);
1374 RooSimultaneous simPdf{simPdfName.c_str(), simPdfName.c_str(), pdfMap, indexCat};
1376 pdf = workspace.
pdf(simPdfName);
1382 extConstraints.
add(constraintsWithData,
true);
1383 attachConstraintsToChannels(*pdf, extConstraints);
1384 if (!extConstraints.
empty())
1385 mc->SetExternalConstraints(extConstraints);
1387 if (analysis.
prior) {
1389 checkPdf(prior, analysis.
prior->name,
"prior");
1390 mc->SetPriorPdf(
static_cast<RooAbsPdf &
>(*prior));
1395 for (
auto const &poi : analysis.parametersOfInterest) {
1399 analysisName +
"' does not exist");
1403 "' is a " + arg->
ClassName() +
", not a variable" +
1404 (
dynamic_cast<RooConstVar *
>(arg) ?
", because no domain lists it" :
""));
1406 parametersOfInterest.
add(*arg);
1409 mc->SetParametersOfInterest(parametersOfInterest);
1410 mc->SetObservables(observables);
1423 for (
RooAbsArg const *extConstraint : extConstraints) {
1426 pars.
add(extPars,
true);
1430 for (
auto const &domainRef : analysis.domains) {
1432 for (
auto const &axis : domain->axes) {
1434 domainPars.
add(*wsvar);
1442 for (
const auto &
p : pars) {
1443 if (mc->GetParametersOfInterest()->find(*
p))
1445 if (
p->isConstant() && !mainPars.find(*
p) && domainPars.
find(*
p)) {
1447 }
else if (domainPars.
find(*
p)) {
1452 globs.
add(constraintObservables,
true);
1453 mc->SetGlobalObservables(globs);
1454 mc->SetNuisanceParameters(nps);
1463 const std::string &initName = analysis.
init->name;
1466 std::string setName = initName;
1467 const std::string prefix = mcname +
"_";
1468 const std::string suffix =
"_snapshot";
1469 if (initName.size() >= prefix.size() + suffix.size() &&
startsWith(initName, prefix) &&
1471 setName = initName.substr(prefix.size(), initName.size() - prefix.size() - suffix.size());
1476 initSet.
setName(setName.c_str());
1477 mc->SetSnapshot(initSet);
1484void warnAboutUnusedArrayComponents(
RooWorkspace const &ws)
1487 auto const *array =
dynamic_cast<RooProdPdf const *
>(arg);
1488 if (!array || !array->getAttribute(
arrayDistTag) || array->hasClients()) {
1493 for (
RooAbsArg const *component : array->pdfList()) {
1494 bool usedElsewhere =
false;
1495 for (
RooAbsArg const *client : component->clients()) {
1496 if (client != array) {
1497 usedElsewhere =
true;
1500 if (usedElsewhere) {
1503 unused += (unused.empty() ?
"'" :
", '") + std::string(component->
GetName()) +
"'";
1506 if (used && !unused.empty()) {
1509 "' are not used by any distribution and are not part of any likelihood");
1516 for (
auto const &item : doc.rooFit.combinedDistributions) {
1517 std::string
const &combinedName = item.first;
1521 std::map<std::string, RooAbsPdf *> pdfMap;
1523 for (std::size_t iChannel = 0; iChannel < info.
labels.size(); ++iChannel) {
1524 indexCat.defineType(info.
labels[iChannel], info.
indices[iChannel]);
1527 "the distribution '" +
name +
"' of the combined distribution '" + combinedName +
"'");
1531 RooSimultaneous simPdf{combinedName.c_str(), combinedName.c_str(), pdfMap, indexCat};
1536void combineDatasets(
HS3Document const &doc, std::vector<std::unique_ptr<RooAbsData>> &datasets)
1538 for (
auto const &item : doc.rooFit.combinedDatasets) {
1539 std::string
const &combinedName = item.first;
1543 std::map<std::string, std::unique_ptr<RooAbsData>> dsMap;
1546 for (std::size_t iChannel = 0; iChannel < info.
labels.size(); ++iChannel) {
1547 auto componentName = combinedName +
"_" + info.
labels[iChannel];
1550 auto *found = findDataset(datasets, componentName);
1554 allVars.add(*(*found)->get(),
true);
1555 dsMap.insert({info.
labels[iChannel], std::move(*found)});
1556 indexCat.defineType(info.
labels[iChannel], info.
indices[iChannel]);
1559 auto combined = std::make_unique<RooDataSet>(combinedName, combinedName, allVars,
RooFit::Import(dsMap),
1561 datasets.emplace_back(std::move(combined));
1568template <
class Ref_t>
1569std::vector<RooCategory *>
1572 std::vector<RooCategory *> columns;
1573 for (std::string
const &column : combined.columns) {
1577 " is not declared by a categorical axis of a domain");
1579 columns.push_back(cat);
1581 for (
auto const &component : combined.components) {
1582 for (std::size_t i = 0; i < columns.size(); ++i) {
1583 if (!columns[i]->hasLabel(component.key[i])) {
1585 " of a key of " + context +
" is not one of the categories of '" +
1598 std::vector<std::unique_ptr<RooAbsData>> &datasets)
1601 auto const *info = std::get_if<HS3::CombinedData>(&
input.body);
1607 std::string
const context =
"the combined data set '" +
name +
"'";
1608 std::vector<RooCategory *> columns = columnCategories(workspace, *info, context);
1611 std::vector<std::string>
const *key;
1612 std::unique_ptr<RooAbsData>
data;
1614 std::vector<Component> components;
1615 for (
auto const &
c : info->components) {
1616 auto *found = findDataset(datasets,
c.ref.name);
1619 " is not a data set of the document, or it is already a component of " +
1620 "another combined data set");
1622 components.push_back({&
c.key, std::move(*found)});
1632 bool weighted =
false;
1633 bool storeErrors =
false;
1635 for (
auto const &
c : components) {
1636 vars.
add(*
c.data->get(),
true);
1637 weighted |=
c.data->isWeighted();
1638 if (
auto *ds =
dynamic_cast<RooDataSet const *
>(
c.data.get()); ds && ds->isWeighted()) {
1639 weightName = ds->weightVar()->GetName();
1641 storeErrors |=
dynamic_cast<RooDataHist const *
>(
c.data.get()) !=
nullptr;
1643 std::unique_ptr<RooDataSet> combined;
1645 RooRealVar weightVar{weightName.c_str(), weightName.c_str(), 1.0};
1646 vars.
add(weightVar);
1651 combined = std::make_unique<RooDataSet>(
name.c_str(),
name.c_str(), vars);
1653 for (
auto const &
c : components) {
1654 for (std::size_t i = 0; i < columns.size(); ++i) {
1655 static_cast<RooCategory &
>(vars[columns[i]->GetName()]).setLabel((*
c.key)[i].c_str());
1657 for (
int i = 0; i <
c.data->numEntries(); ++i) {
1660 combined->add(vars,
c.data->weight(), storeErrors ?
c.data->weightError(
RooAbsData::SumW2) : 0.0);
1662 combined->add(vars);
1666 datasets.emplace_back(std::move(combined));
1671void sortByName(T &coll)
1673 std::sort(coll.begin(), coll.end(), [](
auto &
l,
auto &
r) { return strcmp(l->GetName(), r->GetName()) < 0; });
1683 for (
RooAbsArg const *client : arg.clients()) {
1692template <
class Arg_t>
1695 std::vector<Arg_t *> out;
1697 auto *
object =
dynamic_cast<Arg_t *
>(arg);
1699 out.push_back(
object);
1712 vars.
addOwned(createObservable(axis));
1718 std::vector<double>
const &contents,
1719 std::vector<double>
const *errors)
1721 if (errors && errors->size() != contents.size()) {
1723 std::to_string(errors->size()) +
", contents=" + std::to_string(contents.size()));
1726 std::size_t nBins = 1;
1730 if (contents.size() != nBins) {
1732 ", bins=" + std::to_string(nBins));
1734 auto dh = std::make_unique<RooDataHist>(
name,
name, vars);
1737 if (errors && !errors->empty()) {
1740 for (std::size_t ibin = 0; ibin < nBins; ++ibin) {
1741 dh->set(ibin, contents[ibin], errors ? (*errors)[ibin] : -1);
1749 std::vector<std::string>
const &otherLikelihoodTerms)
1751 std::vector<RooCategory *> columns = columnCategories(*tool.
workspace(), dist,
"the combined_dist '" +
name +
"'");
1752 std::map<std::string, RooAbsPdf *> pdfMap;
1753 for (
auto const &component : dist.components) {
1761 std::vector<RooAbsPdf *> terms;
1762 for (std::string
const &term : otherLikelihoodTerms) {
1764 terms.push_back(pdf);
1768 for (
auto const &item : pdfMap) {
1769 channels.
add(*item.second,
true);
1771 for (
auto &item : pdfMap) {
1775 if (!
dynamic_cast<RooProdPdf *
>(&channel) && isConstraintOfChannel(*term, channel, channels)) {
1779 if (!factors.
empty()) {
1780 factors.
add(channel);
1785 if (columns.size() == 1) {
1791 inputs.
add(*column);
1793 std::string
const indexName =
name +
"_index";
1817 if (mc->GetPdf() == &arg || mc->GetPriorPdf() == &arg ||
1818 (mc->GetExternalConstraints() && mc->GetExternalConstraints()->containsInstance(arg))) {
1832 auto const &componentsWithData =
_import->componentsWithData;
1834 return componentsWithData.count(pdf.
GetName()) ||
1836 return client->getAttribute(arrayDistTag) && componentsWithData.count(client->GetName());
1845 auto const &all =
_import->document.rooFit.channelConstraints;
1846 auto found = all.find(dist);
1847 return found != all.end() ? &found->second :
nullptr;
1869 error(
"unable to stream collection " + std::string(coll.
GetName()) +
" to " + node.
key());
1896 return !str.empty() && (std::isalpha(str[0]) || str[0] ==
'_') &&
1897 std::all_of(str.begin(), str.end(), [](
char c) {
return std::isalnum(
c) ||
c ==
'_'; });
1903 std::stringstream ss;
1904 ss <<
"RooJSONFactoryWSTool() name '" <<
name <<
"' is not valid!" << std::endl
1905 <<
"Sanitize names by setting RooJSONFactoryWSTool::config().allowSanitizeNames = true." << std::endl;
1918 return n[
"name"].val();
1924RooRealVar *RooJSONFactoryWSTool::requestImpl<RooRealVar>(
const std::string &objname)
1927 auto found =
_import->defaultValuesByName.find(objname);
1928 if (found !=
_import->defaultValuesByName.end()) {
1936RooAbsPdf *RooJSONFactoryWSTool::requestImpl<RooAbsPdf>(
const std::string &objname)
1939 auto found =
_import->distributions.find(objname);
1940 if (found !=
_import->distributions.end()) {
1946 auto found =
_import->functions.find(objname);
1947 if (found !=
_import->functions.end()) {
1955RooAbsReal *RooJSONFactoryWSTool::requestImpl<RooAbsReal>(
const std::string &objname)
1959 if (std::optional<double> number =
toNumber(objname))
1961 if (
RooAbsPdf *pdf = requestImpl<RooAbsPdf>(objname))
1963 if (
RooRealVar *var = requestImpl<RooRealVar>(objname))
1966 auto found =
_import->functions.find(objname);
1967 if (found !=
_import->functions.end()) {
1991 axis.
name = cv->GetName();
1998 var.
name =
v->GetName();
1999 var.
value = rrv->getVal();
2000 var.
isConst = rrv->isConstant() || rrv->getMin() >= rrv->getMax();
2001 recordDomains(
_export->domains, *rrv);
2006 const std::string &formula)
2010 error(
"cannot write a function of '" + std::string(original->
GetName()) +
2011 "': names of the form 'const_<number>' are reserved for functions of literal constants");
2015 if (
dynamic_cast<RooAbsPdf const *
>(original)) {
2016 error(
"the parameter '" + std::string(original->
GetName()) +
2017 "' would have to be written as an expression that refers to a pdf, which HS3 doesn't allow");
2020 auto &functions =
_export->doc.functions;
2025 trafo_node[
"type"] <<
"generic";
2027 functions.push_back({newname, {
"generic",
nullptr}});
2036std::string makeValidNameOrError(std::string
const &in)
2038 if (!std::isalpha(in[0])) {
2041 std::string out = in;
2042 std::transform(out.begin(), out.end(), out.begin(), [](
char c) { return std::isalnum(c) ? c :
'_'; });
2044 oocoutW(
nullptr, ObjectHandling) <<
"RooFitHS3: changed '" << in <<
"' to '" << out <<
"' to become a valid name";
2051std::string selectionKeyName(std::vector<std::string>
const &key)
2054 for (std::string
const &label : key) {
2055 out += (out.empty() ?
"" :
"_") + makeValidNameOrError(label);
2063 std::vector<RooSimultaneous const *> candidates;
2064 for (
RooAbsArg *arg : workspace.allPdfs()) {
2066 candidates.push_back(candidate);
2069 sortByName(candidates);
2101 std::set<std::string> &exportedObjectNames =
_export->exportedObjectNames;
2103 if (exportedObjectNames.find(
name) != exportedObjectNames.end()) {
2106 auto const &arrayOfComponent =
_export->arrayOfComponent;
2107 if (
auto found = arrayOfComponent.find(
name); found != arrayOfComponent.end() && found->second !=
name) {
2108 error(
"'" +
name +
"' is written as a component of the array distribution '" + found->second +
2109 "', but another object refers to it by its own name. Set "
2110 "RooJSONFactoryWSTool::config().exportArrayDist = false to write it on its own.");
2115 exportedObjectNames.insert(
name);
2123 DepthGuard(
int &
d) : depth{
d} { ++depth; }
2124 ~DepthGuard() { --depth; }
2140 auto const *prod =
dynamic_cast<RooProdPdf const *
>(&func);
2143 "' of an array distribution, but it is not a RooProdPdf");
2145 for (
RooAbsPdf *pdf : static_range_cast<RooAbsPdf *>(prod->pdfList())) {
2147 if (writtenIn !=
name) {
2148 error(
"cannot write '" +
name +
"' as an array distribution: its component '" + pdf->
GetName() +
2149 "' is already written as '" + writtenIn +
"'");
2158 auto &collectionNode = (*
_export->root)[isPdf ?
"distributions" :
"functions"];
2166 auto it = exporters.find(cl);
2167 if (it != exporters.end()) {
2168 for (
auto &exp : it->second) {
2171 if (!exp->exportObject(
this, &func, elem)) {
2176 elem[
"name"] <<
name;
2182 elem[
"name"] <<
name;
2185 if (!
_export->channelConstraints.empty()) {
2186 _export->doc.rooFit.channelConstraints[
name] = std::move(
_export->channelConstraints);
2188 _export->channelConstraints.clear();
2190 recordGenericObject(
_export->doc, isPdf,
name, elem);
2192 if (exp->autoExportDependants()) {
2204 std::cerr <<
"unable to export '" <<
name <<
"' of class '" << cl->
GetName()
2205 <<
"': no exporter is registered for this class, or none of them could handle the object. Check "
2206 "https://root.cern/doc/master/group__roofit__dev__docs__hs3.html to see how to write one!\n";
2216 const std::string pdfName = pdf.
GetName();
2217 auto &arrayOfComponent =
_export->arrayOfComponent;
2218 auto &arrayComponents =
_export->arrayComponents;
2219 auto &exportedObjectNames =
_export->exportedObjectNames;
2220 if (
auto it = arrayOfComponent.find(pdfName); it != arrayOfComponent.end()) {
2224 if (exportedObjectNames.find(pdfName) != exportedObjectNames.end()) {
2227 if (arrayComponents.find(arrayName) == arrayComponents.end()) {
2230 error(
"cannot write the array distribution '" + arrayName +
2231 "': the workspace has another object of that name");
2233 exportedObjectNames.insert(arrayName);
2235 arrayComponents[arrayName].push_back(&pdf);
2236 arrayOfComponent[pdfName] = arrayName;
2237 exportedObjectNames.insert(pdfName);
2245 std::set<std::string> written;
2246 auto &arrayComponents =
_export->arrayComponents;
2248 auto next = std::find_if(arrayComponents.begin(), arrayComponents.end(),
2249 [&](
auto const &item) { return written.find(item.first) == written.end(); });
2250 if (next == arrayComponents.end()) {
2253 const std::string arrayName = next->first;
2254 written.insert(arrayName);
2256 const std::vector<RooAbsPdf const *> components = next->second;
2259 recordGenericObject(
_export->doc,
true, arrayName, elem);
2260 for (
RooAbsPdf const *pdf : components) {
2276 auto found =
_export->constraintObservables.find(pdfName);
2277 return found ==
_export->constraintObservables.end() ? nullptr : &found->second;
2282 auto &out =
_export->constraintTermsOfPdf[pdfName];
2283 for (
auto const &term : terms) {
2284 addUnique(out, term);
2292 std::string
const &dataName)
2294 std::vector<RooAbsPdf const *> components;
2295 if (
auto found =
_export->arrayComponents.find(term); found !=
_export->arrayComponents.end()) {
2296 components = found->second;
2299 for (
RooAbsArg const *factor : product->pdfList()) {
2300 components.push_back(
static_cast<RooAbsPdf const *
>(factor));
2303 components.push_back(pdf);
2305 std::vector<std::string> observables;
2306 for (
RooAbsPdf const *pdf : components) {
2308 if (!info || !globalObservables || !globalObservables->
find(info->observable.c_str())) {
2311 observables.push_back(info->observable);
2313 if (observables.empty()) {
2316 if (!
_export->writtenConstraintData.insert(dataName).second) {
2320 error(
"cannot write the observed values of the constraint term '" + term +
"' as the data set '" + dataName +
2321 "': the workspace has a data set of that name");
2324 for (std::string
const &
name : observables) {
2327 error(
"the observable '" +
name +
"' of the constraint term '" + term +
"' is not in the workspace");
2332 if (
auto const *rrv =
dynamic_cast<RooRealVar const *
>(var)) {
2333 axis = toContinuousAxis(*rrv);
2335 data.axes.emplace_back(std::move(axis));
2339 if (
data.axes.size() == 1) {
2342 point.
axes.push_back(std::get<HS3::ContinuousAxis>(
data.axes.front()));
2343 _export->doc.data.push_back({dataName, std::move(point)});
2346 _export->doc.data.push_back({dataName, std::move(
data)});
2367 combined.
columns.push_back(column->GetName());
2372 std::set<RooAbsArg const *> replaced;
2373 std::vector<RooAbsArg const *> replacements;
2377 for (std::vector<std::string>
const &key :
selectionKeys(columns)) {
2378 const std::string label =
selectionLabel(indexCat, columns, key);
2384 auto found = renamed->second.find(label);
2385 if (found != renamed->second.end()) {
2386 replaced.insert(pdf);
2387 pdf = found->second;
2388 replacements.push_back(pdf);
2393 _export->doc.distributions.push_back({simPdf.
GetName(), std::move(combined)});
2395 std::vector<RooAbsArg const *> servers;
2397 if (replaced.count(server) == 0) {
2398 servers.push_back(server);
2401 servers.insert(servers.end(), replacements.begin(), replacements.end());
2412 if (found !=
_export->categories.end() && found->second.categories != axis.
categories) {
2414 "' is written to the domain twice, with different states");
2444 std::string prefix = genPrefix(
p,
true);
2445 if (!prefix.empty())
2447 if (!
p.has_child(
"type")) {
2451 std::string functype(
p[
"type"].val());
2454 if (!importAllDependants) {
2458 auto it = importers.find(functype);
2460 if (it != importers.end()) {
2461 for (
auto &imp : it->second) {
2462 ok = imp->importArg(
this,
p);
2469 "RooJSONFactoryWSTool() no handling for type '" + functype +
2470 "' implemented. Check https://root.cern/doc/master/group__roofit__dev__docs__hs3.html "
2471 "to see how to write an importer for it!");
2477 for (std::string
const &component : recorder.unreadComponents()) {
2478 warning(
"RooFitHS3: " +
name +
": unknown component \"" + component +
"\" is ignored");
2485 if (
auto const *generic = std::get_if<HS3::GenericObject>(&distribution.
body)) {
2489 std::string
const &
name = distribution.
name;
2498 if (
auto const *combined = std::get_if<HS3::CombinedDist>(&distribution.
body)) {
2508 this->
importFunction((JSONTree::create(jsonString))->rootnode(), importAllDependants);
2534 if (!
config().renameCombinedObservables) {
2537 std::vector<RooAbsData *> datasets;
2539 datasets.push_back(
data);
2541 sortByName(datasets);
2544 if (columns.empty()) {
2548 for (std::vector<std::string>
const &key :
selectionKeys(columns)) {
2549 const std::string keyName = selectionKeyName(key);
2555 component = simPdf->
getPdf(label);
2560 candidates.
add(*observables);
2564 std::vector<RooAbsArg *> observables;
2567 observables.push_back(arg);
2570 sortByName(observables);
2571 std::map<std::string, std::string> renames;
2572 for (
RooAbsArg const *obs : observables) {
2573 std::string
const newName =
2574 "obs_" + keyName + (observables.size() == 1 ?
"" :
"_" + std::string(obs->GetName()));
2575 if (newName == obs->GetName()) {
2579 error(
"can't rename the observable '" + std::string(obs->GetName()) +
"' of the data set '" +
2580 data->GetName() +
"' to '" + newName +
"': an object of that name already exists");
2582 renames[obs->GetName()] = newName;
2584 if (renames.empty()) {
2594 for (
auto const &[from, to] : renames) {
2599 auto *renamedComponent =
static_cast<RooAbsPdf *
>(customizer.build());
2619 std::string
const name =
data.GetName();
2622 combined.
columns.push_back(column->GetName());
2628 std::unique_ptr<RooSuperCategory> superCat;
2630 std::vector<std::unique_ptr<RooAbsData>> parts;
2633 parts =
data.split(*simPdf,
true);
2634 }
else if (columns.size() == 1) {
2635 indexCat = columns.front();
2636 parts =
data.split(*indexCat,
true);
2640 inputs.
add(*column);
2642 const std::string superName =
name +
"_key";
2643 superCat = std::make_unique<RooSuperCategory>(superName.c_str(), superName.c_str(), inputs);
2644 indexCat = superCat.get();
2645 parts =
data.split(*indexCat,
true);
2649 std::vector<RooAbsData *> subDataSets;
2650 for (std::vector<std::string>
const &key :
selectionKeys(columns)) {
2651 std::string
const label =
selectionLabel(*indexCat, columns, key);
2652 auto found = std::find_if(parts.begin(), parts.end(), [&](
auto &
d) { return d->GetName() == label; });
2653 if (found == parts.end() || ((*found)->numEntries() == 0 && simPdf && !simPdf->
getPdf(label))) {
2656 const std::string keyName = selectionKeyName(key);
2657 const std::string subName =
name +
"_" + keyName;
2658 (*found)->SetName(subName.c_str());
2662 auto renames = renamesOfData->second.find(keyName);
2663 if (renames != renamesOfData->second.end()) {
2664 for (
auto const &[from, to] : renames->second) {
2665 (*found)->changeObservableName(from.c_str(), to.c_str());
2669 combined.
components.push_back({key, {subName}});
2670 subDataSets.push_back(found->get());
2672 _export->doc.data.push_back({
name, std::move(combined)});
2683 error(
"the data set '" + std::string(
data.GetName()) +
"' has the categorical observable '" + obs->GetName() +
2684 "', which is not a RooCategory, and can't be written as a combined data set");
2688 auto write = [&](
auto body) {
_export->doc.data.push_back({
data.GetName(), std::move(body)}); };
2697 if (
auto weightVar = variables.find(
"weightVar")) {
2698 variables.remove(*weightVar);
2703 for (
RooAbsArg const *arg : variables) {
2704 if (
auto const *cat =
dynamic_cast<RooCategory const *
>(arg)) {
2713 return write(toBinnedData(variables, dh->numEntries(), dh->weightArray()));
2723 if (
data.isWeighted() && variables.size() == 1 &&
dynamic_cast<RooRealVar const *
>(variables[0]) &&
2725 auto &
x =
static_cast<RooRealVar const &
>(*variables[0]);
2726 std::vector<double> contents;
2728 for (; i <
data.numEntries(); ++i) {
2730 if (
x.getBin() != i)
2732 contents.push_back(
data.weight());
2736 if (i ==
x.getBins() &&
data.numEntries() ==
x.getBins()) {
2737 return write(toBinnedData(variables, contents.size(), contents.data()));
2742 if (variables.size() == 1 &&
data.numEntries() == 1) {
2744 auto const *var =
dynamic_cast<RooRealVar const *
>(variables[0]);
2745 if (var && (!
data.isWeighted() ||
data.weight() == 1.)) {
2748 point.
axes.push_back(toContinuousAxis(*var));
2749 return write(std::move(point));
2755 for (
RooAbsArg const *arg : variables) {
2757 if (
auto const *cat =
dynamic_cast<RooCategory const *
>(arg)) {
2758 unbinned.axes.emplace_back(toCategoricalAxis(*cat));
2761 auto const &var =
static_cast<RooRealVar const &
>(*arg);
2762 unbinned.axes.emplace_back(toContinuousAxis(var));
2765 std::vector<double> weights;
2766 for (
int i = 0; i <
data.numEntries(); ++i) {
2769 for (
RooAbsArg const *arg : variables) {
2770 if (
auto const *cat =
dynamic_cast<RooCategory const *
>(arg)) {
2773 unbinned.entries.push_back(
static_cast<RooRealVar const *
>(arg)->
getVal());
2776 weights.push_back(
data.weight());
2779 if (std::any_of(weights.begin(), weights.end(), [](
double weight) { return weight != 1.; })) {
2780 unbinned.weights = std::move(weights);
2782 write(std::move(unbinned));
2796std::unique_ptr<RooDataHist>
2799 if (!
n.has_child(
"contents"))
2802 JSONNode const &contents =
n[
"contents"];
2807 std::vector<double> contentVals;
2808 contentVals << contents;
2810 if (!
n.has_child(
"errors")) {
2813 if (!
n[
"errors"].is_seq())
2815 std::vector<double> errorVals;
2816 errorVals <<
n[
"errors"];
2828 if (
p.value && std::holds_alternative<std::string>(*
p.value))
2830 if (!
_import->ranges.contains(
p.name)) {
2832 auto const &attributes =
_import->document.rooFit.attributes;
2833 auto found = attributes.find(
p.name);
2834 if (found != attributes.end() && found->second.isConstVar) {
2838 wsEmplace<RooConstVar>(
p.name, std::get<double>(*
p.value));
2842 RooRealVar &var = wsEmplace<RooRealVar>(
p.name, 1.);
2843 configureVariable(
_import->ranges,
p, var);
2851 warning(std::string(
"RooFitHS3: the parameter '") + var.
GetName() +
2852 "' is not listed in any domain and is not constant: HS3 requires a domain, or \"const\": true. It "
2853 "is read as a free parameter without range.");
2860 for (
const char *section : {
"functions",
"distributions"}) {
2861 if (
auto seq =
n.find(section)) {
2862 for (
const auto &
p : seq->children()) {
2871 const std::vector<RooAbsData *> &singleDataSets)
2873 std::vector<std::string> dataNames;
2876 const std::vector<std::string> columns = namesOf(
selectionColumns(simpdf->indexCat()));
2878 auto const *combined = std::get_if<HS3::CombinedData>(&
data.body);
2879 if (combined && combined->columns == columns) {
2880 dataNames.push_back(
data.name);
2886 dataNames.push_back(
data->GetName());
2892 for (std::string
const &dataName : dataNames) {
2895 if (dataNames.empty()) {
2904std::vector<RooAbsPdf const *> channelsOf(
RooAbsPdf const &pdf, std::map<std::string, RooAbsPdf const *>
const &renamed)
2910 std::vector<RooAbsPdf const *> channels;
2911 for (
auto const &item : simPdf->indexCat()) {
2912 auto found = renamed.find(item.first);
2913 if (
RooAbsPdf const *component = found != renamed.end() ? found->second : simPdf->getPdf(item.first)) {
2914 channels.push_back(component);
2921struct ChannelConstraint {
2922 std::string channel;
2924 bool histFactory =
false;
2926 bool wholeChannel =
false;
2942std::vector<ChannelConstraint>
2944 std::function<
bool(std::string
const &)>
const &isHistFactoryChannel,
2945 std::vector<RooAbsPdf const *>
const &channels)
2949 std::unique_ptr<RooArgSet> constraints{pdf.
getAllConstraints(observables, params,
false)};
2950 std::vector<ChannelConstraint> out;
2951 for (
RooAbsPdf const *channel : channels) {
2952 auto const *prod =
dynamic_cast<RooProdPdf const *
>(channel);
2956 const bool histFactory = isHistFactoryChannel(prod->GetName());
2959 if (!histFactory && !prod->pdfList().empty() &&
2960 std::all_of(prod->pdfList().begin(), prod->pdfList().end(),
2961 [&](
RooAbsArg const *factor) { return constraints->find(*factor) != nullptr; })) {
2962 out.push_back({prod->GetName(), prod,
false,
true});
2967 bool usedElsewhere =
false;
2968 for (
RooAbsArg const *client : prod->clients()) {
2969 if (ws.
arg(client->GetName()) == client && !
dynamic_cast<RooSimultaneous const *
>(client)) {
2970 usedElsewhere =
true;
2973 if (usedElsewhere) {
2978 auto const *nested =
dynamic_cast<RooProdPdf const *
>(&term);
2979 if (!histFactory || !nested) {
2980 out.push_back({prod->GetName(), &term, histFactory});
2983 for (
RooAbsArg const *factor : nested->pdfList()) {
2984 add(
static_cast<RooAbsPdf const &
>(*factor));
2987 for (
RooAbsArg const *factor : prod->pdfList()) {
2988 if (constraints->
find(*factor)) {
2989 add(
static_cast<RooAbsPdf const &
>(*factor));
2999 std::string
const *dataName)
3005 analysis.
name = analysisName;
3009 likelihood.
name = analysisName +
"_likelihood";
3016 likelihood.
terms.push_back({{pdf->
GetName()}, std::monostate{}});
3026 std::vector<std::string> auxiliaryChannelsOfModel;
3027 std::vector<std::string> constraintTerms;
3030 addUnique(constraintTerms, constr->GetName());
3036 auto writtenByHistFactory = [&](std::string
const &channel) {
3037 std::vector<std::string> out;
3038 if (
auto found =
_export->constraintTermsOfPdf.find(channel); found !=
_export->constraintTermsOfPdf.end()) {
3039 for (std::string
const &term : found->second) {
3040 addUnique(out, term);
3044 for (
auto const &ref : found->second) {
3045 addUnique(out, ref.name);
3051 if (!observables || observables->
empty()) {
3054 for (
RooAbsPdf const *channel : channels) {
3055 for (std::string
const &term : writtenByHistFactory(channel->
GetName())) {
3056 addUnique(constraintTerms, term);
3060 auto isHistFactoryChannel = [&](std::string
const &
name) {
3065 std::map<std::string, std::vector<std::string>> termsOfChannel;
3066 for (ChannelConstraint
const &item :
3067 constraintsOfChannels(
_workspace, *pdf, *observables, isHistFactoryChannel, channels)) {
3068 if (item.wholeChannel) {
3069 auxiliaryChannelsOfModel.push_back(item.channel);
3073 std::string term = item.constraint->GetName();
3074 if (
auto found =
_export->arrayOfComponent.find(term); found !=
_export->arrayOfComponent.end()) {
3075 term = found->second;
3077 addUnique(termsOfChannel[item.channel], term);
3079 for (
RooAbsPdf const *channelPdf : channels) {
3080 std::string
const channel = channelPdf->
GetName();
3081 std::vector<std::string>
const &found = termsOfChannel[channel];
3082 if (!isHistFactoryChannel(channel)) {
3083 for (std::string
const &term : found) {
3084 addUnique(constraintTerms, term);
3091 std::vector<std::string>
const written = writtenByHistFactory(channel);
3092 std::set<std::string>
const writtenSet{written.begin(), written.end()};
3093 std::set<std::string>
const foundSet{found.begin(), found.end()};
3094 if (writtenSet != foundSet) {
3095 auto join = [](std::set<std::string>
const &names) {
3097 for (std::string
const &
name : names) {
3098 out += (out.empty() ?
"'" :
", '") +
name +
"'";
3100 return out.empty() ? std::string{
"none"} : out;
3102 error(
"cannot export the constraint terms of the HistFactory channel '" + channel +
3103 "': the channel writes " + join(writtenSet) +
", but RooFit finds " + join(foundSet));
3105 for (std::string
const &term : written) {
3106 addUnique(constraintTerms, term);
3118 auto findCombinedData = [&]() {
return dataName ? HS3::find<HS3::CombinedData>(doc.
data, *dataName) :
nullptr; };
3120 if (simPdf && findCombinedData()) {
3122 if (columns.size() == 1) {
3123 indexColumn = columns.front();
3126 auto addAuxiliaryChannel = [&](std::string
const &term) {
3128 error(
"cannot write the constraint term '" + term +
"' as a component of the combined distribution '" +
3129 simPdf->
GetName() +
"': its index category '" + indexColumn->
GetName() +
"' has a state of that name");
3131 addUnique(
_export->auxiliaryLabels[indexColumn->
GetName()], term);
3133 if (!combinedDist) {
3134 error(
"cannot write the constraint terms of '" + std::string(simPdf->
GetName()) +
3135 "' as its components: it was not written as a combined distribution");
3137 if (!combinedDist->find({term})) {
3138 combinedDist->components.push_back({{term}, {term}});
3142 combinedData->components.push_back({{term},
HS3::DataRef{*dataName +
"_" + term}});
3148 for (std::string
const &channel : auxiliaryChannelsOfModel) {
3149 if (!indexColumn || likelihood.
pureModel) {
3153 for (
auto const &state : simPdf->
indexCat()) {
3155 if (component && channel == component->
GetName()) {
3156 label = state.first;
3159 if (label.empty()) {
3162 auto const *component = findCombinedData()->find({label});
3163 const std::string observed = component ? component->ref.name : *dataName +
"_" + label;
3165 auto written = std::find_if(doc.
data.begin(), doc.
data.end(), [&](
auto const &
d) { return d.name == observed; });
3166 if (written == doc.
data.end() || written->numEntries() == 0) {
3167 std::optional<HS3::Data> empty;
3168 if (written != doc.
data.end()) {
3169 empty = std::move(*written);
3170 doc.
data.erase(written);
3174 combinedData->components.push_back({{label},
HS3::DataRef{observed}});
3178 doc.
data.push_back(std::move(*empty));
3180 warning(
"RooFitHS3: the channel '" + channel +
"' of '" + simPdf->
GetName() +
3181 "' consists of constraint terms, but has no data, and its observables are not all global "
3182 "observables of the ModelConfig: it is written without an observation.");
3188 marked.push_back({channel});
3191 for (std::string
const &term : constraintTerms) {
3195 const bool asChannel = indexColumn &&
_export->arrayComponents.count(term);
3196 const std::string constraintData = asChannel ? *dataName +
"_" + term : term +
"_data";
3200 addAuxiliaryChannel(term);
3211 std::vector<std::string> names = namesOf(*pois);
3212 std::sort(names.begin(), names.end());
3213 for (
auto const &
name : names) {
3218 auto writeProductDomain = [&](
const char *suffix,
RooArgSet const *args) {
3219 if (!args || args->
empty())
3222 domain.
name = analysisName + suffix;
3226 for (
auto *var : static_range_cast<const RooRealVar *>(sorted)) {
3243 doc.
analyses.push_back(std::move(analysis));
3251 std::sort(names.begin(), names.end());
3259 _export = std::make_unique<HS3::ToolExportState>();
3261 StateScope<HS3::ToolExportState> exportScope{
_export};
3275 RooArgSet const *globs = mc ? mc->GetGlobalObservables() :
nullptr;
3280 auto const *gauss =
dynamic_cast<RooGaussian const *
>(arg);
3281 if (gauss && globs->
find(gauss->getMean()) && !globs->
find(gauss->getX()) &&
3297 std::vector<RooAbsData *> alldata;
3299 alldata.push_back(
d);
3301 sortByName(alldata);
3303 std::vector<RooAbsData *> singleData;
3304 for (
auto &
d : alldata) {
3310 singleData.push_back(
d);
3331 if (std::any_of(allpdfs.begin(), allpdfs.end(), [&](
RooAbsPdf *pdf) { return pdf->dependsOn(*arg); })) {
3333 snapshotSorted.
add(*arg);
3336 snapshotSorted.
sort();
3337 std::string
name(snsh->GetName());
3338 if (
name !=
"default_values") {
3346 for (
auto const &item :
_export->auxiliaryLabels) {
3347 auto &categories =
_export->categories[item.first].categories;
3348 for (std::string
const &label : item.second) {
3349 if (std::find(categories.begin(), categories.end(), label) == categories.end()) {
3350 categories.push_back(label);
3356 for (
auto const &item :
_export->categories) {
3357 if (defaultAxes.count(item.first)) {
3359 "' is categorical, but the domain already lists it as continuous");
3363 for (
auto &item :
_export->domains) {
3365 appendDomain(doc.
domains, item.first, std::move(item.second),
3367 : std::map<std::string, HS3::CategoricalAxis>{});
3374 if (
n.has_child(
"distributions")) {
3375 std::map<std::string, JSONNode *> nodes;
3376 for (
JSONNode &node :
n[
"distributions"].children()) {
3377 nodes.emplace(
name(node), &node);
3380 auto found = nodes.find(dist.name);
3381 if (found == nodes.end()) {
3384 if (
auto *generic = std::get_if<HS3::GenericObject>(&dist.body)) {
3385 generic->node = found->second;
3386 }
else if (
auto const *combined = std::get_if<HS3::CombinedDist>(&dist.body)) {
3396 for (std::string
const &factor : factors) {
3397 oocoutI(
nullptr, IO) <<
"RooFitHS3: '" << factor <<
"' is a factor of '" << product
3398 <<
"' and a term of its likelihood; it is written once, as a term of the likelihood"
3408 validateOptions.
isExternal = [](std::string
const &) {
return false; };
3411 for (
auto &diagnostic : diagnostics) {
3412 diagnostic.severity = HS3::Diagnostic::Severity::Warning;
3413 diagnostic.message =
"the exported document is not valid: " + diagnostic.message;
3421 if (!
config().allowExportSharedVariates) {
3423 for (std::string
const &
message : shared) {
3427 ". HS3 requires the terms of a likelihood and the components of a combined distribution to have "
3428 "distinct variates: give the variates their own names (e.g. 'mass_SR' and 'mass_CR' instead of "
3429 "'mass'; RooJSONFactoryWSTool::config().renameCombinedObservables does this for the observables of "
3430 "combined data sets), or set RooJSONFactoryWSTool::config().allowExportSharedVariates = true to "
3431 "write the document anyway, with a warning, and rename them in the JSON.");
3433 for (std::string
const &
message : shared) {
3435 ". HS3 requires distinct variates: the document is not valid HS3, and can't be read back before the "
3436 "variates are renamed.");
3444 std::stringstream ss(s);
3451 std::stringstream ss;
3459 std::unique_ptr<JSONTree> tree = JSONTree::create();
3460 JSONNode &
n = tree->rootnode().set_map();
3469 std::ofstream out(
filename.c_str());
3479 auto const &attributes =
_import->document.rooFit.attributes;
3480 auto found = attributes.find(obj);
3481 if (found == attributes.end())
3483 auto const &tags = found->second.tags;
3484 return std::find(tags.begin(), tags.end(), attrib) != tags.end();
3488 _export->doc.rooFit.attributes[obj].tags.push_back(attrib);
3495 auto const &attributes =
_import->document.rooFit.attributes;
3496 auto found = attributes.find(obj);
3497 if (found == attributes.end())
3499 auto value = found->second.dict.find(attrib);
3500 return value != found->second.dict.end() ?
value->second :
"";
3503 const std::string &
value)
3505 _export->doc.rooFit.attributes[obj].dict[attrib] =
value;
3514 auto histFactoryImporters = importers.find(
"histfactory_dist");
3515 options.
typedHistFactory = histFactoryImporters != importers.end() && !histFactoryImporters->second.empty() &&
3521 std::optional<RooFit::ValidatedHS3Document> validated;
3526 validated =
HS3::validate(std::move(parsed), diagnostics, validateOptions);
3531 std::set<std::string>
const bookkeepingColumns = checkVariates(validated->doc());
3542 StateScope<HS3::ToolImportState> importScope{
_import};
3543 _import = std::make_unique<HS3::ToolImportState>(doc);
3548 for (
auto const &
p :
_import->defaultValues->parameters) {
3552 for (
auto const &function : doc.
functions) {
3566 for (
auto const &var : point.parameters) {
3567 std::string
const *label = var.value ? std::get_if<std::string>(&*var.value) :
nullptr;
3571 }
else if (var.value) {
3572 cat->
setIndex(
static_cast<int>(std::get<double>(*var.value)));
3578 warning(
"RooFitHS3: ignoring the string value of parameter '" + var.name +
"' in parameter point '" +
3579 point.name +
"', because there is no category with that name.");
3587 rrv = &wsEmplace<RooRealVar>(var.name, 1.);
3588 configureVariable(
_import->ranges, var, *rrv);
3591 configureVariable(
_import->ranges, var, *rrv);
3602 importAttributes(arg, item.second);
3608 std::vector<std::unique_ptr<RooAbsData>> datasets;
3609 datasets.reserve(doc.
data.size());
3610 for (
auto const &
data : doc.
data) {
3611 if (!std::holds_alternative<HS3::CombinedData>(
data.body)) {
3615 assembleCombinedData(doc,
_workspace, datasets);
3617 for (
auto const &analysis : doc.
analyses) {
3618 importAnalysis(doc, analysis,
_workspace, datasets, bookkeepingColumns, auxiliary.
terms);
3621 combineDatasets(doc, datasets);
3625 for (
auto &
d : datasets) {
3626 if (
d && auxiliary.
data.count(
d->GetName())) {
3631 for (
auto const &
d : datasets) {
3634 for (
auto const &obs : *
d->get()) {
3635 if (
auto *rrv =
dynamic_cast<RooRealVar *
>(obs)) {
3636 _workspace.
var(rrv->GetName())->setBinning(rrv->getBinning());
3653 std::unique_ptr<JSONTree> tree = JSONTree::create(is);
3661 std::ifstream infile(
filename.c_str());
3662 if (!infile.is_open())
3673 if (
n.find(
"poi")) {
3676 std::string poi =
n.find(
"poi")->val();
3677 std::string pdname =
n.find(
"pdfName")->val();
3679 modelConfig.SetPdf(pdname.c_str());
3692 }
else if (
type->val() ==
"binned" ||
type->val() ==
"unbinned") {
3708 if (!
value && !(min && max)) {
3709 throw std::invalid_argument(
"Invalid Syntax: Please provide either 'value' or 'min' and 'max' or both");
3712 axis.
name = elementNode[
"name"].
val();
3716 axis.
min = min->val_double();
3717 axis.
max = max->val_double();
3725 StateScope<HS3::ToolImportState> importScope{
_import};
3726 _import = std::make_unique<HS3::ToolImportState>(doc);
3730 configureVariable(
_import->ranges, parameter, *rrv);
3744 throw std::runtime_error(s);
3753 for (
char c : str) {
3758 case '(':
result +=
'_';
break;
3763 case '.':
result +=
"_dot_";
break;
3764 case '@':
result +=
"at";
break;
3765 case '-':
result +=
"minus";
break;
3766 case '/':
result +=
"_div_";
break;
3779 if (onlyModelConfig) {
3788 for (
auto *pdf : ws.
allPdfs()) {
3789 if (isTopLevel(*pdf, ws)) {
3795 if (isTopLevel(*func, ws)) {
3814 auto *snshSet =
dynamic_cast<RooArgSet *
>(snsh);
3816 tmpWS.
saveSnapshot(snshSet->GetName(), *snshSet,
true);
3835 auto sanitize = [](
auto const &objects) {
3836 for (
auto *obj : objects) {
3847 sanitize(*
data->get());
3852 auto *snsh =
dynamic_cast<RooArgSet *
>(snshObj);
3854 std::cerr <<
"Warning: found snapshot that is not a RooArgSet, skipping\n";
3862 if (
auto *named =
dynamic_cast<TNamed *
>(obj)) {
3863 named->SetName(
sanitizeName(named->GetName()).c_str());
3865 std::cerr <<
"Warning: object " << obj->GetName() <<
" is not TNamed, cannot rename.\n";
3868 sanitize(*mc->GetObservables());
3869 sanitize(*mc->GetParametersOfInterest());
3870 sanitize(*mc->GetNuisanceParameters());
3871 sanitize(*mc->GetGlobalObservables());
3875 newWS.
SetName((std::string{ws.
GetName()} +
"_sanitized").c_str());
void writeArrayDist(RooJSONFactoryWSTool &tool, JSONNode &elem, std::string const &name, std::vector< RooAbsPdf const * > const &components)
std::optional< ArrayComponentInfo > arrayComponentInfo(RooAbsPdf const &pdf, std::string const *observable, RooJSONFactoryWSTool *tool)
std::optional< double > toNumber(const std::string &str)
std::vector< std::vector< std::string > > selectionKeys(std::vector< RooCategory const * > const &columns)
void writeReference(JSONNode &node, RooAbsArg const &arg)
bool isLiteralIdentifier(std::string_view name)
std::string transformedBaseName(RooAbsReal const ¶m)
std::vector< RooCategory const * > selectionColumns(RooAbsCategory const &indexCat)
std::string transformedArgument(RooAbsReal const ¶m)
std::string selectionKeyToString(std::vector< std::string > const &key)
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)
constexpr const char * helperObjectTag
constexpr const char * arrayDistTag
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
static unsigned int total
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t child
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t TPoint TPoint const char text
Common abstract base class for objects that represent a value and a "shape" in RooFit.
bool dependsOn(const RooAbsCollection &serverList, const RooAbsArg *ignoreArg=nullptr, bool valueOnly=false) const
Test whether we depend on (ie, are served by) any object in the specified collection.
TClass * IsA() const override
void setStringAttribute(const Text_t *key, const Text_t *value)
Associate string 'value' to this object under key 'key'.
bool isConstant() const
Check if the "Constant" attribute is set.
virtual TObject * clone(const char *newname=nullptr) const =0
RooFit::OwningPtr< RooArgSet > getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
RooFit::OwningPtr< RooArgSet > getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
RooFit::OwningPtr< RooArgSet > getComponents() const
Create a RooArgSet with all components (branch nodes) of the expression tree headed by this object.
const RefCountList_t & servers() const
List of all servers of this object.
bool getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
const RefCountList_t & clients() const
List of all clients of this object.
void setAttribute(const Text_t *name, bool value=true)
Set (default) or clear a named boolean attribute of this object.
void leafNodeServerList(RooAbsCollection *list, const RooAbsArg *arg=nullptr, bool recurseNonDerived=false) const
Fill supplied list with all leaf nodes of the arg tree, starting with ourself as top node.
Abstract base class for RooRealVar binning definitions.
Int_t numBins() const
Return number of bins.
virtual double binLow(Int_t bin) const =0
virtual bool isUniform() const
virtual double highBound() const =0
virtual double lowBound() const =0
virtual double binHigh(Int_t bin) const =0
Abstract base class for objects that represent a discrete value that can be set from the outside,...
A space to attach TBranches.
unsigned int getCurrentOrdinalNumber() const
Return ordinal number of the current state.
bool hasLabel(const std::string &label) const
Check if a state with name label exists.
const std::map< std::string, value_type >::value_type & getOrdinal(unsigned int n) const
Return name and index of the nth defined state.
std::size_t size() const
Number of states defined.
const RooCatType * defineType(const char *label)
Abstract container object that can hold multiple RooAbsArg objects.
bool equals(const RooAbsCollection &otherColl) const
Check if this and other collection have identically-named contents.
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
const char * GetName() const override
Returns name of object.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
const_iterator end() const
void assign(const RooAbsCollection &other) const
Sets the value, cache and constant attribute of any argument in our set that also appears in the othe...
Storage_t::size_type size() const
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
virtual RooAbsArg * addClone(const RooAbsArg &var, bool silent=false)
Add a clone of the specified argument to list.
const_iterator begin() const
void sort(bool reverse=false)
Sort collection using std::sort and name comparison.
void setName(const char *name)
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract base class for binned and unbinned datasets.
Abstract base class for objects that are lvalues, i.e.
virtual Int_t numBins(const char *rangeName=nullptr) const =0
Abstract interface for all probability density functions.
std::unique_ptr< RooArgSet > getAllConstraints(const RooArgSet &observables, RooArgSet &constrainedParams, bool stripDisconnected=true) const
This helper function finds and collects all constraints terms of all component p.d....
bool canBeExtended() const
If true, PDF can provide extended likelihood term.
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
virtual Int_t getBins(const char *name=nullptr) const
Get number of bins of currently defined range.
void setConstant(bool value=true)
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
Abstract base class for objects that represent a real value and implements functionality common to al...
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
bool containsInstance(const RooAbsArg &var) const override
Check if this exact instance is in this collection.
Implements a RooAbsBinning in terms of an array of boundary values, posing no constraints on the choi...
Object to represent discrete states.
bool setIndex(Int_t index, bool printError=true) override
Set value by specifying the index code of the desired state.
bool defineType(const std::string &label)
Define a state with given name.
bool setLabel(const char *label, bool printError=true) override
Set value by specifying the name of the desired state.
Named container for two doubles, two integers two object points and three string pointers that can be...
const char * getString(Int_t idx) const
Return string stored in slot idx.
Represents a constant real-valued object.
RooCustomizer is a factory class to produce clones of a prototype composite PDF object with the same ...
Container class to hold N-dimensional binned data.
Container class to hold unbinned data.
JSONNode & append_child()
size_t num_children() const
JSONNode const * find(std::string const &key) const
The component at the end of a path of keys, or nullptr if a component on the path doesn't exist.
bool has_child(std::string const &) const
Records which components of a JSON map are read while it is alive, through any node of the tree.
static std::unique_ptr< JSONTree > create()
An HS3 document. It is move-only, because generic objects refer to the JSON tree of the document.
std::vector< HS3::Analysis > analyses
std::vector< HS3::Data > data
std::vector< HS3::Likelihood > likelihoods
std::vector< HS3::Function > functions
std::vector< HS3::ParameterPoint > parameterPoints
HS3::RooFitInternal rooFit
std::vector< HS3::Domain > domains
std::vector< HS3::Distribution > distributions
Where RooFit takes the ranges of variables from: the "default_domain" if there is one,...
DomainAxis const * find(std::string const &name) const
The axis that defines the range of a variable, or null.
bool contains(std::string const &name) const
Whether any domain has the variable.
static RooMsgService & instance()
Return reference to singleton instance.
std::ostream & log(const RooAbsArg *self, RooFit::MsgLevel level, RooFit::MsgTopic topic, bool forceSkipPrefix=false)
Log error message associated with RooAbsArg object self at given level and topic.
static constexpr double infinity()
Return internal infinity representation.
static constexpr int isInfinite(double x)
Return true if x is infinite by RooNumber internal specification.
Efficient implementation of a product of PDFs of the form.
Binning/range definition that only defines a range but no binning.
Variable that can be changed from the outside.
void removeMin(const char *name=nullptr)
Remove lower range limit for binning with given name. Empty name means default range.
void setVal(double value) override
Set value of variable to 'value'.
void setError(double value)
void setMin(const char *name, double value, bool shared=true)
Set minimum of name range to given value.
void setRange(const char *name, double min, double max, bool shared=true)
Set a fit or plotting range.
void setBins(Int_t nBins, const char *name=nullptr, bool shared=true)
Create a uniform binning under name 'name' for this variable.
bool hasError(bool allowZero=true) const
void setBinning(const RooAbsBinning &binning, const char *name=nullptr, bool shared=true)
Add given binning under name 'name' with this variable.
void setMax(const char *name, double value, bool shared=true)
Set maximum of name range to given value.
void removeMax(const char *name=nullptr)
Remove upper range limit for binning with given name. Empty name means default range.
const RooAbsBinning & getBinning(const char *name=nullptr, bool verbose=true, bool createOnTheFly=false, bool shared=true) const override
Return binning definition with name.
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
RooAbsPdf * getPdf(RooStringView catName) const
Return the p.d.f associated with the given index category name.
const RooAbsCategoryLValue & indexCat() const
< A class that holds configuration information for a model using a workspace as a store
const std::string & GetSnapshotName() const
get the name of the workspace snapshot saved by SetSnapshot() (empty if there is none)
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return nullptr if not existing)
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return nullptr if not existing)
void SetWS(RooWorkspace &ws) override
Set a workspace that owns all the necessary components for the analysis.
const RooArgSet * GetObservables() const
get RooArgSet for observables (return nullptr if not existing)
const RooArgSet * GetExternalConstraints() const
get RooArgSet for global observables (return nullptr if not existing)
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
RooAbsPdf * GetPriorPdf() const
get parameters prior pdf (return nullptr if not existing)
Joins several RooAbsCategoryLValue objects into a single category.
Persistable container for RooFit projects.
TObject * obj(RooStringView name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name)
const RooArgSet * getSnapshot(const char *name) const
Return the RooArgSet containing a snapshot of variables contained in the workspace.
RooAbsPdf * pdf(RooStringView name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
RooCategory * cat(RooStringView name) const
Retrieve discrete variable (RooCategory) with given name. A null pointer is returned if not found.
RooArgSet allVars() const
Return set with all variable objects.
RooArgSet argSet(RooStringView nameList) const
Return set of RooAbsArgs matching to given list of names.
RooArgSet allResolutionModels() const
Return set with all resolution model objects.
bool saveSnapshot(RooStringView, const char *paramNames)
Save snapshot of values and attributes (including "Constant") of given parameters.
RooArgSet allPdfs() const
Return set with all probability density function objects.
std::list< RooAbsData * > allData() const
Return list of all dataset in the workspace.
RooLinkedList const & getSnapshots() const
std::list< TObject * > allGenericObjects() const
Return list of all generic objects in the workspace.
RooAbsReal * function(RooStringView name) const
Retrieve function (RooAbsReal) with given name. Note that all RooAbsPdfs are also RooAbsReals....
RooAbsArg * arg(RooStringView name) const
Return RooAbsArg with given name. A null pointer is returned if none is found.
const RooArgSet & components() const
RooArgSet allFunctions() const
Return set with all function objects.
RooRealVar * var(RooStringView name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found.
RooAbsData * data(RooStringView name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found.
std::list< RooAbsData * > allEmbeddedData() const
Return list of all dataset in the workspace.
bool loadSnapshot(const char *name)
Load the values and attributes of the parameters in the snapshot saved with the given name.
bool import(const RooAbsArg &arg, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}, const RooCmdArg &arg9={})
Import a RooAbsArg object, e.g.
TClass instances represent classes, structs and namespaces in the ROOT type system.
The TNamed class is the base class for all named ROOT classes.
const char * GetName() const override
Returns name of object.
virtual void SetName(const char *name)
Set the name of the TNamed.
Mother of all ROOT objects.
virtual const char * ClassName() const
Returns name of class to which the object belongs.
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
const char * Data() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
RooCmdArg RecycleConflictNodes(bool flag=true)
RooConstVar & RooConst(double val)
RooCmdArg Silence(bool flag=true)
RooCmdArg Index(RooCategory &icat)
RooCmdArg StoreError(const RooArgSet &aset)
RooCmdArg WeightVar(const char *name="weight", bool reinterpretAsWeight=false)
RooCmdArg Import(const char *state, TH1 &histo)
CoordSystem::Scalar get(DisplacementVector2D< CoordSystem, Tag > const &p)
double product(DoubleArray factors, std::size_t nFactors)
Types that represent an HS3 document.
AuxiliaryConstraints readAuxiliaryChannelsAsConstraints(ValidatedHS3Document &doc, bool asChannels)
RooFit writes an array of constraints with the data set of its observed values as a component of the ...
std::variant< std::string, double > NameOrNumber
A name, or a number for a constant.
std::vector< std::string > unlistedConstraints(HS3Document const &doc, std::vector< std::string > *warnings=nullptr)
In HS3 0.3 (HS3 PR #154, decision on HS3 #156), the "constraints" of a HistFactory modifier add no te...
void checkDocument(HS3Document const &doc, Diagnostics &diagnostics, ValidateOptions const &options={})
The checks of validate(), for a document that is still needed if it doesn't pass them.
auto findByName(Objects_t &objects, std::string const &name) -> decltype(&*objects.begin())
The object with the name name among the objects of a section, e.g. findByName(doc....
std::map< std::string, std::set< std::string > > writeLikelihoodTermsOnce(HS3Document const &doc, Detail::JSONNode &root)
Writes each term of the likelihoods once: a factor of a product that is a term of the likelihood as w...
std::string const & nameOf(std::variant< Ts... > const &v)
The name of the alternative that a variant holds, e.g. of an axis or of a modifier.
void buildCombinedDist(RooJSONFactoryWSTool &tool, std::string const &name, CombinedDist const &dist, std::vector< std::string > const &otherLikelihoodTerms={})
Create the RooSimultaneous of a combined distribution in the workspace of the tool.
std::variant< RegularBinning, EdgesBinning > Binning
std::set< std::string > variatesOf(HS3Document const &doc, Likelihood const &likelihood)
The variates of the "distributions" of a likelihood, which it pairs with data, with the columns of it...
std::map< std::string, std::set< std::string > > sharedAxes(HS3Document const &doc)
The axes that several components of a combined data set have, by combined data set.
constexpr char const * defaultDomainName
The domain in which RooFit writes the ranges of all variables. It is a convention of RooFit,...
std::optional< ValidatedHS3Document > validate(HS3Document doc, Diagnostics &diagnostics, ValidateOptions const &options)
Checks what can be checked without knowing how the objects are built: unique names,...
std::set< std::string > componentsWithData(HS3Document const &doc)
The distributions that are components of a combined distribution of a likelihood, with a component of...
void setExportedObject(RooJSONFactoryWSTool &tool, HistFactoryDist dist, std::vector< DistributionRef > channelConstraints)
Called by the exporter of a HistFactory distribution, which is then written by the tool.
void writeDocument(Detail::JSONNode &root, HS3Document const &doc)
Writes everything but the distributions and functions, which are written one by one while they are ex...
void report(Diagnostics const &diagnostics, std::string const &object="")
Forward the warnings to the message service, and throw if there are errors.
void writeContents(Detail::JSONNode &node, std::vector< double > const &values)
Writes an array of numbers, each as an integer if it has no fractional part.
RooArgSet createObservables(std::vector< BinnedAxis > const &axes)
Create the variables for the axes of binned data.
bool hasErrors(Diagnostics const &diagnostics)
Whether there is a diagnostic with error severity.
std::map< std::string, std::set< std::string > > bookkeepingColumns(HS3Document const &doc)
The bookkeeping columns by combined data set: the declared ones (RooFitInternal::bookkeepingColumns),...
void writeHistFactoryDist(Detail::JSONNode &node, HistFactoryDist const &dist)
Writes a HistFactory distribution, without its name.
std::vector< std::string > sharedVariates(HS3Document const &doc)
Describes each variate that is shared although it must be distinct: by the terms of a likelihood,...
std::vector< Diagnostic > Diagnostics
NameOrNumber toNameOrNumber(RooAbsArg const &arg)
The name of an object, or the value of a literal constant.
void upgradeDocument(Detail::JSONNode &root, Diagnostics &diagnostics, ParseOptions const &options={})
Rewrites everything that older versions of RooFit wrote in a way that is not part of the standard int...
std::vector< std::string > resolvePointData(ValidatedHS3Document &doc)
A number given in-line in the "data" of a likelihood, or a point data set without axes,...
BinnedAxis toBinnedAxis(RooRealVar const &var)
The axis of binned data for a variable, or for a binning of a variable with the given name.
void writeBinnedData(Detail::JSONNode &node, BinnedData const &data)
Writes the axes, contents and uncertainties of binned data, without its name and type.
std::unique_ptr< RooDataHist > createDataHist(std::string const &name, RooArgSet const &vars, std::vector< double > const &contents, std::vector< double > const *errors)
Create a RooDataHist with the contents and errors. If there are no errors, it doesn't store squared w...
void writeCombinedDist(Detail::JSONNode &node, CombinedDist const &dist)
Writes a combined distribution, without its name.
Data parseData(Detail::JSONNode const &data, Diagnostics &diagnostics)
Reads a data set that doesn't come with a document.
bool isBuiltinHistFactoryImporter(JSONIO::Importer const &importer)
Whether this is the importer that RooFit registers for HistFactory distributions, as opposed to the o...
std::vector< BinnedAxis > parseBinnedAxes(Detail::JSONNode const &axes, Diagnostics &diagnostics)
Reads the axes of binned data that doesn't come with a document.
void buildHistFactoryDist(RooJSONFactoryWSTool &tool, std::string const &name, HistFactoryDist const &dist)
Create the RooFit objects of a HistFactory distribution in the workspace of the tool.
HS3Document parseDocument(Detail::JSONNode const &root, Diagnostics &diagnostics, ParseOptions const &options={})
Reads a document.
void initialize(typename Architecture_t::Matrix_t &A, EInitialization m)
std::optional< DistributionRef > prior
std::optional< ParameterPointRef > init
std::vector< DomainRef > domains
std::vector< ParameterRef > parametersOfInterest
std::vector< std::string > tags
std::map< std::string, std::string > dict
What readAuxiliaryChannelsAsConstraints() changed.
std::set< std::string > data
Their data sets, which hold the observed values of global observables.
std::set< std::string > terms
The components that are no longer components of their combined distribution.
std::vector< std::string > warnings
std::vector< std::string > categories
The data set of a RooSimultaneous as older versions of RooFit wrote it. It is only read.
std::vector< int > indices
std::vector< std::string > labels
A RooSimultaneous as older versions of RooFit wrote it, before HS3 had combined distributions....
std::vector< std::string > labels
std::vector< DistributionRef > distributions
std::vector< int > indices
Data sets or distributions that are selected by a key (HS3 #119). The columns are categorical axes of...
CombinedComponent< Ref_t > const * find(std::vector< std::string > const &key) const
The component for a key, or nullptr.
std::vector< CombinedComponent< Ref_t > > components
std::vector< std::string > columns
The axis of point data or unbinned data, which can't have a binning.
std::optional< double > max
std::optional< double > min
std::variant< GenericObject, HistFactoryDist, CombinedDist > body
std::optional< double > min
std::optional< double > max
std::optional< Binning > binning
std::optional< double > fixedValue
std::vector< std::variant< DomainAxis, CategoricalAxis > > axes
std::vector< double > edges
A distribution or function that is not interpreted by the document, but by the importers that are reg...
The data of a likelihood term: a data set, a number given in-line, or none (std::monostate) in a pure...
DistributionRef distribution
std::variant< DataRef, double, std::monostate > data
bool pureModel
Whether "data" is null: the likelihood only groups the distributions of a model without data (e....
std::vector< DistributionRef > auxDistributions
std::vector< LikelihoodTerm > terms
std::optional< std::string > pdfName
std::optional< std::vector< std::string > > observables
The observables of the ModelConfig, written if no data set of the likelihood gives them.
std::optional< std::string > mcName
std::optional< std::string > combinedDataName
std::vector< ParameterValue > parameters
std::optional< std::variant< double, std::string > > value
A string selects the state of a category.
Options for upgradeDocument() and parseDocument().
bool typedHistFactory
If false, HistFactory distributions stay generic objects, because a user registered their own importe...
std::vector< ContinuousAxis > axes
A name that says what it refers to, so that for example a DataRef can't be used as a DomainRef.
std::map< std::string, Attributes > attributes
std::map< std::string, std::vector< DistributionRef > > auxiliaryChannels
The components of a combined distribution that are auxiliary measurements (constraint terms paired wi...
std::map< std::string, std::vector< DistributionRef > > channelConstraints
The constraint terms of HistFactory distributions that no modifier names, by distribution.
std::vector< StepWidth > stepWidths
std::map< std::string, std::vector< std::string > > bookkeepingColumns
Axes that the components of a combined data set share, but that are no variate of any distribution,...
std::map< std::string, ModelConfigInfo > modelConfigs
bool strict
Whether the document has to follow the current standard, or if older conventions of RooFit are tolera...
std::function< bool(std::string const &) isExternal)
Whether a name that is not defined in the document is known in some other way, e.g....