Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooNLLVarNew.h
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*
4 * Project: RooFit
5 * Authors:
6 * Jonas Rembser, CERN 2021
7 * Emmanouil Michalainas, CERN 2021
8 *
9 * Copyright (c) 2021, CERN
10 *
11 * Redistribution and use in source and binary forms,
12 * with or without modification, are permitted according to the terms
13 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
14 */
15
16#ifndef RooFit_RooNLLVarNew_h
17#define RooFit_RooNLLVarNew_h
18
19#include <RooAbsPdf.h>
20#include <RooAbsReal.h>
21#include <RooDataHist.h>
22#include <RooGlobalFunc.h>
23#include <RooTemplateProxy.h>
24
25#include <Math/Util.h>
26
27namespace RooFit {
28namespace Detail {
29
30class RooNLLVarNew : public RooAbsReal {
31
32public:
33 // The names for the special variables that the RooNLLVarNew expects
34 static constexpr const char *weightVarName = "_weight";
35 static constexpr const char *weightVarNameSumW2 = "_weight_sumW2";
36 static constexpr const char *binVolumeVarName = "_bin_volume";
37 static constexpr const char *weightErrorLoVarName = "_weight_err_lo";
38 static constexpr const char *weightErrorHiVarName = "_weight_err_hi";
39
40 enum class Statistic {
41 NLL,
42 Chi2
43 };
44
45 /// Configuration struct for the unified constructor. Note that `offsetMode`
46 /// only applies to `Statistic::NLL`, and `chi2ErrorType` only applies to
47 /// `Statistic::Chi2`.
48 struct Config {
49 Statistic statistic = Statistic::NLL;
50 bool extended = false;
53 };
54
55 RooNLLVarNew(const char *name, const char *title, RooAbsReal &func, RooArgSet const &observables, Config const &cfg);
56 RooNLLVarNew(const RooNLLVarNew &other, const char *name = nullptr);
57 TObject *clone(const char *newname) const override { return new RooNLLVarNew(*this, newname); }
58
59 /// Return default level for MINUIT error analysis.
60 double defaultErrorLevel() const override { return _statistic == Statistic::Chi2 ? 1.0 : 0.5; }
61
62 void doEval(RooFit::EvalContext &) const override;
63 bool canComputeBatchWithCuda() const override { return _statistic == Statistic::NLL && !_binnedL; }
64 bool isReducerNode() const override { return true; }
65 bool canUseLogarithmOf(RooAbsArg const &server, bool outputsLogarithm) const override;
66
67 void setPrefix(std::string const &prefix);
68
69 void applyWeightSquared(bool flag) override;
70
71 void enableOffsetting(bool) override;
72
73 void enableBinOffsetting(bool on = true) { _doBinOffset = on; }
74
75 void setSimCount(int simCount)
76 {
77 _simCount = simCount;
78 _logSimCount = std::log(static_cast<double>(simCount));
79 }
80
81 enum class FuncMode {
82 Pdf,
83 ExtendedPdf,
84 Function
85 };
86
87 RooAbsReal const &func() const { return *_func; }
88 RooAbsReal const &weightVar() const { return *_weightVar; }
89 RooAbsReal const &weightSquaredVar() const { return *_weightSquaredVar; }
90 bool binnedL() const { return _binnedL; }
91 int simCount() const { return _simCount; }
92 Statistic statistic() const { return _statistic; }
93 FuncMode funcMode() const { return _funcMode; }
94 RooDataHist::ErrorType chi2ErrorType() const { return _chi2ErrorType; }
95 RooAbsReal const *expectedEvents() const { return _expectedEvents ? &**_expectedEvents : nullptr; }
96 RooAbsReal const *binVolumes() const { return _binVolumes ? &**_binVolumes : nullptr; }
97 RooAbsReal const *weightErrLo() const { return _weightErrLo ? &**_weightErrLo : nullptr; }
98 RooAbsReal const *weightErrHi() const { return _weightErrHi ? &**_weightErrHi : nullptr; }
99
100private:
101 double evaluate() const override { return _value; }
102 void resetWeightVarNames();
103 double sumOfWeights(RooFit::EvalContext &, std::span<const double> weights, bool squared) const;
104 void finalizeResult(RooFit::EvalContext &, ROOT::Math::KahanSum<double> result, double weightSum) const;
105 void fillBinWidthsFromPdfBoundaries(RooAbsReal const &pdf, RooArgSet const &observables);
106 void doEvalBinnedL(RooFit::EvalContext &, std::span<const double> preds, std::span<const double> weights) const;
107 void doEvalChi2(RooFit::EvalContext &, std::span<const double> preds, std::span<const double> weights,
108 std::span<const double> weightsSumW2) const;
109
112 RooTemplateProxy<RooAbsReal> _weightSquaredVar;
113 std::unique_ptr<RooTemplateProxy<RooAbsReal>> _expectedEvents;
114 std::unique_ptr<RooTemplateProxy<RooAbsPdf>> _offsetPdf;
115 std::unique_ptr<RooTemplateProxy<RooAbsReal>> _binVolumes;
116 std::unique_ptr<RooTemplateProxy<RooAbsReal>> _weightErrLo;
117 std::unique_ptr<RooTemplateProxy<RooAbsReal>> _weightErrHi;
118 bool _weightSquared = false;
119 bool _binnedL = false;
120 bool _doOffset = false;
121 bool _doBinOffset = false;
122 Statistic _statistic = Statistic::NLL;
123 FuncMode _funcMode = FuncMode::Pdf;
125 int _simCount = 1;
126 double _logSimCount = 0.;
127 std::string _prefix;
128 std::vector<double> _binw;
129 mutable ROOT::Math::KahanSum<double> _offset{0.}; ///<! Offset as KahanSum to avoid loss of precision
130 mutable std::size_t _sumWeightGen = 0; ///<! Input data generation of the cached weight sum
131 mutable double _sumWeightCache = 0.0; ///<! Cached sum of the event weights
132 mutable std::size_t _sumWeight2Gen = 0; ///<! Input data generation of the cached squared-weight sum
133 mutable double _sumWeight2Cache = 0.0; ///<! Cached sum of the squared event weights
134
135 ClassDefOverride(RooFit::Detail::RooNLLVarNew, 0);
136};
137
138} // namespace Detail
139} // namespace RooFit
140
141#endif
142
143/// \endcond
#define ClassDefOverride(name, id)
Definition Rtypes.h:347
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
char name[80]
Definition TGX11.cxx:142
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
Definition Util.h:141
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Mother of all ROOT objects.
Definition TObject.h:42
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
OffsetMode
For setting the offset mode with the Offset() command argument to RooAbsPdf::fitTo()
void evaluate(typename Architecture_t::Tensor_t &A, EActivationFunction f)
Apply the given activation function to each value in the given tensor A.
Definition Functions.h:98