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 // Extra options for NLL creation:
63 static RooCmdArg ReuseNLL(bool flag); // if should try to reuse the NLL object when it changes dataset
64 static RooCmdArg Tolerance(double value);
65 static RooCmdArg StrategySequence(const char *stratSeq); // control minimization strategy sequence
66 static constexpr double OBS = std::numeric_limits<double>::quiet_NaN();
67
68 // Helper function for matching precision of a value and its error
69 static std::pair<double, double> matchPrecision(const std::pair<double, double> &in);
70
71 // Static methods that work with the 'first class' object types:
72 // Pdfs: RooAbsPdf
73 // Datasets: std::pair<RooAbsData,const RooAbsCollection>
74 // NLLOptions: RooLinkedList
75 // FitOptions: ROOT::Fit::FitConfig
76
77 // fit result flags in its constPars list which are global observables with the "global" attribute
78 static std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>>
79 generateFrom(RooAbsPdf &pdf, const RooFitResult &fr, bool expected = false, int seed = 0);
80 static std::shared_ptr<const RooFitResult>
81 fitTo(RooAbsPdf &pdf, const std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>> &data,
82 const RooLinkedList &nllOpts, const ROOT::Fit::FitConfig &fitConf);
83 static std::shared_ptr<const RooFitResult> fitTo(RooAbsPdf &pdf,
84 const std::pair<RooAbsData *, const RooAbsCollection *> &data,
85 const RooLinkedList &nllOpts, const ROOT::Fit::FitConfig &fitConf);
86
87 static xRooNLLVar createNLL(const std::shared_ptr<RooAbsPdf> pdf, const std::shared_ptr<RooAbsData> data,
88 const RooLinkedList &nllOpts);
89 static xRooNLLVar createNLL(RooAbsPdf &pdf, RooAbsData *data, const RooLinkedList &nllOpts);
90 static xRooNLLVar createNLL(RooAbsPdf &pdf, RooAbsData *data, const RooCmdArg &arg1 = RooCmdArg::none(),
91 const RooCmdArg &arg2 = RooCmdArg::none(), const RooCmdArg &arg3 = RooCmdArg::none(),
92 const RooCmdArg &arg4 = RooCmdArg::none(), const RooCmdArg &arg5 = RooCmdArg::none(),
93 const RooCmdArg &arg6 = RooCmdArg::none(), const RooCmdArg &arg7 = RooCmdArg::none(),
94 const RooCmdArg &arg8 = RooCmdArg::none());
95
96 static std::shared_ptr<ROOT::Fit::FitConfig> createFitConfig(); // obtain instance of default fit configuration
97 static std::shared_ptr<RooLinkedList> createNLLOptions(); // obtain instance of default nll options
98 static std::shared_ptr<RooLinkedList> defaultNLLOptions(); // access default NLL options for modifications
99 static std::shared_ptr<ROOT::Fit::FitConfig> defaultFitConfig();
100
101 static std::shared_ptr<const RooFitResult> minimize(RooAbsReal &nll,
102 const std::shared_ptr<ROOT::Fit::FitConfig> &fitConfig = nullptr,
103 const std::shared_ptr<RooLinkedList> &nllOpts = nullptr);
104 static int minos(RooAbsReal &nll, const RooFitResult &ufit, const char *parName = "",
105 const std::shared_ptr<ROOT::Fit::FitConfig> &_fitConfig = nullptr);
106
107 // this class is used to store a shared_ptr in a TDirectory's List, so that retrieval of cached fits
108 // can share the fit result (and avoid re-reading from disk as well)
109 class StoredFitResult : public TNamed {
110 public:
112 StoredFitResult(const std::shared_ptr<RooFitResult> &_fr);
113
114 public:
115 std::shared_ptr<RooFitResult> fr; //!
117 };
118
119 // can't use an enum class because pyROOT doesn't handle it
121 public:
122 enum Type {
123 // basic likelihood ratio
124 tmu = -1,
125 // upper limit test statistics
126 qmu = -2,
127 qmutilde = -3,
128 // discovery test statistics
129 q0 = -4,
130 uncappedq0 = -5
131 };
132 };
133
135
136 public:
137 typedef std::vector<std::pair<double, int>> IncompatFunc;
138
139 enum PLLType {
140 TwoSided = 0,
141 OneSidedPositive, // for exclusions
142 OneSidedNegative, // for discovery
143 OneSidedAbsolute, // for exclusions by magnitude
144 Uncapped, // for discovery with interest in deficits as well as excesses
145 Unknown
146 };
147
148 // The incompatibility function (taking mu_hat as an input) is defined by its transitions
149 // it takes values of -1, 0, or 1 ... when it 0 that means mu_hat is compatible with the hypothesis
150 // Standard incompatibility functions are parameterized by mu
151 // Note: the default value is taken to be 1, so an empty vector is function=1
153 {
154 std::vector<std::pair<double, int>> out;
155 if (type == TwoSided) {
156 // standard PLL
157 } else if (type == OneSidedPositive) {
158 out.emplace_back(std::make_pair(mu, 0)); // becomes compatible @ mu_hat = mu
159 } else if (type == OneSidedNegative) {
160 out.emplace_back(std::make_pair(-std::numeric_limits<double>::infinity(), 0)); // compatible at -inf
161 out.emplace_back(std::make_pair(mu, 1)); // becomes incompatible at mu_hat = mu
162 } else if (type == OneSidedAbsolute) {
163 out.emplace_back(std::make_pair(-std::numeric_limits<double>::infinity(), 0)); // compatible at -inf
164 out.emplace_back(std::make_pair(-mu, 1)); // incompatible @ mu_hat = -mu
165 out.emplace_back(std::make_pair(mu, 0)); // compatible again @ mu_hat = mu
166 } else if (type == Uncapped) {
167 out.emplace_back(std::make_pair(-std::numeric_limits<double>::infinity(), -1)); // reversed at -inf
168 out.emplace_back(std::make_pair(mu, 1)); // becomes normal @ mu_hat = mu
169 } else {
170 throw std::runtime_error("Unknown PLL Type");
171 }
172 return out;
173 }
174
175 // inverse of PValue function
176 static double k(const IncompatFunc &compatRegions, double pValue, double poiVal, double poiPrimeVal,
177 double sigma_mu = 0, double mu_low = -std::numeric_limits<double>::infinity(),
178 double mu_high = std::numeric_limits<double>::infinity());
179
180 static double k(const PLLType &pllType, double pValue, double mu, double mu_prime, double sigma_mu = 0,
181 double mu_low = -std::numeric_limits<double>::infinity(),
182 double mu_high = std::numeric_limits<double>::infinity())
183 {
184 return k(IncompatibilityFunction(pllType, mu), pValue, mu, mu_prime, sigma_mu, mu_low, mu_high);
185 }
186
187 // Recommend sigma_mu = |mu - mu_prime|/sqrt(pll_mu(asimov_mu_prime))
188 static double PValue(const IncompatFunc &compatRegions, double k, double mu, double mu_prime, double sigma_mu = 0,
189 double mu_low = -std::numeric_limits<double>::infinity(),
190 double mu_high = std::numeric_limits<double>::infinity());
191
192 static double PValue(const PLLType &pllType, double k, double mu, double mu_prime, double sigma_mu = 0,
193 double mu_low = -std::numeric_limits<double>::infinity(),
194 double mu_high = std::numeric_limits<double>::infinity())
195 {
196 return PValue(IncompatibilityFunction(pllType, mu), k, mu, mu_prime, sigma_mu, mu_low, mu_high);
197 }
198
199 static double Phi_m(double mu, double mu_prime, double a, double sigma, const IncompatFunc &compatRegions);
200
201 static int CompatFactor(const IncompatFunc &func, double mu_hat);
202
203 static int CompatFactor(int type, double mu, double mu_hat)
204 {
205 return CompatFactor(IncompatibilityFunction((PLLType)type, mu), mu_hat);
206 }
207
208 // converts pvalues to significances and finds where they equal the target pvalue
209 // return is x-axis value with potentially an error on that value if input pVals had errors
210 // static RooRealVar FindLimit(TGraph *pVals, double target_pVal = 0.05);
211 };
212
213 static std::shared_ptr<RooLinkedList> sDefaultNLLOptions;
214 static std::shared_ptr<ROOT::Fit::FitConfig> sDefaultFitConfig;
215
216 // Run hypothesis test(s) on the given pdf
217 // Uses hypoPoint binning on model parameters to determine points to scan
218 // if hypoPoint binning has nBins==0 then will auto-scan (assumes CL=95%, can override with setStringAttribute)
219 // TODO: specifying number of null and alt toys per point
220 static TCanvas *
221 hypoTest(RooWorkspace &w, const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown)
222 {
223 return hypoTest(w, 0, 0, pllType);
224 }
225 static TCanvas *hypoTest(RooWorkspace &w, int nToysNull, int nToysAlt,
226 const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown);
227};
228
230
231#include "xRooHypoSpace.h"
232#include "xRooNLLVar.h"
233#include "xRooNode.h"
234
235#endif // include guard
#define a(i)
Definition RSha256.hxx:99
#define ClassDef(name, id)
Definition Rtypes.h:337
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:192
static int CompatFactor(int type, double mu, double mu_hat)
Definition xRooFit.h:203
static IncompatFunc IncompatibilityFunction(const PLLType &type, double mu)
Definition xRooFit.h:152
std::vector< std::pair< double, int > > IncompatFunc
Definition xRooFit.h:137
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:180
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 std::shared_ptr< RooLinkedList > sDefaultNLLOptions
Definition xRooFit.h:213
static TCanvas * hypoTest(RooWorkspace &w, const xRooFit::Asymptotics::PLLType &pllType=xRooFit::Asymptotics::Unknown)
Definition xRooFit.h:221
static std::shared_ptr< ROOT::Fit::FitConfig > sDefaultFitConfig
Definition xRooFit.h:214
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:59
Class describing the configuration of the fit, options and parameter settings using the ROOT::Fit::Pa...
Definition FitConfig.h:47
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:51
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
RooLinkedList is an collection class for internal use, storing a collection of RooAbsArg pointers in ...
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