Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ModelConfig.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Kyle Cranmer,
5 * Lorenzo Moneta,
6 * Gregory Schott,
7 * Wouter Verkerke,
8 * Sven Kreiss
9 *
10 * Copyright (c) 2023, CERN
11 *
12 * Redistribution and use in source and binary forms,
13 * with or without modification, are permitted according to the terms
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
15 */
16
17#ifndef RooFit_ModelConfig_h
18#define RooFit_ModelConfig_h
19
20#include <RooAbsData.h>
21#include <RooAbsPdf.h>
22#include <RooArgSet.h>
23#include <RooGlobalFunc.h>
24#include <RooWorkspaceHandle.h>
25
26#include <TRef.h>
27
28#include <string>
29
30class RooFitResult;
31
32// ModelConfig kept in the RooStats namespace for backwards compatibility.
33namespace RooStats {
34
35class ModelConfig final : public TNamed, public RooWorkspaceHandle {
36
37public:
39 {
40 if (ws)
41 SetWS(*ws);
42 }
43
44 ModelConfig(const char *name, RooWorkspace *ws = nullptr) : TNamed(name, name)
45 {
46 if (ws)
47 SetWS(*ws);
48 }
49
50 ModelConfig(const char *name, const char *title, RooWorkspace *ws = nullptr) : TNamed(name, title)
51 {
52 if (ws)
53 SetWS(*ws);
54 }
55
56 /// clone
57 ModelConfig *Clone(const char *name = "") const override
58 {
59 ModelConfig *mc = new ModelConfig(*this);
60 if (strcmp(name, "") == 0) {
61 mc->SetName(this->GetName());
62 } else {
63 mc->SetName(name);
64 }
65 return mc;
66 }
67
68 /// Set a workspace that owns all the necessary components for the analysis.
69 void SetWS(RooWorkspace &ws) override;
70 //// alias for SetWS(...)
71 virtual void SetWorkspace(RooWorkspace &ws) { SetWS(ws); }
72
73 /// Remove the existing reference to a workspace and replace it with this new one.
74 void ReplaceWS(RooWorkspace *ws) override
75 {
76 fRefWS = nullptr;
77 SetWS(*ws);
78 }
79
80 /// Set the proto DataSet, add to the workspace if not already there
82 {
84 SetProtoData(data.GetName());
85 }
86
87 /// Set the Pdf, add to the workspace if not already there
88 virtual void SetPdf(const RooAbsPdf &pdf)
89 {
90 ImportPdfInWS(pdf);
91 SetPdf(pdf.GetName());
92 }
93
94 /// Set the Prior Pdf, add to the workspace if not already there
95 virtual void SetPriorPdf(const RooAbsPdf &pdf)
96 {
97 ImportPdfInWS(pdf);
98 SetPriorPdf(pdf.GetName());
99 }
100
101 /// Specify parameters of the PDF.
102 virtual void SetParameters(const RooArgSet &set)
103 {
104 if (!SetHasOnlyParameters(set, "ModelConfig::SetParameters"))
105 return;
106 fPOIName = std::string(GetName()) + "_POI";
107 DefineSetInWS(fPOIName.c_str(), set);
108 }
109
110 /// Specify parameters of interest.
111 virtual void SetParametersOfInterest(const RooArgSet &set)
112 {
113 if (!SetHasOnlyParameters(set, "ModelConfig::SetParametersOfInterest"))
114 return;
115 SetParameters(set);
116 }
117
118 /// Specify parameters
119 /// using a list of comma-separated list of arguments already in the workspace.
120 virtual void SetParameters(const char *argList)
121 {
122 if (!GetWS())
123 return;
124 SetParameters(GetWS()->argSet(argList));
125 }
126
127 /// Specify parameters of interest
128 /// using a comma-separated list of arguments already in the workspace.
129 virtual void SetParametersOfInterest(const char *argList) { SetParameters(argList); }
130
131 /// Specify the nuisance parameters (parameters that are not POI).
132 virtual void SetNuisanceParameters(const RooArgSet &set)
133 {
134 if (!SetHasOnlyParameters(set, "ModelConfig::SetNuisanceParameters"))
135 return;
136 fNuisParamsName = std::string(GetName()) + "_NuisParams";
137 DefineSetInWS(fNuisParamsName.c_str(), set);
138 }
139
140 /// Specify the nuisance parameters
141 /// using a comma-separated list of arguments already in the workspace.
142 virtual void SetNuisanceParameters(const char *argList)
143 {
144 if (!GetWS())
145 return;
146 SetNuisanceParameters(GetWS()->argSet(argList));
147 }
148
149 /// Specify the constraint parameters
150 virtual void SetConstraintParameters(const RooArgSet &set)
151 {
152 if (!SetHasOnlyParameters(set, "ModelConfig::SetConstrainedParameters"))
153 return;
154 fConstrParamsName = std::string(GetName()) + "_ConstrainedParams";
155 DefineSetInWS(fConstrParamsName.c_str(), set);
156 }
157 /// Specify the constraint parameters
158 /// through a comma-separated list of arguments already in the workspace.
159 virtual void SetConstraintParameters(const char *argList)
160 {
161 if (!GetWS())
162 return;
163 SetConstraintParameters(GetWS()->argSet(argList));
164 }
165
166 /// Specify the observables.
167 virtual void SetObservables(const RooArgSet &set)
168 {
169 if (!SetHasOnlyParameters(set, "ModelConfig::SetObservables"))
170 return;
171 fObservablesName = std::string(GetName()) + "_Observables";
172 DefineSetInWS(fObservablesName.c_str(), set);
173 }
174 /// specify the observables
175 /// through a comma-separated list of arguments already in the workspace.
176 virtual void SetObservables(const char *argList)
177 {
178 if (!GetWS())
179 return;
180 SetObservables(GetWS()->argSet(argList));
181 }
182
183 virtual void SetConditionalObservables(const RooArgSet &set);
184 /// Specify the conditional observables
185 /// through a comma-separated list of arguments already in the workspace.
186 virtual void SetConditionalObservables(const char *argList)
187 {
188 if (!GetWS())
189 return;
190 SetConditionalObservables(GetWS()->argSet(argList));
191 }
192
193 virtual void SetGlobalObservables(const RooArgSet &set);
194 /// Specify the global observables
195 /// through a comma-separated list of arguments already in the workspace.
196 virtual void SetGlobalObservables(const char *argList)
197 {
198 if (!GetWS())
199 return;
200 SetGlobalObservables(GetWS()->argSet(argList));
201 }
202
203 void SetExternalConstraints(const RooArgSet &set);
204 /// Specify the external constraints
205 /// through a comma-separated list of arguments already in the workspace.
206 virtual void SetExternalConstraints(const char *argList)
207 {
208 if (!GetWS())
209 return;
210 SetExternalConstraints(GetWS()->argSet(argList));
211 }
212
213 /// Set parameter values for a particular hypothesis if using a common PDF
214 /// by saving a snapshot in the workspace.
215 virtual void SetSnapshot(const RooArgSet &set);
216
217 /// Specify the name of the PDF in the workspace to be used.
218 virtual void SetPdf(const char *name)
219 {
220 if (!GetWS())
221 return;
222
223 if (GetWS()->pdf(name)) {
224 fPdfName = name;
225 } else {
226 std::stringstream ss;
227 ss << "pdf " << name << " does not exist in workspace";
228 const std::string errorMsg = ss.str();
229 coutE(ObjectHandling) << errorMsg << std::endl;
230 throw std::runtime_error(errorMsg);
231 }
232 }
233
234 /// Specify the name of the PDF in the workspace to be used.
235 virtual void SetPriorPdf(const char *name)
236 {
237 if (!GetWS())
238 return;
239
240 if (GetWS()->pdf(name)) {
242 } else {
243 std::stringstream ss;
244 ss << "pdf " << name << " does not exist in workspace";
245 const std::string errorMsg = ss.str();
246 coutE(ObjectHandling) << errorMsg << std::endl;
247 throw std::runtime_error(errorMsg);
248 }
249 }
250
251 /// Specify the name of the dataset in the workspace to be used.
252 virtual void SetProtoData(const char *name)
253 {
254 if (!GetWS())
255 return;
256
257 if (GetWS()->data(name)) {
259 } else {
260 std::stringstream ss;
261 ss << "dataset " << name << " does not exist in workspace";
262 const std::string errorMsg = ss.str();
263 coutE(ObjectHandling) << errorMsg << std::endl;
264 throw std::runtime_error(errorMsg);
265 }
266 }
267
268 /* getter methods */
269
270 /// get model PDF (return nullptr if pdf has not been specified or does not exist)
271 RooAbsPdf *GetPdf() const { return (GetWS()) ? GetWS()->pdf(fPdfName) : nullptr; }
272
273 /// get RooArgSet containing the parameter of interest (return nullptr if not existing)
274 const RooArgSet *GetParametersOfInterest() const { return (GetWS()) ? GetWS()->set(fPOIName) : nullptr; }
275
276 /// get RooArgSet containing the nuisance parameters (return nullptr if not existing)
277 const RooArgSet *GetNuisanceParameters() const { return (GetWS()) ? GetWS()->set(fNuisParamsName) : nullptr; }
278
279 /// get RooArgSet containing the constraint parameters (return nullptr if not existing)
280 const RooArgSet *GetConstraintParameters() const { return (GetWS()) ? GetWS()->set(fConstrParamsName) : nullptr; }
281
282 /// get parameters prior pdf (return nullptr if not existing)
283 RooAbsPdf *GetPriorPdf() const { return (GetWS()) ? GetWS()->pdf(fPriorPdfName) : nullptr; }
284
285 /// get RooArgSet for observables (return nullptr if not existing)
286 const RooArgSet *GetObservables() const { return (GetWS()) ? GetWS()->set(fObservablesName) : nullptr; }
287
288 /// get RooArgSet for conditional observables (return nullptr if not existing)
290 {
291 return (GetWS()) ? GetWS()->set(fConditionalObsName) : nullptr;
292 }
293
294 /// get RooArgSet for global observables (return nullptr if not existing)
295 const RooArgSet *GetGlobalObservables() const { return (GetWS()) ? GetWS()->set(fGlobalObsName) : nullptr; }
296
297 /// get RooArgSet for global observables (return nullptr if not existing)
298 const RooArgSet *GetExternalConstraints() const { return (GetWS()) ? GetWS()->set(fExtConstraintsName) : nullptr; }
299
300 /// get Proto data set (return nullptr if not existing)
301 RooAbsData *GetProtoData() const { return (GetWS()) ? GetWS()->data(fProtoDataName) : nullptr; }
302
303 /// get RooArgSet for parameters for a particular hypothesis (return nullptr if not existing)
304 const RooArgSet *GetSnapshot() const;
305
306 void LoadSnapshot() const;
307
308 RooWorkspace *GetWS() const override;
309 /// alias for GetWS()
310 RooWorkspace *GetWorkspace() const { return GetWS(); }
311
312 void GuessObsAndNuisance(const RooAbsData &data, bool printModelConfig = true);
313
314 /// overload the print method
315 void Print(Option_t *option = "") const override;
316
317 template <typename... CmdArgs_t>
318 std::unique_ptr<RooAbsReal> createNLL(RooAbsData &data, CmdArgs_t const &...cmdArgs) const
319 {
320 return createNLLImpl(data, *RooFit::Detail::createCmdList(&cmdArgs...));
321 }
322
323 template <typename... CmdArgs_t>
324 std::unique_ptr<RooFitResult> fitTo(RooAbsData &data, CmdArgs_t const &...cmdArgs)
325 {
326 return fitToImpl(data, *RooFit::Detail::createCmdList(&cmdArgs...));
327 }
328
329protected:
330 /// helper function to check that content of a given set is exclusively parameters
331 bool SetHasOnlyParameters(const RooArgSet &set, const char *errorMsgPrefix = nullptr);
332
333 /// helper functions to define a set in the WS
334 void DefineSetInWS(const char *name, const RooArgSet &set);
335
336 /// internal function to import Pdf in WS
337 void ImportPdfInWS(const RooAbsPdf &pdf);
338
339 /// internal function to import data in WS
341
342 TRef fRefWS; ///< WS reference used in the file
343
344 std::string fWSName; ///< name of the WS
345
346 std::string fPdfName; ///< name of PDF in workspace
347 std::string fDataName; ///< name of data set in workspace
348 std::string fPOIName; ///< name for RooArgSet specifying parameters of interest
349
350 std::string fNuisParamsName; ///< name for RooArgSet specifying nuisance parameters
351 std::string fConstrParamsName; ///< name for RooArgSet specifying constrained parameters
352 std::string fPriorPdfName; ///< name for RooAbsPdf specifying a prior on the parameters
353
354 std::string fConditionalObsName; ///< name for RooArgSet specifying conditional observables
355 std::string fGlobalObsName; ///< name for RooArgSet specifying global observables
356 std::string fExtConstraintsName; ///< name for RooArgSet specifying external constraints
357 std::string fProtoDataName; ///< name for RooArgSet specifying dataset that should be used as proto-data
358
359 std::string fSnapshotName; ///< name for RooArgSet that specifies a particular hypothesis
360
361 std::string fObservablesName; ///< name for RooArgSet specifying observable parameters.
362
363private:
364 std::unique_ptr<RooAbsReal> createNLLImpl(RooAbsData &data, const RooLinkedList &cmdList) const;
365 std::unique_ptr<RooFitResult> fitToImpl(RooAbsData &data, const RooLinkedList &cmdList);
366
368 6); ///< A class that holds configuration information for a model using a workspace as a store
369};
370
371} // end namespace RooStats
372
373namespace RooFit {
375}
376
377#endif
#define coutE(a)
const char Option_t
Definition RtypesCore.h:66
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
char name[80]
Definition TGX11.cxx:110
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
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...
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition ModelConfig.h:35
virtual void SetSnapshot(const RooArgSet &set)
Set parameter values for a particular hypothesis if using a common PDF by saving a snapshot in the wo...
std::unique_ptr< RooFitResult > fitTo(RooAbsData &data, CmdArgs_t const &...cmdArgs)
Wrapper around RooAbsPdf::fitTo(), where the pdf and some configuration options are retrieved from th...
virtual void SetObservables(const RooArgSet &set)
Specify the observables.
std::string fSnapshotName
name for RooArgSet that specifies a particular hypothesis
std::string fExtConstraintsName
name for RooArgSet specifying external constraints
ModelConfig(const char *name, const char *title, RooWorkspace *ws=nullptr)
Definition ModelConfig.h:50
void ImportPdfInWS(const RooAbsPdf &pdf)
internal function to import Pdf in WS
std::string fNuisParamsName
name for RooArgSet specifying nuisance parameters
virtual void SetPriorPdf(const RooAbsPdf &pdf)
Set the Prior Pdf, add to the workspace if not already there.
Definition ModelConfig.h:95
virtual void SetObservables(const char *argList)
specify the observables through a comma-separated list of arguments already in the workspace.
void SetExternalConstraints(const RooArgSet &set)
Specify the external constraints.
RooAbsData * GetProtoData() const
get Proto data set (return nullptr if not existing)
void DefineSetInWS(const char *name, const RooArgSet &set)
helper functions to define a set in the WS
virtual void SetNuisanceParameters(const char *argList)
Specify the nuisance parameters using a comma-separated list of arguments already in the workspace.
std::string fWSName
name of the WS
std::string fPriorPdfName
name for RooAbsPdf specifying a prior on the parameters
std::string fDataName
name of data set in workspace
virtual void SetWorkspace(RooWorkspace &ws)
Definition ModelConfig.h:71
RooWorkspace * GetWorkspace() const
alias for GetWS()
const RooArgSet * GetConditionalObservables() const
get RooArgSet for conditional observables (return nullptr if not existing)
void GuessObsAndNuisance(const RooAbsData &data, bool printModelConfig=true)
Makes sensible guesses of observables, parameters of interest and nuisance parameters if one or multi...
virtual void SetProtoData(const char *name)
Specify the name of the dataset in the workspace to be used.
std::string fConditionalObsName
name for RooArgSet specifying conditional observables
ModelConfig(const char *name, RooWorkspace *ws=nullptr)
Definition ModelConfig.h:44
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return nullptr if not existing)
virtual void SetConditionalObservables(const RooArgSet &set)
Specify the conditional observables.
ModelConfig * Clone(const char *name="") const override
clone
Definition ModelConfig.h:57
std::unique_ptr< RooAbsReal > createNLL(RooAbsData &data, CmdArgs_t const &...cmdArgs) const
Wrapper around RooAbsPdf::createNLL(), where the pdf and some configuration options are retrieved fro...
virtual void SetParametersOfInterest(const RooArgSet &set)
Specify parameters of interest.
TRef fRefWS
WS reference used in the file.
std::unique_ptr< RooFitResult > fitToImpl(RooAbsData &data, const RooLinkedList &cmdList)
void ReplaceWS(RooWorkspace *ws) override
Remove the existing reference to a workspace and replace it with this new one.
Definition ModelConfig.h:74
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
std::string fPdfName
name of PDF in workspace
std::string fObservablesName
name for RooArgSet specifying observable parameters.
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return nullptr if not existing)
virtual void SetConstraintParameters(const char *argList)
Specify the constraint parameters through a comma-separated list of arguments already in the workspac...
virtual void SetParameters(const char *argList)
Specify parameters using a list of comma-separated list of arguments already in the workspace.
std::unique_ptr< RooAbsReal > createNLLImpl(RooAbsData &data, const RooLinkedList &cmdList) const
void LoadSnapshot() const
load the snapshot from ws if it exists
std::string fConstrParamsName
name for RooArgSet specifying constrained parameters
void Print(Option_t *option="") const override
overload the print method
std::string fGlobalObsName
name for RooArgSet specifying global observables
void ImportDataInWS(RooAbsData &data)
internal function to import data in WS
void SetWS(RooWorkspace &ws) override
Set a workspace that owns all the necessary components for the analysis.
bool SetHasOnlyParameters(const RooArgSet &set, const char *errorMsgPrefix=nullptr)
helper function to check that content of a given set is exclusively parameters
std::string fPOIName
name for RooArgSet specifying parameters of interest
virtual void SetParametersOfInterest(const char *argList)
Specify parameters of interest using a comma-separated list of arguments already in the workspace.
const RooArgSet * GetObservables() const
get RooArgSet for observables (return nullptr if not existing)
ModelConfig(RooWorkspace *ws=nullptr)
Definition ModelConfig.h:38
virtual void SetExternalConstraints(const char *argList)
Specify the external constraints through a comma-separated list of arguments already in the workspace...
virtual void SetConditionalObservables(const char *argList)
Specify the conditional observables through a comma-separated list of arguments already in the worksp...
virtual void SetGlobalObservables(const char *argList)
Specify the global observables through a comma-separated list of arguments already in the workspace.
const RooArgSet * GetSnapshot() const
get RooArgSet for parameters for a particular hypothesis (return nullptr if not existing)
const RooArgSet * GetConstraintParameters() const
get RooArgSet containing the constraint parameters (return nullptr if not existing)
const RooArgSet * GetExternalConstraints() const
get RooArgSet for global observables (return nullptr if not existing)
virtual void SetParameters(const RooArgSet &set)
Specify parameters of the PDF.
RooWorkspace * GetWS() const override
get from TRef
virtual void SetPriorPdf(const char *name)
Specify the name of the PDF in the workspace to be used.
std::string fProtoDataName
name for RooArgSet specifying dataset that should be used as proto-data
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
virtual void SetNuisanceParameters(const RooArgSet &set)
Specify the nuisance parameters (parameters that are not POI).
virtual void SetConstraintParameters(const RooArgSet &set)
Specify the constraint parameters.
virtual void SetPdf(const char *name)
Specify the name of the PDF in the workspace to be used.
virtual void SetProtoData(RooAbsData &data)
Set the proto DataSet, add to the workspace if not already there.
Definition ModelConfig.h:81
RooAbsPdf * GetPriorPdf() const
get parameters prior pdf (return nullptr if not existing)
virtual void SetPdf(const RooAbsPdf &pdf)
Set the Pdf, add to the workspace if not already there.
Definition ModelConfig.h:88
virtual void SetGlobalObservables(const RooArgSet &set)
Specify the global observables.
An interface to set and retrieve a workspace.
Persistable container for RooFit projects.
RooAbsPdf * pdf(RooStringView name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
const RooArgSet * set(RooStringView name)
Return pointer to previously defined named set with given nmame If no such set is found a null pointe...
RooAbsData * data(RooStringView name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found.
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
Persistent Reference link to a TObject A TRef is a lightweight object pointing to any TObject.
Definition TRef.h:32
std::unique_ptr< RooLinkedList > createCmdList()
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:64
Namespace for the RooStats classes.
Definition CodegenImpl.h:58