Logo ROOT  
Reference Guide
ModelConfig.h
Go to the documentation of this file.
1// @(#)root/roostats:$Id$
2// Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke, Sven Kreiss
3/*************************************************************************
4 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOSTATS_ModelConfig
12#define ROOSTATS_ModelConfig
13
14
15#include "RooAbsPdf.h"
16
17#include "RooAbsData.h"
18
19#include "RooArgSet.h"
20
21#include "RooWorkspaceHandle.h"
22
23#include "TRef.h"
24
25#include <string>
26
27
28namespace RooStats {
29
30class ModelConfig final : public TNamed, public RooWorkspaceHandle {
31
32public:
33
34 ModelConfig(RooWorkspace * ws = nullptr) :
35 TNamed()
36 {
37 if(ws) SetWS(*ws);
38 }
39
40 ModelConfig(const char* name, RooWorkspace *ws = nullptr) :
42 {
43 if(ws) SetWS(*ws);
44 }
45
46 ModelConfig(const char* name, const char* title, RooWorkspace *ws = nullptr) :
47 TNamed(name, title)
48 {
49 if(ws) SetWS(*ws);
50 }
51
52
53 /// clone
54 ModelConfig * Clone(const char * name = "") const override {
55 ModelConfig * mc = new ModelConfig(*this);
56 if(strcmp(name,"")==0)
57 mc->SetName(this->GetName());
58 else
59 mc->SetName(name);
60 return mc;
61 }
62
63 /// Set a workspace that owns all the necessary components for the analysis.
64 void SetWS(RooWorkspace & ws) override;
65 //// alias for SetWS(...)
66 virtual void SetWorkspace(RooWorkspace & ws) { SetWS(ws); }
67
68 /// Remove the existing reference to a workspace and replace it with this new one.
69 void ReplaceWS(RooWorkspace *ws) override {
70 fRefWS = nullptr;
71 SetWS(*ws);
72 }
73
74 /// Set the proto DataSet, add to the workspace if not already there
75 virtual void SetProtoData(RooAbsData & data) {
77 SetProtoData( data.GetName() );
78 }
79
80 /// Set the Pdf, add to the workspace if not already there
81 virtual void SetPdf(const RooAbsPdf& pdf) {
82 ImportPdfInWS(pdf);
83 SetPdf( pdf.GetName() );
84 }
85
86 /// Set the Prior Pdf, add to the workspace if not already there
87 virtual void SetPriorPdf(const RooAbsPdf& pdf) {
88 ImportPdfInWS(pdf);
89 SetPriorPdf( pdf.GetName() );
90 }
91
92 /// Specify parameters of the PDF.
93 virtual void SetParameters(const RooArgSet& set) {
94 if (!SetHasOnlyParameters(set,"ModelConfig::SetParameters")) return ;
95 fPOIName=std::string(GetName()) + "_POI";
96 DefineSetInWS(fPOIName.c_str(), set);
97 }
98
99 /// Specify parameters of interest.
100 virtual void SetParametersOfInterest(const RooArgSet& set) {
101 if (!SetHasOnlyParameters(set,"ModelConfig::SetParametersOfInterest")) return ;
102 SetParameters(set);
103 }
104
105 /// Specify parameters
106 /// using a list of comma-separated list of arguments already in the workspace.
107 virtual void SetParameters(const char *argList) {
108 if(!GetWS()) return;
109 SetParameters(GetWS()->argSet(argList));
110 }
111
112 /// Specify parameters of interest
113 /// using a comma-separated list of arguments already in the workspace.
114 virtual void SetParametersOfInterest(const char *argList) {
115 SetParameters(argList);
116 }
117
118 /// Specify the nuisance parameters (parameters that are not POI).
119 virtual void SetNuisanceParameters(const RooArgSet& set) {
120 if (!SetHasOnlyParameters(set,"ModelConfig::SetNuisanceParameters")) return ;
121 fNuisParamsName=std::string(GetName()) + "_NuisParams";
122 DefineSetInWS(fNuisParamsName.c_str(), set);
123 }
124
125 /// Specify the nuisance parameters
126 /// using a comma-separated list of arguments already in the workspace.
127 virtual void SetNuisanceParameters(const char *argList) {
128 if(!GetWS()) return;
129 SetNuisanceParameters(GetWS()->argSet(argList));
130 }
131
132 /// Specify the constraint parameters
133 virtual void SetConstraintParameters(const RooArgSet& set) {
134 if (!SetHasOnlyParameters(set,"ModelConfig::SetConstainedParameters")) return ;
135 fConstrParamsName=std::string(GetName()) + "_ConstrainedParams";
136 DefineSetInWS(fConstrParamsName.c_str(), set);
137 }
138 /// Specify the constraint parameters
139 /// through a comma-separated list of arguments already in the workspace.
140 virtual void SetConstraintParameters(const char *argList) {
141 if(!GetWS()) return;
142 SetConstraintParameters(GetWS()->argSet(argList));
143 }
144
145 /// Specify the observables.
146 virtual void SetObservables(const RooArgSet& set) {
147 if (!SetHasOnlyParameters(set,"ModelConfig::SetObservables")) return ;
148 fObservablesName=std::string(GetName()) + "_Observables";
149 DefineSetInWS(fObservablesName.c_str(), set);
150 }
151 /// specify the observables
152 /// through a comma-separated list of arguments already in the workspace.
153 virtual void SetObservables(const char *argList) {
154 if(!GetWS()) return;
155 SetObservables(GetWS()->argSet(argList));
156 }
157
158 /// Specify the conditional observables.
159 virtual void SetConditionalObservables(const RooArgSet& set) {
160 if (!SetHasOnlyParameters(set,"ModelConfig::SetConditionalObservables")) return ;
161 fConditionalObsName=std::string(GetName()) + "_ConditionalObservables";
163 }
164 /// Specify the conditional observables
165 /// through a comma-separated list of arguments already in the workspace.
166 virtual void SetConditionalObservables(const char *argList) {
167 if(!GetWS()) return;
168 SetConditionalObservables(GetWS()->argSet(argList));
169 }
170
171 /// Specify the global observables.
172 virtual void SetGlobalObservables(const RooArgSet& set) {
173
174 if (!SetHasOnlyParameters(set,"ModelConfig::SetGlobalObservables")) return ;
175
176 // make global observables constant
177 for (auto *arg : set){
178 arg->setAttribute("Constant", true);
179 }
180
181 fGlobalObsName=std::string(GetName()) + "_GlobalObservables";
182 DefineSetInWS(fGlobalObsName.c_str(), set);
183 }
184 /// Specify the global observables
185 /// through a comma-separated list of arguments already in the workspace.
186 virtual void SetGlobalObservables(const char *argList) {
187 if(!GetWS()) return;
188 SetGlobalObservables(GetWS()->argSet(argList));
189 }
190
191 /// Set parameter values for a particular hypothesis if using a common PDF
192 /// by saving a snapshot in the workspace.
193 virtual void SetSnapshot(const RooArgSet& set);
194
195 /// Specify the name of the PDF in the workspace to be used.
196 virtual void SetPdf(const char* name) {
197 if (! GetWS() ) return;
198
199 if(GetWS()->pdf(name))
200 fPdfName = name;
201 else
202 coutE(ObjectHandling) << "pdf "<<name<< " does not exist in workspace"<<std::endl;
203 }
204
205 /// Specify the name of the PDF in the workspace to be used.
206 virtual void SetPriorPdf(const char* name) {
207 if (! GetWS() ) return;
208
209 if(GetWS()->pdf(name))
211 else
212 coutE(ObjectHandling) << "pdf "<<name<< " does not exist in workspace"<<std::endl;
213 }
214
215
216 /// Specify the name of the dataset in the workspace to be used.
217 virtual void SetProtoData(const char* name){
218 if (! GetWS() ) return;
219
220 if(GetWS()->data(name))
222 else
223 coutE(ObjectHandling) << "dataset "<<name<< " does not exist in workspace"<<std::endl;
224 }
225
226
227 /* getter methods */
228
229
230 /// get model PDF (return nullptr if pdf has not been specified or does not exist)
231 RooAbsPdf * GetPdf() const { return (GetWS()) ? GetWS()->pdf(fPdfName.c_str()) : nullptr; }
232
233 /// get RooArgSet containing the parameter of interest (return nullptr if not existing)
234 const RooArgSet * GetParametersOfInterest() const { return (GetWS()) ? GetWS()->set(fPOIName.c_str()) : nullptr; }
235
236 /// get RooArgSet containing the nuisance parameters (return nullptr if not existing)
237 const RooArgSet * GetNuisanceParameters() const { return (GetWS()) ? GetWS()->set(fNuisParamsName.c_str()) : nullptr; }
238
239 /// get RooArgSet containing the constraint parameters (return nullptr if not existing)
240 const RooArgSet * GetConstraintParameters() const { return (GetWS()) ? GetWS()->set(fConstrParamsName.c_str()) : nullptr; }
241
242 /// get parameters prior pdf (return nullptr if not existing)
243 RooAbsPdf * GetPriorPdf() const { return (GetWS()) ? GetWS()->pdf(fPriorPdfName.c_str()) : nullptr; }
244
245 /// get RooArgSet for observables (return nullptr if not existing)
246 const RooArgSet * GetObservables() const { return (GetWS()) ? GetWS()->set(fObservablesName.c_str()) : nullptr; }
247
248 /// get RooArgSet for conditional observables (return nullptr if not existing)
249 const RooArgSet * GetConditionalObservables() const { return (GetWS()) ? GetWS()->set(fConditionalObsName.c_str()) : nullptr; }
250
251 /// get RooArgSet for global observables (return nullptr if not existing)
252 const RooArgSet * GetGlobalObservables() const { return (GetWS()) ? GetWS()->set(fGlobalObsName.c_str()) : nullptr; }
253
254 /// get Proto data set (return nullptr if not existing)
255 RooAbsData * GetProtoData() const { return (GetWS()) ? GetWS()->data(fProtoDataName.c_str()) : nullptr; }
256
257 /// get RooArgSet for parameters for a particular hypothesis (return nullptr if not existing)
258 const RooArgSet * GetSnapshot() const;
259
260 void LoadSnapshot() const;
261
262 RooWorkspace * GetWS() const override;
263 /// alias for GetWS()
264 RooWorkspace * GetWorkspace() const { return GetWS(); }
265
266 void GuessObsAndNuisance(const RooAbsData& data, bool printModelConfig = true);
267
268 /// overload the print method
269 void Print(Option_t* option = "") const override;
270
271protected:
272
273 /// helper function to check that content of a given set is exclusively parameters
274 bool SetHasOnlyParameters(const RooArgSet& set, const char* errorMsgPrefix=nullptr) ;
275
276 /// helper functions to define a set in the WS
277 void DefineSetInWS(const char* name, const RooArgSet& set);
278
279 /// internal function to import Pdf in WS
280 void ImportPdfInWS(const RooAbsPdf & pdf);
281
282 /// internal function to import data in WS
284
285 TRef fRefWS; ///< WS reference used in the file
286
287 std::string fWSName; ///< name of the WS
288
289 std::string fPdfName; ///< name of PDF in workspace
290 std::string fDataName; ///< name of data set in workspace
291 std::string fPOIName; ///< name for RooArgSet specifying parameters of interest
292
293 std::string fNuisParamsName; ///< name for RooArgSet specifying nuisance parameters
294 std::string fConstrParamsName; ///< name for RooArgSet specifying constrained parameters
295 std::string fPriorPdfName; ///< name for RooAbsPdf specifying a prior on the parameters
296
297 std::string fConditionalObsName; ///< name for RooArgSet specifying conditional observables
298 std::string fGlobalObsName; ///< name for RooArgSet specifying global observables
299 std::string fProtoDataName; ///< name for RooArgSet specifying dataset that should be used as proto-data
300
301 std::string fSnapshotName; ///< name for RooArgSet that specifies a particular hypothesis
302
303 std::string fObservablesName; ///< name for RooArgSet specifying observable parameters.
304
305 ClassDefOverride(ModelConfig,5) ///< A class that holds configuration information for a model using a workspace as a store
306
307};
308
309} // end namespace RooStats
310
311
312#endif
#define coutE(a)
Definition: RooMsgService.h:37
const char Option_t
Definition: RtypesCore.h:66
#define ClassDefOverride(name, id)
Definition: Rtypes.h:339
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:61
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:30
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.
Definition: ModelConfig.h:146
std::string fSnapshotName
name for RooArgSet that specifies a particular hypothesis
Definition: ModelConfig.h:301
ModelConfig(const char *name, const char *title, RooWorkspace *ws=nullptr)
Definition: ModelConfig.h:46
void ImportPdfInWS(const RooAbsPdf &pdf)
internal function to import Pdf in WS
std::string fNuisParamsName
name for RooArgSet specifying nuisance parameters
Definition: ModelConfig.h:293
virtual void SetPriorPdf(const RooAbsPdf &pdf)
Set the Prior Pdf, add to the workspace if not already there.
Definition: ModelConfig.h:87
virtual void SetObservables(const char *argList)
specify the observables through a comma-separated list of arguments already in the workspace.
Definition: ModelConfig.h:153
RooAbsData * GetProtoData() const
get Proto data set (return nullptr if not existing)
Definition: ModelConfig.h:255
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.
Definition: ModelConfig.h:127
std::string fWSName
name of the WS
Definition: ModelConfig.h:287
std::string fPriorPdfName
name for RooAbsPdf specifying a prior on the parameters
Definition: ModelConfig.h:295
std::string fDataName
name of data set in workspace
Definition: ModelConfig.h:290
virtual void SetWorkspace(RooWorkspace &ws)
Definition: ModelConfig.h:66
RooWorkspace * GetWorkspace() const
alias for GetWS()
Definition: ModelConfig.h:264
const RooArgSet * GetConditionalObservables() const
get RooArgSet for conditional observables (return nullptr if not existing)
Definition: ModelConfig.h:249
void GuessObsAndNuisance(const RooAbsData &data, bool printModelConfig=true)
Makes sensible guesses of observables, parameters of interest and nuisance parameters if one or multi...
Definition: ModelConfig.cxx:66
virtual void SetProtoData(const char *name)
Specify the name of the dataset in the workspace to be used.
Definition: ModelConfig.h:217
std::string fConditionalObsName
name for RooArgSet specifying conditional observables
Definition: ModelConfig.h:297
ModelConfig(const char *name, RooWorkspace *ws=nullptr)
Definition: ModelConfig.h:40
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return nullptr if not existing)
Definition: ModelConfig.h:252
ModelConfig * Clone(const char *name="") const override
clone
Definition: ModelConfig.h:54
virtual void SetParametersOfInterest(const RooArgSet &set)
Specify parameters of interest.
Definition: ModelConfig.h:100
TRef fRefWS
WS reference used in the file.
Definition: ModelConfig.h:285
void ReplaceWS(RooWorkspace *ws) override
Remove the existing reference to a workspace and replace it with this new one.
Definition: ModelConfig.h:69
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
Definition: ModelConfig.h:234
std::string fPdfName
name of PDF in workspace
Definition: ModelConfig.h:289
std::string fObservablesName
name for RooArgSet specifying observable parameters.
Definition: ModelConfig.h:303
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return nullptr if not existing)
Definition: ModelConfig.h:237
virtual void SetGlobalObservables(const RooArgSet &set)
Specify the global observables.
Definition: ModelConfig.h:172
virtual void SetConstraintParameters(const char *argList)
Specify the constraint parameters through a comma-separated list of arguments already in the workspac...
Definition: ModelConfig.h:140
virtual void SetParameters(const char *argList)
Specify parameters using a list of comma-separated list of arguments already in the workspace.
Definition: ModelConfig.h:107
void LoadSnapshot() const
load the snapshot from ws if it exists
std::string fConstrParamsName
name for RooArgSet specifying constrained parameters
Definition: ModelConfig.h:294
void Print(Option_t *option="") const override
overload the print method
std::string fGlobalObsName
name for RooArgSet specifying global observables
Definition: ModelConfig.h:298
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
Definition: ModelConfig.h:291
virtual void SetParametersOfInterest(const char *argList)
Specify parameters of interest using a comma-separated list of arguments already in the workspace.
Definition: ModelConfig.h:114
const RooArgSet * GetObservables() const
get RooArgSet for observables (return nullptr if not existing)
Definition: ModelConfig.h:246
ModelConfig(RooWorkspace *ws=nullptr)
Definition: ModelConfig.h:34
virtual void SetConditionalObservables(const char *argList)
Specify the conditional observables through a comma-separated list of arguments already in the worksp...
Definition: ModelConfig.h:166
virtual void SetGlobalObservables(const char *argList)
Specify the global observables through a comma-separated list of arguments already in the workspace.
Definition: ModelConfig.h:186
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)
Definition: ModelConfig.h:240
virtual void SetParameters(const RooArgSet &set)
Specify parameters of the PDF.
Definition: ModelConfig.h:93
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.
Definition: ModelConfig.h:206
std::string fProtoDataName
name for RooArgSet specifying dataset that should be used as proto-data
Definition: ModelConfig.h:299
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
Definition: ModelConfig.h:231
virtual void SetNuisanceParameters(const RooArgSet &set)
Specify the nuisance parameters (parameters that are not POI).
Definition: ModelConfig.h:119
virtual void SetConstraintParameters(const RooArgSet &set)
Specify the constraint parameters.
Definition: ModelConfig.h:133
virtual void SetPdf(const char *name)
Specify the name of the PDF in the workspace to be used.
Definition: ModelConfig.h:196
virtual void SetProtoData(RooAbsData &data)
Set the proto DataSet, add to the workspace if not already there.
Definition: ModelConfig.h:75
virtual void SetConditionalObservables(const RooArgSet &set)
Specify the conditional observables.
Definition: ModelConfig.h:159
RooAbsPdf * GetPriorPdf() const
get parameters prior pdf (return nullptr if not existing)
Definition: ModelConfig.h:243
virtual void SetPdf(const RooAbsPdf &pdf)
Set the Pdf, add to the workspace if not already there.
Definition: ModelConfig.h:81
An interface to set and retrieve a workspace.
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:43
RooAbsPdf * pdf(RooStringView name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
RooAbsData * data(RooStringView name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found.
const RooArgSet * set(const char *name)
Return pointer to previously defined named set with given nmame If no such set is found a null pointe...
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
@ ObjectHandling
Definition: RooGlobalFunc.h:63
Namespace for the RooStats classes.
Definition: Asimov.h:19