Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
xRooNLLVar.h
Go to the documentation of this file.
1/*
2 * Project: xRooFit
3 * Author:
4 * Will Buttinger, RAL 2022
5 *
6 * Copyright (c) 2022, 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#include "Config.h"
14
15#ifdef XROOFIT_USE_PRAGMA_ONCE
16#pragma once
17#endif
18#if !defined(XROOFIT_XROONLLVAR_H) || defined(XROOFIT_USE_PRAGMA_ONCE)
19#ifndef XROOFIT_USE_PRAGMA_ONCE
20#define XROOFIT_XROONLLVAR_H
21#endif
22
23#include "xRooFit.h"
24
25#include <RooFitResult.h>
26#include <RooLinkedList.h>
27
28#include <Fit/FitConfig.h>
29#include <Math/IOptions.h>
30#include <TAttFill.h>
31#include <TAttLine.h>
32#include <TAttMarker.h>
33
34#include <map>
35#include <set>
36
37class RooAbsReal;
38class RooAbsPdf;
39class RooAbsData;
41class RooNLLVar;
43class RooRealVar;
44class RooCmdArg;
45
46class TGraph;
47class TGraphErrors;
48
49namespace RooStats {
50class HypoTestResult;
51class HypoTestInverterResult;
52} // namespace RooStats
53
55
56class xRooNode;
57
58class xRooNLLVar : public std::shared_ptr<RooAbsReal> {
59
60public:
61 void Print(Option_t *opt = "");
62
63 xRooNLLVar(RooAbsPdf &pdf, const std::pair<RooAbsData *, const RooAbsCollection *> &data,
64 const RooLinkedList &nllOpts = RooLinkedList());
65 xRooNLLVar(const std::shared_ptr<RooAbsPdf> &pdf, const std::shared_ptr<RooAbsData> &data,
66 const RooLinkedList &opts = RooLinkedList());
67 xRooNLLVar(const std::shared_ptr<RooAbsPdf> &pdf,
68 const std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>> &data,
69 const RooLinkedList &opts = RooLinkedList());
70
72
73 // whenever implicitly converted to a RooAbsReal we will make sure our globs are set
74 RooAbsReal *get() const { return func().get(); }
75 RooAbsReal *operator->() const { return get(); }
76
77 void reinitialize();
78
79 void AddOption(const RooCmdArg &opt);
80
81 std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>>
82 getData() const; // returns pointer to data and snapshot of globs
83 std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>>
84 generate(bool expected = false, int seed = 0);
85 // std::shared_ptr<const RooFitResult> snapshot();
86
87 class xRooFitResult : public std::shared_ptr<const RooFitResult> {
88 public:
89 xRooFitResult(const std::shared_ptr<xRooNode> &in); // : fNode(in) { }
90 const RooFitResult *operator->() const;
91 // operator std::shared_ptr<const RooFitResult>() const;
92 operator const RooFitResult *() const;
93 void Draw(Option_t *opt = "");
94 std::shared_ptr<xRooNode> fNode;
95 };
96
97 xRooFitResult minimize(const std::shared_ptr<ROOT::Fit::FitConfig> & = nullptr);
98
99 void SetFitConfig(const std::shared_ptr<ROOT::Fit::FitConfig> &in) { fFitConfig = in; }
100 std::shared_ptr<ROOT::Fit::FitConfig> fitConfig(); // returns fit config, or creates a default one if not existing
101 ROOT::Math::IOptions *fitConfigOptions(); // return pointer to non-const version of the options inside the fit config
102
104 public:
105 static std::set<int> allowedStatusCodes;
106 void Print();
107 void Draw(Option_t *opt = "");
108
109 // status bitmask of the available fit results
110 // 0 = all ok
111 int status() const;
112
113 std::pair<double, double> pll(bool readOnly = false); // observed test statistic value
114 std::pair<double, double> sigma_mu(bool readOnly = false); // estimate of sigma_mu parameter
115 std::shared_ptr<const RooFitResult> ufit(bool readOnly = false);
116 std::shared_ptr<const RooFitResult> cfit_null(bool readOnly = false);
117 std::shared_ptr<const RooFitResult> cfit_alt(bool readOnly = false);
118
119 std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>> data;
120
121 std::pair<double, double> getVal(const char *what);
122
123 // leave nSigma=NaN for observed p-value
124 std::pair<double, double> pNull_asymp(double nSigma = std::numeric_limits<double>::quiet_NaN());
125 std::pair<double, double> pAlt_asymp(double nSigma = std::numeric_limits<double>::quiet_NaN());
126 std::pair<double, double> pCLs_asymp(double nSigma = std::numeric_limits<double>::quiet_NaN());
127 std::pair<double, double>
128 ts_asymp(double nSigma = std::numeric_limits<double>::quiet_NaN()); // test statistic value
129
130 std::pair<double, double> pNull_toys(double nSigma = std::numeric_limits<double>::quiet_NaN());
131 std::pair<double, double> pAlt_toys(double nSigma = std::numeric_limits<double>::quiet_NaN());
132 std::pair<double, double> pCLs_toys(double nSigma = std::numeric_limits<double>::quiet_NaN())
133 {
134 if (fNullVal() == fAltVal())
135 return std::pair(1, 0); // by construction
136 auto null = pNull_toys(nSigma);
137 auto alt = pAlt_toys(nSigma);
138 double pval = (null.first == 0) ? 0 : null.first / alt.first;
139 // TODO: should do error calculation like for asymp (calulate up and down separately and then take err)
140 return std::make_pair(pval, pval * sqrt(pow(null.second / null.first, 2) + pow(alt.second / alt.first, 2)));
141 }
142 std::pair<double, double>
143 ts_toys(double nSigma = std::numeric_limits<double>::quiet_NaN()); // test statistic value
144
145 // Create a HypoTestResult representing the current state of this hypoPoint
147
148 xRooHypoPoint generateNull(int seed = 0);
149 xRooHypoPoint generateAlt(int seed = 0);
150
151 void addNullToys(int nToys = 1, int seed = 0); // if seed=0 will use a random seed
152 void addAltToys(int nToys = 1, int seed = 0); // if seed=0 will use a random seed
153
154 RooArgList poi();
155 RooArgList alt_poi(); // values of the poi in the alt hypothesis (will be nans if not defined)
156 RooRealVar &mu_hat(); // throws exception if ufit not available
157
158 std::shared_ptr<xRooHypoPoint>
159 asimov(bool readOnly =
160 false); // a two-sided hypoPoint with the alt hypothesis asimov dataset (used in sigma_mu() calculation)
161
162 // std::string fPOIName;
163 const char *fPOIName();
164 xRooFit::Asymptotics::PLLType fPllType = xRooFit::Asymptotics::Unknown;
165 // double fNullVal=1; double fAltVal=0;
166 double fNullVal();
167 double fAltVal();
168
169 std::shared_ptr<const RooAbsCollection> coords; // pars of the nll that will be held const alongside POI
170
171 std::shared_ptr<const RooFitResult> fUfit, fNull_cfit, fAlt_cfit;
172 std::shared_ptr<const RooFitResult> fGenFit; // if the data was generated, this is the fit is was generated from
173 bool isExpected = false; // if genFit, flag says is asimov or not
174
175 std::shared_ptr<xRooHypoPoint>
176 fAsimov; // same as this point but pllType is twosided and data is expected post alt-fit
177
178 // first is seed, second is ts value, third is weight
179 std::vector<std::tuple<int, double, double>> nullToys; // would have to save these vectors for specific: null_cfit
180 // (genPoint), ufit, poiName, pllType, nullVal
181 std::vector<std::tuple<int, double, double>> altToys;
182
183 std::shared_ptr<xRooNLLVar> nllVar = nullptr; // hypopoints get a copy
184
185 private:
186 std::pair<double, double> pX_toys(bool alt, double nSigma = std::numeric_limits<double>::quiet_NaN());
187 void addToys(bool alt, int nToys, int initialSeed = 0);
188
189 TString tsTitle();
190 };
191
192 // use alt_value = nan to skip the asimov calculations
193 xRooHypoPoint hypoPoint(const char *parName, double value,
194 double alt_value = std::numeric_limits<double>::quiet_NaN(),
195 const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown);
196 // this next method requires poi to be flagged in the model already (with "poi" attribute) .. must be exactly one
197 xRooHypoPoint hypoPoint(double value, double alt_value = std::numeric_limits<double>::quiet_NaN(),
198 const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown);
199
200 class xRooHypoSpace : public TNamed,
201 public TAttFill,
202 public TAttMarker,
203 public TAttLine,
204 public std::vector<xRooHypoPoint> {
205 public:
206 friend class xRooNLLVar;
207 xRooHypoSpace(const char *name = "", const char *title = "");
208
209 bool AddWorkspace(const char *wsFilename, const char *extraPars = "");
210
211 bool AddModel(const xRooNode &pdf, const char *validity = "");
212
213 void LoadFits(const char *apath);
214
215 // A points over given parameter, number of points between low and high
216 int AddPoints(const char *parName, size_t nPoints, double low, double high);
217
218 void Print(Option_t *opt = "") const override;
219
220 void Draw(Option_t *opt = "") override;
221
222 RooArgList poi();
223 std::shared_ptr<RooArgSet> pars() const { return fPars; };
224 RooArgList axes() const;
225
226 xRooHypoPoint &AddPoint(const char *coords = ""); // adds a new point at given coords or returns existing
227
228 xRooHypoPoint &point(size_t i) { return at(i); }
229
230 // build a TGraphErrors of pValues over the existing points
231 // opt should include any of the following:
232 // cls: do pCLs, otherwise do pNull
233 // expX: do expected, X sigma (use +X or -X for contour, otherwise will return band unless X=0)
234 // toys: pvalues from available toys
235 // readonly: don't compute anything, just return available values
236 std::shared_ptr<TGraphErrors> BuildGraph(const char *opt);
237
238 // estimates where corresponding pValues graph becomes equal to 0.05
239 // linearly interpolates log(pVal) when obtaining limits.
240 // returns value and error
241 static std::pair<double, double> GetLimit(const TGraph &pValues, double target = 0.05);
242
243 // will evaluate more points until limit is below given relative uncert
244
245 std::pair<double, double> FindLimit(const char *opt, double relUncert = std::numeric_limits<double>::infinity());
246
247 // key is nSigma or "obs" for observed
248 std::map<std::string, std::pair<double, double>> limits(const char *opt = "cls", double relUncert = 0.1);
249
250 std::shared_ptr<xRooNode> pdf(const RooAbsCollection &parValues) const;
251 std::shared_ptr<xRooNode> pdf(const char *parValues = "") const;
252
253 // caller needs to take ownership of the returned object
255
256 private:
257 static RooArgList toArgs(const char *str);
258
259 xRooFit::Asymptotics::PLLType fTestStatType = xRooFit::Asymptotics::Unknown;
260 std::shared_ptr<RooArgSet> fPars;
261
262 std::map<std::shared_ptr<xRooNode>, std::shared_ptr<xRooNLLVar>> fNlls; // existing NLL functions of added pdfs;
263
264 std::set<std::shared_ptr<xRooNode>> fWorkspaces; // added workspaces (kept open)
265
266 std::set<std::pair<std::shared_ptr<RooArgList>, std::shared_ptr<xRooNode>>> fPdfs;
267 };
268
269 xRooHypoSpace hypoSpace(const char *parName, int nPoints, double low, double high,
270 double alt_value = std::numeric_limits<double>::quiet_NaN(),
271 const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown);
273 hypoSpace(const char *parName = "", const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown);
274 xRooHypoSpace hypoSpace(int nPoints, double low, double high,
275 double alt_value = std::numeric_limits<double>::quiet_NaN(),
276 const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown);
277
278 std::shared_ptr<RooArgSet> pars(bool stripGlobalObs = true);
279
280 void Draw(Option_t *opt = "");
281
282 std::shared_ptr<RooAbsReal> func() const; // will assign globs when called
283 std::shared_ptr<RooAbsPdf> pdf() const { return fPdf; }
284 RooAbsData *data() const; // returns the data hidden inside the NLLVar if there is some
285
286 // NLL = nllTerm + constraintTerm
287 // nllTerm = sum( entryVals ) + extendedTerm + simTerm [+ binnedDataTerm if activated binnedL option]
288 // this is what it should be, at least
289
290 // total nll should be all these values + constraint term + extended term + simTerm [+binnedDataTerm if activated
291 // binnedL option]
292 RooNLLVar *nllTerm() const;
293 RooConstraintSum *constraintTerm() const;
294
295 double getEntryVal(size_t entry); // get the Nll value for a specific entry
296 double extendedTerm() const;
297 double simTerm() const;
298 double binnedDataTerm() const;
299
300 // change the dataset - will check globs are the same
301 Bool_t setData(const std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>> &_data);
302 Bool_t setData(const std::shared_ptr<RooAbsData> &data, const std::shared_ptr<const RooAbsCollection> &globs)
303 {
304 return setData(std::make_pair(data, globs));
305 }
306 Bool_t setData(const xRooNode &data);
307
308 // using shared ptrs everywhere, even for RooLinkedList which needs custom deleter to clear itself
309 // but still work ok for assignment operations
310 std::shared_ptr<RooAbsPdf> fPdf;
311 std::shared_ptr<RooAbsData> fData;
312 std::shared_ptr<const RooAbsCollection> fGlobs;
313
314 std::shared_ptr<RooLinkedList> fOpts;
315 std::shared_ptr<ROOT::Fit::FitConfig> fFitConfig;
316
317 std::shared_ptr<RooAbsCollection> fFuncVars;
318 std::shared_ptr<RooAbsCollection> fConstVars;
319 std::shared_ptr<RooAbsCollection> fFuncGlobs;
320 std::string fFuncCreationLog; // messaging from when function was last created -- to save from printing to screen
321
322 bool kReuseNLL = true;
323};
324
326
327#endif // include guard
const char Option_t
Definition RtypesCore.h:66
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 Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
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 value
char name[80]
Definition TGX11.cxx:110
Generic interface for defining configuration options of a numerical algorithm.
Definition IOptions.h:31
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:59
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:62
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooCmdArg is a named container for two doubles, two integers two object points and three string point...
Definition RooCmdArg.h:26
RooConstraintSum calculates the sum of the -(log) likelihoods of a set of RooAbsPfs that represent co...
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
std::pair< double, double > pCLs_toys(double nSigma=std::numeric_limits< double >::quiet_NaN())
Definition xRooNLLVar.h:132
std::shared_ptr< const RooAbsCollection > coords
Definition xRooNLLVar.h:169
std::shared_ptr< const RooFitResult > fAlt_cfit
Definition xRooNLLVar.h:171
std::pair< std::shared_ptr< RooAbsData >, std::shared_ptr< const RooAbsCollection > > data
Definition xRooNLLVar.h:119
std::vector< std::tuple< int, double, double > > altToys
Definition xRooNLLVar.h:181
std::shared_ptr< const RooFitResult > fGenFit
Definition xRooNLLVar.h:172
std::vector< std::tuple< int, double, double > > nullToys
Definition xRooNLLVar.h:179
std::shared_ptr< xRooHypoPoint > fAsimov
Definition xRooNLLVar.h:176
std::shared_ptr< RooArgSet > pars() const
Definition xRooNLLVar.h:223
std::set< std::pair< std::shared_ptr< RooArgList >, std::shared_ptr< xRooNode > > > fPdfs
Definition xRooNLLVar.h:266
std::set< std::shared_ptr< xRooNode > > fWorkspaces
Definition xRooNLLVar.h:264
bool AddWorkspace(const char *wsFilename, const char *extraPars="")
std::map< std::shared_ptr< xRooNode >, std::shared_ptr< xRooNLLVar > > fNlls
Definition xRooNLLVar.h:262
std::shared_ptr< RooAbsCollection > fFuncGlobs
Definition xRooNLLVar.h:319
std::shared_ptr< RooAbsCollection > fFuncVars
Definition xRooNLLVar.h:317
std::shared_ptr< RooAbsPdf > fPdf
Definition xRooNLLVar.h:310
std::shared_ptr< RooAbsCollection > fConstVars
Definition xRooNLLVar.h:318
std::shared_ptr< const RooAbsCollection > fGlobs
Definition xRooNLLVar.h:312
std::shared_ptr< RooAbsData > fData
Definition xRooNLLVar.h:311
std::shared_ptr< RooAbsPdf > pdf() const
Definition xRooNLLVar.h:283
std::shared_ptr< RooLinkedList > fOpts
Definition xRooNLLVar.h:314
RooAbsReal * operator->() const
Definition xRooNLLVar.h:75
Bool_t setData(const std::shared_ptr< RooAbsData > &data, const std::shared_ptr< const RooAbsCollection > &globs)
Definition xRooNLLVar.h:302
void SetFitConfig(const std::shared_ptr< ROOT::Fit::FitConfig > &in)
Definition xRooNLLVar.h:99
std::shared_ptr< ROOT::Fit::FitConfig > fFitConfig
Definition xRooNLLVar.h:315
RooLinkedList is an collection class for internal use, storing a collection of RooAbsArg pointers in ...
Class RooNLLVar implements a -log(likelihood) calculation from a dataset and a PDF.
Definition RooNLLVar.h:30
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:40
HypoTestInverterResult class holds the array of hypothesis test results and compute a confidence inte...
HypoTestResult is a base class for results from hypothesis tests.
Fill Area Attributes class.
Definition TAttFill.h:19
Line Attributes class.
Definition TAttLine.h:18
Marker Attributes class.
Definition TAttMarker.h:19
A TGraphErrors is a TGraph with error bars.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
Basic string class.
Definition TString.h:139
Namespace for the RooStats classes.
Definition Asimov.h:19
#define BEGIN_XROOFIT_NAMESPACE
Definition Config.h:24
#define END_XROOFIT_NAMESPACE
Definition Config.h:25
static const char * what
Definition stlLoader.cc:6
th1 Draw()