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::SetConstainedParameters"))
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 coutE(ObjectHandling) << "pdf " << name << " does not exist in workspace" << std::endl;
226 }
227
228 /// Specify the name of the PDF in the workspace to be used.
229 virtual void SetPriorPdf(const char *name)
230 {
231 if (!GetWS())
232 return;
233
234 if (GetWS()->pdf(name))
236 else
237 coutE(ObjectHandling) << "pdf " << name << " does not exist in workspace" << std::endl;
238 }
239
240 /// Specify the name of the dataset in the workspace to be used.
241 virtual void SetProtoData(const char *name)
242 {
243 if (!GetWS())
244 return;
245
246 if (GetWS()->data(name))
248 else
249 coutE(ObjectHandling) << "dataset " << name << " does not exist in workspace" << std::endl;
250 }
251
252 /* getter methods */
253
254 /// get model PDF (return nullptr if pdf has not been specified or does not exist)
255 RooAbsPdf *GetPdf() const { return (GetWS()) ? GetWS()->pdf(fPdfName) : nullptr; }
256
257 /// get RooArgSet containing the parameter of interest (return nullptr if not existing)
258 const RooArgSet *GetParametersOfInterest() const { return (GetWS()) ? GetWS()->set(fPOIName) : nullptr; }
259
260 /// get RooArgSet containing the nuisance parameters (return nullptr if not existing)
261 const RooArgSet *GetNuisanceParameters() const { return (GetWS()) ? GetWS()->set(fNuisParamsName) : nullptr; }
262
263 /// get RooArgSet containing the constraint parameters (return nullptr if not existing)
264 const RooArgSet *GetConstraintParameters() const { return (GetWS()) ? GetWS()->set(fConstrParamsName) : nullptr; }
265
266 /// get parameters prior pdf (return nullptr if not existing)
267 RooAbsPdf *GetPriorPdf() const { return (GetWS()) ? GetWS()->pdf(fPriorPdfName) : nullptr; }
268
269 /// get RooArgSet for observables (return nullptr if not existing)
270 const RooArgSet *GetObservables() const { return (GetWS()) ? GetWS()->set(fObservablesName) : nullptr; }
271
272 /// get RooArgSet for conditional observables (return nullptr if not existing)
274 {
275 return (GetWS()) ? GetWS()->set(fConditionalObsName) : nullptr;
276 }
277
278 /// get RooArgSet for global observables (return nullptr if not existing)
279 const RooArgSet *GetGlobalObservables() const { return (GetWS()) ? GetWS()->set(fGlobalObsName) : nullptr; }
280
281 /// get RooArgSet for global observables (return nullptr if not existing)
282 const RooArgSet *GetExternalConstraints() const { return (GetWS()) ? GetWS()->set(fExtConstraintsName) : nullptr; }
283
284 /// get Proto data set (return nullptr if not existing)
285 RooAbsData *GetProtoData() const { return (GetWS()) ? GetWS()->data(fProtoDataName) : nullptr; }
286
287 /// get RooArgSet for parameters for a particular hypothesis (return nullptr if not existing)
288 const RooArgSet *GetSnapshot() const;
289
290 void LoadSnapshot() const;
291
292 RooWorkspace *GetWS() const override;
293 /// alias for GetWS()
294 RooWorkspace *GetWorkspace() const { return GetWS(); }
295
296 void GuessObsAndNuisance(const RooAbsData &data, bool printModelConfig = true);
297
298 /// overload the print method
299 void Print(Option_t *option = "") const override;
300
301 std::unique_ptr<RooAbsReal> createNLL(RooAbsData &data, const RooLinkedList &cmdList = {}) const;
302
303 /// Takes an arbitrary number of RooCmdArg command options and calls
304 /// createNLL(RooAbsData& data, const RooLinkedList& cmdList).
305 template <typename... Args>
306 std::unique_ptr<RooAbsReal> createNLL(RooAbsData &data, RooCmdArg const &arg1, Args const &...args) const
307 {
308 return createNLL(data, *RooFit::Detail::createCmdList(&arg1, &args...));
309 }
310
311 std::unique_ptr<RooFitResult> fitTo(RooAbsData &data, const RooLinkedList &cmdList = {});
312
313 /// Takes an arbitrary number of RooCmdArg command options and calls
314 /// ModelConfig::fitTo(RooAbsData& data, const RooLinkedList& cmdList).
315 template <typename... Args>
316 std::unique_ptr<RooFitResult> fitTo(RooAbsData &data, RooCmdArg const &arg1, Args const &...args)
317 {
318 return fitTo(data, *RooFit::Detail::createCmdList(&arg1, &args...));
319 }
320
321protected:
322 /// helper function to check that content of a given set is exclusively parameters
323 bool SetHasOnlyParameters(const RooArgSet &set, const char *errorMsgPrefix = nullptr);
324
325 /// helper functions to define a set in the WS
326 void DefineSetInWS(const char *name, const RooArgSet &set);
327
328 /// internal function to import Pdf in WS
329 void ImportPdfInWS(const RooAbsPdf &pdf);
330
331 /// internal function to import data in WS
333
334 TRef fRefWS; ///< WS reference used in the file
335
336 std::string fWSName; ///< name of the WS
337
338 std::string fPdfName; ///< name of PDF in workspace
339 std::string fDataName; ///< name of data set in workspace
340 std::string fPOIName; ///< name for RooArgSet specifying parameters of interest
341
342 std::string fNuisParamsName; ///< name for RooArgSet specifying nuisance parameters
343 std::string fConstrParamsName; ///< name for RooArgSet specifying constrained parameters
344 std::string fPriorPdfName; ///< name for RooAbsPdf specifying a prior on the parameters
345
346 std::string fConditionalObsName; ///< name for RooArgSet specifying conditional observables
347 std::string fGlobalObsName; ///< name for RooArgSet specifying global observables
348 std::string fExtConstraintsName; ///< name for RooArgSet specifying external constraints
349 std::string fProtoDataName; ///< name for RooArgSet specifying dataset that should be used as proto-data
350
351 std::string fSnapshotName; ///< name for RooArgSet that specifies a particular hypothesis
352
353 std::string fObservablesName; ///< name for RooArgSet specifying observable parameters.
354
356 6); ///< A class that holds configuration information for a model using a workspace as a store
357};
358
359} // end namespace RooStats
360
361namespace RooFit {
363}
364
365#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
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:59
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooCmdArg is a named container for two doubles, two integers two object points and three string point...
Definition RooCmdArg.h:26
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...
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
std::unique_ptr< RooFitResult > fitTo(RooAbsData &data, const RooLinkedList &cmdList={})
Wrapper around RooAbsPdf::fitTo(RooAbsData&, const RooLinkedList&), where the pdf and some configurat...
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
virtual void SetParametersOfInterest(const RooArgSet &set)
Specify parameters of interest.
TRef fRefWS
WS reference used in the file.
void ReplaceWS(RooWorkspace *ws) override
Remove the existing reference to a workspace and replace it with this new one.
Definition ModelConfig.h:73
std::unique_ptr< RooAbsReal > createNLL(RooAbsData &data, RooCmdArg const &arg1, Args const &...args) const
Takes an arbitrary number of RooCmdArg command options and calls createNLL(RooAbsData& data,...
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 > createNLL(RooAbsData &data, const RooLinkedList &cmdList={}) const
Wrapper around RooAbsPdf::createNLL(RooAbsData&, const RooLinkedList&), where the pdf and some config...
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)
std::unique_ptr< RooFitResult > fitTo(RooAbsData &data, RooCmdArg const &arg1, Args const &...args)
Takes an arbitrary number of RooCmdArg command options and calls ModelConfig::fitTo(RooAbsData& data,...
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.
The RooWorkspace is a 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(Args &&... args)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition Common.h:18
Namespace for the RooStats classes.
Definition Asimov.h:19