Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ConstraintHelpers.cxx
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*
4 * Project: RooFit
5 * Authors:
6 * Jonas Rembser, CERN 2021
7 *
8 * Copyright (c) 2022, CERN
9 *
10 * Redistribution and use in source and binary forms,
11 * with or without modification, are permitted according to the terms
12 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
13 */
14
15#include "ConstraintHelpers.h"
16
17#include <RooAbsData.h>
18#include <RooAbsPdf.h>
19#include <RooConstraintSum.h>
20#include <RooMsgService.h>
21
22#include "RooFitImplHelpers.h"
23
24namespace {
25
26std::unique_ptr<RooArgSet>
27getGlobalObservables(RooAbsPdf const &pdf, RooArgSet const *globalObservables, const char *globalObservablesTag)
28{
29
30 if (globalObservables && globalObservablesTag) {
31 // error!
32 std::string errMsg = "RooAbsPdf::fitTo: GlobalObservables and GlobalObservablesTag options mutually exclusive!";
33 oocoutE(&pdf, Minimization) << errMsg << std::endl;
34 throw std::invalid_argument(errMsg);
35 }
36 if (globalObservables) {
37 // pass-through of global observables
38 return std::make_unique<RooArgSet>(*globalObservables);
39 }
40
42 oocoutI(&pdf, Minimization) << "User-defined specification of global observables definition with tag named '"
43 << globalObservablesTag << "'" << std::endl;
44 } else {
45 // Neither GlobalObservables nor GlobalObservablesTag has been processed -
46 // try if a default tag is defined in the head node Check if head not
47 // specifies default global observable tag
48 if (auto defaultGlobalObservablesTag = pdf.getStringAttribute("DefaultGlobalObservablesTag")) {
49 oocoutI(&pdf, Minimization) << "p.d.f. provides built-in specification of global observables definition "
50 << "with tag named '" << defaultGlobalObservablesTag << "'" << std::endl;
52 }
53 }
54
56 std::unique_ptr<RooArgSet> allVars{pdf.getVariables()};
57 return std::unique_ptr<RooArgSet>{allVars->selectByAttrib(globalObservablesTag, true)};
58 }
59
60 // no global observables specified
61 return nullptr;
62}
63
64} // namespace
65
66////////////////////////////////////////////////////////////////////////////////
67/// Create the parameter constraint sum to add to the negative log-likelihood.
68/// \return If there are constraints, returns a pointer to the constraint NLL.
69/// Returns a `nullptr` if the parameters are unconstrained.
70/// \param[in] name Name of the created RooConstraintSum object.
71/// \param[in] pdf The PDF model whose parameters should be constrained.
72/// Constraint terms will be extracted from RooProdPdf instances
73/// that are servers of the PDF (internal constraints).
74/// \param[in] data Dataset used in the fit with the constraint sum. It is
75/// used to figure out which are the observables and also to get the
76/// global observables definition and values if they are stored in
77/// the dataset.
78/// \param[in] constrainedParameters Set of parameters to constrain. If `nullptr`, all
79/// parameters will be considered.
80/// \param[in] externalConstraints Set of constraint terms that are not
81/// embedded in the PDF (external constraints).
82/// \param[in] globalObservables The normalization set for the constraint terms.
83/// If it is `nullptr`, the set of all constrained parameters will
84/// be used as the normalization set.
85/// \param[in] globalObservablesTag Alternative to define the normalization set
86/// for the constraint terms. All constrained parameters that have
87/// the attribute with the tag defined by `globalObservablesTag` are
88/// used. The `globalObservables` and `globalObservablesTag`
89/// parameters are mutually exclusive, meaning at least one of them
90/// has to be `nullptr`.
91/// \param[in] takeGlobalObservablesFromData If the dataset should be used to automatically
92/// define the set of global observables. If this is the case and the
93/// set of global observables is still defined manually with the
94/// `globalObservables` or `globalObservablesTag` parameters, the
95/// values of all global observables that are not stored in the
96/// dataset are taken from the model.
97std::unique_ptr<RooAbsReal> createConstraintTerm(std::string const &name, RooAbsPdf const &pdf, RooAbsData const &data,
100 RooArgSet const *globalObservables, const char *globalObservablesTag,
102{
103 RooArgSet const &observables = *data.get();
104
105 bool doStripDisconnected = false;
106
107 // If no explicit list of parameters to be constrained is specified apply default algorithm
108 // All terms of RooProdPdfs that do not contain observables and share a parameters with one or more
109 // terms that do contain observables are added as constrainedParameters.
113 } else {
114 pdf.getParameters(&observables, cPars, false);
115 doStripDisconnected = true;
116 }
117
118 // Collect internal and external constraint specifications
120
122 auto constraintSetCacheName = std::string("CACHE_CONSTR_OF_PDF_") + pdf.GetName() + "_FOR_OBS_" + observableNames;
123
124 if (!cPars.empty()) {
125 std::unique_ptr<RooArgSet> internalConstraints{
126 pdf.getAllConstraints(observables, cPars, doStripDisconnected)};
128 }
131 }
132
133 if (!allConstraints.empty()) {
134
135 oocoutI(&pdf, Minimization) << " Including the following constraint terms in minimization: " << allConstraints
136 << std::endl;
137
138 // Identify global observables in the model.
139 auto glObs = getGlobalObservables(pdf, globalObservables, globalObservablesTag);
140 if (data.getGlobalObservables() && takeGlobalObservablesFromData) {
141 if (!glObs) {
142 // There were no global observables specified, but there are some in the
143 // dataset. We will just take them from the dataset.
144 oocoutI(&pdf, Minimization)
145 << "The following global observables have been automatically defined according to the dataset "
146 << "which also provides their values: " << *data.getGlobalObservables() << std::endl;
147 glObs = std::make_unique<RooArgSet>(*data.getGlobalObservables());
148 } else {
149 // There are global observables specified by the user and also some in
150 // the dataset.
152 data.getGlobalObservables()->selectCommon(*glObs, globalsFromDataset);
153 oocoutI(&pdf, Minimization) << "The following global observables have been defined: " << *glObs << ","
154 << " with the values of " << globalsFromDataset
155 << " obtained from the dataset and the other values from the model."
156 << std::endl;
157 }
158 } else if (glObs) {
159 oocoutI(&pdf, Minimization)
160 << "The following global observables have been defined and their values are taken from the model: "
161 << *glObs << std::endl;
162 // In this case we don't take global observables from data
164 } else {
165 if (!glObs) {
166 oocoutI(&pdf, Minimization)
167 << "The global observables are not defined , normalize constraints with respect to the parameters "
168 << cPars << std::endl;
169 }
171 }
172
173 return std::make_unique<RooConstraintSum>(name.c_str(), "nllCons", allConstraints, glObs ? *glObs : cPars,
175 }
176
177 // no constraints
178 return nullptr;
179}
180
181/// \endcond
#define oocoutE(o, a)
#define oocoutI(o, a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
char name[80]
Definition TGX11.cxx:110
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...
const Text_t * getStringAttribute(const Text_t *key) const
Get string attribute mapped under key 'key'.
RooFit::OwningPtr< RooArgSet > getVariables(bool stripDisconnected=true) const
Return RooArgSet with all variables (tree leaf nodes of expression tree)
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
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....
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
std::string getColonSeparatedNameString(RooArgSet const &argSet, char delim=':')