Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
ConfidenceBelt.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
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 /*****************************************************************************
12  * Project: RooStats
13  * Package: RooFit/RooStats
14  * @(#)root/roofit/roostats:$Id$
15  * Original Author: Kyle Cranmer
16  * Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
17  *
18  *****************************************************************************/
19 
20 /** \class RooStats::ConfidenceBelt
21  \ingroup Roostats
22 
23 ConfidenceBelt is a concrete implementation of the ConfInterval interface.
24 It implements simple general purpose interval of arbitrary dimensions and shape.
25 It does not assume the interval is connected.
26 It uses either a RooDataSet (eg. a list of parameter points in the interval) or
27 a RooDataHist (eg. a Histogram-like object for small regions of the parameter space) to
28 store the interval.
29 
30 */
31 
33 
34 #include "RooDataSet.h"
35 #include "RooDataHist.h"
36 
37 #include "RooStats/RooStatsUtils.h"
38 
39 ClassImp(RooStats::ConfidenceBelt); ;
40 
41 using namespace RooStats;
42 using namespace std;
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// Default constructor
46 
47 ConfidenceBelt::ConfidenceBelt() :
48  TNamed(), fParameterPoints(0)
49 {
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Alternate constructor
54 
55 ConfidenceBelt::ConfidenceBelt(const char* name) :
56  TNamed(name,name), fParameterPoints(0)
57 {
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Alternate constructor
62 
63 ConfidenceBelt::ConfidenceBelt(const char* name, const char* title) :
64  TNamed(name,title), fParameterPoints(0)
65 {
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// Alternate constructor
70 
71 ConfidenceBelt::ConfidenceBelt(const char* name, RooAbsData& data) :
72  TNamed(name,name), fParameterPoints((RooAbsData*)data.Clone("PointsToTestForBelt"))
73 {
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Alternate constructor
78 
79 ConfidenceBelt::ConfidenceBelt(const char* name, const char* title, RooAbsData& data) :
80  TNamed(name,title), fParameterPoints((RooAbsData*)data.Clone("PointsToTestForBelt"))
81 {
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Destructor
86 
87 ConfidenceBelt::~ConfidenceBelt()
88 {
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 
93 Double_t ConfidenceBelt::GetAcceptanceRegionMin(RooArgSet& parameterPoint, Double_t cl, Double_t leftside) {
94  if(cl>0 || leftside > 0) cout <<"using default cl, leftside for now" <<endl;
95  AcceptanceRegion * region = GetAcceptanceRegion(parameterPoint, cl,leftside);
96  return (region) ? region->GetLowerLimit() : TMath::QuietNaN();
97 }
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 
101 Double_t ConfidenceBelt::GetAcceptanceRegionMax(RooArgSet& parameterPoint, Double_t cl, Double_t leftside) {
102  if(cl>0 || leftside > 0) cout <<"using default cl, leftside for now" <<endl;
103  AcceptanceRegion * region = GetAcceptanceRegion(parameterPoint, cl,leftside);
104  return (region) ? region->GetUpperLimit() : TMath::QuietNaN();
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 
109 vector<Double_t> ConfidenceBelt::ConfidenceLevels() const {
110  vector<Double_t> levels;
111  return levels;
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 
116 void ConfidenceBelt::AddAcceptanceRegion(RooArgSet& parameterPoint, Int_t dsIndex,
117  Double_t lower, Double_t upper,
118  Double_t cl, Double_t leftside){
119  if(cl>0 || leftside > 0) cout <<"using default cl, leftside for now" <<endl;
120 
121  RooDataSet* tree = dynamic_cast<RooDataSet*>( fParameterPoints );
122  RooDataHist* hist = dynamic_cast<RooDataHist*>( fParameterPoints );
123 
124  // cout << "add: " << tree << " " << hist << endl;
125 
126  if( !this->CheckParameters(parameterPoint) )
127  std::cout << "problem with parameters" << std::endl;
128 
129  Int_t luIndex = fSamplingSummaryLookup.GetLookupIndex(cl, leftside);
130  // cout << "lookup index = " << luIndex << endl;
131  if(luIndex <0 ) {
132  fSamplingSummaryLookup.Add(cl,leftside);
133  luIndex = fSamplingSummaryLookup.GetLookupIndex(cl, leftside);
134  cout << "lookup index = " << luIndex << endl;
135  }
136  AcceptanceRegion* thisRegion = new AcceptanceRegion(luIndex, lower, upper);
137 
138  if( hist ) {
139  // need a way to get index for given point
140  // Can do this by setting hist's internal parameters to desired values
141  // need a better way
142  // RooStats::SetParameters(&parameterPoint, const_cast<RooArgSet*>(hist->get()));
143  // int index = hist->calcTreeIndex(); // get index
144  int index = hist->getIndex(parameterPoint); // get index
145  // cout << "hist index = " << index << endl;
146 
147  // allocate memory if necessary. numEntries is overkill?
148  if((Int_t)fSamplingSummaries.size() <= index) {
149  fSamplingSummaries.reserve( hist->numEntries() );
150  fSamplingSummaries.resize( hist->numEntries() );
151  }
152 
153  // set the region for this point (check for duplicate?)
154  fSamplingSummaries.at(index) = *thisRegion;
155  }
156  else if( tree ){
157  // int index = tree->getIndex(parameterPoint);
158  int index = dsIndex;
159  // cout << "tree index = " << index << endl;
160 
161  // allocate memory if necessary. numEntries is overkill?
162  if((Int_t)fSamplingSummaries.size() <= index){
163  fSamplingSummaries.reserve( tree->numEntries() );
164  fSamplingSummaries.resize( tree->numEntries() );
165  }
166 
167  // set the region for this point (check for duplicate?)
168  fSamplingSummaries.at( index ) = *thisRegion;
169  }
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 
174 void ConfidenceBelt::AddAcceptanceRegion(RooArgSet& parameterPoint, AcceptanceRegion region,
175  Double_t cl, Double_t leftside){
176  if(cl>0 || leftside > 0) cout <<"using default cl, leftside for now" <<endl;
177 
178  RooDataSet* tree = dynamic_cast<RooDataSet*>( fParameterPoints );
179  RooDataHist* hist = dynamic_cast<RooDataHist*>( fParameterPoints );
180 
181  if( !this->CheckParameters(parameterPoint) )
182  std::cout << "problem with parameters" << std::endl;
183 
184 
185  if( hist ) {
186  // need a way to get index for given point
187  // Can do this by setting hist's internal parameters to desired values
188  // need a better way
189  // RooStats::SetParameters(&parameterPoint, const_cast<RooArgSet*>(hist->get()));
190  // int index = hist->calcTreeIndex(); // get index
191  int index = hist->getIndex(parameterPoint); // get index
192 
193  // allocate memory if necessary. numEntries is overkill?
194  if((Int_t)fSamplingSummaries.size() < index) fSamplingSummaries.reserve( hist->numEntries() );
195 
196  // set the region for this point (check for duplicate?)
197  fSamplingSummaries.at(index) = region;
198  }
199  else if( tree ){
200  tree->add( parameterPoint ); // assume it's unique for now
201  int index = tree->numEntries() - 1; //check that last point added has index nEntries -1
202  // allocate memory if necessary. numEntries is overkill?
203  if((Int_t)fSamplingSummaries.size() < index) fSamplingSummaries.reserve( tree->numEntries() );
204 
205  // set the region for this point (check for duplicate?)
206  fSamplingSummaries.at( index ) = region;
207  }
208 }
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// Method to determine if a parameter point is in the interval
212 
213 AcceptanceRegion* ConfidenceBelt::GetAcceptanceRegion(RooArgSet &parameterPoint, Double_t cl, Double_t leftside)
214 {
215  if(cl>0 || leftside > 0) cout <<"using default cl, leftside for now" <<endl;
216 
217  RooDataSet* tree = dynamic_cast<RooDataSet*>( fParameterPoints );
218  RooDataHist* hist = dynamic_cast<RooDataHist*>( fParameterPoints );
219 
220  if( !this->CheckParameters(parameterPoint) ){
221  std::cout << "problem with parameters" << std::endl;
222  return 0;
223  }
224 
225  if( hist ) {
226  // need a way to get index for given point
227  // Can do this by setting hist's internal parameters to desired values
228  // need a better way
229  // RooStats::SetParameters(&parameterPoint, const_cast<RooArgSet*>(hist->get()));
230  // Int_t index = hist->calcTreeIndex(); // get index
231  int index = hist->getIndex(parameterPoint); // get index
232  return &(fSamplingSummaries.at(index).GetAcceptanceRegion());
233  }
234  else if( tree ){
235  // need a way to get index for given point
236  // RooStats::SetParameters(&parameterPoint, tree->get()); // set tree's parameters to desired values
237  Int_t index = 0; //need something like tree->calcTreeIndex();
238  const RooArgSet* thisPoint = 0;
239  for(index=0; index<tree->numEntries(); ++index){
240  thisPoint = tree->get(index);
241  bool samePoint = true;
242  TIter it = parameterPoint.createIterator();
243  RooRealVar *myarg;
244  while ( samePoint && (myarg = (RooRealVar *)it.Next())) {
245  if(myarg->getVal() != thisPoint->getRealValue(myarg->GetName()))
246  samePoint = false;
247  }
248  if(samePoint)
249  break;
250  }
251 
252  return &(fSamplingSummaries.at(index).GetAcceptanceRegion());
253  }
254  else {
255  std::cout << "dataset is not initialized properly" << std::endl;
256  }
257 
258  return 0;
259 
260 }
261 
262 ////////////////////////////////////////////////////////////////////////////////
263 /// returns list of parameters
264 
265 RooArgSet* ConfidenceBelt::GetParameters() const
266 {
267  return new RooArgSet(*(fParameterPoints->get()));
268 }
269 
270 ////////////////////////////////////////////////////////////////////////////////
271 
272 Bool_t ConfidenceBelt::CheckParameters(RooArgSet &parameterPoint) const
273 {
274  if (parameterPoint.getSize() != fParameterPoints->get()->getSize() ) {
275  std::cout << "size is wrong, parameters don't match" << std::endl;
276  return false;
277  }
278  if ( ! parameterPoint.equals( *(fParameterPoints->get() ) ) ) {
279  std::cout << "size is ok, but parameters don't match" << std::endl;
280  return false;
281  }
282  return true;
283 }