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 "RooAbsPdf.h"
16#include "RooAbsData.h"
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 handle common tasks.
54RooAbsL::RooAbsL(std::shared_ptr<RooAbsPdf> pdf, std::shared_ptr<RooAbsData> data,
55 std::size_t N_events, std::size_t N_components, Extended extended)
56 : pdf_(std::move(pdf)), data_(std::move(data)), N_events_(N_events), N_components_(N_components)
57{
58 extended_ = isExtendedHelper(pdf_.get(), extended);
59 if (extended == Extended::Auto) {
60 if (extended_) {
61 oocoutI((TObject *)nullptr, Minimization)
62 << "in RooAbsL ctor: p.d.f. provides expected number of events, including extended term in likelihood."
63 << std::endl;
64 }
65 }
66}
67
68/// Constructor that clones the pdf/data and owns those cloned copies.
69///
70/// This constructor is used for classes that need a pdf/data clone (RooBinnedL and RooUnbinnedL).
71///
72/// \param in Struct containing raw pointers to the pdf and dataset that are to be cloned.
73/// \param N_events The number of events in this likelihood's dataset.
74/// \param N_components The number of components in the likelihood.
75/// \param extended Set extended term calculation on, off or use Extended::Auto to determine automatically based on the pdf whether to activate or not.
76RooAbsL::RooAbsL(RooAbsL::ClonePdfData in, std::size_t N_events, std::size_t N_components, Extended extended)
77 : RooAbsL(std::shared_ptr<RooAbsPdf>(static_cast<RooAbsPdf *>(in.pdf->cloneTree())),
78 std::shared_ptr<RooAbsData>(static_cast<RooAbsData *>(in.data->Clone())), N_events, N_components, extended)
79{
80 initClones(*in.pdf, *in.data);
81}
82
83/// Constructor that does not clone pdf/data and uses the shared_ptr aliasing constructor to make it non-owning.
84///
85/// This constructor is used for classes where a reference to the external pdf/dataset is good enough (RooSumL and RooSubsidiaryL).
86///
87/// \param inpdf Raw pointer to the pdf.
88/// \param indata Raw pointer to the dataset.
89/// \param N_events The number of events in this likelihood's dataset.
90/// \param N_components The number of components in the likelihood.
91/// \param extended Set extended term calculation on, off or use Extended::Auto to determine automatically based on the pdf whether to activate or not.
92RooAbsL::RooAbsL(RooAbsPdf *inpdf, RooAbsData *indata, std::size_t N_events, std::size_t N_components,
93 Extended extended)
94 : RooAbsL({std::shared_ptr<RooAbsPdf>(nullptr), inpdf}, {std::shared_ptr<RooAbsData>(nullptr), indata}, N_events, N_components, extended)
95{}
96
97
99 : pdf_(other.pdf_), data_(other.data_), N_events_(other.N_events_), N_components_(other.N_components_), extended_(other.extended_), sim_count_(other.sim_count_)
100{
101 // 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)
102 assert((pdf_.use_count() != 1) && (data_.use_count() != 1) && (pdf_.use_count() == data_.use_count()));
103 if ((pdf_.use_count() > 1) && (data_.use_count() > 1)) {
104 pdf_.reset(static_cast<RooAbsPdf *>(other.pdf_->cloneTree()));
105 data_.reset(static_cast<RooAbsData *>(other.data_->Clone()));
106 initClones(*other.pdf_, *other.data_);
107 }
108}
109
111{
112 // ******************************************************************
113 // *** PART 1 *** Clone incoming pdf, attach to each other *
114 // ******************************************************************
115
116 // Attach FUNC to data set
117 auto _funcObsSet = pdf_->getObservables(indata);
118
119 if (pdf_->getAttribute("BinnedLikelihood")) {
120 pdf_->setAttribute("BinnedLikelihoodActive");
121 }
122
123 // Reattach FUNC to original parameters
124 std::unique_ptr<RooArgSet> origParams{inpdf.getParameters(indata)};
125 pdf_->recursiveRedirectServers(*origParams);
126
127 // Store normalization set
128 normSet_.reset((RooArgSet *)indata.get()->snapshot(kFALSE));
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, kTRUE);
136 _funcObsSet->add(tmp2, kTRUE);
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((TObject *)0, 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)) {
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 // If dataset is binned, activate caching of bins that are invalid because they're outside the
184 // updated range definition (WVE need to add virtual interface here)
185 RooDataHist *tmph = dynamic_cast<RooDataHist *>(data_.get());
186 if (tmph) {
187 tmph->cacheValidEntries();
188 }
189
190 // This is deferred from part 2 - but must happen after part 3 - otherwise invalid bins cannot be properly marked in
191 // cacheValidEntries
192 data_->attachBuffers(*_funcObsSet);
193
194 // *********************************************************************
195 // *** PART 4 *** Adjust normalization range for projected observables *
196 // *********************************************************************
197
198 // TODO
199
200 // *********************************************************************
201 // *** PART 4 *** Finalization and activation of optimization *
202 // *********************************************************************
203
204 // optimization steps (copied from ROATS::optimizeCaching)
205
206 pdf_->getVal(normSet_.get());
207 // Set value caching mode for all nodes that depend on any of the observables to ADirty
208 pdf_->optimizeCacheMode(*_funcObsSet);
209 // Disable propagation of dirty state flags for observables
210 data_->setDirtyProp(kFALSE);
211
212 // Disable reading of observables that are not used
213 data_->optimizeReadingWithCaching(*pdf_, RooArgSet(), RooArgSet());
214}
215
217{
218 auto ding = pdf_->getParameters(*data_);
219 return ding;
220}
221
223{
224 // to be further implemented, this is just a first test implementation
225 if (opcode == RooAbsArg::Activate) {
226 ConstantTermsOptimizer::enableConstantTermsOptimization(pdf_.get(), normSet_.get(), data_.get(), doAlsoTrackingOpt);
227 }
228}
229
230std::string RooAbsL::GetName() const
231{
232 std::string output("likelihood of pdf ");
233 output.append(pdf_->GetName());
234 return output;
235}
236
237std::string RooAbsL::GetTitle() const
238{
239 std::string output("likelihood of pdf ");
240 output.append(pdf_->GetTitle());
241 return output;
242}
243
244std::size_t RooAbsL::numDataEntries() const
245{
246 return static_cast<std::size_t>(data_->numEntries());
247}
248
249} // namespace TestStatistics
250} // namespace RooFit
#define e(i)
Definition RSha256.hxx:103
#define oocoutE(o, a)
#define oocoutI(o, a)
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
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_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
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:82
virtual const RooArgSet * get() const
Definition RooAbsData.h:128
@ CanBeExtended
Definition RooAbsPdf.h:256
@ MustBeExtended
Definition RooAbsPdf.h:256
virtual ExtendMode extendMode() const
Returns ability of PDF to provide extended likelihood terms.
Definition RooAbsPdf.h:260
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:35
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:158
The RooDataHist is a container class to hold N-dimensional binned data.
Definition RooDataHist.h:45
void cacheValidEntries()
Compute which bins of the dataset are part of the currently set fit range.
static void softAbort()
std::shared_ptr< RooAbsData > data_
Definition RooAbsL.h:130
static bool isExtendedHelper(RooAbsPdf *pdf, Extended extended)
Definition RooAbsL.cxx:35
virtual std::string GetName() const
Definition RooAbsL.cxx:230
virtual std::string GetTitle() const
Definition RooAbsL.cxx:237
std::unique_ptr< RooArgSet > normSet_
Definition RooAbsL.h:131
void initClones(RooAbsPdf &inpdf, RooAbsData &indata)
Definition RooAbsL.cxx:110
virtual void constOptimizeTestStatistic(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt)
Interface function signaling a request to perform constant term optimization.
Definition RooAbsL.cxx:222
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 RooArgSet * getParameters()
Definition RooAbsL.cxx:216
virtual std::size_t numDataEntries() const
Number of dataset entries.
Definition RooAbsL.cxx:244
std::shared_ptr< RooAbsPdf > pdf_
Definition RooAbsL.h:129
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
virtual const char * GetName() const
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 Common.h:18
@ InputArguments
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:38
static void output(int code)
Definition gifencode.c:226