Logo ROOT   6.08/07
Reference Guide
ProposalHelper.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Authors: Kevin Belasco 7/22/2009
3 // Authors: Kyle Cranmer 7/22/2009
4 /*************************************************************************
5  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 ////////////////////////////////////////////////////////////////////////////////
13 
14 /*
15 BEGIN_HTML
16 END_HTML
17 */
18 //_________________________________________________
19 
20 #ifndef ROOT_Rtypes
21 #include "Rtypes.h"
22 #endif
23 #ifndef RooStats_ProposalHelper
25 #endif
26 #ifndef ROOSTATS_PdfProposal
27 #include "RooStats/PdfProposal.h"
28 #endif
29 #ifndef RooStats_RooStatsUtils
30 #include "RooStats/RooStatsUtils.h"
31 #endif
32 #ifndef ROO_ARG_SET
33 #include "RooArgSet.h"
34 #endif
35 #ifndef ROO_DATA_SET
36 #include "RooDataSet.h"
37 #endif
38 #ifndef ROO_ABS_PDF
39 #include "RooAbsPdf.h"
40 #endif
41 #ifndef ROO_ADD_PDF
42 #include "RooAddPdf.h"
43 #endif
44 #ifndef ROO_KEYS_PDF
45 #include "RooNDKeysPdf.h"
46 #endif
47 #ifndef ROO_UNIFORM
48 #include "RooUniform.h"
49 #endif
50 #ifndef ROO_MSG_SERVICE
51 #include "RooMsgService.h"
52 #endif
53 #ifndef ROO_REAL_VAR
54 #include "RooRealVar.h"
55 #endif
56 #ifndef ROOT_TIterator
57 #include "TIterator.h"
58 #endif
59 #ifndef ROO_MULTI_VAR_GAUSSIAN
60 #include "RooMultiVarGaussian.h"
61 #endif
62 #ifndef ROO_CONST_VAR
63 #include "RooConstVar.h"
64 #endif
65 #ifndef ROOT_TString
66 #include "TString.h"
67 #endif
68 
69 #include <map>
70 
71 namespace RooStats {
72  class ProposalFunction;
73 }
74 
76 
77 using namespace RooFit;
78 using namespace RooStats;
79 using namespace std;
80 
81 //static const Double_t DEFAULT_UNI_FRAC = 0.10;
82 static const Double_t DEFAULT_CLUES_FRAC = 0.20;
83 //static const Double_t SIGMA_RANGE_DIVISOR = 6;
84 static const Double_t SIGMA_RANGE_DIVISOR = 5;
85 //static const Int_t DEFAULT_CACHE_SIZE = 100;
86 //static const Option_t* CLUES_OPTIONS = "a";
87 
89 {
90  fPdfProp = new PdfProposal();
91  fVars = NULL;
92  fOwnsPdfProp = kTRUE;
93  fOwnsPdf = kFALSE;
94  fOwnsCluesPdf = kFALSE;
95  fOwnsVars = kFALSE;
96  fUseUpdates = kFALSE;
97  fPdf = NULL;
98  fSigmaRangeDivisor = SIGMA_RANGE_DIVISOR;
99  fCluesPdf = NULL;
100  fUniformPdf = NULL;
101  fClues = NULL;
102  fCovMatrix = NULL;
103  fCluesFrac = -1;
104  fUniFrac = -1;
105  fCacheSize = -1;
106  fCluesOptions = NULL;
107 }
108 
110 {
111  if (fPdf == NULL)
112  CreatePdf();
113  // kbelasco: check here for memory leaks: does RooAddPdf make copies or
114  // take ownership of components, coeffs
115  RooArgList* components = new RooArgList();
116  RooArgList* coeffs = new RooArgList();
117  if (fCluesPdf == NULL)
118  CreateCluesPdf();
119  if (fCluesPdf != NULL) {
120  if (fCluesFrac < 0)
121  fCluesFrac = DEFAULT_CLUES_FRAC;
122  printf("added clues from dataset %s with fraction %g\n",
123  fClues->GetName(), fCluesFrac);
124  components->add(*fCluesPdf);
125  coeffs->add(RooConst(fCluesFrac));
126  }
127  if (fUniFrac > 0.) {
128  CreateUniformPdf();
129  components->add(*fUniformPdf);
130  coeffs->add(RooConst(fUniFrac));
131  }
132  components->add(*fPdf);
133  RooAddPdf* addPdf = new RooAddPdf("proposalFunction", "Proposal Density",
134  *components, *coeffs);
135  fPdfProp->SetPdf(*addPdf);
136  fPdfProp->SetOwnsPdf(kTRUE);
137  if (fCacheSize > 0)
138  fPdfProp->SetCacheSize(fCacheSize);
139  fOwnsPdfProp = kFALSE;
140  return fPdfProp;
141 }
142 
144 {
145  // kbelasco: check here for memory leaks:
146  // does RooMultiVarGaussian make copies of xVec and muVec?
147  // or should we delete them?
148  if (fVars == NULL) {
149  coutE(InputArguments) << "ProposalHelper::CreatePdf(): " <<
150  "Variables to create proposal function for are not set." << endl;
151  return;
152  }
153  RooArgList* xVec = new RooArgList();
154  RooArgList* muVec = new RooArgList();
155  TIterator* it = fVars->createIterator();
156  RooRealVar* r;
157  RooRealVar* clone;
158  while ((r = (RooRealVar*)it->Next()) != NULL) {
159  xVec->add(*r);
160  TString cloneName = TString::Format("%s%s", "mu__", r->GetName());
161  clone = (RooRealVar*)r->clone(cloneName.Data());
162  muVec->add(*clone);
163  if (fUseUpdates)
164  fPdfProp->AddMapping(*clone, *r);
165  }
166  if (fCovMatrix == NULL)
167  CreateCovMatrix(*xVec);
168  fPdf = new RooMultiVarGaussian("mvg", "MVG Proposal", *xVec, *muVec,
169  *fCovMatrix);
170  delete xVec;
171  delete muVec;
172  delete it;
173 }
174 
176 {
177  Int_t size = xVec.getSize();
178  fCovMatrix = new TMatrixDSym(size);
179  RooRealVar* r;
180  for (Int_t i = 0; i < size; i++) {
181  r = (RooRealVar*)xVec.at(i);
182  Double_t range = r->getMax() - r->getMin();
183  (*fCovMatrix)(i,i) = range / fSigmaRangeDivisor;
184  }
185 }
186 
188 {
189  if (fClues != NULL) {
190  if (fCluesOptions == NULL)
191  fCluesPdf = new RooNDKeysPdf("cluesPdf", "Clues PDF", *fVars, *fClues);
192  else
193  fCluesPdf = new RooNDKeysPdf("cluesPdf", "Clues PDF", *fVars, *fClues,
194  fCluesOptions);
195  }
196 }
197 
199 {
200  fUniformPdf = new RooUniform("uniform", "Uniform Proposal PDF",
201  RooArgSet(*fVars));
202 }
virtual Double_t getMin(const char *name=0) const
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
ProposalFunction is an interface for all proposal functions that would be used with a Markov Chain Mo...
#define coutE(a)
Definition: RooMsgService.h:35
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
virtual Double_t getMax(const char *name=0) const
virtual TObject * clone(const char *newname) const
Definition: RooRealVar.h:48
static const Double_t DEFAULT_CLUES_FRAC
virtual ProposalFunction * GetProposalFunction()
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
Iterator abstract base class.
Definition: TIterator.h:32
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2335
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
Int_t getSize() const
TRandom2 r(17)
RooAbsArg * at(Int_t idx) const
Definition: RooArgList.h:84
static const Double_t SIGMA_RANGE_DIVISOR
Flat p.d.f.
Definition: RooUniform.h:24
Generic N-dimensional implementation of a kernel estimation p.d.f.
Definition: RooNDKeysPdf.h:45
Multivariate Gaussian p.d.f.
Namespace for the RooStats classes.
Definition: Asimov.h:20
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
TMatrixTSym< Double_t > TMatrixDSym
PdfProposal is a concrete implementation of the ProposalFunction interface.
Definition: PdfProposal.h:85
RooConstVar & RooConst(Double_t val)
virtual TObject * Next()=0
#define NULL
Definition: Rtypes.h:82
void CreateCovMatrix(RooArgList &xVec)
const Bool_t kTRUE
Definition: Rtypes.h:91