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