Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
xRooFit.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#include "Config.h"
13
14// when not using the namespace will use the once pragma.
15// when using the namespace (as happens in the ROOT build of xRooFit) then
16// will effectively use an include guard
17#ifdef XROOFIT_USE_PRAGMA_ONCE
18#pragma once
19#endif
20#if (!defined(XROOFIT_USE_PRAGMA_ONCE) && !defined(XROOFIT_XROOFIT_H)) || \
21 (defined(XROOFIT_USE_PRAGMA_ONCE) && !defined(XROOFIT_XROOFIT_H_XROOFIT))
22#ifndef XROOFIT_USE_PRAGMA_ONCE
23#define XROOFIT_XROOFIT_H
24#else
25#define XROOFIT_XROOFIT_H_XROOFIT
26// even with using pragma once, need include guard otherwise cannot include this header
27// as part of an interpreted file ... the other headers in xRooFit are similarly affected
28// however for now users of xRooFit should only need to include the main xRooFit header to use it all
29// in future we should try removing the pragma once altogether (undef XROOFIT_USE_PRAGMA_ONCE)
30// and see if it has negative consequences anywhere
31#endif
32
33/**
34 * This is the main include for the xRooFit project.
35 * Including this should give you access to all xRooFit features
36 */
37
38class RooAbsData;
40class RooFitResult;
41class RooAbsPdf;
42class RooAbsReal;
43class RooLinkedList;
44class RooWorkspace;
45
46#include "Fit/FitConfig.h"
47
48#include "RooCmdArg.h"
49#include "TNamed.h"
50
51class TCanvas;
52
53#include <memory>
54
56
57class xRooNLLVar;
58
59class xRooFit {
60
61public:
62 static const char *GetVersion();
63 static const char *GetVersionDate();
64
65 // Extra options for NLL creation:
66 static RooCmdArg ReuseNLL(bool flag); // if should try to reuse the NLL object when it changes dataset
67 static RooCmdArg Tolerance(double value);
68 static RooCmdArg StrategySequence(const char *stratSeq); // control minimization strategy sequence
69 static RooCmdArg MaxIterations(int nIterations);
70
71 static constexpr double OBS = std::numeric_limits<double>::quiet_NaN();
72
73 // Helper function for matching precision of a value and its error
74 static std::pair<double, double> matchPrecision(const std::pair<double, double> &in);
75
76 // Static methods that work with the 'first class' object types:
77 // Pdfs: RooAbsPdf
78 // Datasets: std::pair<RooAbsData,const RooAbsCollection>
79 // NLLOptions: RooLinkedList
80 // FitOptions: ROOT::Fit::FitConfig
81
82 // fit result flags in its constPars list which are global observables with the "global" attribute
83 static std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>>
84 generateFrom(RooAbsPdf &pdf, const RooFitResult &fr, bool expected = false, int seed = 0);
85 static std::shared_ptr<const RooFitResult>
86 fitTo(RooAbsPdf &pdf, const std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>> &data,
87 const RooLinkedList &nllOpts, const ROOT::Fit::FitConfig &fitConf);
88 static std::shared_ptr<const RooFitResult> fitTo(RooAbsPdf &pdf,
89 const std::pair<RooAbsData *, const RooAbsCollection *> &data,
90 const RooLinkedList &nllOpts, const ROOT::Fit::FitConfig &fitConf);
91
92 static xRooNLLVar createNLL(const std::shared_ptr<RooAbsPdf> pdf, const std::shared_ptr<RooAbsData> data,
93 const RooLinkedList &nllOpts);
94 static xRooNLLVar createNLL(RooAbsPdf &pdf, RooAbsData *data, const RooLinkedList &nllOpts);
95 static xRooNLLVar createNLL(RooAbsPdf &pdf, RooAbsData *data, const RooCmdArg &arg1 = RooCmdArg::none(),
96 const RooCmdArg &arg2 = RooCmdArg::none(), const RooCmdArg &arg3 = RooCmdArg::none(),
97 const RooCmdArg &arg4 = RooCmdArg::none(), const RooCmdArg &arg5 = RooCmdArg::none(),
98 const RooCmdArg &arg6 = RooCmdArg::none(), const RooCmdArg &arg7 = RooCmdArg::none(),
99 const RooCmdArg &arg8 = RooCmdArg::none());
100
101 static std::shared_ptr<ROOT::Fit::FitConfig> createFitConfig(); // obtain instance of default fit configuration
102 static std::shared_ptr<RooLinkedList> createNLLOptions(); // obtain instance of default nll options
103 static std::shared_ptr<RooLinkedList> defaultNLLOptions(); // access default NLL options for modifications
104 static std::shared_ptr<ROOT::Fit::FitConfig> defaultFitConfig();
105 static ROOT::Math::IOptions *defaultFitConfigOptions();
106
107 static std::shared_ptr<const RooFitResult> minimize(RooAbsReal &nll,
108 const std::shared_ptr<ROOT::Fit::FitConfig> &fitConfig = nullptr,
109 const std::shared_ptr<RooLinkedList> &nllOpts = nullptr);
110 static int minos(RooAbsReal &nll, const RooFitResult &ufit, const char *parName = "",
111 const std::shared_ptr<ROOT::Fit::FitConfig> &_fitConfig = nullptr);
112
113 // this class is used to store a shared_ptr in a TDirectory's List, so that retrieval of cached fits
114 // can share the fit result (and avoid re-reading from disk as well)
115 class StoredFitResult : public TNamed {
116 public:
118 StoredFitResult(const std::shared_ptr<RooFitResult> &_fr);
119
120 public:
121 std::shared_ptr<RooFitResult> fr; //!
123 };
124
125 // can't use an enum class because pyROOT doesn't handle it
127 public:
128 enum Type {
129 // basic likelihood ratio
130 tmu = -1,
131 // upper limit test statistics
132 qmu = -2,
133 qmutilde = -3,
134 // discovery test statistics
135 q0 = -4,
136 uncappedq0 = -5,
137 u0 = -5
138 };
139 };
140
142
143 public:
144 typedef std::vector<std::pair<double, int>> IncompatFunc;
145
146 enum PLLType {
147 TwoSided = 0,
148 OneSidedPositive, // for exclusions
149 OneSidedNegative, // for discovery
150 OneSidedAbsolute, // for exclusions by magnitude
151 Uncapped, // for discovery with interest in deficits as well as excesses
152 Unknown
153 };
154
155 // The incompatibility function (taking mu_hat as an input) is defined by its transitions
156 // it takes values of -1, 0, or 1 ... when it 0 that means mu_hat is compatible with the hypothesis
157 // Standard incompatibility functions are parameterized by mu
158 // Note: the default value is taken to be 1, so an empty vector is function=1
160 {
161 std::vector<std::pair<double, int>> out;
162 if (type == TwoSided) {
163 // standard PLL
164 } else if (type == OneSidedPositive) {
165 out.emplace_back(std::make_pair(mu, 0)); // becomes compatible @ mu_hat = mu
166 } else if (type == OneSidedNegative) {
167 out.emplace_back(std::make_pair(-std::numeric_limits<double>::infinity(), 0)); // compatible at -inf
168 out.emplace_back(std::make_pair(mu, 1)); // becomes incompatible at mu_hat = mu
169 } else if (type == OneSidedAbsolute) {
170 out.emplace_back(std::make_pair(-std::numeric_limits<double>::infinity(), 0)); // compatible at -inf
171 out.emplace_back(std::make_pair(-mu, 1)); // incompatible @ mu_hat = -mu
172 out.emplace_back(std::make_pair(mu, 0)); // compatible again @ mu_hat = mu
173 } else if (type == Uncapped) {
174 out.emplace_back(std::make_pair(-std::numeric_limits<double>::infinity(), -1)); // reversed at -inf
175 out.emplace_back(std::make_pair(mu, 1)); // becomes normal @ mu_hat = mu
176 } else {
177 throw std::runtime_error("Unknown PLL Type");
178 }
179 return out;
180 }
181
182 // inverse of PValue function
183 static double k(const IncompatFunc &compatRegions, double pValue, double poiVal, double poiPrimeVal,
184 double sigma_mu = 0, double mu_low = -std::numeric_limits<double>::infinity(),
185 double mu_high = std::numeric_limits<double>::infinity());
186
187 static double k(const PLLType &pllType, double pValue, double mu, double mu_prime, double sigma_mu = 0,
188 double mu_low = -std::numeric_limits<double>::infinity(),
189 double mu_high = std::numeric_limits<double>::infinity())
190 {
191 return k(IncompatibilityFunction(pllType, mu), pValue, mu, mu_prime, sigma_mu, mu_low, mu_high);
192 }
193
194 // Recommend sigma_mu = |mu - mu_prime|/sqrt(pll_mu(asimov_mu_prime))
195 static double PValue(const IncompatFunc &compatRegions, double k, double mu, double mu_prime, double sigma_mu = 0,
196 double mu_low = -std::numeric_limits<double>::infinity(),
197 double mu_high = std::numeric_limits<double>::infinity());
198
199 static double PValue(const PLLType &pllType, double k, double mu, double mu_prime, double sigma_mu = 0,
200 double mu_low = -std::numeric_limits<double>::infinity(),
201 double mu_high = std::numeric_limits<double>::infinity())
202 {
203 return PValue(IncompatibilityFunction(pllType, mu), k, mu, mu_prime, sigma_mu, mu_low, mu_high);
204 }
205
206 static double Phi_m(double mu, double mu_prime, double a, double sigma, const IncompatFunc &compatRegions);
207
208 static int CompatFactor(const IncompatFunc &func, double mu_hat);
209
210 static int CompatFactor(int type, double mu, double mu_hat)
211 {
212 return CompatFactor(IncompatibilityFunction((PLLType)type, mu), mu_hat);
213 }
214
215 // converts pvalues to significances and finds where they equal the target pvalue
216 // return is x-axis value with potentially an error on that value if input pVals had errors
217 // static RooRealVar FindLimit(TGraph *pVals, double target_pVal = 0.05);
218 };
219
220 static std::shared_ptr<RooLinkedList> sDefaultNLLOptions;
221 static std::shared_ptr<ROOT::Fit::FitConfig> sDefaultFitConfig;
222
223 // Run hypothesis test(s) on the given pdf
224 // Uses hypoPoint binning on model parameters to determine points to scan
225 // if hypoPoint binning has nBins==0 then will auto-scan (assumes CL=95%, can override with setStringAttribute)
226 // TODO: specifying number of null and alt toys per point
227 static TCanvas *
228 hypoTest(RooWorkspace &w, const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown)
229 {
230 return hypoTest(w, 0, 0, pllType);
231 }
232 static TCanvas *hypoTest(RooWorkspace &w, int nToysNull, int nToysAlt,
233 const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown);
234};
235
237
238#include "xRooHypoSpace.h"
239#include "xRooNLLVar.h"
240#include "xRooNode.h"
241
242#endif // include guard
#define a(i)
Definition RSha256.hxx:99
#define ClassDef(name, id)
Definition Rtypes.h:342
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
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 Atom_t Time_t type
static double k(const IncompatFunc &compatRegions, double pValue, double poiVal, double poiPrimeVal, double sigma_mu=0, double mu_low=-std::numeric_limits< double >::infinity(), double mu_high=std::numeric_limits< double >::infinity())
static double PValue(const PLLType &pllType, double k, double mu, double mu_prime, double sigma_mu=0, double mu_low=-std::numeric_limits< double >::infinity(), double mu_high=std::numeric_limits< double >::infinity())
Definition xRooFit.h:199
static int CompatFactor(int type, double mu, double mu_hat)
Definition xRooFit.h:210
static IncompatFunc IncompatibilityFunction(const PLLType &type, double mu)
Definition xRooFit.h:159
std::vector< std::pair< double, int > > IncompatFunc
Definition xRooFit.h:144
static double k(const PLLType &pllType, double pValue, double mu, double mu_prime, double sigma_mu=0, double mu_low=-std::numeric_limits< double >::infinity(), double mu_high=std::numeric_limits< double >::infinity())
Definition xRooFit.h:187
static double PValue(const IncompatFunc &compatRegions, double k, double mu, double mu_prime, double sigma_mu=0, double mu_low=-std::numeric_limits< double >::infinity(), double mu_high=std::numeric_limits< double >::infinity())
static TCanvas * hypoTest(RooWorkspace &w, const xRooFit::Asymptotics::PLLType &pllType=xRooFit::Asymptotics::Unknown)
Definition xRooFit.h:228
static std::shared_ptr< RooLinkedList > sDefaultNLLOptions
Definition xRooFit.h:220
static std::shared_ptr< ROOT::Fit::FitConfig > sDefaultFitConfig
Definition xRooFit.h:221
static TCanvas * hypoTest(RooWorkspace &w, int nToysNull, int nToysAlt, const xRooFit::Asymptotics::PLLType &pllType=xRooFit::Asymptotics::Unknown)
This xRooNLLVar object has several special methods, e.g.
Definition xRooNLLVar.h:58
Class describing the configuration of the fit, options and parameter settings using the ROOT::Fit::Pa...
Definition FitConfig.h:47
Generic interface for defining configuration options of a numerical algorithm.
Definition IOptions.h:28
Abstract container object that can hold multiple RooAbsArg objects.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
Named container for two doubles, two integers two object points and three string pointers that can be...
Definition RooCmdArg.h:26
static const RooCmdArg & none()
Return reference to null argument.
Definition RooCmdArg.cxx:52
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Collection class for internal use, storing a collection of RooAbsArg pointers in a doubly linked list...
Persistable container for RooFit projects.
The Canvas class.
Definition TCanvas.h:23
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const Double_t sigma
#define BEGIN_XROOFIT_NAMESPACE
Definition Config.h:24
#define END_XROOFIT_NAMESPACE
Definition Config.h:25