Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooSumL.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
14#include <RooAbsData.h>
16#ifdef ROOFIT_MULTIPROCESS
19#endif
20
21#include <algorithm> // min, max
22
23namespace RooFit {
24namespace TestStatistics {
25
26/** \class RooSumL
27 * \ingroup Roofitcore
28 *
29 * \brief Likelihood class that sums over multiple -log components
30 *
31 * The likelihood is often a product of components, for instance when fitting simultaneous pdfs, but also when using
32 * subsidiary pdfs. Hence, the negative log likelihood that we, in fact, calculate is often a sum over these components.
33 * This sum is implemented by this class.
34 **/
35
36/// \param[in] pdf Raw pointer to the pdf; will not be cloned in this object.
37/// \param[in] data Raw pointer to the dataset; will not be cloned in this object.
38/// \param[in] components The component likelihoods.
39/// \param extended Set extended term calculation on, off or use Extended::Auto to determine automatically based on the
40/// pdf whether to activate or not. \warning components must be passed with std::move, otherwise it cannot be moved into
41/// the RooSumL because of the unique_ptr! \note The number of events in RooSumL is that of the full dataset. Components
42/// will have their own number of events that may be more relevant.
43RooSumL::RooSumL(RooAbsPdf *pdf, RooAbsData *data, std::vector<std::unique_ptr<RooAbsL>> components,
44 RooAbsL::Extended extended)
45 : RooAbsL(pdf, data, data->numEntries(), components.size(), extended), components_(std::move(components))
46{
47}
48// Developer note on the std::move() warning above:
49//
50// The point here was that you don't want to clone RooAbsL's too much, because they contain clones of the pdf and
51// dataset that may have been mangled for optimization. You probably don't want to be doing that all the time, although
52// it is a premature optimization, since we haven't timed its impact. That is the motivation behind using unique_ptrs
53// for the components. The way the classes are built, the RooSumL doesn't care about what components it gets, so by
54// definition it cannot create them internally, so they have to be passed in somehow. Forcing the user to call the
55// function with a std::move is a way to make them fully realize that their local components will be destroyed and the
56// contents moved into the RooSumL.
57//
58// We could change the type to an rvalue reference to make it clearer from the compiler error that std::move is
59// necessary, instead of the obscure error that you get now. Compare the compiler error messages from these two types:
60//
61//#include <vector>
62//#include <memory>
63//#include <cstdio>
64//
65// struct Clear {
66// Clear(std::vector<std::unique_ptr<int>>&& vec) : vec_(std::move(vec)) {
67// printf("number is %d", *vec_[0]);
68// }
69//
70// std::vector<std::unique_ptr<int>> vec_;
71//};
72//
73// struct Obscure {
74// Obscure(std::vector<std::unique_ptr<int>> vec) : vec_(std::move(vec)) {
75// printf("number is %d", *vec_[0]);
76// }
77//
78// std::vector<std::unique_ptr<int>> vec_;
79//};
80//
81// int main() {
82// std::vector<std::unique_ptr<int>> vec;
83// vec.emplace_back(new int(4));
84// Clear thing(vec);
85// Obscure thingy(vec);
86//}
87
88/// \note Compared to the RooAbsTestStatistic implementation that this was taken from, we leave out Hybrid and
89/// SimComponents interleaving support here. This should be implemented by a calculator (i.e. LikelihoodWrapper or
90/// LikelihoodGradientWrapper derived class), if desired.
92RooSumL::evaluatePartition(Section events, std::size_t components_begin, std::size_t components_end)
93{
94 // Evaluate specified range of owned GOF objects
96
97 // from RooAbsOptTestStatistic::combinedValue (which is virtual, so could be different for non-RooNLLVar!):
98 for (std::size_t ix = components_begin; ix < components_end; ++ix) {
99#ifdef ROOFIT_MULTIPROCESS
101#endif
102 ret += components_[ix]->evaluatePartition(events, 0, 0);
103#ifdef ROOFIT_MULTIPROCESS
105#endif
106 }
107
108 return ret;
109}
110
111/// \note This function assumes there is only one subsidiary component.
113{
114 // iterate in reverse, because the subsidiary component is usually at the end:
115 for (auto component = components_.rbegin(); component != components_.rend(); ++component) {
116 if (dynamic_cast<RooSubsidiaryL *>((*component).get()) != nullptr) {
117 return (*component)->evaluatePartition({0, 1}, 0, 0);
118 }
119 }
121}
122
123void RooSumL::constOptimizeTestStatistic(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt)
124{
125 for (auto &component : components_) {
126 component->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
127 }
128}
129
130} // namespace TestStatistics
131} // namespace RooFit
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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
static bool getTimingAnalysis()
Definition Config.cxx:87
static void start_timer(std::string section_name)
static void end_timer(std::string section_name)
virtual std::string GetName() const
Definition RooAbsL.cxx:251
std::vector< std::unique_ptr< RooAbsL > > components_
Definition RooSumL.h:45
ROOT::Math::KahanSum< double > evaluatePartition(Section events, std::size_t components_begin, std::size_t components_end) override
Definition RooSumL.cxx:92
RooSumL(RooAbsPdf *pdf, RooAbsData *data, std::vector< std::unique_ptr< RooAbsL > > components, RooAbsL::Extended extended=RooAbsL::Extended::Auto)
Definition RooSumL.cxx:43
std::string GetClassName() const override
Definition RooSumL.h:40
ROOT::Math::KahanSum< double > getSubsidiaryValue()
Definition RooSumL.cxx:112
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