Logo ROOT  
Reference Guide
RooAbsL.h
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
13#ifndef ROOT_ROOFIT_TESTSTATISTICS_RooAbsL
14#define ROOT_ROOFIT_TESTSTATISTICS_RooAbsL
15
16#include "RooArgSet.h"
17#include "RooAbsArg.h" // enum ConstOpCode
18
19#include "Math/Util.h" // KahanSum
20
21#include <cstddef> // std::size_t
22#include <string>
23#include <memory>
24
25// forward declarations
26class RooAbsPdf;
27class RooAbsData;
28
29namespace RooFit {
30namespace TestStatistics {
31
32class RooAbsL {
33public:
34 enum class Extended { Auto, Yes, No };
35 static bool isExtendedHelper(RooAbsPdf *pdf, Extended extended);
36
37 /// Convenience wrapper class used to distinguish between pdf/data owning and non-owning constructors.
38 struct ClonePdfData {
41 };
42
43private:
44 RooAbsL(std::shared_ptr<RooAbsPdf> pdf, std::shared_ptr<RooAbsData> data, std::size_t N_events,
45 std::size_t N_components, Extended extended);
46
47public:
48 RooAbsL(RooAbsPdf *pdf, RooAbsData *data, std::size_t N_events, std::size_t N_components,
49 Extended extended = Extended::Auto);
50 RooAbsL(ClonePdfData in, std::size_t N_events, std::size_t N_components, Extended extended = Extended::Auto);
51 RooAbsL(const RooAbsL &other);
52 virtual ~RooAbsL() = default;
53
54 void initClones(RooAbsPdf &inpdf, RooAbsData &indata);
55
56 /// A part of some range delimited by two fractional points between 0 and 1 (inclusive).
57 struct Section {
59 {
60 if ((begin > end) || (begin < 0) || (end > 1)) {
61 throw std::domain_error("Invalid input values for section; begin must be >= 0, end <= 1 and begin < end.");
62 }
63 }
64
65 Section(const Section &section) = default;
66
67 std::size_t begin(std::size_t N_total) const { return static_cast<std::size_t>(N_total * begin_fraction); }
68
69 std::size_t end(std::size_t N_total) const
70 {
71 if (end_fraction == 1) {
72 return N_total;
73 } else {
74 return static_cast<std::size_t>(N_total * end_fraction);
75 }
76 }
77
80 };
81
82 /*
83 * \brief Evaluate (part of) the likelihood over a given range of events and components
84 *
85 * A fractional event range is used because components may have different numbers of events. For a
86 * multi-component RooSumL, for instance, this means the caller need not indicate for each component which event
87 * ranges they want to evaluate, but can just pass one overall fractional range.
88 *
89 * \param[in] events The fractional event range.
90 * \param[in] components_begin The first component to be calculated.
91 * \param[in] components_end The *exclusive* upper limit to the range of components to be calculated, i.e. the
92 * component *before this one* is the last to be included. \return The value of part of the negative log likelihood,
93 * returned as a KahanSum object which also includes a carry term.
94 */
96 evaluatePartition(Section events, std::size_t components_begin, std::size_t components_end) = 0;
97
98 // necessary from MinuitFcnGrad to reach likelihood properties:
99 virtual RooArgSet *getParameters();
100
101 /// \brief Interface function signaling a request to perform constant term optimization.
102 ///
103 /// The default implementation takes no action other than to forward the calls to all servers. May be overridden in
104 /// likelihood classes without a cached dataset, like RooSubsidiaryL.
105 virtual void constOptimizeTestStatistic(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt);
106
107 virtual std::string GetName() const;
108 virtual std::string GetTitle() const;
109
110 // necessary in RooMinimizer (via LikelihoodWrapper)
111 inline virtual double defaultErrorLevel() const { return 0.5; }
112
113 // necessary in LikelihoodJob
114 /// Number of dataset entries. Typically equal to the number of dataset events, except in RooSubsidiaryL, which has
115 /// no events.
116 virtual std::size_t numDataEntries() const;
117 inline std::size_t getNEvents() const { return N_events_; }
118 inline std::size_t getNComponents() const { return N_components_; }
119 inline bool isExtended() const { return extended_; }
120 inline void setSimCount(std::size_t value) { sim_count_ = value; }
121
122protected:
123 // Note: pdf_ and data_ can be constructed in two ways, one of which implies ownership and the other does not.
124 // Inspired by this: https://stackoverflow.com/a/61227865/1199693.
125 // The owning variant is used for classes that need a pdf/data clone (RooBinnedL and RooUnbinnedL), whereas the
126 // non-owning version is used for when a reference to the external pdf/dataset is good enough (RooSumL).
127 // This means that pdf_ and data_ are not meant to actually be shared! If there were a unique_ptr with optional
128 // ownership, we would have used that instead.
129 std::shared_ptr<RooAbsPdf> pdf_;
130 std::shared_ptr<RooAbsData> data_;
131 std::unique_ptr<RooArgSet> normSet_; // Pointer to set with observables used for normalization
132
133 std::size_t N_events_ = 1;
134 std::size_t N_components_ = 1;
135
136 bool extended_ = false;
137
138 std::size_t sim_count_ = 1; // Total number of component p.d.f.s in RooSimultaneous (if any)
139};
140
141} // namespace TestStatistics
142} // namespace RooFit
143
144#endif // ROOT_ROOFIT_TESTSTATISTICS_RooAbsL
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
Definition: Util.h:122
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:79
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:35
virtual double defaultErrorLevel() const
Definition: RooAbsL.h:111
std::shared_ptr< RooAbsData > data_
Definition: RooAbsL.h:130
std::size_t getNComponents() const
Definition: RooAbsL.h:118
virtual ROOT::Math::KahanSum< double > evaluatePartition(Section events, std::size_t components_begin, std::size_t components_end)=0
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
void setSimCount(std::size_t value)
Definition: RooAbsL.h:120
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::size_t getNEvents() const
Definition: RooAbsL.h:117
std::shared_ptr< RooAbsPdf > pdf_
Definition: RooAbsL.h:129
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Convenience wrapper class used to distinguish between pdf/data owning and non-owning constructors.
Definition: RooAbsL.h:38
A part of some range delimited by two fractional points between 0 and 1 (inclusive).
Definition: RooAbsL.h:57
std::size_t begin(std::size_t N_total) const
Definition: RooAbsL.h:67
Section(const Section &section)=default
std::size_t end(std::size_t N_total) const
Definition: RooAbsL.h:69
Section(double begin, double end)
Definition: RooAbsL.h:58