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:
38 ModelConfig(RooWorkspace *ws = nullptr) : TNamed()
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 return mc;
65 }
66
67 /// Set a workspace that owns all the necessary components for the analysis.
68 void SetWS(RooWorkspace &ws) override;
69 //// alias for SetWS(...)
70 virtual void SetWorkspace(RooWorkspace &ws) { SetWS(ws); }
71
72 /// Remove the existing reference to a workspace and replace it with this new one.
73 void ReplaceWS(RooWorkspace *ws) override
74 {
75 fRefWS = nullptr;
76 SetWS(*ws);
77 }
78
79 /// Set the proto DataSet, add to the workspace if not already there
81 {
83 SetProtoData(data.GetName());
84 }
85
86 /// Set the Pdf, add to the workspace if not already there
87 virtual void SetPdf(const RooAbsPdf &pdf)
88 {
89 ImportPdfInWS(pdf);
90 SetPdf(pdf.GetName());
91 }
92
93 /// Set the Prior Pdf, add to the workspace if not already there
94 virtual void SetPriorPdf(const RooAbsPdf &pdf)
95 {
96 ImportPdfInWS(pdf);
97 SetPriorPdf(pdf.GetName());
98 }
99
100 /// Specify parameters of the PDF.
101 virtual void SetParameters(const RooArgSet &set)
102 {
103 if (!SetHasOnlyParameters(set, "ModelConfig::SetParameters"))
104 return;
105 fPOIName = std::string(GetName()) + "_POI";
106 DefineSetInWS(fPOIName.c_str(), set);
107 }
108
109 /// Specify parameters of interest.
110 virtual void SetParametersOfInterest(const RooArgSet &set)
111 {
112 if (!SetHasOnlyParameters(set, "ModelConfig::SetParametersOfInterest"))
113 return;
114 SetParameters(set);
115 }
116
117 /// Specify parameters
118 /// using a list of comma-separated list of arguments already in the workspace.
119 virtual void SetParameters(const char *argList)
120 {
121 if (!GetWS())
122 return;
123 SetParameters(GetWS()->argSet(argList));
124 }
125
126 /// Specify parameters of interest
127 /// using a comma-separated list of arguments already in the workspace.
128 virtual void SetParametersOfInterest(const char *argList) { SetParameters(argList); }
129
130 /// Specify the nuisance parameters (parameters that are not POI).
131 virtual void SetNuisanceParameters(const RooArgSet &set)
132 {
133 if (!SetHasOnlyParameters(set, "ModelConfig::SetNuisanceParameters"))
134 return;
135 fNuisParamsName = std::string(GetName()) + "_NuisParams";
136 DefineSetInWS(fNuisParamsName.c_str(), set);
137 }
138
139 /// Specify the nuisance parameters
140 /// using a comma-separated list of arguments already in the workspace.
141 virtual void SetNuisanceParameters(const char *argList)
142 {
143 if (!GetWS())
144 return;
145 SetNuisanceParameters(GetWS()->argSet(argList));
146 }
147
148 /// Specify the constraint parameters
149 virtual void SetConstraintParameters(const RooArgSet &set)
150 {
151 if (!SetHasOnlyParameters(set, "ModelConfig::SetConstrainedParameters"))
152 return;
153 fConstrParamsName = std::string(GetName()) + "_ConstrainedParams";
154 DefineSetInWS(fConstrParamsName.c_str(), set);
155 }
156 /// Specify the constraint parameters
157 /// through a comma-separated list of arguments already in the workspace.
158 virtual void SetConstraintParameters(const char *argList)
159 {
160 if (!GetWS())
161 return;
162 SetConstraintParameters(GetWS()->argSet(argList));
163 }
164
165 /// Specify the observables.
166 virtual void SetObservables(const RooArgSet &set)
167 {
168 if (!SetHasOnlyParameters(set, "ModelConfig::SetObservables"))
169 return;
170 fObservablesName = std::string(GetName()) + "_Observables";
171 DefineSetInWS(fObservablesName.c_str(), set);
172 }
173 /// specify the observables
174 /// through a comma-separated list of arguments already in the workspace.
175 virtual void SetObservables(const char *argList)
176 {
177 if (!GetWS())
178 return;
179 SetObservables(GetWS()->argSet(argList));
180 }
181
182 virtual void SetConditionalObservables(const RooArgSet &set);
183 /// Specify the conditional observables
184 /// through a comma-separated list of arguments already in the workspace.
185 virtual void SetConditionalObservables(const char *argList)
186 {
187 if (!GetWS())
188 return;
189 SetConditionalObservables(GetWS()->argSet(argList));
190 }
191
192 virtual void SetGlobalObservables(const RooArgSet &set);
193 /// Specify the global observables
194 /// through a comma-separated list of arguments already in the workspace.
195 virtual void SetGlobalObservables(const char *argList)
196 {
197 if (!GetWS())
198 return;
199 SetGlobalObservables(GetWS()->argSet(argList));
200 }
201
202 void SetExternalConstraints(const RooArgSet &set);
203 /// Specify the external constraints
204 /// through a comma-separated list of arguments already in the workspace.
205 virtual void SetExternalConstraints(const char *argList)
206 {
207 if (!GetWS())
208 return;
209 SetExternalConstraints(GetWS()->argSet(argList));
210 }
211
212 /// Set parameter values for a particular hypothesis if using a common PDF
213 /// by saving a snapshot in the workspace.
214 virtual void SetSnapshot(const RooArgSet &set);
215
216 /// Specify the name of the PDF in the workspace to be used.
217 virtual void SetPdf(const char *name)
218 {
219 if (!GetWS())
220 return;
221
222 if (GetWS()->pdf(name))
223 fPdfName = name;
224 else {
225 std::stringstream ss;
226 ss << "pdf " << name << " does not exist in workspace";
227 const std::string errorMsg = ss.str();
228 coutE(ObjectHandling) << errorMsg << std::endl;
229 throw std::runtime_error(errorMsg);
230 }
231 }
232
233 /// Specify the name of the PDF in the workspace to be used.
234 virtual void SetPriorPdf(const char *name)
235 {
236 if (!GetWS())
237 return;
238
239 if (GetWS()->pdf(name))
241 else {
242 std::stringstream ss;
243 ss << "pdf " << name << " does not exist in workspace";
244 const std::string errorMsg = ss.str();
245 coutE(ObjectHandling) << errorMsg << std::endl;
246 throw std::runtime_error(errorMsg);
247 }
248 }
249
250 /// Specify the name of the dataset in the workspace to be used.
251 virtual void SetProtoData(const char *name)
252 {
253 if (!GetWS())
254 return;
255
256 if (GetWS()->data(name))
258 else {
259 std::stringstream ss;
260 ss << "dataset " << name << " does not exist in workspace";
261 const std::string errorMsg = ss.str();
262 coutE(ObjectHandling) << errorMsg << std::endl;
263 throw std::runtime_error(errorMsg);
264 }
265 }
266
267 /* getter methods */
268
269 /// get model PDF (return nullptr if pdf has not been specified or does not exist)
270 RooAbsPdf *GetPdf() const { return (GetWS()) ? GetWS()->pdf(fPdfName) : nullptr; }
271
272 /// get RooArgSet containing the parameter of interest (return nullptr if not existing)
273 const RooArgSet *GetParametersOfInterest() const { return (GetWS()) ? GetWS()->set(fPOIName) : nullptr; }
274
275 /// get RooArgSet containing the nuisance parameters (return nullptr if not existing)
276 const RooArgSet *GetNuisanceParameters() const { return (GetWS()) ? GetWS()->set(fNuisParamsName) : nullptr; }
277
278 /// get RooArgSet containing the constraint parameters (return nullptr if not existing)
279 const RooArgSet *GetConstraintParameters() const { return (GetWS()) ? GetWS()->set(fConstrParamsName) : nullptr; }
280
281 /// get parameters prior pdf (return nullptr if not existing)
282 RooAbsPdf *GetPriorPdf() const { return (GetWS()) ? GetWS()->pdf(fPriorPdfName) : nullptr; }
283
284 /// get RooArgSet for observables (return nullptr if not existing)
285 const RooArgSet *GetObservables() const { return (GetWS()) ? GetWS()->set(fObservablesName) : nullptr; }
286
287 /// get RooArgSet for conditional observables (return nullptr if not existing)
289 {
290 return (GetWS()) ? GetWS()->set(fConditionalObsName) : nullptr;
291 }
292
293 /// get RooArgSet for global observables (return nullptr if not existing)
294 const RooArgSet *GetGlobalObservables() const { return (GetWS()) ? GetWS()->set(fGlobalObsName) : nullptr; }
295
296 /// get RooArgSet for global observables (return nullptr if not existing)
297 const RooArgSet *GetExternalConstraints() const { return (GetWS()) ? GetWS()->set(fExtConstraintsName) : nullptr; }
298
299 /// get Proto data set (return nullptr if not existing)
300 RooAbsData *GetProtoData() const { return (GetWS()) ? GetWS()->data(fProtoDataName) : nullptr; }
301
302 /// get RooArgSet for parameters for a particular hypothesis (return nullptr if not existing)
303 const RooArgSet *GetSnapshot() const;
304
305 void LoadSnapshot() const;
306
307 RooWorkspace *GetWS() const override;
308 /// alias for GetWS()
309 RooWorkspace *GetWorkspace() const { return GetWS(); }
310
311 void GuessObsAndNuisance(const RooAbsData &data, bool printModelConfig = true);
312
313 /// overload the print method
314 void Print(Option_t *option = "") const override;
315
316 template <typename... CmdArgs_t>
317 std::unique_ptr<RooAbsReal> createNLL(RooAbsData &data, CmdArgs_t const &...cmdArgs) const
318 {
319 return createNLLImpl(data, *RooFit::Detail::createCmdList(&cmdArgs...));
320 }
321
322 template <typename... CmdArgs_t>
323 std::unique_ptr<RooFitResult> fitTo(RooAbsData &data, CmdArgs_t const &...cmdArgs)
324 {
325 return fitToImpl(data, *RooFit::Detail::createCmdList(&cmdArgs...));
326 }
327
328protected:
329 /// helper function to check that content of a given set is exclusively parameters
330 bool SetHasOnlyParameters(const RooArgSet &set, const char *errorMsgPrefix = nullptr);
331
332 /// helper functions to define a set in the WS
333 void DefineSetInWS(const char *name, const RooArgSet &set);
334
335 /// internal function to import Pdf in WS
336 void ImportPdfInWS(const RooAbsPdf &pdf);
337
338 /// internal function to import data in WS
340
341 TRef fRefWS; ///< WS reference used in the file
342
343 std::string fWSName; ///< name of the WS
344
345 std::string fPdfName; ///< name of PDF in workspace
346 std::string fDataName; ///< name of data set in workspace
347 std::string fPOIName; ///< name for RooArgSet specifying parameters of interest
348
349 std::string fNuisParamsName; ///< name for RooArgSet specifying nuisance parameters
350 std::string fConstrParamsName; ///< name for RooArgSet specifying constrained parameters
351 std::string fPriorPdfName; ///< name for RooAbsPdf specifying a prior on the parameters
352
353 std::string fConditionalObsName; ///< name for RooArgSet specifying conditional observables
354 std::string fGlobalObsName; ///< name for RooArgSet specifying global observables
355 std::string fExtConstraintsName; ///< name for RooArgSet specifying external constraints
356 std::string fProtoDataName; ///< name for RooArgSet specifying dataset that should be used as proto-data
357
358 std::string fSnapshotName; ///< name for RooArgSet that specifies a particular hypothesis
359
360 std::string fObservablesName; ///< name for RooArgSet specifying observable parameters.
361
362private:
363 std::unique_ptr<RooAbsReal> createNLLImpl(RooAbsData &data, const RooLinkedList &cmdList) const;
364 std::unique_ptr<RooFitResult> fitToImpl(RooAbsData &data, const RooLinkedList &cmdList);
365
367 6); ///< A class that holds configuration information for a model using a workspace as a store
368};
369
370} // end namespace RooStats
371
372namespace RooFit {
374}
375
376#endif
#define coutE(a)
const char Option_t
Definition RtypesCore.h:66
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
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:55
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 ...
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:94
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:70
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:73
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:80
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:87
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 JSONIO.h:26
Namespace for the RooStats classes.
Definition Asimov.h:19