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