Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HS3Variates.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Carsten D. Burgard, DESY/ATLAS, Nov 2026
5 * Jonas Rembser, CERN, Nov 2026
6 *
7 * Copyright (c) 2026, CERN
8 *
9 * Redistribution and use in source and binary forms,
10 * with or without modification, are permitted according to the terms
11 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
12 */
13
14// HS3 requires the terms of a likelihood, and the components of a combined distribution or data set, to have distinct
15// variates: a variate in two of them is one quantity modelled twice (e.g. a constraint that is a term of the likelihood
16// and a factor of a channel), or two quantities with one name (e.g. the observable "mass" of two channels, which need
17// their own names, "mass_SR" and "mass_CR"). All cases are checked with the same function, sharedVariates(), on the
18// document, when it is exported and when it is imported. The generic distributions are read as far as the standard
19// says what they consist of, through the nodes that the document refers to.
20//
21// This file also has what else follows from which distribution is paired with which data: how often a constraint
22// contributes to a likelihood, and how a document is changed so that RooFit can read its auxiliary measurements and
23// its point data.
24
25#include "HS3Document.h"
26
28
29#include <algorithm>
30#include <functional>
31#include <iterator>
32#include <map>
33#include <set>
34#include <string>
35#include <utility>
36#include <variant>
37#include <vector>
38
40
41namespace RooFit {
42namespace HS3 {
43namespace {
44
45// Append a name to a list that has every name once.
46void addUnique(std::vector<std::string> &names, std::string const &name)
47{
48 if (std::find(names.begin(), names.end(), name) == names.end()) {
49 names.push_back(name);
50 }
51}
52
53bool isName(JSONNode const &node)
54{
55 return !node.is_container() && !node.is_null() && !node.is_number();
56}
57
58// Add the names in `node` (one name or a list) to `out`.
59void addNames(JSONNode const &node, std::set<std::string> &out)
60{
61 if (node.is_seq()) {
62 for (JSONNode const &child : node.children()) {
63 addNames(child, out);
64 }
65 } else if (isName(node)) {
66 out.insert(node.val());
67 }
68}
69
70// The names in the list `key` of a generic distribution. The entries of the list are the names, or components that
71// give the name as `field`.
72std::vector<std::string> namesIn(JSONNode const &node, const char *key, const char *field = nullptr)
73{
74 std::vector<std::string> out;
75 if (JSONNode const *list = node.find(key)) {
76 for (JSONNode const &entry : list->children()) {
77 JSONNode const *name = field ? entry.find(field) : &entry;
78 if (name && isName(*name)) {
79 out.push_back(name->val());
80 }
81 }
82 }
83 return out;
84}
85
86// The constraint terms that the modifiers of a HistFactory distribution name in "constraints". The "constraint_name"
87// of older documents is not counted: it predates the requirement of distinct variates.
88std::vector<std::string> namedConstraints(HistFactoryDist const &dist)
89{
90 std::vector<std::string> out;
91 auto add = overloaded{[&](ConstraintPdf const &constraint) {
92 if (!constraint.legacyName) {
93 out.push_back(constraint.pdf.name);
94 }
95 },
96 [&](GammaConstraintPdfs const &constraints) {
97 for (auto const &pdf : constraints.pdfs) {
98 if (pdf) {
99 out.push_back(pdf->name);
100 }
101 }
102 },
103 [](auto const &) {}};
104 for (Sample const &sample : dist.samples) {
105 for (Modifier const &modifier : sample.modifiers) {
106 std::visit(overloaded{[](CustomModifier const &) {}, [&](auto const &m) { std::visit(add, m.constraint); }},
107 modifier);
108 }
109 }
110 return out;
111}
112
113// The same for a HistFactory distribution that the document doesn't interpret, because a user registered their own
114// importer or exporter for it.
115std::vector<std::string> namedConstraints(JSONNode const &dist)
116{
117 std::vector<std::string> out;
118 if (JSONNode const *samples = dist.find("samples")) {
119 for (JSONNode const &sample : samples->children()) {
120 if (JSONNode const *modifiers = sample.find("modifiers")) {
121 for (JSONNode const &modifier : modifiers->children()) {
122 for (std::string const &constraint : namesIn(modifier, "constraints")) {
123 out.push_back(constraint);
124 }
125 }
126 }
127 }
128 }
129 return out;
130}
131
132// The names of the components of a combined distribution.
133std::vector<std::string> componentsOf(CombinedDist const &dist)
134{
135 std::vector<std::string> out;
136 for (auto const &component : dist.components) {
137 out.push_back(component.ref.name);
138 }
139 return out;
140}
141
142// The node of a distribution that the document doesn't interpret, if it has one and, optionally, the type `type`.
143JSONNode const *genericNode(Distribution const &dist, const char *type = nullptr)
144{
145 auto const *generic = std::get_if<GenericObject>(&dist.body);
146 return generic && (!type || generic->type == type) ? generic->node : nullptr;
147}
148
149// The distributions of a document by name, and what they say about the variates. The document must outlive this
150// object.
151class Distributions {
152public:
153 explicit Distributions(HS3Document const &doc) : _doc{doc}
154 {
155 for (Distribution const &dist : doc.distributions) {
156 _byName.emplace(dist.name, &dist);
157 }
158 }
159
160 // The distribution `name` if the document represents it as a `Body_t`, or nullptr.
161 template <class Body_t>
162 Body_t const *find(std::string const &name) const
163 {
164 auto found = _byName.find(name);
165 return found != _byName.end() ? std::get_if<Body_t>(&found->second->body) : nullptr;
166 }
167
168 // The node of the generic distribution `name` if it has the type `type`, or nullptr.
169 JSONNode const *nodeOf(std::string const &name, const char *type) const
170 {
171 auto found = _byName.find(name);
172 return found != _byName.end() ? genericNode(*found->second, type) : nullptr;
173 }
174
175 // Add the variates of the distribution `name` to `out`: the "x" of a fundamental distribution (a name or a list)
176 // and the axes of a distribution with axes (e.g. histfactory_dist), through products, mixtures and combined
177 // distributions, and the "x" of the components of an array distribution. The constraint terms named by a
178 // histfactory_dist are terms of their own, see likelihoodTerms(). Distributions in `visited` are skipped.
179 void addVariates(std::string const &name, std::set<std::string> &out, std::set<std::string> &visited) const
180 {
181 if (!visited.insert(name).second) {
182 return;
183 }
184 if (auto const *histFactory = find<HistFactoryDist>(name)) {
185 for (BinnedAxis const &axis : histFactory->axes) {
186 out.insert(axis.name);
187 }
188 }
189 auto addVariatesOf = [&](std::vector<std::string> const &parts) {
190 for (std::string const &part : parts) {
191 addVariates(part, out, visited);
192 }
193 };
194 if (auto const *combined = find<CombinedDist>(name)) {
195 addVariatesOf(componentsOf(*combined));
196 }
197 auto const *generic = find<GenericObject>(name);
198 if (!generic || !generic->node) {
199 return;
200 }
201 JSONNode const &node = *generic->node;
202 if (generic->type == "product_dist") {
203 addVariatesOf(namesIn(node, "factors"));
204 } else if (generic->type == "mixture_dist") {
205 addVariatesOf(namesIn(node, "summands"));
206 } else if (generic->type == "array_dist") {
207 if (JSONNode const *components = node.find("components")) {
208 for (JSONNode const &component : components->children()) {
209 if (JSONNode const *x = component.find("x")) {
210 addNames(*x, out);
211 }
212 }
213 }
214 } else {
215 if (JSONNode const *x = node.find("x")) {
216 addNames(*x, out);
217 }
218 for (std::string const &axis : namesIn(node, "axes", "name")) {
219 out.insert(axis);
220 }
221 }
222 }
223
224 // The terms of a likelihood, each once: its distributions (the components of a combined distribution, or of a
225 // RooSimultaneous written to ROOT_internal by older versions, are terms of their own), its "aux_distributions",
226 // and the constraint terms named by the modifiers of its histfactory_dist terms.
227 std::vector<std::string> likelihoodTerms(Likelihood const &likelihood) const
228 {
229 std::vector<std::string> terms;
230 auto add = [&](std::string const &name) {
231 auto legacy = _doc.rooFit.combinedDistributions.find(name);
232 if (auto const *combined = find<CombinedDist>(name)) {
233 for (std::string const &component : componentsOf(*combined)) {
234 addUnique(terms, component);
235 }
236 } else if (!_byName.count(name) && legacy != _doc.rooFit.combinedDistributions.end()) {
237 for (DistributionRef const &component : legacy->second.distributions) {
238 addUnique(terms, component.name);
239 }
240 } else {
241 addUnique(terms, name);
242 }
243 };
244 for (LikelihoodTerm const &term : likelihood.terms) {
245 add(term.distribution.name);
246 }
247 for (DistributionRef const &aux : likelihood.auxDistributions) {
248 add(aux.name);
249 }
250 // The list grows by the constraint terms while it is read.
251 for (std::size_t i = 0; i < terms.size(); ++i) {
252 std::vector<std::string> constraints;
253 if (auto const *histFactory = find<HistFactoryDist>(terms[i])) {
254 constraints = namedConstraints(*histFactory);
255 } else if (JSONNode const *generic = nodeOf(terms[i], "histfactory_dist")) {
256 constraints = namedConstraints(*generic);
257 }
258 for (std::string const &constraint : constraints) {
259 addUnique(terms, constraint);
260 }
261 }
262 return terms;
263 }
264
265private:
266 HS3Document const &_doc;
267 std::map<std::string, Distribution const *> _byName;
268};
269
270// The parts (terms or components) of something that must have distinct variates, with their variates.
271using PartVariates = std::vector<std::pair<std::string, std::set<std::string>>>;
272
273// Append a message for each variate that several of `parts` share. `what` names the parts and `context` the whole,
274// e.g. "terms" of "the likelihood 'lh'".
275void addShared(PartVariates const &parts, std::string const &what, std::string const &context,
276 std::vector<std::string> &messages)
277{
278 std::map<std::string, std::vector<std::string>> partsOfVariate;
279 std::vector<std::string> order;
280 for (auto const &[part, variates] : parts) {
281 for (std::string const &variate : variates) {
282 auto &list = partsOfVariate[variate];
283 if (list.empty()) {
284 order.push_back(variate);
285 }
286 addUnique(list, part);
287 }
288 }
289 for (std::string const &variate : order) {
290 auto const &list = partsOfVariate[variate];
291 if (list.size() < 2) {
292 continue;
293 }
294 std::string names;
295 for (std::size_t i = 0; i < list.size(); ++i) {
296 names += (i == 0 ? "'" : (i + 1 == list.size() ? "' and '" : "', '")) + list[i];
297 }
298 messages.push_back("the variate '" + variate + "' is shared by the " + what + " " + names + "' of " + context);
299 }
300}
301
302// The axes of the components of a combined data set, by component.
303PartVariates componentAxes(HS3Document const &doc, CombinedData const &combined)
304{
305 PartVariates parts;
306 for (auto const &component : combined.components) {
307 std::set<std::string> axes;
308 if (Data const *found = findByName(doc.data, component.ref.name)) {
309 std::visit(overloaded{[](CombinedData const &) {},
310 [&](PointData const &data) {
311 for (ContinuousAxis const &axis : data.axes) {
312 axes.insert(axis.name);
313 }
314 },
315 [&](auto const &data) {
316 for (auto const &axis : data.axes) {
317 axes.insert(nameOf(axis));
318 }
319 }},
320 found->body);
321 }
322 parts.emplace_back(component.ref.name, axes);
323 }
324 return parts;
325}
326
327// The number of entries of the data set `name` of the document (see Data::numEntries()). Without such a data set, the
328// document is not valid, which is reported elsewhere: one is assumed.
329int numDataEntries(HS3Document const &doc, std::string const &name)
330{
331 Data const *data = findByName(doc.data, name);
332 return data ? static_cast<int>(data->numEntries()) : 1;
333}
334
335} // namespace
336
337std::set<std::string> variatesOf(HS3Document const &doc, Likelihood const &likelihood)
338{
339 Distributions distributions{doc};
340 std::set<std::string> variates;
341 std::set<std::string> visited;
342 for (LikelihoodTerm const &term : likelihood.terms) {
343 distributions.addVariates(term.distribution.name, variates, visited);
344 if (auto const *combined = distributions.find<CombinedDist>(term.distribution.name)) {
345 variates.insert(combined->columns.begin(), combined->columns.end());
346 }
347 }
348 return variates;
349}
350
351std::set<std::string> variatesOf(HS3Document const &doc, std::string const &distribution)
352{
353 std::set<std::string> variates;
354 std::set<std::string> visited;
355 Distributions{doc}.addVariates(distribution, variates, visited);
356 return variates;
357}
358
359std::map<std::string, std::set<std::string>> sharedAxes(HS3Document const &doc)
360{
361 std::map<std::string, std::set<std::string>> out;
362 for (Data const &data : doc.data) {
363 if (auto const *combined = std::get_if<CombinedData>(&data.body)) {
364 std::map<std::string, int> counts;
365 for (auto const &[component, axes] : componentAxes(doc, *combined)) {
366 for (std::string const &axis : axes) {
367 if (++counts[axis] == 2) {
368 out[data.name].insert(axis);
369 }
370 }
371 }
372 }
373 }
374 return out;
375}
376
377std::map<std::string, std::set<std::string>> bookkeepingColumns(HS3Document const &doc)
378{
379 std::map<std::string, std::set<std::string>> out;
380 if (doc.rooFit.bookkeepingColumns.empty()) {
381 return out;
382 }
383 Distributions distributions{doc};
384 std::set<std::string> variates;
385 std::set<std::string> visited;
386 for (Distribution const &dist : doc.distributions) {
387 distributions.addVariates(dist.name, variates, visited);
388 }
389 for (auto const &[dataset, columns] : doc.rooFit.bookkeepingColumns) {
390 for (std::string const &column : columns) {
391 if (!variates.count(column)) {
392 out[dataset].insert(column);
393 }
394 }
395 }
396 return out;
397}
398
399std::vector<std::string> unlistedConstraints(HS3Document const &doc, std::vector<std::string> *warnings)
400{
401 Distributions distributions{doc};
402 std::vector<std::string> messages;
403 for (Likelihood const &likelihood : doc.likelihoods) {
404 std::map<std::string, int> contributions;
405 // The combined distributions in which a distribution is a component without data.
406 std::map<std::string, std::set<std::string>> withoutData;
407 std::set<std::string> named;
408 // The products that are being expanded, since a document that was not imported yet can have circular ones.
409 std::set<std::string> expanding;
410 // `combined` is the combined distribution whose component without data the distribution is reached through.
411 std::function<void(std::string const &, int, std::string const &)> contribute =
412 [&](std::string const &name, int entries, std::string const &combined) {
413 contributions[name] += entries;
414 if (entries == 0) {
415 withoutData[name].insert(combined);
416 }
417 if (auto const *histFactory = distributions.find<HistFactoryDist>(name)) {
418 for (std::string const &constraint : namedConstraints(*histFactory)) {
419 named.insert(constraint);
420 }
421 } else if (JSONNode const *generic = distributions.nodeOf(name, "histfactory_dist")) {
422 for (std::string const &constraint : namedConstraints(*generic)) {
423 named.insert(constraint);
424 }
425 } else if (JSONNode const *product = distributions.nodeOf(name, "product_dist");
426 product && expanding.insert(name).second) {
427 for (std::string const &factor : namesIn(*product, "factors")) {
428 contribute(factor, entries, combined);
429 }
430 expanding.erase(name);
431 }
432 };
433 // The components of a combined distribution, each with the entries of the component of the paired combined data
434 // set that has the same key. Without paired data (a pure model), every component contributes once.
435 auto contributeCombined = [&](std::string const &distName, CombinedDist const &dist, Data const *data) {
436 auto const *combinedData = data ? std::get_if<CombinedData>(&data->body) : nullptr;
437 for (auto const &component : dist.components) {
438 int entries = 1;
439 if (combinedData) {
440 auto const *dataComponent = combinedData->find(component.key);
441 entries = dataComponent ? numDataEntries(doc, dataComponent->ref.name) : 0;
442 if (!dataComponent && warnings) {
443 warnings->push_back("the component '" + component.ref.name + "' of the combined distribution '" +
444 distName + "' has no component in the data set '" + data->name +
445 "' that the likelihood '" + likelihood.name +
446 "' pairs it with, so it does not contribute to the likelihood");
447 }
448 }
449 contribute(component.ref.name, entries, distName);
450 }
451 };
452 for (LikelihoodTerm const &term : likelihood.terms) {
453 // The paired data: a data set, a value given in-line, or nothing (a pure model).
454 auto const *dataRef = std::get_if<DataRef>(&term.data);
455 Data const *data = dataRef ? findByName(doc.data, dataRef->name) : nullptr;
456 std::string const &name = term.distribution.name;
457 if (auto const *combined = distributions.find<CombinedDist>(name)) {
458 ++contributions[name];
459 contributeCombined(name, *combined, data);
460 } else {
461 contribute(name, data ? numDataEntries(doc, data->name) : 1, "");
462 }
463 }
464 for (DistributionRef const &aux : likelihood.auxDistributions) {
465 contribute(aux.name, 1, "");
466 }
467 for (std::string const &constraint : named) {
468 const int n = contributions[constraint];
469 auto const &regions = withoutData[constraint];
470 std::string inRegions;
471 for (std::string const &region : regions) {
472 inRegions += (inRegions.empty() ? "'" : ", '") + region + "'";
473 }
474 if (n != 1) {
475 messages.push_back("the constraint '" + constraint +
476 "' named by a modifier of a HistFactory channel contributes " + std::to_string(n) +
477 " times to the likelihood '" + likelihood.name +
478 "', but it must contribute exactly once" +
479 (n == 0 && !regions.empty() ? " (it is a component of the combined distribution " +
480 inRegions + " without data, which is no observation)"
481 : ""));
482 } else if (!regions.empty() && warnings) {
483 warnings->push_back("the constraint '" + constraint + "' is a term of the likelihood '" + likelihood.name +
484 "' and also a component of the combined distribution " + inRegions +
485 " without data. It contributes once, as the term");
486 }
487 }
488 }
489 return messages;
490}
491
492std::set<std::string> componentsWithData(HS3Document const &doc)
493{
494 Distributions distributions{doc};
495 std::set<std::string> out;
496 for (Likelihood const &likelihood : doc.likelihoods) {
497 for (LikelihoodTerm const &term : likelihood.terms) {
498 auto const *dataRef = std::get_if<DataRef>(&term.data);
499 auto const *combinedData = dataRef ? find<CombinedData>(doc.data, dataRef->name) : nullptr;
500 auto const *dist = distributions.find<CombinedDist>(term.distribution.name);
501 if (!dist || !combinedData) {
502 continue;
503 }
504 for (auto const &component : dist->components) {
505 auto const *dataComponent = combinedData->find(component.key);
506 if (dataComponent && numDataEntries(doc, dataComponent->ref.name) > 0) {
507 out.insert(component.ref.name);
508 }
509 }
510 }
511 }
512 return out;
513}
514
515std::vector<std::string> sharedVariates(HS3Document const &doc)
516{
517 Distributions distributions{doc};
518 std::vector<std::string> messages;
519 auto withVariates = [&](std::vector<std::string> const &names) {
520 PartVariates parts;
521 for (std::string const &name : names) {
522 std::set<std::string> visited;
523 distributions.addVariates(name, parts.emplace_back(name, std::set<std::string>{}).second, visited);
524 }
525 return parts;
526 };
527 std::set<std::string> inLikelihoods;
528 for (Likelihood const &likelihood : doc.likelihoods) {
529 for (LikelihoodTerm const &term : likelihood.terms) {
530 inLikelihoods.insert(term.distribution.name);
531 }
532 for (DistributionRef const &aux : likelihood.auxDistributions) {
533 inLikelihoods.insert(aux.name);
534 }
535 addShared(withVariates(distributions.likelihoodTerms(likelihood)), "terms",
536 "the likelihood '" + likelihood.name + "'", messages);
537 }
538 for (Distribution const &dist : doc.distributions) {
539 auto const *combined = std::get_if<CombinedDist>(&dist.body);
540 if (combined && !inLikelihoods.count(dist.name)) {
541 addShared(withVariates(componentsOf(*combined)), "components", "the combined_dist '" + dist.name + "'",
542 messages);
543 }
544 }
545 // The axes of the components of every combined data set, also of those that a likelihood lists.
546 std::map<std::string, std::set<std::string>> const bookkeeping = bookkeepingColumns(doc);
547 for (Data const &data : doc.data) {
548 auto const *combined = std::get_if<CombinedData>(&data.body);
549 if (!combined) {
550 continue;
551 }
552 PartVariates parts = componentAxes(doc, *combined);
553 if (auto found = bookkeeping.find(data.name); found != bookkeeping.end()) {
554 for (auto &part : parts) {
555 for (std::string const &column : found->second) {
556 part.second.erase(column);
557 }
558 }
559 }
560 addShared(parts, "components", "the combined data set '" + data.name + "'", messages);
561 }
562 return messages;
563}
564
566{
567 HS3Document &doc = validated._doc;
569 // The components that are taken out of their combined distribution, by combined distribution, with their labels.
570 std::map<std::string, std::map<std::string, std::string>> dropped;
571 std::set<std::string> labels;
572
573 // Components without data that are also terms with data. A data set can only be a component of one combined data
574 // set, so that the data of a term gets a copy for every further one. The copies are added to the document after
575 // the loop, which keeps pointers into the data sets.
576 std::vector<Data> copies;
577 auto hasEntries = [&](std::string const &name) {
578 Data const *data = findByName(doc.data, name);
579 data = data ? data : findByName(copies, name);
580 return data && data->numEntries() > 0;
581 };
582 std::set<std::string> dataOfComponents;
583 for (Likelihood &likelihood : doc.likelihoods) {
584 std::set<std::string> termsToRemove;
585 for (LikelihoodTerm const &term : likelihood.terms) {
586 auto const *combinedDist = find<CombinedDist>(doc.distributions, term.distribution.name);
587 auto const *dataRef = std::get_if<DataRef>(&term.data);
588 auto *combined = dataRef ? find<CombinedData>(doc.data, dataRef->name) : nullptr;
589 if (!combinedDist || !combined) {
590 continue;
591 }
592 for (auto const &component : combinedDist->components) {
593 if (component.key.size() != 1) {
594 continue;
595 }
596 std::string const &label = component.key.front();
597 std::string const &dist = component.ref.name;
598 auto ownTerm =
599 std::find_if(likelihood.terms.begin(), likelihood.terms.end(), [&](LikelihoodTerm const &other) {
600 return other.distribution.name == dist && std::holds_alternative<DataRef>(other.data);
601 });
602 if (ownTerm == likelihood.terms.end()) {
603 continue;
604 }
605 auto dataComponent = std::find_if(combined->components.begin(), combined->components.end(),
606 [&](auto const &c) { return c.key == component.key; });
607 if (dataComponent != combined->components.end() && hasEntries(dataComponent->ref.name)) {
608 continue; // two contributions, which is not for RooFit to resolve
609 }
610 out.warnings.push_back("the distribution '" + dist + "' is a component of '" + term.distribution.name +
611 "' without data and a term of the likelihood '" + likelihood.name +
612 "' with data. It is read as " +
613 (asChannels ? "the component, with the data of the term." : "the term only."));
614 if (asChannels) {
615 DataRef termData = std::get<DataRef>(ownTerm->data);
616 if (!dataOfComponents.insert(termData.name).second) {
617 Data const *original = findByName(doc.data, termData.name);
618 const std::string copyName = dataRef->name + "_" + label;
619 if (original && !findByName(doc.data, copyName) && !findByName(copies, copyName)) {
620 copies.push_back(*original);
621 copies.back().name = copyName;
622 termData.name = copyName;
623 }
624 }
625 if (dataComponent != combined->components.end()) {
626 dataComponent->ref = termData;
627 } else {
628 combined->components.push_back({{label}, termData});
629 }
630 termsToRemove.insert(dist);
631 } else {
632 dropped[term.distribution.name][dist] = label;
633 labels.insert(label);
634 }
635 }
636 }
637 likelihood.terms.erase(
638 std::remove_if(likelihood.terms.begin(), likelihood.terms.end(),
639 [&](LikelihoodTerm const &term) { return termsToRemove.count(term.distribution.name) > 0; }),
640 likelihood.terms.end());
641 }
642 doc.data.insert(doc.data.end(), copies.begin(), copies.end());
643 if (asChannels) {
644 return out;
645 }
646
647 for (auto const &item : doc.rooFit.auxiliaryChannels) {
648 auto const *combinedDist = find<CombinedDist>(doc.distributions, item.first);
649 for (auto const &ref : item.second) {
650 std::string label = ref.name;
651 if (combinedDist) {
652 for (auto const &component : combinedDist->components) {
653 if (component.ref.name == ref.name && component.key.size() == 1) {
654 label = component.key.front();
655 }
656 }
657 }
658 dropped[item.first][ref.name] = label;
659 labels.insert(label);
660 }
661 }
662 auto isLabel = [&](std::vector<std::string> const &key) { return key.size() == 1 && labels.count(key.front()); };
663 for (Likelihood &likelihood : doc.likelihoods) {
664 std::vector<LikelihoodTerm> added;
665 for (LikelihoodTerm const &term : likelihood.terms) {
666 auto found = dropped.find(term.distribution.name);
667 if (found == dropped.end()) {
668 continue;
669 }
670 auto const *dataRef = std::get_if<DataRef>(&term.data);
671 auto const *combined = dataRef ? find<CombinedData>(doc.data, dataRef->name) : nullptr;
672 for (auto const &item : found->second) {
673 std::string const &dist = item.first;
674 std::string const &label = item.second;
675 if (std::any_of(likelihood.terms.begin(), likelihood.terms.end(),
676 [&](auto const &other) { return other.distribution.name == dist; })) {
677 continue; // a term of its own already
678 }
679 if (auto const *component = combined ? combined->find({label}) : nullptr) {
680 added.push_back({{dist}, component->ref});
681 } else if (std::none_of(likelihood.auxDistributions.begin(), likelihood.auxDistributions.end(),
682 [&](auto const &ref) { return ref.name == dist; })) {
683 // Without data, the observed values are the values of the constant observables.
684 likelihood.auxDistributions.push_back({dist});
685 }
686 }
687 }
688 likelihood.terms.insert(likelihood.terms.end(), added.begin(), added.end());
689 }
690 for (Data &data : doc.data) {
691 if (auto *combined = std::get_if<CombinedData>(&data.body)) {
692 auto &components = combined->components;
693 for (auto const &component : components) {
694 // Not the data sets without entries: they hold no values, and stay data sets of the workspace.
695 if (isLabel(component.key) && hasEntries(component.ref.name)) {
696 out.data.insert(component.ref.name);
697 }
698 }
699 components.erase(std::remove_if(components.begin(), components.end(),
700 [&](auto const &component) { return isLabel(component.key); }),
701 components.end());
702 }
703 }
704 for (Domain &domain : doc.domains) {
705 for (auto &axis : domain.axes) {
706 if (auto *categorical = std::get_if<CategoricalAxis>(&axis)) {
707 auto &categories = categorical->categories;
708 categories.erase(std::remove_if(categories.begin(), categories.end(),
709 [&](std::string const &label) { return labels.count(label) > 0; }),
710 categories.end());
711 }
712 }
713 }
714 for (auto const &droppedFrom : dropped) {
715 std::map<std::string, std::string> const &components = droppedFrom.second;
716 for (auto const &item : components) {
717 out.terms.insert(item.first);
718 }
719 if (auto *combinedDist = find<CombinedDist>(doc.distributions, droppedFrom.first)) {
720 auto &all = combinedDist->components;
721 all.erase(std::remove_if(all.begin(), all.end(),
722 [&](auto const &component) { return components.count(component.ref.name) > 0; }),
723 all.end());
724 }
725 }
726 return out;
727}
728
729std::vector<std::string> resolvePointData(ValidatedHS3Document &validated)
730{
731 HS3Document &doc = validated._doc;
732 std::vector<std::string> errors;
733 auto soleVariateOf = [&](std::string const &distribution, std::string const &what) {
734 std::set<std::string> const variates = variatesOf(doc, distribution);
735 if (variates.size() == 1) {
736 return *variates.begin();
737 }
738 errors.push_back("the variate of the distribution '" + distribution + "', which " + what +
739 " is paired with, can't be determined, since it doesn't have a single variate");
740 return std::string{};
741 };
742 std::vector<Data> inlineData;
743 std::map<std::string, std::string> variateOfPointData; // of the point data sets without axes
744 for (Likelihood &likelihood : doc.likelihoods) {
745 for (LikelihoodTerm &term : likelihood.terms) {
746 std::string const &distribution = term.distribution.name;
747 if (auto const *value = std::get_if<double>(&term.data)) {
748 // Older versions of RooFit wrote "data": [0], with the number 0 as the only entry, for a ModelConfig
749 // without data. In this exact form, it is no observation in documents before 0.3 (HS3 PR #154). RooFit
750 // now writes "data": null.
751 if (*value == 0. && likelihood.terms.size() == 1 && doc.metadata.version < Version{0, 3}) {
752 term.data = std::monostate{};
753 continue;
754 }
755 std::string name = distribution + "_data";
756 for (std::size_t k = 1; findByName(doc.data, name) || findByName(inlineData, name); ++k) {
757 name = distribution + "_data_" + std::to_string(k);
758 }
759 PointData point;
760 point.value = *value;
761 point.axes.push_back(
762 {soleVariateOf(distribution, "the number in the \"data\" of the likelihood '" + likelihood.name + "'"),
763 std::nullopt, std::nullopt});
764 inlineData.push_back({name, std::move(point)});
765 term.data = DataRef{name};
766 continue;
767 }
768 auto const *ref = std::get_if<DataRef>(&term.data);
769 auto const *point = ref ? find<PointData>(doc.data, ref->name) : nullptr;
770 if (!point) {
771 continue;
772 }
773 if (!point->axes.empty()) {
774 // The axis of point data is named like the variate of the paired distribution.
775 std::set<std::string> const variates = variatesOf(doc, distribution);
776 if (variates.size() == 1 && point->axes.front().name != *variates.begin()) {
777 errors.push_back("the axis '" + point->axes.front().name + "' of the point data set '" + ref->name +
778 "' is not the variate '" + *variates.begin() + "' of the distribution '" +
779 distribution + "' it is paired with");
780 }
781 continue;
782 }
783 std::string const variate = soleVariateOf(distribution, "the point data set '" + ref->name + "'");
784 auto inserted = variateOfPointData.emplace(ref->name, variate);
785 if (!inserted.second && inserted.first->second != variate) {
786 errors.push_back("the point data set '" + ref->name +
787 "' without axes is paired with distributions of different variates, '" +
788 inserted.first->second + "' and '" + variate + "'");
789 }
790 }
791 }
792 for (auto const &[name, variate] : variateOfPointData) {
793 find<PointData>(doc.data, name)->axes.push_back({variate, std::nullopt, std::nullopt});
794 }
795 doc.data.insert(doc.data.end(), std::make_move_iterator(inlineData.begin()),
796 std::make_move_iterator(inlineData.end()));
797 return errors;
798}
799
800std::map<std::string, std::set<std::string>> writeLikelihoodTermsOnce(HS3Document const &doc, JSONNode &root)
801{
802 std::map<std::string, std::set<std::string>> removed;
803 if (!root.has_child("distributions")) {
804 return removed;
805 }
806 Distributions distributions{doc};
807 // The products that are terms, with the factors that are terms wherever the product is one.
808 std::map<std::string, std::set<std::string>> removable;
809 for (Likelihood const &likelihood : doc.likelihoods) {
810 std::vector<std::string> const terms = distributions.likelihoodTerms(likelihood);
811 for (std::string const &term : terms) {
812 JSONNode const *product = distributions.nodeOf(term, "product_dist");
813 if (!product) {
814 continue;
815 }
816 std::set<std::string> factorsThatAreTerms;
817 for (std::string const &factor : namesIn(*product, "factors")) {
818 if (factor != term && std::find(terms.begin(), terms.end(), factor) != terms.end()) {
819 factorsThatAreTerms.insert(factor);
820 }
821 }
822 auto found = removable.find(term);
823 if (found == removable.end()) {
824 removable[term] = factorsThatAreTerms;
825 } else {
826 std::set<std::string> both;
827 std::set_intersection(found->second.begin(), found->second.end(), factorsThatAreTerms.begin(),
828 factorsThatAreTerms.end(), std::inserter(both, both.begin()));
829 found->second = both;
830 }
831 }
832 }
833 // Products that another distribution uses are left alone.
834 for (Distribution const &dist : doc.distributions) {
835 if (JSONNode const *node = genericNode(dist)) {
836 for (const char *key : {"factors", "summands"}) {
837 for (std::string const &part : namesIn(*node, key)) {
838 removable.erase(part);
839 }
840 }
841 }
842 }
843 for (JSONNode &dist : root["distributions"].children()) {
844 auto found = removable.find(dist["name"].val());
845 if (found == removable.end() || found->second.empty()) {
846 continue;
847 }
848 std::vector<std::string> keep;
849 for (JSONNode const &factor : dist["factors"].children()) {
850 if (!found->second.count(factor.val())) {
851 keep.push_back(factor.val());
852 }
853 }
854 if (!keep.empty()) {
855 dist["factors"].clear();
856 dist["factors"].fill_seq(keep);
857 removed.insert(*found);
858 }
859 }
860 return removed;
861}
862
863} // namespace HS3
864} // namespace RooFit
HS3Document const & _doc
std::map< std::string, Distribution const * > _byName
#define c(i)
Definition RSha256.hxx:101
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 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 modifier
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
char name[80]
Definition TGX11.cxx:142
std::string val() 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
An HS3 document. It is move-only, because generic objects refer to the JSON tree of the document.
std::vector< HS3::Data > data
HS3::Metadata metadata
std::vector< HS3::Likelihood > likelihoods
HS3::RooFitInternal rooFit
std::vector< HS3::Domain > domains
std::vector< HS3::Distribution > distributions
A document that passed validate().
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
double dist(Rotation3D const &r1, Rotation3D const &r2)
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::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...
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....
Definition HS3Document.h:65
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::variant< NormFactor, NormSys, HistoSys, ShapeSys, StatError, CustomModifier > Modifier
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.
Definition HS3Document.h:58
Combined< DataRef > CombinedData
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.
std::set< std::string > componentsWithData(HS3Document const &doc)
The distributions that are components of a combined distribution of a likelihood, with a component of...
Ref< struct DistributionTag > DistributionRef
std::map< std::string, std::set< std::string > > bookkeepingColumns(HS3Document const &doc)
The bookkeeping columns by combined data set: the declared ones (RooFitInternal::bookkeepingColumns),...
auto find(Objects_t &objects, std::string const &name) -> decltype(std::get_if< Body_t >(&objects.begin() ->body))
The body of the object name if it is a Body_t, e.g. find<CombinedData>(doc.data, "obsData"),...
Definition HS3Document.h:77
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< std::string > resolvePointData(ValidatedHS3Document &doc)
A number given in-line in the "data" of a likelihood, or a point data set without axes,...
overloaded(Ts...) -> overloaded< Ts... >
Combined< DistributionRef > CombinedDist
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
static const char * what
Definition stlLoader.cc:5
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
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.
The data of a likelihood term: a data set, a number given in-line, or none (std::monostate) in a pure...
std::vector< LikelihoodTerm > terms
Version version
The parsed version. Versions that can't be parsed count as the oldest one.
std::vector< ContinuousAxis > axes
std::string name
Definition HS3Document.h:86
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< std::string > > bookkeepingColumns
Axes that the components of a combined data set share, but that are no variate of any distribution,...
TMarker m
Definition textangle.C:8