Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooAbsL.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl
5 *
6 * Copyright (c) 2021, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
14#include "RooAbsData.h"
15
16// for dynamic casts in init_clones:
17#include "RooAbsRealLValue.h"
18#include "RooRealVar.h"
19#include "RooDataHist.h"
20
21// other stuff in init_clones:
22#include "RooErrorHandler.h"
23#include "RooMsgService.h"
24
25// concrete classes in getParameters (testing, remove later)
26#include "RooRealSumPdf.h"
27#include "RooProdPdf.h"
28
29namespace RooFit {
30namespace TestStatistics {
31
32// static function
34{
35 switch (extended) {
37 return false;
38 }
40 return true;
41 }
44 }
45 default: {
46 throw std::logic_error("RooAbsL::isExtendedHelper got an unknown extended value!");
47 }
48 }
49}
50
51/// After handling cloning (or not) of the pdf and dataset, the public constructors call this private constructor to
52/// handle common tasks.
53RooAbsL::RooAbsL(std::shared_ptr<RooAbsPdf> pdf, std::shared_ptr<RooAbsData> data, std::size_t N_events,
54 std::size_t N_components, Extended extended)
55 : pdf_(std::move(pdf)),
56 data_(std::move(data)),
57 N_events_(N_events),
58 N_components_(N_components),
59 extended_(isExtendedHelper(pdf_.get(), extended))
60{
61 if (extended == Extended::Auto) {
62 if (extended_) {
63 oocoutI(nullptr, Minimization)
64 << "in RooAbsL ctor: p.d.f. provides expected number of events, including extended term in likelihood."
65 << std::endl;
66 }
67 }
68}
69
70/// Constructor that clones the pdf/data and owns those cloned copies.
71///
72/// This constructor is used for classes that need a pdf/data clone (RooBinnedL and RooUnbinnedL).
73///
74/// \param in Struct containing raw pointers to the pdf and dataset that are to be cloned.
75/// \param N_events The number of events in this likelihood's dataset.
76/// \param N_components The number of components in the likelihood.
77/// \param extended Set extended term calculation on, off or use Extended::Auto to determine automatically based on the
78/// pdf whether to activate or not.
80 : RooAbsL(in.ownedPdf ? std::move(in.ownedPdf)
81 : std::unique_ptr<RooAbsPdf>(static_cast<RooAbsPdf *>(in.pdf->cloneTree())),
82 std::shared_ptr<RooAbsData>(static_cast<RooAbsData *>(in.data->Clone())), N_events, N_components, extended)
83{
84 initClones(*in.pdf, *in.data);
85}
86
87/// Constructor that does not clone pdf/data and uses the shared_ptr aliasing constructor to make it non-owning.
88///
89/// This constructor is used for classes where a reference to the external pdf/dataset is good enough (RooSumL and
90/// RooSubsidiaryL).
91///
92/// \param inpdf Raw pointer to the pdf.
93/// \param indata Raw pointer to the dataset.
94/// \param N_events The number of events in this likelihood's dataset.
95/// \param N_components The number of components in the likelihood.
96/// \param extended Set extended term calculation on, off or use Extended::Auto to determine automatically based on the
97/// pdf whether to activate or not.
100 : RooAbsL({std::shared_ptr<RooAbsPdf>(nullptr), inpdf}, {std::shared_ptr<RooAbsData>(nullptr), indata}, N_events,
102{
103}
104
106 : pdf_(other.pdf_),
107 data_(other.data_),
108 N_events_(other.N_events_),
109 N_components_(other.N_components_),
110 extended_(other.extended_),
111 sim_count_(other.sim_count_)
112{
113 // it can never be one, since we just copied the shared_ptr; if it is, something really weird is going on; also they
114 // must be equal (usually either zero or two)
115 assert((pdf_.use_count() != 1) && (data_.use_count() != 1) && (pdf_.use_count() == data_.use_count()));
116 if ((pdf_.use_count() > 1) && (data_.use_count() > 1)) {
117 pdf_.reset(static_cast<RooAbsPdf *>(other.pdf_->cloneTree()));
118 data_.reset(static_cast<RooAbsData *>(other.data_->Clone()));
119 initClones(*other.pdf_, *other.data_);
120 }
121}
122
124{
125 // ******************************************************************
126 // *** PART 1 *** Clone incoming pdf, attach to each other *
127 // ******************************************************************
128
129 // Attach FUNC to data set
130 std::unique_ptr<RooArgSet> _funcObsSet{pdf_->getObservables(indata)};
131
132 if (pdf_->getAttribute("BinnedLikelihood")) {
133 pdf_->setAttribute("BinnedLikelihoodActive");
134 }
135
136 // Reattach FUNC to original parameters
137 std::unique_ptr<RooArgSet> origParams{inpdf.getParameters(indata)};
138 pdf_->recursiveRedirectServers(*origParams);
139
140 // Store normalization set
141 normSet_ = std::make_unique<RooArgSet>();
142 indata.get()->snapshot(*normSet_, false);
143
144 // Expand list of observables with any observables used in parameterized ranges
145 for (const auto realDep : *_funcObsSet) {
146 auto realDepRLV = dynamic_cast<RooAbsRealLValue *>(realDep);
147 if (realDepRLV && realDepRLV->isDerived()) {
149 realDepRLV->leafNodeServerList(&tmp2, nullptr, true);
150 _funcObsSet->add(tmp2, true);
151 }
152 }
153
154 // ******************************************************************
155 // *** PART 2 *** Clone and adjust incoming data, attach to PDF *
156 // ******************************************************************
157
158 // Check if the fit ranges of the dependents in the data and in the FUNC are consistent
159 const RooArgSet *dataDepSet = indata.get();
160 for (const auto arg : *_funcObsSet) {
161
162 // Check that both dataset and function argument are of type RooRealVar
163 auto realReal = dynamic_cast<RooRealVar *>(arg);
164 if (!realReal) {
165 continue;
166 }
167 auto datReal = dynamic_cast<RooRealVar *>(dataDepSet->find(realReal->GetName()));
168 if (!datReal) {
169 continue;
170 }
171
172 // Check that range of observables in pdf is equal or contained in range of observables in data
173
174 if (!realReal->getBinning().lowBoundFunc() && realReal->getMin() < (datReal->getMin() - 1e-6)) {
175 oocoutE(nullptr, InputArguments) << "RooAbsL: ERROR minimum of FUNC observable " << arg->GetName() << "("
176 << realReal->getMin() << ") is smaller than that of " << arg->GetName()
177 << " in the dataset (" << datReal->getMin() << ")" << std::endl;
179 return;
180 }
181
182 if (!realReal->getBinning().highBoundFunc() && realReal->getMax() > (datReal->getMax() + 1e-6)) {
183 oocoutE(nullptr, InputArguments)
184 << "RooAbsL: ERROR maximum of FUNC observable " << arg->GetName() << " is larger than that of "
185 << arg->GetName() << " in the dataset" << std::endl;
187 return;
188 }
189 }
190
191 // ******************************************************************
192 // *** PART 3 *** Make adjustments for fit ranges, if specified *
193 // ******************************************************************
194
195 // TODO
196
197 // Jonas R.: The following code is commented out, because the functionality
198 // to mask out-of-range entries with `RooDataHist::cacheValidEntries` has
199 // been removed from the RooDataHist. If you want to implement ranged fits
200 // properly, please create a RooDataHist for the requested range with
201 // `RooDataHist::reduce`.
202
203 //// If dataset is binned, activate caching of bins that are invalid because they're outside the
204 //// updated range definition (WVE need to add virtual interface here)
205 // RooDataHist *tmph = dynamic_cast<RooDataHist *>(data_.get());
206 // if (tmph) {
207 // tmph->cacheValidEntries();
208 //}
209
210 // This is deferred from part 2 - but must happen after part 3 - otherwise invalid bins cannot be properly marked in
211 // cacheValidEntries
212 data_->attachBuffers(*_funcObsSet);
213
214 // *********************************************************************
215 // *** PART 4 *** Adjust normalization range for projected observables *
216 // *********************************************************************
217
218 // TODO
219
220 // *********************************************************************
221 // *** PART 4 *** Finalization and activation of optimization *
222 // *********************************************************************
223
224 // optimization steps (copied from ROATS::optimizeCaching)
225
226 pdf_->getVal(normSet_.get());
227 // Set value caching mode for all nodes that depend on any of the observables to ADirty
228 pdf_->optimizeCacheMode(*_funcObsSet);
229 // Disable propagation of dirty state flags for observables
230 data_->setDirtyProp(false);
231}
232
233std::unique_ptr<RooArgSet> RooAbsL::getParameters()
234{
235 return std::unique_ptr<RooArgSet>{pdf_->getParameters(*data_)};
236}
237
238std::string RooAbsL::GetName() const
239{
240 std::string output("likelihood of pdf ");
241 output.append(pdf_->GetName());
242 return output;
243}
244
245std::string RooAbsL::GetTitle() const
246{
247 std::string output("likelihood of pdf ");
248 output.append(pdf_->GetTitle());
249 return output;
250}
251
252std::size_t RooAbsL::numDataEntries() const
253{
254 return static_cast<std::size_t>(data_->numEntries());
255}
256
257} // namespace TestStatistics
258} // namespace RooFit
#define e(i)
Definition RSha256.hxx:103
#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
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:55
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
@ CanBeExtended
Definition RooAbsPdf.h:208
@ MustBeExtended
Definition RooAbsPdf.h:208
virtual ExtendMode extendMode() const
Returns ability of PDF to provide extended likelihood terms.
Definition RooAbsPdf.h:212
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
static void softAbort()
Soft abort function that interrupts macro execution but doesn't kill ROOT.
Convenience wrapper class used to distinguish between pdf/data owning and non-owning constructors.
Definition RooAbsL.h:38
std::shared_ptr< RooAbsData > data_
Definition RooAbsL.h:136
static bool isExtendedHelper(RooAbsPdf *pdf, Extended extended)
Definition RooAbsL.cxx:33
virtual std::string GetName() const
Definition RooAbsL.cxx:238
virtual std::string GetTitle() const
Definition RooAbsL.cxx:245
std::unique_ptr< RooArgSet > normSet_
Pointer to set with observables used for normalization.
Definition RooAbsL.h:137
void initClones(RooAbsPdf &inpdf, RooAbsData &indata)
Definition RooAbsL.cxx:123
virtual std::unique_ptr< RooArgSet > getParameters()
Definition RooAbsL.cxx:233
RooAbsL(std::shared_ptr< RooAbsPdf > pdf, std::shared_ptr< RooAbsData > data, std::size_t N_events, std::size_t N_components, Extended extended)
After handling cloning (or not) of the pdf and dataset, the public constructors call this private con...
Definition RooAbsL.cxx:53
virtual std::size_t numDataEntries() const
Number of dataset entries.
Definition RooAbsL.cxx:252
std::shared_ptr< RooAbsPdf > pdf_
Definition RooAbsL.h:135
Variable that can be changed from the outside.
Definition RooRealVar.h:37
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
@ InputArguments