Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
28 namespace RooStats {
29 
30 class ModelConfig final : public TNamed, public RooWorkspaceHandle {
31 
32 public:
33 
34  ModelConfig(RooWorkspace * ws = 0) :
35  TNamed()
36  {
37  if(ws) SetWS(*ws);
38  }
39 
40  ModelConfig(const char* name, RooWorkspace *ws = 0) :
41  TNamed(name, name)
42  {
43  if(ws) SetWS(*ws);
44  }
45 
46  ModelConfig(const char* name, const char* title, RooWorkspace *ws = 0) :
47  TNamed(name, title)
48  {
49  if(ws) SetWS(*ws);
50  }
51 
52 
53  /// clone
54  virtual 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  virtual 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  virtual void ReplaceWS(RooWorkspace *ws) override {
70  fRefWS = nullptr;
71  SetWS(*ws);
72  }
73 
74  /// Set the proto DataSet, add to the the workspace if not already there
75  virtual void SetProtoData(RooAbsData & data) {
76  ImportDataInWS(data);
77  SetProtoData( data.GetName() );
78  }
79 
80  /// Set the Pdf, add to the 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 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";
162  DefineSetInWS(fConditionalObsName.c_str(), set);
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  RooFIter iter = set.fwdIterator();
178  RooAbsArg *arg = iter.next();
179  while(arg != NULL) {
180  arg->setAttribute("Constant", kTRUE);
181  arg = iter.next();
182  }
183 
184  fGlobalObsName=std::string(GetName()) + "_GlobalObservables";
185  DefineSetInWS(fGlobalObsName.c_str(), set);
186  }
187  /// Specify the global observables
188  /// through a comma-separated list of arguments already in the workspace.
189  virtual void SetGlobalObservables(const char *argList) {
190  if(!GetWS()) return;
191  SetGlobalObservables(GetWS()->argSet(argList));
192  }
193 
194  /// Set parameter values for a particular hypothesis if using a common PDF
195  /// by saving a snapshot in the workspace.
196  virtual void SetSnapshot(const RooArgSet& set);
197 
198  /// Specify the name of the PDF in the workspace to be used.
199  virtual void SetPdf(const char* name) {
200  if (! GetWS() ) return;
201 
202  if(GetWS()->pdf(name))
203  fPdfName = name;
204  else
205  coutE(ObjectHandling) << "pdf "<<name<< " does not exist in workspace"<<std::endl;
206  }
207 
208  /// Specify the name of the PDF in the workspace to be used.
209  virtual void SetPriorPdf(const char* name) {
210  if (! GetWS() ) return;
211 
212  if(GetWS()->pdf(name))
213  fPriorPdfName = name;
214  else
215  coutE(ObjectHandling) << "pdf "<<name<< " does not exist in workspace"<<std::endl;
216  }
217 
218 
219  /// Specify the name of the dataset in the workspace to be used.
220  virtual void SetProtoData(const char* name){
221  if (! GetWS() ) return;
222 
223  if(GetWS()->data(name))
224  fProtoDataName = name;
225  else
226  coutE(ObjectHandling) << "dataset "<<name<< " does not exist in workspace"<<std::endl;
227  }
228 
229 
230  /* getter methods */
231 
232 
233  /// get model PDF (return NULL if pdf has not been specified or does not exist)
234  RooAbsPdf * GetPdf() const { return (GetWS()) ? GetWS()->pdf(fPdfName.c_str()) : 0; }
235 
236  /// get RooArgSet containing the parameter of interest (return NULL if not existing)
237  const RooArgSet * GetParametersOfInterest() const { return (GetWS()) ? GetWS()->set(fPOIName.c_str()) : 0; }
238 
239  /// get RooArgSet containing the nuisance parameters (return NULL if not existing)
240  const RooArgSet * GetNuisanceParameters() const { return (GetWS()) ? GetWS()->set(fNuisParamsName.c_str()) : 0; }
241 
242  /// get RooArgSet containing the constraint parameters (return NULL if not existing)
243  const RooArgSet * GetConstraintParameters() const { return (GetWS()) ? GetWS()->set(fConstrParamsName.c_str()) : 0; }
244 
245  /// get parameters prior pdf (return NULL if not existing)
246  RooAbsPdf * GetPriorPdf() const { return (GetWS()) ? GetWS()->pdf(fPriorPdfName.c_str()) : 0; }
247 
248  /// get RooArgSet for observables (return NULL if not existing)
249  const RooArgSet * GetObservables() const { return (GetWS()) ? GetWS()->set(fObservablesName.c_str()) : 0; }
250 
251  /// get RooArgSet for conditional observables (return NULL if not existing)
252  const RooArgSet * GetConditionalObservables() const { return (GetWS()) ? GetWS()->set(fConditionalObsName.c_str()) : 0; }
253 
254  /// get RooArgSet for global observables (return NULL if not existing)
255  const RooArgSet * GetGlobalObservables() const { return (GetWS()) ? GetWS()->set(fGlobalObsName.c_str()) : 0; }
256 
257  /// get Proto data set (return NULL if not existing)
258  RooAbsData * GetProtoData() const { return (GetWS()) ? GetWS()->data(fProtoDataName.c_str()) : 0; }
259 
260  /// get RooArgSet for parameters for a particular hypothesis (return NULL if not existing)
261  const RooArgSet * GetSnapshot() const;
262 
263  void LoadSnapshot() const;
264 
265  RooWorkspace * GetWS() const override;
266  /// alias for GetWS()
267  RooWorkspace * GetWorkspace() const { return GetWS(); }
268 
269  void GuessObsAndNuisance(const RooAbsData& data);
270 
271  /// overload the print method
272  virtual void Print(Option_t* option = "") const override;
273 
274 protected:
275 
276  /// helper function to check that content of a given set is exclusively parameters
277  Bool_t SetHasOnlyParameters(const RooArgSet& set, const char* errorMsgPrefix=0) ;
278 
279  /// helper functions to define a set in the WS
280  void DefineSetInWS(const char* name, const RooArgSet& set);
281 
282  /// internal function to import Pdf in WS
283  void ImportPdfInWS(const RooAbsPdf & pdf);
284 
285  /// internal function to import data in WS
286  void ImportDataInWS(RooAbsData & data);
287 
288  TRef fRefWS; /// WS reference used in the file
289 
290  std::string fWSName; /// name of the WS
291 
292  std::string fPdfName; /// name of PDF in workspace
293  std::string fDataName; /// name of data set in workspace
294  std::string fPOIName; /// name for RooArgSet specifying parameters of interest
295 
296  std::string fNuisParamsName; /// name for RooArgSet specifying nuisance parameters
297  std::string fConstrParamsName; /// name for RooArgSet specifying constrained parameters
298  std::string fPriorPdfName; /// name for RooAbsPdf specifying a prior on the parameters
299 
300  std::string fConditionalObsName; /// name for RooArgSet specifying conditional observables
301  std::string fGlobalObsName; /// name for RooArgSet specifying global observables
302  std::string fProtoDataName; /// name for RooArgSet specifying dataset that should be used as proto-data
303 
304  std::string fSnapshotName; /// name for RooArgSet that specifies a particular hypothesis
305 
306  std::string fObservablesName; /// name for RooArgSet specifying observable parameters.
307 
308  ClassDefOverride(ModelConfig,5) /// A class that holds configuration information for a model using a workspace as a store
309 
310 };
311 
312 } // end namespace RooStats
313 
314 
315 #endif