63RooArgSet getConstraintsSet(RooAbsPdf *pdf, RooAbsData *data, RooArgSet constrained_parameters,
64 RooArgSet
const &external_constraints, RooArgSet global_observables,
65 std::string
const &global_observables_tag)
69 bool doStripDisconnected =
false;
74 bool did_default_constraint_algo =
false;
75 std::size_t N_default_constraints = 0;
77 if (constrained_parameters.
empty()) {
78 std::unique_ptr<RooArgSet> default_constraints{pdf->
getParameters(*data,
false)};
79 constrained_parameters.
add(*default_constraints);
80 doStripDisconnected =
true;
82 did_default_constraint_algo =
true;
83 N_default_constraints = default_constraints->size();
87 if (did_default_constraint_algo) {
88 assert(N_default_constraints ==
static_cast<std::size_t
>(constrained_parameters.
size()));
93 RooArgSet allConstraints;
95 if (!global_observables_tag.empty()) {
96 if (!global_observables.
empty()) {
100 global_observables.
add(
101 *std::unique_ptr<RooArgSet>{allVars->selectByAttrib(global_observables_tag.c_str(),
true)});
102 oocoutI(
nullptr,
Minimization) <<
"User-defined specification of global observables definition with tag named '"
103 << global_observables_tag <<
"'" << std::endl;
104 }
else if (global_observables.
empty()) {
110 <<
"p.d.f. provides built-in specification of global observables definition with tag named '"
111 << defGlobObsTag <<
"'" << std::endl;
112 std::unique_ptr<RooArgSet> allVars{pdf->
getVariables()};
113 global_observables.
add(*std::unique_ptr<RooArgSet>{allVars->selectByAttrib(defGlobObsTag,
true)});
119 if (!constrained_parameters.
empty()) {
120 std::unique_ptr<RooArgSet> constraints{
121 pdf->
getAllConstraints(*data->get(), constrained_parameters, doStripDisconnected)};
122 allConstraints.
add(*constraints);
124 if (!external_constraints.
empty()) {
125 allConstraints.
add(external_constraints);
128 return allConstraints;
150std::unique_ptr<RooSubsidiaryL> buildSubsidiaryL(RooAbsPdf *pdf, RooAbsData *data, RooArgSet constrained_parameters,
151 RooArgSet
const &external_constraints, RooArgSet global_observables,
152 std::string
const &global_observables_tag)
154 auto allConstraints = getConstraintsSet(pdf, data, constrained_parameters, external_constraints, global_observables,
155 global_observables_tag);
157 std::unique_ptr<RooSubsidiaryL> subsidiary_likelihood;
159 if (!allConstraints.
empty()) {
161 oocoutI(
nullptr,
Minimization) <<
" Including the following constraint terms in minimization: " << allConstraints
163 if (!global_observables.
empty()) {
164 oocoutI(
nullptr,
Minimization) <<
"The following global observables have been defined: " << global_observables
167 std::string
name(
"likelihood for pdf ");
169 subsidiary_likelihood = std::make_unique<RooSubsidiaryL>(
170 name, allConstraints, (!global_observables.
empty()) ? global_observables : constrained_parameters);
173 return subsidiary_likelihood;
184RooAbsPdf *getBinnedPdf(RooAbsPdf *pdf)
186 RooAbsPdf *binnedPdf =
nullptr;
192 for (
const auto component : (
static_cast<RooProdPdf *
>(pdf))->pdfList()) {
193 if (component->getAttribute(
"BinnedLikelihood") && component->IsA()->InheritsFrom(
RooRealSumPdf::Class())) {
194 binnedPdf =
static_cast<RooAbsPdf *
>(component);
223 TString simCatName(simCat.GetName());
226 std::vector<std::unique_ptr<RooAbsData>> dsetList{
_data.split(*sim_pdf, process_empty_data_sets)};
229 std::size_t N_components = 0;
231 for (
const auto &catState : simCat) {
233 RooAbsPdf *component_pdf = sim_pdf->getPdf(catState.first.c_str());
234 auto found = std::find_if(dsetList.begin(), dsetList.end(), [&](
auto const &item) {
235 return catState.first == item->GetName();
237 RooAbsData *dset = found != dsetList.end() ? found->get() :
nullptr;
239 if (component_pdf && dset && (0. != dset->
sumEntries() || process_empty_data_sets)) {
245 std::vector<std::unique_ptr<RooAbsL>> components;
246 components.reserve(N_components);
251 for (
const auto &catState : simCat) {
252 const std::string &catName = catState.first;
254 RooAbsPdf *component_pdf = sim_pdf->getPdf(catName.c_str());
255 auto found = std::find_if(dsetList.begin(), dsetList.end(), [&](
auto const &item) {
256 return catName == item->GetName();
258 RooAbsData *dset = found != dsetList.end() ? found->get() :
nullptr;
260 if (component_pdf && dset && (0. != dset->
sumEntries() || process_empty_data_sets)) {
261 ooccoutI(
nullptr,
Fitting) <<
"getSimultaneousComponents: creating slave calculator #" <<
n <<
" for state "
262 << catName <<
" (" << dset->
numEntries() <<
" dataset entries)" << std::endl;
264 RooAbsPdf *binnedPdf = getBinnedPdf(component_pdf);
265 bool binnedL = (binnedPdf !=
nullptr);
268 for (
const auto component : (
static_cast<RooProdPdf *
>(component_pdf))->pdfList()) {
269 if (component->getAttribute(
"MAIN_MEASUREMENT")) {
272 binnedPdf =
static_cast<RooAbsPdf *
>(component);
280 components.push_back(std::make_unique<RooBinnedL>((binnedPdf ? binnedPdf : component_pdf), dset));
282 components.push_back(
283 std::make_unique<RooUnbinnedL>((binnedPdf ? binnedPdf : component_pdf), dset,
_extended,
_evalBackend));
286 components.back()->setSimCount(N_components);
290 std::unique_ptr<RooArgSet> actualParams{binnedPdf ? binnedPdf->
getParameters(dset)
297 assert(selTargetParams.
equals(*components.back()->getParameters()));
301 if ((!dset || (0. != dset->
sumEntries() && !process_empty_data_sets)) && component_pdf) {
302 ooccoutD(
nullptr,
Fitting) <<
"getSimultaneousComponents: state " << catName
303 <<
" has no data entries, no slave calculator created" << std::endl;
307 oocoutI(
nullptr,
Fitting) <<
"getSimultaneousComponents: created " <<
n <<
" slave calculators." << std::endl;
334 std::unique_ptr<RooAbsL> likelihood;
335 std::vector<std::unique_ptr<RooAbsL>> components;
339 }
else if (
auto binnedPdf = getBinnedPdf(&
_pdf)) {
340 likelihood = std::make_unique<RooBinnedL>(binnedPdf, &
_data);
349 components.push_back(std::move(likelihood));
353 if (!components.empty()) {
354 likelihood = std::make_unique<RooSumL>(&
_pdf, &
_data, std::move(components),
_extended);
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'.
bool getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
RooFit::OwningPtr< RooArgSet > getVariables(bool stripDisconnected=true) const
Return RooArgSet with all variables (tree leaf nodes of expression tree).
Abstract base class for objects that represent a discrete value that can be set from the outside,...
bool equals(const RooAbsCollection &otherColl) const
Check if this and other collection have identically-named contents.
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Storage_t::size_type size() const
Abstract base class for binned and unbinned datasets.
virtual double sumEntries() const =0
Return effective number of entries in dataset, i.e., sum all weights.
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
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....
TClass * IsA() const override
RooArgSet is a container object that can hold multiple RooAbsArg objects.
RooArgSet * selectCommon(const RooAbsCollection &refColl) const
Use RooAbsCollection::selecCommon(), but return as RooArgSet.
RooArgSet _globalObservables
RooFit::EvalBackend _evalBackend
std::unique_ptr< RooAbsL > build()
std::vector< std::unique_ptr< RooAbsL > > getSimultaneousComponents()
NLLFactory & EvalBackend(RooFit::EvalBackend evalBackend)
RooArgSet _externalConstraints
NLLFactory(RooAbsPdf &pdf, RooAbsData &data)
Create a likelihood builder for a given pdf and dataset.
std::string _globalObservablesTag
RooAbsL::Extended _extended
NLLFactory & ExternalConstraints(const RooArgSet &externalconstraints)
NLLFactory & Extended(RooAbsL::Extended extended)
NLLFactory & GlobalObservables(const RooArgSet &globalObservables)
NLLFactory & ConstrainedParameters(const RooArgSet &constrainedParameters)
RooArgSet _constrainedParameters
NLLFactory & GlobalObservablesTag(const char *globalObservablesTag)
static bool isExtendedHelper(RooAbsPdf *pdf, Extended extended)
Efficient implementation of a product of PDFs of the form.
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
const char * GetName() const override
Returns name of object.
Namespace for new RooFit test statistic calculation.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...