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
15#include "RooAbsData.h"
16#include "RooAbsDataStore.h" // complete class for access in constOptimizeTestStatistic
17
18// for dynamic casts in init_clones:
19#include "RooAbsRealLValue.h"
20#include "RooRealVar.h"
21#include "RooDataHist.h"
22
23// other stuff in init_clones:
24#include "RooErrorHandler.h"
25#include "RooMsgService.h"
26
27// concrete classes in getParameters (testing, remove later)
28#include "RooRealSumPdf.h"
29#include "RooProdPdf.h"
30
31namespace RooFit {
32namespace TestStatistics {
33
34// static function
36{
37 switch (extended) {
39 return false;
40 }
42 return true;
43 }
46 }
47 default: {
48 throw std::logic_error("RooAbsL::isExtendedHelper got an unknown extended value!");
49 }
50 }
51}
52
53/// After handling cloning (or not) of the pdf and dataset, the public constructors call this private constructor to
54/// handle common tasks.
55RooAbsL::RooAbsL(std::shared_ptr<RooAbsPdf> pdf, std::shared_ptr<RooAbsData> data, std::size_t N_events,
56 std::size_t N_components, Extended extended)
57 : pdf_(std::move(pdf)),
58 data_(std::move(data)),
59 N_events_(N_events),
60 N_components_(N_components),
61 extended_(isExtendedHelper(pdf_.get(), extended))
62{
63 if (extended == Extended::Auto) {
64 if (extended_) {
65 oocoutI(nullptr, Minimization)
66 << "in RooAbsL ctor: p.d.f. provides expected number of events, including extended term in likelihood."
67 << std::endl;
68 }
69 }
70}
71
72/// Constructor that clones the pdf/data and owns those cloned copies.
73///
74/// This constructor is used for classes that need a pdf/data clone (RooBinnedL and RooUnbinnedL).
75///
76/// \param in Struct containing raw pointers to the pdf and dataset that are to be cloned.
77/// \param N_events The number of events in this likelihood's dataset.
78/// \param N_components The number of components in the likelihood.
79/// \param extended Set extended term calculation on, off or use Extended::Auto to determine automatically based on the
80/// pdf whether to activate or not.
81RooAbsL::RooAbsL(RooAbsL::ClonePdfData in, std::size_t N_events, std::size_t N_components, Extended extended)
82 : RooAbsL(in.ownedPdf ? std::move(in.ownedPdf)
83 : std::unique_ptr<RooAbsPdf>(static_cast<RooAbsPdf *>(in.pdf->cloneTree())),
84 std::shared_ptr<RooAbsData>(static_cast<RooAbsData *>(in.data->Clone())), N_events, N_components, extended)
85{
86 initClones(*in.pdf, *in.data);
87}
88
89/// Constructor that does not clone pdf/data and uses the shared_ptr aliasing constructor to make it non-owning.
90///
91/// This constructor is used for classes where a reference to the external pdf/dataset is good enough (RooSumL and
92/// RooSubsidiaryL).
93///
94/// \param inpdf Raw pointer to the pdf.
95/// \param indata Raw pointer to the dataset.
96/// \param N_events The number of events in this likelihood's dataset.
97/// \param N_components The number of components in the likelihood.
98/// \param extended Set extended term calculation on, off or use Extended::Auto to determine automatically based on the
99/// pdf whether to activate or not.
100RooAbsL::RooAbsL(RooAbsPdf *inpdf, RooAbsData *indata, std::size_t N_events, std::size_t N_components,
101 Extended extended)
102 : RooAbsL({std::shared_ptr<RooAbsPdf>(nullptr), inpdf}, {std::shared_ptr<RooAbsData>(nullptr), indata}, N_events,
103 N_components, extended)
104{
105}
106
108 : pdf_(other.pdf_),
109 data_(other.data_),
110 N_events_(other.N_events_),
111 N_components_(other.N_components_),
112 extended_(other.extended_),
113 sim_count_(other.sim_count_)
114{
115 // it can never be one, since we just copied the shared_ptr; if it is, something really weird is going on; also they
116 // must be equal (usually either zero or two)
117 assert((pdf_.use_count() != 1) && (data_.use_count() != 1) && (pdf_.use_count() == data_.use_count()));
118 if ((pdf_.use_count() > 1) && (data_.use_count() > 1)) {
119 pdf_.reset(static_cast<RooAbsPdf *>(other.pdf_->cloneTree()));
120 data_.reset(static_cast<RooAbsData *>(other.data_->Clone()));
121 initClones(*other.pdf_, *other.data_);
122 }
123}
124
126{
127 // ******************************************************************
128 // *** PART 1 *** Clone incoming pdf, attach to each other *
129 // ******************************************************************
130
131 // Attach FUNC to data set
132 auto _funcObsSet = pdf_->getObservables(indata);
133
134 if (pdf_->getAttribute("BinnedLikelihood")) {
135 pdf_->setAttribute("BinnedLikelihoodActive");
136 }
137
138 // Reattach FUNC to original parameters
139 std::unique_ptr<RooArgSet> origParams{inpdf.getParameters(indata)};
140 pdf_->recursiveRedirectServers(*origParams);
141
142 // Store normalization set
143 normSet_ = std::make_unique<RooArgSet>();
144 indata.get()->snapshot(*normSet_, false);
145
146 // Expand list of observables with any observables used in parameterized ranges
147 for (const auto realDep : *_funcObsSet) {
148 auto realDepRLV = dynamic_cast<RooAbsRealLValue *>(realDep);
149 if (realDepRLV && realDepRLV->isDerived()) {
150 RooArgSet tmp2;
151 realDepRLV->leafNodeServerList(&tmp2, nullptr, true);
152 _funcObsSet->add(tmp2, true);
153 }
154 }
155
156 // ******************************************************************
157 // *** PART 2 *** Clone and adjust incoming data, attach to PDF *
158 // ******************************************************************
159
160 // Check if the fit ranges of the dependents in the data and in the FUNC are consistent
161 const RooArgSet *dataDepSet = indata.get();
162 for (const auto arg : *_funcObsSet) {
163
164 // Check that both dataset and function argument are of type RooRealVar
165 auto realReal = dynamic_cast<RooRealVar *>(arg);
166 if (!realReal) {
167 continue;
168 }
169 auto datReal = dynamic_cast<RooRealVar *>(dataDepSet->find(realReal->GetName()));
170 if (!datReal) {
171 continue;
172 }
173
174 // Check that range of observables in pdf is equal or contained in range of observables in data
175
176 if (!realReal->getBinning().lowBoundFunc() && realReal->getMin() < (datReal->getMin() - 1e-6)) {
177 oocoutE(nullptr, InputArguments) << "RooAbsL: ERROR minimum of FUNC observable " << arg->GetName() << "("
178 << realReal->getMin() << ") is smaller than that of " << arg->GetName()
179 << " in the dataset (" << datReal->getMin() << ")" << std::endl;
181 return;
182 }
183
184 if (!realReal->getBinning().highBoundFunc() && realReal->getMax() > (datReal->getMax() + 1e-6)) {
185 oocoutE(nullptr, InputArguments)
186 << "RooAbsL: ERROR maximum of FUNC observable " << arg->GetName() << " is larger than that of "
187 << arg->GetName() << " in the dataset" << std::endl;
189 return;
190 }
191 }
192
193 // ******************************************************************
194 // *** PART 3 *** Make adjustments for fit ranges, if specified *
195 // ******************************************************************
196
197 // TODO
198
199 // Jonas R.: The following code is commented out, because the functionality
200 // to mask out-of-range entries with `RooDataHist::cacheValidEntries` has
201 // been removed from the RooDataHist. If you want to implement ranged fits
202 // properly, please create a RooDataHist for the requested range with
203 // `RooDataHist::reduce`.
204
205 //// If dataset is binned, activate caching of bins that are invalid because they're outside the
206 //// updated range definition (WVE need to add virtual interface here)
207 // RooDataHist *tmph = dynamic_cast<RooDataHist *>(data_.get());
208 // if (tmph) {
209 // tmph->cacheValidEntries();
210 //}
211
212 // This is deferred from part 2 - but must happen after part 3 - otherwise invalid bins cannot be properly marked in
213 // cacheValidEntries
214 data_->attachBuffers(*_funcObsSet);
215
216 // *********************************************************************
217 // *** PART 4 *** Adjust normalization range for projected observables *
218 // *********************************************************************
219
220 // TODO
221
222 // *********************************************************************
223 // *** PART 4 *** Finalization and activation of optimization *
224 // *********************************************************************
225
226 // optimization steps (copied from ROATS::optimizeCaching)
227
228 pdf_->getVal(normSet_.get());
229 // Set value caching mode for all nodes that depend on any of the observables to ADirty
230 pdf_->optimizeCacheMode(*_funcObsSet);
231 // Disable propagation of dirty state flags for observables
232 data_->setDirtyProp(false);
233
234 // Disable reading of observables that are not used
235 data_->optimizeReadingWithCaching(*pdf_, RooArgSet(), RooArgSet());
236}
237
238std::unique_ptr<RooArgSet> RooAbsL::getParameters()
239{
240 return std::unique_ptr<RooArgSet>{pdf_->getParameters(*data_)};
241}
242
244{
245 if (data_.get()->hasFilledCache() && opcode == RooAbsArg::Activate) {
246 opcode = RooAbsArg::ValueChange;
247 }
248
249 switch (opcode) {
251 oocxcoutI((TObject *)nullptr, Optimization)
252 << "RooAbsL::constOptimizeTestStatistic(" << GetName()
253 << ") optimizing evaluation of test statistic by finding all nodes in p.d.f that depend exclusively"
254 << " on observables and constant parameters and precalculating their values" << std::endl;
256 doAlsoTrackingOpt);
257 break;
258
260 oocxcoutI((TObject *)nullptr, Optimization)
261 << "RooAbsL::constOptimizeTestStatistic(" << GetName()
262 << ") deactivating optimization of constant terms in test statistic" << std::endl;
264 break;
265
267 oocxcoutI((TObject *)nullptr, Optimization)
268 << "RooAbsL::constOptimizeTestStatistic(" << GetName()
269 << ") one ore more parameter were changed from constant to floating or vice versa, "
270 << "re-evaluating constant term optimization" << std::endl;
273 doAlsoTrackingOpt);
274 break;
275
277 oocxcoutI((TObject *)nullptr, Optimization)
278 << "RooAbsL::constOptimizeTestStatistic(" << GetName()
279 << ") the value of one ore more constant parameter were changed re-evaluating constant term optimization"
280 << std::endl;
281 // Request a forcible cache update of all cached nodes
282 data_.get()->store()->forceCacheUpdate();
283 break;
284 }
285}
286
287std::string RooAbsL::GetName() const
288{
289 std::string output("likelihood of pdf ");
290 output.append(pdf_->GetName());
291 return output;
292}
293
294std::string RooAbsL::GetTitle() const
295{
296 std::string output("likelihood of pdf ");
297 output.append(pdf_->GetTitle());
298 return output;
299}
300
301std::size_t RooAbsL::numDataEntries() const
302{
303 return static_cast<std::size_t>(data_->numEntries());
304}
305
306} // namespace TestStatistics
307} // namespace RooFit
#define e(i)
Definition RSha256.hxx:103
#define oocoutE(o, a)
#define oocoutI(o, a)
#define oocxcoutI(o, a)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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...
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
virtual const RooArgSet * get() const
Definition RooAbsData.h:101
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
@ CanBeExtended
Definition RooAbsPdf.h:212
@ MustBeExtended
Definition RooAbsPdf.h:212
virtual ExtendMode extendMode() const
Returns ability of PDF to provide extended likelihood terms.
Definition RooAbsPdf.h:216
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
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:154
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:39
std::shared_ptr< RooAbsData > data_
Definition RooAbsL.h:143
static bool isExtendedHelper(RooAbsPdf *pdf, Extended extended)
Definition RooAbsL.cxx:35
virtual std::string GetName() const
Definition RooAbsL.cxx:287
virtual std::string GetTitle() const
Definition RooAbsL.cxx:294
std::unique_ptr< RooArgSet > normSet_
Pointer to set with observables used for normalization.
Definition RooAbsL.h:144
void initClones(RooAbsPdf &inpdf, RooAbsData &indata)
Definition RooAbsL.cxx:125
virtual std::unique_ptr< RooArgSet > getParameters()
Definition RooAbsL.cxx:238
virtual void constOptimizeTestStatistic(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt)
Interface function signaling a request to perform constant term optimization.
Definition RooAbsL.cxx:243
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:55
virtual std::size_t numDataEntries() const
Number of dataset entries.
Definition RooAbsL.cxx:301
std::shared_ptr< RooAbsPdf > pdf_
Definition RooAbsL.h:142
Variable that can be changed from the outside.
Definition RooRealVar.h:37
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:41
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
@ InputArguments
static void disableConstantTermsOptimization(RooAbsReal *function, RooArgSet *norm_set, RooAbsData *dataset, RooArgSet *observables=nullptr)
static void enableConstantTermsOptimization(RooAbsReal *function, RooArgSet *norm_set, RooAbsData *dataset, bool applyTrackingOpt)
static void output()