Logo ROOT   6.14/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 
14 ModelConfig is a simple class that holds configuration information specifying how a model
15 should be used in the context of various RooStats tools. A single model can be used
16 in different ways, and this class should carry all that is needed to specify how it should be used.
17 ModelConfig requires a workspace to be set.
18 */
19 
20 #include "RooStats/ModelConfig.h"
21 
22 #include "TROOT.h"
23 
24 #include "RooMsgService.h"
25 
26 #include "RooStats/RooStatsUtils.h"
27 
28 #include <sstream>
29 
30 
32 
33 using namespace std;
34 
35 namespace 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 
50 void ModelConfig::GuessObsAndNuisance(const RooAbsData& data) {
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()) {
60  RooArgSet co(*GetObservables());
61  const RooArgSet * obs = GetPdf()->getObservables(data);
62  co.remove(*obs);
64  if(co.getSize()>0)
65  SetGlobalObservables(co);
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);
83  p.remove(*GetParametersOfInterest());
85  if(p.getSize()>0)
86  SetNuisanceParameters(p);
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  }
112  if(GetParametersOfInterest()) {
113  os << "Parameters of Interest: ";
114  GetParametersOfInterest()->Print("");
115  }
116  if(GetNuisanceParameters()){
117  os << "Nuisance Parameters: ";
118  GetNuisanceParameters()->Print("");
119  }
120  if(GetGlobalObservables()){
121  os << "Global Observables: ";
122  GetGlobalObservables()->Print("");
123  }
124  if(GetConstraintParameters()){
125  os << "Constraint Parameters: ";
126  GetConstraintParameters()->Print("");
127  }
128  if(GetConditionalObservables()){
129  os << "Conditional Observables: ";
130  GetConditionalObservables()->Print("");
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 /// set a workspace that owns all the necessary components for the analysis
160 
161 void ModelConfig::SetWS(RooWorkspace & ws) {
162  if( !fRefWS.GetObject() ) {
163  fRefWS = &ws;
164  fWSName = ws.GetName();
165  }
166  else{
169  GetWS()->merge(ws);
171  }
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// get from TRef
176 
177 RooWorkspace * ModelConfig::GetWS() const {
178  RooWorkspace *ws = dynamic_cast<RooWorkspace *>(fRefWS.GetObject() );
179  if(!ws) {
180  coutE(ObjectHandling) << "workspace not set" << endl;
181  return NULL;
182  }
183  return ws;
184 }
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// save snapshot in the workspace
188 /// and use values passed with the set
189 
190 void ModelConfig::SetSnapshot(const RooArgSet& set) {
191  if ( !GetWS() ) return;
192 
193  fSnapshotName = GetName();
194  if (fSnapshotName.size() > 0) fSnapshotName += "_";
195  fSnapshotName += set.GetName();
196  if (fSnapshotName.size() > 0) fSnapshotName += "_";
197  fSnapshotName += "snapshot";
198  GetWS()->saveSnapshot(fSnapshotName.c_str(), set, true); // import also the given parameter values
199  DefineSetInWS(fSnapshotName.c_str(), set);
200 }
201 
202 ////////////////////////////////////////////////////////////////////////////////
203 /// Load the snapshot from ws and return the corresponding set with the snapshot values.
204 /// User must delete returned RooArgSet.
205 
206 const RooArgSet * ModelConfig::GetSnapshot() const{
207  if ( !GetWS() ) return 0;
208  if (!fSnapshotName.length()) return 0;
209  // calling loadSnapshot will also copy the current parameter values in the workspaces
210  // since we do not want to change the model parameters - we restore the previous ones
211  if (! GetWS()->set(fSnapshotName.c_str() ) )return 0;
212  RooArgSet snapshotVars(*GetWS()->set(fSnapshotName.c_str() ) );
213  if (snapshotVars.getSize() == 0) return 0;
214  // make my snapshot which will contain a copy of the snapshot variables
215  RooArgSet tempSnapshot;
216  snapshotVars.snapshot(tempSnapshot);
217  // load snapshot value from the workspace
218  if (!(GetWS()->loadSnapshot(fSnapshotName.c_str())) ) return 0;
219  // by doing this snapshotVars will have the snapshot values - make the snapshot to return
220  const RooArgSet * modelSnapshot = dynamic_cast<const RooArgSet*>( snapshotVars.snapshot());
221  // restore now the variables of snapshot in ws to their original values
222  // need to const cast since assign is not const (but in reality in just assign values and does not change the set)
223  // and anyway the set is const
224  snapshotVars.assignFast(tempSnapshot);
225  return modelSnapshot;
226 }
227 
228 ////////////////////////////////////////////////////////////////////////////////
229 /// load the snapshot from ws if it exists
230 
231 void ModelConfig::LoadSnapshot() const{
232  if ( !GetWS() ) return;
233  GetWS()->loadSnapshot(fSnapshotName.c_str());
234 }
235 
236 ////////////////////////////////////////////////////////////////////////////////
237 /// helper functions to avoid code duplication
238 
239 void ModelConfig::DefineSetInWS(const char* name, const RooArgSet& set) {
240  if ( !GetWS() ) return;
241 
242  const RooArgSet * prevSet = GetWS()->set(name);
243  if ( prevSet ) {
244  //be careful not to remove passed set in case it is the same updated
245  if (prevSet != &set)
246  GetWS()->removeSet(name);
247  }
248 
249  // suppress warning when we re-define a previously defined set (when set == prevSet )
250  // and set is not removed in that case
253 
254 
255  GetWS()->defineSet(name, set,true);
256 
258 
259 }
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 /// internal function to import Pdf in WS
263 
264 void ModelConfig::ImportPdfInWS(const RooAbsPdf & pdf) {
265  if ( !GetWS() ) return;
266 
267  if (! GetWS()->pdf( pdf.GetName() ) ){
270  GetWS()->import(pdf, RooFit::RecycleConflictNodes());
272  }
273 }
274 
275 ////////////////////////////////////////////////////////////////////////////////
276 /// internal function to import data in WS
277 
278 void ModelConfig::ImportDataInWS(RooAbsData & data) {
279  if ( !GetWS() ) return;
280 
281  if (! GetWS()->data( data.GetName() ) ){
284  GetWS()->import(data);
286  }
287 }
288 
289 ////////////////////////////////////////////////////////////////////////////////
290 
291 Bool_t ModelConfig::SetHasOnlyParameters(const RooArgSet& set, const char* errorMsgPrefix) {
292 
293  RooArgSet nonparams ;
294  RooFIter iter = set.fwdIterator() ;
295  RooAbsArg* arg ;
296  while ((arg=iter.next())) {
297  if (!arg->isFundamental()) {
298  nonparams.add(*arg) ;
299  }
300  }
301 
302  if (errorMsgPrefix && nonparams.getSize()>0) {
303  cout << errorMsgPrefix << " ERROR: specified set contains non-parameters: " << nonparams << endl ;
304  }
305  return (nonparams.getSize()==0) ;
306  }
307 
308 } // end namespace RooStats
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
std::string GetName(const std::string &scope_name)
Definition: Cppyy.cxx:145
#define coutE(a)
Definition: RooMsgService.h:34
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:30
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:86
void ws()
Definition: ws.C:62
const char Option_t
Definition: RtypesCore.h:62
RooFit::MsgLevel globalKillBelow() const
bool Bool_t
Definition: RtypesCore.h:59
static RooMsgService & instance()
Return reference to singleton instance.
STL namespace.
RooCmdArg RecycleConflictNodes(Bool_t flag=kTRUE)
#define ccoutI(a)
Definition: RooMsgService.h:38
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.
virtual Bool_t isFundamental() const
Definition: RooAbsArg.h:157
Int_t getSize() const
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
void setGlobalKillBelow(RooFit::MsgLevel level)
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
RooAbsArg * next()
static std::ostream & defaultPrintStream(std::ostream *os=0)
Return a reference to the current default stream to use in Print().
Namespace for the RooStats classes.
Definition: Asimov.h:20
#define ClassImp(name)
Definition: Rtypes.h:359
void Print(std::ostream &os, const OptionType &opt)
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
void RemoveConstantParameters(RooArgSet *set)
Definition: RooStatsUtils.h:62
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
char name[80]
Definition: TGX11.cxx:109
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:43