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