Logo ROOT  
Reference Guide
ModelConfig.cxx
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/** \class RooStats::ModelConfig
12 \ingroup Roostats
13
14ModelConfig is a simple class that holds configuration information specifying how a model
15should be used in the context of various RooStats tools. A single model can be used
16in different ways, and this class should carry all that is needed to specify how it should be used.
17ModelConfig requires a workspace to be set.
18
19A ModelConfig holds sets of parameters of the likelihood function that have different interpretations:
20- **Parameter of interest** Parameters that are measured (*i.e.* fitted).
21- **Nuisance parameters** Parameters that are fitted, but their post-fit value is not interesting. Often,
22they might be constrained because external knowledge about them exists, *e.g.* from external measurements.
23- **Constraint parameters** No direct use in RooFit/RooStats. Can be used by the user for bookkeeping.
24- **Observables** Parameters that have been measured externally, *i.e.* they exist in a dataset. These are not fitted,
25but read during fitting from the entries of a dataset.
26- **Conditional observables** Observables that are not integrated when the normalisation of the PDF is calculated.
27See *e.g.* `rf306_condpereventerrors` in the RooFit tutorials.
28- **Global observables** Observables that to the fit look like "constant" values, *i.e.* they are not being
29fitted and they are not loaded from a dataset, but some knowledge exists that allows to set them to a
30specific value. Examples:
31-- A signal efficiency measured in a Monte Carlo study.
32-- When constraining a parameter \f$ b \f$, the target value (\f$ b_0 \f$) that this parameter is constrained to:
33\f[
34 \mathrm{Constraint}_b = \mathrm{Gauss}(b_0 \, | \, b, 0.2)
35\f]
36*/
37
39
40#include "TROOT.h"
41
42#include "RooMsgService.h"
43
45
46#include <sstream>
47
48
50
51using namespace std;
52
53namespace RooStats {
54
55////////////////////////////////////////////////////////////////////////////////
56/// Makes sensible guesses of observables, parameters of interest
57/// and nuisance parameters if one or multiple have been set by the creator of this ModelConfig.
58///
59/// Defaults:
60/// - Observables: determined from data,
61/// - Global observables: explicit obs - obs from data - constant observables
62/// - Parameters of interest: empty,
63/// - Nuisance parameters: all parameters except parameters of interest
64///
65/// We use NULL to mean not set, so we don't want to fill
66/// with empty RooArgSets.
67
68void ModelConfig::GuessObsAndNuisance(const RooAbsData& data, bool printModelConfig) {
69
70 // observables
71 if (!GetObservables()) {
72 const RooArgSet * obs = GetPdf()->getObservables(data);
73 SetObservables(*obs);
74 delete obs;
75 }
76 // global observables
77 if (!GetGlobalObservables()) {
79 const RooArgSet * obs = GetPdf()->getObservables(data);
80 co.remove(*obs);
82 if(co.getSize()>0)
84
85 // TODO BUG This does not work as observables with the same name are already in the workspace.
86 /*
87 RooArgSet o(*GetObservables());
88 o.remove(co);
89 SetObservables(o);
90 */
91 delete obs;
92 }
93
94 // parameters
95 // if (!GetParametersOfInterest()) {
96 // SetParametersOfInterest(RooArgSet());
97 // }
98 if (!GetNuisanceParameters()) {
99 const RooArgSet * params = GetPdf()->getParameters(data);
100 RooArgSet p(*params);
103 if(p.getSize()>0)
105 delete params;
106 }
107
108 // print Modelconfig as an info message
109
110 if (printModelConfig) {
111 std::ostream& oldstream = RooPrintable::defaultPrintStream(&ccoutI(InputArguments));
112 Print();
114 }
115}
116
117////////////////////////////////////////////////////////////////////////////////
118/// print contents of Model on the default print stream
119/// It can be changed using RooPrintable
120
122 ostream& os = RooPrintable::defaultPrintStream();
123
124 os << endl << "=== Using the following for " << GetName() << " ===" << endl;
125
126
127 // args
128 if(GetObservables()){
129 os << "Observables: ";
130 GetObservables()->Print("");
131 }
133 os << "Parameters of Interest: ";
135 }
137 os << "Nuisance Parameters: ";
139 }
141 os << "Global Observables: ";
143 }
145 os << "Constraint Parameters: ";
147 }
149 os << "Conditional Observables: ";
151 }
152 if(GetProtoData()){
153 os << "Proto Data: ";
154 GetProtoData()->Print("");
155 }
156
157 // pdfs
158 if(GetPdf()) {
159 os << "PDF: ";
160 GetPdf()->Print("");
161 }
162 if(GetPriorPdf()) {
163 os << "Prior PDF: ";
164 GetPriorPdf()->Print("");
165 }
166
167 // snapshot
168 const RooArgSet * snapshot = GetSnapshot();
169 if(snapshot) {
170 os << "Snapshot: " << endl;
171 snapshot->Print("v");
172 delete snapshot;
173 }
174
175 os << endl;
176}
177
178////////////////////////////////////////////////////////////////////////////////
179/// If a workspace already exists in this ModelConfig, RooWorkspace::merge(ws) will be called
180/// on the existing workspace.
181
183 if( !fRefWS.GetObject() ) {
184 fRefWS = &ws;
185 fWSName = ws.GetName();
186 }
187 else{
190 GetWS()->merge(ws);
192 }
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// get from TRef
197
199 RooWorkspace *ws = dynamic_cast<RooWorkspace *>(fRefWS.GetObject() );
200 if(!ws) {
201 coutE(ObjectHandling) << "workspace not set" << endl;
202 return NULL;
203 }
204 return ws;
205}
206
207////////////////////////////////////////////////////////////////////////////////
208/// save snapshot in the workspace
209/// and use values passed with the set
210
212 if ( !GetWS() ) return;
213
215 if (fSnapshotName.size() > 0) fSnapshotName += "_";
216 fSnapshotName += set.GetName();
217 if (fSnapshotName.size() > 0) fSnapshotName += "_";
218 fSnapshotName += "snapshot";
219 GetWS()->saveSnapshot(fSnapshotName.c_str(), set, true); // import also the given parameter values
220 DefineSetInWS(fSnapshotName.c_str(), set);
221}
222
223////////////////////////////////////////////////////////////////////////////////
224/// Load the snapshot from ws and return the corresponding set with the snapshot values.
225/// User must delete returned RooArgSet.
226
228 if ( !GetWS() ) return 0;
229 if (!fSnapshotName.length()) return 0;
230 // calling loadSnapshot will also copy the current parameter values in the workspaces
231 // since we do not want to change the model parameters - we restore the previous ones
232 if (! GetWS()->set(fSnapshotName.c_str() ) )return 0;
233 RooArgSet snapshotVars(*GetWS()->set(fSnapshotName.c_str() ) );
234 if (snapshotVars.getSize() == 0) return 0;
235 // make my snapshot which will contain a copy of the snapshot variables
236 RooArgSet tempSnapshot;
237 snapshotVars.snapshot(tempSnapshot);
238 // load snapshot value from the workspace
239 if (!(GetWS()->loadSnapshot(fSnapshotName.c_str())) ) return 0;
240 // by doing this snapshotVars will have the snapshot values - make the snapshot to return
241 const RooArgSet * modelSnapshot = dynamic_cast<const RooArgSet*>( snapshotVars.snapshot());
242 // restore now the variables of snapshot in ws to their original values
243 // need to const cast since assign is not const (but in reality in just assign values and does not change the set)
244 // and anyway the set is const
245 snapshotVars.assignFast(tempSnapshot);
246 return modelSnapshot;
247}
248
249////////////////////////////////////////////////////////////////////////////////
250/// load the snapshot from ws if it exists
251
253 if ( !GetWS() ) return;
254 GetWS()->loadSnapshot(fSnapshotName.c_str());
255}
256
257////////////////////////////////////////////////////////////////////////////////
258/// helper functions to avoid code duplication
259
260void ModelConfig::DefineSetInWS(const char* name, const RooArgSet& set) {
261 if ( !GetWS() ) return;
262
263 const RooArgSet * prevSet = GetWS()->set(name);
264 if ( prevSet ) {
265 //be careful not to remove passed set in case it is the same updated
266 if (prevSet != &set)
267 GetWS()->removeSet(name);
268 }
269
270 // suppress warning when we re-define a previously defined set (when set == prevSet )
271 // and set is not removed in that case
274
275
276 GetWS()->defineSet(name, set,true);
277
279
280}
281
282////////////////////////////////////////////////////////////////////////////////
283/// internal function to import Pdf in WS
284
286 if ( !GetWS() ) return;
287
288 if (! GetWS()->pdf( pdf.GetName() ) ){
293 }
294}
295
296////////////////////////////////////////////////////////////////////////////////
297/// internal function to import data in WS
298
300 if ( !GetWS() ) return;
301
302 if (! GetWS()->data( data.GetName() ) ){
305 GetWS()->import(data);
307 }
308}
309
310////////////////////////////////////////////////////////////////////////////////
311
312Bool_t ModelConfig::SetHasOnlyParameters(const RooArgSet& set, const char* errorMsgPrefix) {
313
314 RooArgSet nonparams ;
315 RooFIter iter = set.fwdIterator() ;
316 RooAbsArg* arg ;
317 while ((arg=iter.next())) {
318 if (!arg->isFundamental()) {
319 nonparams.add(*arg) ;
320 }
321 }
322
323 if (errorMsgPrefix && nonparams.getSize()>0) {
324 cout << errorMsgPrefix << " ERROR: specified set contains non-parameters: " << nonparams << endl ;
325 }
326 return (nonparams.getSize()==0) ;
327 }
328
329} // end namespace RooStats
#define coutE(a)
Definition: RooMsgService.h:33
#define ccoutI(a)
Definition: RooMsgService.h:38
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
char name[80]
Definition: TGX11.cxx:109
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:73
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Return the observables of this pdf given a set of observables.
Definition: RooAbsArg.h:276
virtual Bool_t isFundamental() const
Is this object a fundamental type that can be added to a dataset? Fundamental-type subclasses overrid...
Definition: RooAbsArg.h:243
virtual void Print(Option_t *options=0) const
Print the object to the defaultPrintStream().
Definition: RooAbsArg.h:302
RooArgSet * getParameters(const RooAbsData *data, Bool_t stripDisconnected=kTRUE) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
Definition: RooAbsArg.cxx:544
Int_t getSize() const
RooFIter fwdIterator() const
One-time forward iterator.
void assignFast(const RooAbsCollection &other, Bool_t setValDirty=kTRUE)
Functional equivalent of operator=() but assumes this and other collection have same layout.
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
const char * GetName() const
Returns name of object.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:44
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:175
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:126
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:88
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
RooAbsArg * next()
Return next element or nullptr if at end.
static RooMsgService & instance()
Return reference to singleton instance.
void setGlobalKillBelow(RooFit::MsgLevel level)
RooFit::MsgLevel globalKillBelow() const
static std::ostream & defaultPrintStream(std::ostream *os=0)
Return a reference to the current default stream to use in Print().
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 specifying dataset that should be used as proto-data
Definition: ModelConfig.h:304
void ImportPdfInWS(const RooAbsPdf &pdf)
internal function to import Pdf in WS
RooAbsData * GetProtoData() const
get Proto data set (return NULL if not existing)
Definition: ModelConfig.h:258
void DefineSetInWS(const char *name, const RooArgSet &set)
helper functions to define a set in the WS
std::string fWSName
WS reference used in the file.
Definition: ModelConfig.h:290
const RooArgSet * GetConditionalObservables() const
get RooArgSet for conditional observables (return NULL if not existing)
Definition: ModelConfig.h:252
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:68
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return NULL if not existing)
Definition: ModelConfig.h:255
Bool_t SetHasOnlyParameters(const RooArgSet &set, const char *errorMsgPrefix=0)
helper function to check that content of a given set is exclusively parameters
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return NULL if not existing)
Definition: ModelConfig.h:237
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return NULL if not existing)
Definition: ModelConfig.h:240
virtual void SetGlobalObservables(const RooArgSet &set)
Specify the global observables.
Definition: ModelConfig.h:172
void LoadSnapshot() const
load the snapshot from ws if it exists
virtual void Print(Option_t *option="") const override
overload the print method
void ImportDataInWS(RooAbsData &data)
internal function to import data in WS
virtual void SetWS(RooWorkspace &ws) override
Set a workspace that owns all the necessary components for the analysis.
const RooArgSet * GetObservables() const
get RooArgSet for observables (return NULL if not existing)
Definition: ModelConfig.h:249
const RooArgSet * GetSnapshot() const
get RooArgSet for parameters for a particular hypothesis (return NULL if not existing)
const RooArgSet * GetConstraintParameters() const
get RooArgSet containing the constraint parameters (return NULL if not existing)
Definition: ModelConfig.h:243
RooWorkspace * GetWS() const override
get from TRef
RooAbsPdf * GetPdf() const
get model PDF (return NULL if pdf has not been specified or does not exist)
Definition: ModelConfig.h:234
virtual void SetNuisanceParameters(const RooArgSet &set)
Specify the nuisance parameters (parameters that are not POI).
Definition: ModelConfig.h:119
RooAbsPdf * GetPriorPdf() const
get parameters prior pdf (return NULL if not existing)
Definition: ModelConfig.h:246
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:43
void merge(const RooWorkspace &)
Definition: RooWorkspace.h:96
Bool_t removeSet(const char *name)
Remove a named set from the workspace.
Bool_t defineSet(const char *name, const RooArgSet &aset, Bool_t importMissing=kFALSE)
Define a named RooArgSet with given constituents.
Bool_t saveSnapshot(const char *name, const char *paramNames)
Save snapshot of values and attributes (including "Constant") of given parameters.
Bool_t loadSnapshot(const char *name)
Load the values and attributes of the parameters in the snapshot saved with the given name.
Bool_t import(const RooAbsArg &arg, const RooCmdArg &arg1=RooCmdArg(), const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg(), const RooCmdArg &arg9=RooCmdArg())
Import a RooAbsArg object, e.g.
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...
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TObject * GetObject() const
Return a pointer to the referenced object.
Definition: TRef.cxx:378
RooCmdArg RecycleConflictNodes(Bool_t flag=kTRUE)
MsgLevel
Verbosity level for RooMsgService::StreamConfig in RooMsgService.
Definition: RooGlobalFunc.h:65
@ InputArguments
Definition: RooGlobalFunc.h:68
@ ObjectHandling
Definition: RooGlobalFunc.h:68
Namespace for the RooStats classes.
Definition: Asimov.h:19
void RemoveConstantParameters(RooArgSet *set)
Definition: RooStatsUtils.h:69
void ws()
Definition: ws.C:66