Logo ROOT  
Reference Guide
RooUnbinnedL.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
13/**
14\file RooUnbinnedL.cxx
15\class RooUnbinnedL
16\ingroup Roofitcore
17
18Class RooUnbinnedL implements a -log(likelihood) calculation from a dataset
19(assumed to be unbinned) and a PDF. The NLL is calculated as
20\f[
21 \sum_\mathrm{data} -\log( \mathrm{pdf}(x_\mathrm{data}))
22\f]
23In extended mode, a
24\f$ N_\mathrm{expect} - N_\mathrm{observed}*log(N_\mathrm{expect}) \f$ term is added.
25**/
26
28
29#include "RooAbsData.h"
30#include "RooAbsPdf.h"
31#include "RooAbsDataStore.h"
32#include "RooNLLVar.h" // RooNLLVar::ComputeScalar
33#include "RunContext.h"
34#include "RooChangeTracker.h"
35
36namespace RooFit {
37namespace TestStatistics {
38
40 bool useBatchedEvaluations)
41 : RooAbsL(RooAbsL::ClonePdfData{pdf, data}, data->numEntries(), 1, extended),
42 useBatchedEvaluations_(useBatchedEvaluations)
43{
44 std::unique_ptr<RooArgSet> params(pdf->getParameters(data));
45 paramTracker_ = std::make_unique<RooChangeTracker>("chtracker","change tracker",*params,true);
46}
47
49 : RooAbsL(other), apply_weight_squared(other.apply_weight_squared), _first(other._first),
50 useBatchedEvaluations_(other.useBatchedEvaluations_), lastSection_(other.lastSection_),
51 cachedResult_(other.cachedResult_)
52{
53 paramTracker_ = std::make_unique<RooChangeTracker>(*other.paramTracker_);
54}
55
57
58//////////////////////////////////////////////////////////////////////////////////
59
60/// Returns true if value was changed, false otherwise.
62{
63 if (apply_weight_squared != flag) {
65 return true;
66 }
67 // setValueDirty();
68 return false;
69}
70
73}
74
75//////////////////////////////////////////////////////////////////////////////////
76/// Calculate and return likelihood on subset of data from firstEvent to lastEvent
77/// processed with a step size of 'stepSize'. If this an extended likelihood and
78/// and the zero event is processed the extended term is added to the return
79/// likelihood.
80///
82RooUnbinnedL::evaluatePartition(Section events, std::size_t /*components_begin*/, std::size_t /*components_end*/)
83{
84 // Throughout the calculation, we use Kahan's algorithm for summing to
85 // prevent loss of precision - this is a factor four more expensive than
86 // straight addition, but since evaluating the PDF is usually much more
87 // expensive than that, we tolerate the additional cost...
89 double sumWeight;
90
91 // Do not reevaluate likelihood if parameters nor event range have changed
92 if (!paramTracker_->hasChanged(true) && events == lastSection_ && (cachedResult_.Sum() != 0 || cachedResult_.Carry() != 0)) return cachedResult_;
93
94 data_->store()->recalculateCache(nullptr, events.begin(N_events_), events.end(N_events_), 1, true);
95
97 std::unique_ptr<RooBatchCompute::RunContext> evalData;
98 std::tie(result, sumWeight) = RooNLLVar::computeBatchedFunc(pdf_.get(), data_.get(), evalData, normSet_.get(), apply_weight_squared,
99 1, events.begin(N_events_), events.end(N_events_));
100 } else {
101 std::tie(result, sumWeight) = RooNLLVar::computeScalarFunc(pdf_.get(), data_.get(), normSet_.get(), apply_weight_squared,
102 1, events.begin(N_events_), events.end(N_events_));
103 }
104
105 // include the extended maximum likelihood term, if requested
106 if (extended_ && events.begin_fraction == 0) {
107 result += pdf_->extendedTerm(*data_, apply_weight_squared);
108 }
109
110 // If part of simultaneous PDF normalize probability over
111 // number of simultaneous PDFs: -sum(log(p/n)) = -sum(log(p)) + N*log(n)
112 if (sim_count_ > 1) {
113 result += sumWeight * log(1.0 * sim_count_);
114 }
115
116 // At the end of the first full calculation, wire the caches
117 if (_first) {
118 _first = false;
119 pdf_->wireAllCaches();
120 }
121
123 lastSection_ = events;
124 return result;
125}
126
127} // namespace TestStatistics
128} // namespace RooFit
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 char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
T Sum() const
Definition: Util.h:240
T Carry() const
Definition: Util.h:250
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
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:61
std::shared_ptr< RooAbsData > data_
Definition: RooAbsL.h:135
std::unique_ptr< RooArgSet > normSet_
Pointer to set with observables used for normalization.
Definition: RooAbsL.h:136
std::shared_ptr< RooAbsPdf > pdf_
Definition: RooAbsL.h:134
ROOT::Math::KahanSum< double > cachedResult_
Definition: RooUnbinnedL.h:53
bool setApplyWeightSquared(bool flag)
Returns true if value was changed, false otherwise.
ROOT::Math::KahanSum< double > evaluatePartition(Section events, std::size_t components_begin, std::size_t components_end) override
Calculate and return likelihood on subset of data from firstEvent to lastEvent processed with a step ...
std::unique_ptr< RooChangeTracker > paramTracker_
Definition: RooUnbinnedL.h:51
bool apply_weight_squared
Apply weights squared?
Definition: RooUnbinnedL.h:48
RooUnbinnedL(RooAbsPdf *pdf, RooAbsData *data, RooAbsL::Extended extended=RooAbsL::Extended::Auto, bool useBatchedEvaluations=false)
static RooNLLVar::ComputeResult computeScalarFunc(const RooAbsPdf *pdfClone, RooAbsData *dataClone, RooArgSet *normSet, bool weightSq, std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent, bool doBinOffset=false)
Definition: RooNLLVar.cxx:473
static RooNLLVar::ComputeResult computeBatchedFunc(const RooAbsPdf *pdfClone, RooAbsData *dataClone, std::unique_ptr< RooBatchCompute::RunContext > &evalData, RooArgSet *normSet, bool weightSq, std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent)
Definition: RooNLLVar.cxx:365
RVec< PromoteType< T > > log(const RVec< T > &v)
Definition: RVec.hxx:1787
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: Common.h:18
Convenience wrapper class used to distinguish between pdf/data owning and non-owning constructors.
Definition: RooAbsL.h:39
A part of some range delimited by two fractional points between 0 and 1 (inclusive).
Definition: RooAbsL.h:58
std::size_t begin(std::size_t N_total) const
Definition: RooAbsL.h:66
std::size_t end(std::size_t N_total) const
Definition: RooAbsL.h:68