Logo ROOT   6.08/07
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 #include "RooStats/ModelConfig.h"
12 
13 #include "TROOT.h"
14 
15 #ifndef ROO_MSG_SERVICE
16 #include "RooMsgService.h"
17 #endif
18 
19 #ifndef RooStats_RooStatsUtils
20 #include "RooStats/RooStatsUtils.h"
21 #endif
22 
23 #include <sstream>
24 
25 
27 
28 using namespace std;
29 
30 namespace RooStats {
31 
32 void ModelConfig::GuessObsAndNuisance(const RooAbsData& data) {
33  // Makes sensible guesses of observables, parameters of interest
34  // and nuisance parameters.
35  //
36  // Defaults:
37  // observables: determined from data,
38  // global observables = explicit obs - obs from data
39  // parameters of interest: empty,
40  // nuisance parameters: all parameters except parameters of interest
41  //
42  // We use NULL to mean not set, so we don't want to fill
43  // with empty RooArgSets
44 
45  // observables
46  if (!GetObservables()) {
47  const RooArgSet * obs = GetPdf()->getObservables(data);
48  SetObservables(*obs);
49  delete obs;
50  }
51  // global observables
52  if (!GetGlobalObservables()) {
53  RooArgSet co(*GetObservables());
54  const RooArgSet * obs = GetPdf()->getObservables(data);
55  co.remove(*obs);
57  if(co.getSize()>0)
58  SetGlobalObservables(co);
59 
60  // TODO BUG This does not work as observables with the same name are already in the workspace.
61  /*
62  RooArgSet o(*GetObservables());
63  o.remove(co);
64  SetObservables(o);
65  */
66  delete obs;
67  }
68 
69  // parameters
70  // if (!GetParametersOfInterest()) {
71  // SetParametersOfInterest(RooArgSet());
72  // }
73  if (!GetNuisanceParameters()) {
74  const RooArgSet * params = GetPdf()->getParameters(data);
75  RooArgSet p(*params);
76  p.remove(*GetParametersOfInterest());
78  if(p.getSize()>0)
79  SetNuisanceParameters(p);
80  delete params;
81  }
82 
83  // print Modelconfig as an info message
84 
85  std::ostream& oldstream = RooPrintable::defaultPrintStream(&ccoutI(InputArguments));
86  Print();
88 }
89 
91  // print contents of Model on the default print stream
92  // It can be changed using RooPrintable
93  ostream& os = RooPrintable::defaultPrintStream();
94 
95  os << endl << "=== Using the following for " << GetName() << " ===" << endl;
96 
97 
98  // args
99  if(GetObservables()){
100  os << "Observables: ";
101  GetObservables()->Print("");
102  }
103  if(GetParametersOfInterest()) {
104  os << "Parameters of Interest: ";
105  GetParametersOfInterest()->Print("");
106  }
107  if(GetNuisanceParameters()){
108  os << "Nuisance Parameters: ";
109  GetNuisanceParameters()->Print("");
110  }
111  if(GetGlobalObservables()){
112  os << "Global Observables: ";
113  GetGlobalObservables()->Print("");
114  }
115  if(GetConstraintParameters()){
116  os << "Constraint Parameters: ";
117  GetConstraintParameters()->Print("");
118  }
119  if(GetConditionalObservables()){
120  os << "Conditional Observables: ";
121  GetConditionalObservables()->Print("");
122  }
123  if(GetProtoData()){
124  os << "Proto Data: ";
125  GetProtoData()->Print("");
126  }
127 
128  // pdfs
129  if(GetPdf()) {
130  os << "PDF: ";
131  GetPdf()->Print("");
132  }
133  if(GetPriorPdf()) {
134  os << "Prior PDF: ";
135  GetPriorPdf()->Print("");
136  }
137 
138  // snapshot
139  const RooArgSet * snapshot = GetSnapshot();
140  if(snapshot) {
141  os << "Snapshot: " << endl;
142  snapshot->Print("v");
143  delete snapshot;
144  }
145 
146  os << endl;
147 }
148 
149 
150 void ModelConfig::SetWS(RooWorkspace & ws) {
151  // set a workspace that owns all the necessary components for the analysis
152  if( !fRefWS.GetObject() ) {
153  fRefWS = &ws;
154  fWSName = ws.GetName();
155  }
156  else{
159  GetWS()->merge(ws);
161  }
162 }
163 
164 RooWorkspace * ModelConfig::GetWS() const {
165  // get from TRef
166  RooWorkspace *ws = dynamic_cast<RooWorkspace *>(fRefWS.GetObject() );
167  if(!ws) {
168  coutE(ObjectHandling) << "workspace not set" << endl;
169  return NULL;
170  }
171  return ws;
172 }
173 
174 void ModelConfig::SetSnapshot(const RooArgSet& set) {
175  // save snaphot in the workspace
176  // and use values passed with the set
177  if ( !GetWS() ) return;
178 
179  fSnapshotName = GetName();
180  if (fSnapshotName.size() > 0) fSnapshotName += "_";
181  fSnapshotName += set.GetName();
182  if (fSnapshotName.size() > 0) fSnapshotName += "_";
183  fSnapshotName += "snapshot";
184  GetWS()->saveSnapshot(fSnapshotName.c_str(), set, true); // import also the given parameter values
185  DefineSetInWS(fSnapshotName.c_str(), set);
186 }
187 
188 const RooArgSet * ModelConfig::GetSnapshot() const{
189  // Load the snapshot from ws and return the corresponding set with the snapshot values.
190  // User must delete returned RooArgSet.
191  if ( !GetWS() ) return 0;
192  if (!fSnapshotName.length()) return 0;
193  // calling loadSnapshot will also copy the current parameter values in the workspaces
194  // since we do not want to change the model parameters - we restore the previous ones
195  if (! GetWS()->set(fSnapshotName.c_str() ) )return 0;
196  RooArgSet snapshotVars(*GetWS()->set(fSnapshotName.c_str() ) );
197  if (snapshotVars.getSize() == 0) return 0;
198  // make my snapshot which will contain a copy of the snapshot variables
199  RooArgSet tempSnapshot;
200  snapshotVars.snapshot(tempSnapshot);
201  // load snapshot value from the workspace
202  if (!(GetWS()->loadSnapshot(fSnapshotName.c_str())) ) return 0;
203  // by doing this snapshotVars will have the snapshot values - make the snapshot to return
204  const RooArgSet * modelSnapshot = dynamic_cast<const RooArgSet*>( snapshotVars.snapshot());
205  // restore now the variables of snapshot in ws to their original values
206  // need to const cast since assign is not const (but in reality in just assign values and does not change the set)
207  // and anyway the set is const
208  snapshotVars.assignFast(tempSnapshot);
209  return modelSnapshot;
210 }
211 
212 void ModelConfig::LoadSnapshot() const{
213  // load the snapshot from ws if it exists
214  if ( !GetWS() ) return;
215  GetWS()->loadSnapshot(fSnapshotName.c_str());
216 }
217 
218 void ModelConfig::DefineSetInWS(const char* name, const RooArgSet& set) {
219  // helper functions to avoid code duplication
220  if ( !GetWS() ) return;
221 
222  const RooArgSet * prevSet = GetWS()->set(name);
223  if ( prevSet ) {
224  //be careful not to remove passed set in case it is the same updated
225  if (prevSet != &set)
226  GetWS()->removeSet(name);
227  }
228 
229  // suppress warning when we re-define a previously defined set (when set == prevSet )
230  // and set is not removed in that case
233 
234 
235  GetWS()->defineSet(name, set,true);
236 
238 
239 }
240 
241 void ModelConfig::ImportPdfInWS(const RooAbsPdf & pdf) {
242  // internal function to import Pdf in WS
243  if ( !GetWS() ) return;
244 
245  if (! GetWS()->pdf( pdf.GetName() ) ){
248  GetWS()->import(pdf, RooFit::RecycleConflictNodes());
250  }
251 }
252 
253 void ModelConfig::ImportDataInWS(RooAbsData & data) {
254  // internal function to import data in WS
255  if ( !GetWS() ) return;
256 
257  if (! GetWS()->data( data.GetName() ) ){
260  GetWS()->import(data);
262  }
263 }
264 
265 
266  Bool_t ModelConfig::SetHasOnlyParameters(const RooArgSet& set, const char* errorMsgPrefix) {
267 
268  RooArgSet nonparams ;
269  RooFIter iter = set.fwdIterator() ;
270  RooAbsArg* arg ;
271  while ((arg=iter.next())) {
272  if (!arg->isFundamental()) {
273  nonparams.add(*arg) ;
274  }
275  }
276 
277  if (errorMsgPrefix && nonparams.getSize()>0) {
278  cout << errorMsgPrefix << " ERROR: specified set contains non-parameters: " << nonparams << endl ;
279  }
280  return (nonparams.getSize()==0) ;
281  }
282 
283 } // end namespace RooStats
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
std::string GetName(const std::string &scope_name)
Definition: Cppyy.cxx:140
#define coutE(a)
Definition: RooMsgService.h:35
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:52
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
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:39
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:279
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:73
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
#define NULL
Definition: Rtypes.h:82
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:42