Logo ROOT   6.18/05
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*/
19
21
22#include "TROOT.h"
23
24#include "RooMsgService.h"
25
27
28#include <sstream>
29
30
32
33using namespace std;
34
35namespace RooStats {
36
37////////////////////////////////////////////////////////////////////////////////
38/// Makes sensible guesses of observables, parameters of interest
39/// and nuisance parameters.
40///
41/// Defaults:
42/// observables: determined from data,
43/// global observables = explicit obs - obs from data
44/// parameters of interest: empty,
45/// nuisance parameters: all parameters except parameters of interest
46///
47/// We use NULL to mean not set, so we don't want to fill
48/// with empty RooArgSets/
49
51
52 // observables
53 if (!GetObservables()) {
54 const RooArgSet * obs = GetPdf()->getObservables(data);
55 SetObservables(*obs);
56 delete obs;
57 }
58 // global observables
59 if (!GetGlobalObservables()) {
61 const RooArgSet * obs = GetPdf()->getObservables(data);
62 co.remove(*obs);
64 if(co.getSize()>0)
66
67 // TODO BUG This does not work as observables with the same name are already in the workspace.
68 /*
69 RooArgSet o(*GetObservables());
70 o.remove(co);
71 SetObservables(o);
72 */
73 delete obs;
74 }
75
76 // parameters
77 // if (!GetParametersOfInterest()) {
78 // SetParametersOfInterest(RooArgSet());
79 // }
80 if (!GetNuisanceParameters()) {
81 const RooArgSet * params = GetPdf()->getParameters(data);
82 RooArgSet p(*params);
85 if(p.getSize()>0)
87 delete params;
88 }
89
90 // print Modelconfig as an info message
91
92 std::ostream& oldstream = RooPrintable::defaultPrintStream(&ccoutI(InputArguments));
93 Print();
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// print contents of Model on the default print stream
99/// It can be changed using RooPrintable
100
102 ostream& os = RooPrintable::defaultPrintStream();
103
104 os << endl << "=== Using the following for " << GetName() << " ===" << endl;
105
106
107 // args
108 if(GetObservables()){
109 os << "Observables: ";
110 GetObservables()->Print("");
111 }
113 os << "Parameters of Interest: ";
115 }
117 os << "Nuisance Parameters: ";
119 }
121 os << "Global Observables: ";
123 }
125 os << "Constraint Parameters: ";
127 }
129 os << "Conditional Observables: ";
131 }
132 if(GetProtoData()){
133 os << "Proto Data: ";
134 GetProtoData()->Print("");
135 }
136
137 // pdfs
138 if(GetPdf()) {
139 os << "PDF: ";
140 GetPdf()->Print("");
141 }
142 if(GetPriorPdf()) {
143 os << "Prior PDF: ";
144 GetPriorPdf()->Print("");
145 }
146
147 // snapshot
148 const RooArgSet * snapshot = GetSnapshot();
149 if(snapshot) {
150 os << "Snapshot: " << endl;
151 snapshot->Print("v");
152 delete snapshot;
153 }
154
155 os << endl;
156}
157
158////////////////////////////////////////////////////////////////////////////////
159/// If a workspace already exists in this ModelConfig, RooWorkspace::merge(ws) will be called
160/// on the existing workspace.
161
163 if( !fRefWS.GetObject() ) {
164 fRefWS = &ws;
165 fWSName = ws.GetName();
166 }
167 else{
170 GetWS()->merge(ws);
172 }
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// get from TRef
177
179 RooWorkspace *ws = dynamic_cast<RooWorkspace *>(fRefWS.GetObject() );
180 if(!ws) {
181 coutE(ObjectHandling) << "workspace not set" << endl;
182 return NULL;
183 }
184 return ws;
185}
186
187////////////////////////////////////////////////////////////////////////////////
188/// save snapshot in the workspace
189/// and use values passed with the set
190
192 if ( !GetWS() ) return;
193
195 if (fSnapshotName.size() > 0) fSnapshotName += "_";
196 fSnapshotName += set.GetName();
197 if (fSnapshotName.size() > 0) fSnapshotName += "_";
198 fSnapshotName += "snapshot";
199 GetWS()->saveSnapshot(fSnapshotName.c_str(), set, true); // import also the given parameter values
200 DefineSetInWS(fSnapshotName.c_str(), set);
201}
202
203////////////////////////////////////////////////////////////////////////////////
204/// Load the snapshot from ws and return the corresponding set with the snapshot values.
205/// User must delete returned RooArgSet.
206
208 if ( !GetWS() ) return 0;
209 if (!fSnapshotName.length()) return 0;
210 // calling loadSnapshot will also copy the current parameter values in the workspaces
211 // since we do not want to change the model parameters - we restore the previous ones
212 if (! GetWS()->set(fSnapshotName.c_str() ) )return 0;
213 RooArgSet snapshotVars(*GetWS()->set(fSnapshotName.c_str() ) );
214 if (snapshotVars.getSize() == 0) return 0;
215 // make my snapshot which will contain a copy of the snapshot variables
216 RooArgSet tempSnapshot;
217 snapshotVars.snapshot(tempSnapshot);
218 // load snapshot value from the workspace
219 if (!(GetWS()->loadSnapshot(fSnapshotName.c_str())) ) return 0;
220 // by doing this snapshotVars will have the snapshot values - make the snapshot to return
221 const RooArgSet * modelSnapshot = dynamic_cast<const RooArgSet*>( snapshotVars.snapshot());
222 // restore now the variables of snapshot in ws to their original values
223 // need to const cast since assign is not const (but in reality in just assign values and does not change the set)
224 // and anyway the set is const
225 snapshotVars.assignFast(tempSnapshot);
226 return modelSnapshot;
227}
228
229////////////////////////////////////////////////////////////////////////////////
230/// load the snapshot from ws if it exists
231
233 if ( !GetWS() ) return;
234 GetWS()->loadSnapshot(fSnapshotName.c_str());
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// helper functions to avoid code duplication
239
240void ModelConfig::DefineSetInWS(const char* name, const RooArgSet& set) {
241 if ( !GetWS() ) return;
242
243 const RooArgSet * prevSet = GetWS()->set(name);
244 if ( prevSet ) {
245 //be careful not to remove passed set in case it is the same updated
246 if (prevSet != &set)
247 GetWS()->removeSet(name);
248 }
249
250 // suppress warning when we re-define a previously defined set (when set == prevSet )
251 // and set is not removed in that case
254
255
256 GetWS()->defineSet(name, set,true);
257
259
260}
261
262////////////////////////////////////////////////////////////////////////////////
263/// internal function to import Pdf in WS
264
266 if ( !GetWS() ) return;
267
268 if (! GetWS()->pdf( pdf.GetName() ) ){
273 }
274}
275
276////////////////////////////////////////////////////////////////////////////////
277/// internal function to import data in WS
278
280 if ( !GetWS() ) return;
281
282 if (! GetWS()->data( data.GetName() ) ){
285 GetWS()->import(data);
287 }
288}
289
290////////////////////////////////////////////////////////////////////////////////
291
292Bool_t ModelConfig::SetHasOnlyParameters(const RooArgSet& set, const char* errorMsgPrefix) {
293
294 RooArgSet nonparams ;
295 RooFIter iter = set.fwdIterator() ;
296 RooAbsArg* arg ;
297 while ((arg=iter.next())) {
298 if (!arg->isFundamental()) {
299 nonparams.add(*arg) ;
300 }
301 }
302
303 if (errorMsgPrefix && nonparams.getSize()>0) {
304 cout << errorMsgPrefix << " ERROR: specified set contains non-parameters: " << nonparams << endl ;
305 }
306 return (nonparams.getSize()==0) ;
307 }
308
309} // end namespace RooStats
#define coutE(a)
Definition: RooMsgService.h:34
#define ccoutI(a)
Definition: RooMsgService.h:38
bool Bool_t
Definition: RtypesCore.h:59
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:365
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:70
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Return the observables of this pdf given a set of observables.
Definition: RooAbsArg.h:240
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:206
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsArg.h:272
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:543
RooFIter fwdIterator() const R__SUGGEST_ALTERNATIVE("begin()
One-time forward iterator.
Int_t getSize() const
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:37
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:162
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:134
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:140
std::string fSnapshotName
name for RooArgSet specifying dataset that should be used as proto-data
Definition: ModelConfig.h:299
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:252
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:285
const RooArgSet * GetConditionalObservables() const
get RooArgSet for conditional observables (return NULL if not existing)
Definition: ModelConfig.h:246
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return NULL if not existing)
Definition: ModelConfig.h:249
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:231
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return NULL if not existing)
Definition: ModelConfig.h:234
virtual void SetGlobalObservables(const RooArgSet &set)
specify the global observables
Definition: ModelConfig.h:166
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:243
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:237
void GuessObsAndNuisance(const RooAbsData &data)
guesses Observables and ParametersOfInterest if not already set
Definition: ModelConfig.cxx:50
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:228
virtual void SetNuisanceParameters(const RooArgSet &set)
specify the nuisance parameters (e.g. the rest of the parameters)
Definition: ModelConfig.h:114
RooAbsPdf * GetPriorPdf() const
get parameters prior pdf (return NULL if not existing)
Definition: ModelConfig.h:240
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 parameters 'params' If importValues ...
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:377
RooCmdArg RecycleConflictNodes(Bool_t flag=kTRUE)
@ InputArguments
Definition: RooGlobalFunc.h:58
@ ObjectHandling
Definition: RooGlobalFunc.h:58
Namespace for the RooStats classes.
Definition: Asimov.h:20
void RemoveConstantParameters(RooArgSet *set)
Definition: RooStatsUtils.h:62
void ws()
Definition: ws.C:66