Logo ROOT  
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 /** \class RooStats::ProposalHelper
13  \ingroup Roostats
14 */
15 
16 #include "Rtypes.h"
18 #include "RooStats/PdfProposal.h"
19 #include "RooStats/RooStatsUtils.h"
20 #include "RooArgSet.h"
21 #include "RooDataSet.h"
22 #include "RooAbsPdf.h"
23 #include "RooAddPdf.h"
24 #include "RooNDKeysPdf.h"
25 #include "RooUniform.h"
26 #include "RooMsgService.h"
27 #include "RooRealVar.h"
28 #include "TIterator.h"
29 #include "RooMultiVarGaussian.h"
30 #include "RooConstVar.h"
31 #include "TString.h"
32 
33 #include <map>
34 
35 namespace RooStats {
36  class ProposalFunction;
37 }
38 
40 
41 using namespace RooFit;
42 using namespace RooStats;
43 using namespace std;
44 
45 //static const Double_t DEFAULT_UNI_FRAC = 0.10;
46 static const Double_t DEFAULT_CLUES_FRAC = 0.20;
47 //static const Double_t SIGMA_RANGE_DIVISOR = 6;
48 static const Double_t SIGMA_RANGE_DIVISOR = 5;
49 //static const Int_t DEFAULT_CACHE_SIZE = 100;
50 //static const Option_t* CLUES_OPTIONS = "a";
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 
54 ProposalHelper::ProposalHelper()
55 {
56  fPdfProp = new PdfProposal();
57  fVars = NULL;
58  fOwnsPdfProp = kTRUE;
59  fOwnsPdf = kFALSE;
60  fOwnsCluesPdf = kFALSE;
61  fOwnsVars = kFALSE;
62  fUseUpdates = kFALSE;
63  fPdf = NULL;
64  fSigmaRangeDivisor = SIGMA_RANGE_DIVISOR;
65  fCluesPdf = NULL;
66  fUniformPdf = NULL;
67  fClues = NULL;
68  fCovMatrix = NULL;
69  fCluesFrac = -1;
70  fUniFrac = -1;
71  fCacheSize = -1;
72  fCluesOptions = NULL;
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 
77 ProposalFunction* ProposalHelper::GetProposalFunction()
78 {
79  if (fPdf == NULL)
80  CreatePdf();
81  // kbelasco: check here for memory leaks: does RooAddPdf make copies or
82  // take ownership of components, coeffs
83  RooArgList* components = new RooArgList();
84  RooArgList* coeffs = new RooArgList();
85  if (fCluesPdf == NULL)
86  CreateCluesPdf();
87  if (fCluesPdf != NULL) {
88  if (fCluesFrac < 0)
89  fCluesFrac = DEFAULT_CLUES_FRAC;
90  printf("added clues from dataset %s with fraction %g\n",
91  fClues->GetName(), fCluesFrac);
92  components->add(*fCluesPdf);
93  coeffs->add(RooConst(fCluesFrac));
94  }
95  if (fUniFrac > 0.) {
96  CreateUniformPdf();
97  components->add(*fUniformPdf);
98  coeffs->add(RooConst(fUniFrac));
99  }
100  components->add(*fPdf);
101  RooAddPdf* addPdf = new RooAddPdf("proposalFunction", "Proposal Density",
102  *components, *coeffs);
103  fPdfProp->SetPdf(*addPdf);
104  fPdfProp->SetOwnsPdf(kTRUE);
105  if (fCacheSize > 0)
106  fPdfProp->SetCacheSize(fCacheSize);
107  fOwnsPdfProp = kFALSE;
108  return fPdfProp;
109 }
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 
113 void ProposalHelper::CreatePdf()
114 {
115  // kbelasco: check here for memory leaks:
116  // does RooMultiVarGaussian make copies of xVec and muVec?
117  // or should we delete them?
118  if (fVars == NULL) {
119  coutE(InputArguments) << "ProposalHelper::CreatePdf(): " <<
120  "Variables to create proposal function for are not set." << endl;
121  return;
122  }
123  RooArgList* xVec = new RooArgList();
124  RooArgList* muVec = new RooArgList();
125  TIterator* it = fVars->createIterator();
126  RooRealVar* r;
127  RooRealVar* clone;
128  while ((r = (RooRealVar*)it->Next()) != NULL) {
129  xVec->add(*r);
130  TString cloneName = TString::Format("%s%s", "mu__", r->GetName());
131  clone = (RooRealVar*)r->clone(cloneName.Data());
132  muVec->add(*clone);
133  if (fUseUpdates)
134  fPdfProp->AddMapping(*clone, *r);
135  }
136  if (fCovMatrix == NULL)
137  CreateCovMatrix(*xVec);
138  fPdf = new RooMultiVarGaussian("mvg", "MVG Proposal", *xVec, *muVec,
139  *fCovMatrix);
140  delete xVec;
141  delete muVec;
142  delete it;
143 }
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 
147 void ProposalHelper::CreateCovMatrix(RooArgList& xVec)
148 {
149  Int_t size = xVec.getSize();
150  fCovMatrix = new TMatrixDSym(size);
151  RooRealVar* r;
152  for (Int_t i = 0; i < size; i++) {
153  r = (RooRealVar*)xVec.at(i);
154  Double_t range = r->getMax() - r->getMin();
155  (*fCovMatrix)(i,i) = range / fSigmaRangeDivisor;
156  }
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 
161 void ProposalHelper::CreateCluesPdf()
162 {
163  if (fClues != NULL) {
164  if (fCluesOptions == NULL)
165  fCluesPdf = new RooNDKeysPdf("cluesPdf", "Clues PDF", *fVars, *fClues);
166  else
167  fCluesPdf = new RooNDKeysPdf("cluesPdf", "Clues PDF", *fVars, *fClues,
168  fCluesOptions);
169  }
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 
174 void ProposalHelper::CreateUniformPdf()
175 {
176  fUniformPdf = new RooUniform("uniform", "Uniform Proposal PDF",
177  RooArgSet(*fVars));
178 }
RooMultiVarGaussian.h
RooUniform
Definition: RooUniform.h:24
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooStats::PdfProposal
Definition: PdfProposal.h:36
RooMsgService.h
RooAddPdf
Definition: RooAddPdf.h:32
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
RooArgSet.h
TString::Data
const char * Data() const
Definition: TString.h:369
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
r
ROOT::R::TRInterface & r
Definition: Object.C:4
RooMultiVarGaussian
Definition: RooMultiVarGaussian.h:31
coutE
#define coutE(a)
Definition: RooMsgService.h:33
RooArgList
Definition: RooArgList.h:21
RooAddPdf.h
RooArgList::at
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition: RooArgList.h:88
TString::Format
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:2311
TString
Definition: TString.h:136
RooDataSet.h
TString.h
TIterator
Definition: TIterator.h:30
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
DEFAULT_CLUES_FRAC
static const Double_t DEFAULT_CLUES_FRAC
Definition: ProposalHelper.cxx:46
SIGMA_RANGE_DIVISOR
static const Double_t SIGMA_RANGE_DIVISOR
Definition: ProposalHelper.cxx:48
RooStats::ProposalHelper
Definition: ProposalHelper.h:35
RooFit
Definition: RooCFunction1Binding.h:29
RooAbsPdf.h
PdfProposal.h
TIterator.h
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooAbsCollection::add
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Definition: RooAbsCollection.cxx:437
RooRealVar.h
TIterator::Next
virtual TObject * Next()=0
RooConstVar.h
RooStatsUtils.h
Double_t
double Double_t
Definition: RtypesCore.h:59
RooStats
Definition: Asimov.h:19
RooNDKeysPdf.h
RooStats::ProposalFunction
Definition: ProposalFunction.h:48
RooNDKeysPdf
Definition: RooNDKeysPdf.h:48
TMatrixDSym
TMatrixTSym< Double_t > TMatrixDSym
Definition: TMatrixDSymfwd.h:22
RooRealVar
Definition: RooRealVar.h:36
Rtypes.h
RooAbsCollection::getSize
Int_t getSize() const
Definition: RooAbsCollection.h:171
RooUniform.h
RooArgSet
Definition: RooArgSet.h:28
ProposalHelper.h
int
RooFit::RooConst
RooConstVar & RooConst(Double_t val)
Definition: RooGlobalFunc.cxx:341