CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SimplerLikelihoodRatioTestStatExt.cc
Go to the documentation of this file.
1 #include "../interface/SimplerLikelihoodRatioTestStatExt.h"
2 #include "../interface/utils.h"
3 
5  const RooArgSet &obs,
6  RooAbsPdf &pdfNull, RooAbsPdf &pdfAlt,
7  const RooArgSet & paramsNull, const RooArgSet & paramsAlt,
8  bool factorize) :
9  obs_(&obs)
10 {
11  // take a snapshot of the pdf so we can modify it at will
12  RooAbsPdf *cloneNull = utils::fullClonePdf(&pdfNull, pdfCompNull_);
13  RooAbsPdf *cloneAlt = utils::fullClonePdf(&pdfAlt, pdfCompAlt_);
14 
15  // factorize away constraints
16  RooArgList constraints;
17  pdfNull_ = factorize ? utils::factorizePdf(obs, *cloneNull, constraints) : cloneNull;
18  if (pdfNull_ == 0) throw std::invalid_argument("SimplerLikelihoodRatioTestStatOpt:: pdf does not depend on observables");
19  if (pdfNull_ != cloneNull) pdfNullOwned_.reset(pdfNull_); // if new, then take ownership of it
20  if (cloneNull == cloneAlt) {
21  pdfAlt_ = pdfNull_;
22  } else {
23  pdfAlt_ = factorize ? utils::factorizePdf(obs, *cloneAlt, constraints) : cloneAlt;
24  if (pdfAlt_ == 0) throw std::invalid_argument("SimplerLikelihoodRatioTestStatOpt:: pdf does not depend on observables");
25  if (pdfAlt_ != cloneAlt) pdfAltOwned_.reset(pdfAlt_); // if new, then take ownership of it
26  }
27 
28  // take snapshot of parameters
29  snapNull_.addClone(paramsNull);
30  snapAlt_.addClone(paramsAlt);
31 
32  // if the pdf is a RooSimultaneous, unroll it
33  simPdfNull_ = dynamic_cast<RooSimultaneous *>(pdfNull_);
34  simPdfAlt_ = dynamic_cast<RooSimultaneous *>(pdfAlt_);
37 
38  // optimize caching
39  pdfNull_->optimizeCacheMode(*obs_);
40  pdfAlt_->optimizeCacheMode(*obs_);
41 
42  // find nodes that directly depend on observables
45 }
46 
48 {
49 }
50 
51 Double_t
52 SimplerLikelihoodRatioTestStatOpt::Evaluate(RooAbsData& data, RooArgSet& nullPOI)
53 {
54  // get parameters, if not there already
55  if (paramsNull_.get() == 0) paramsNull_.reset(pdfNull_->getParameters(data));
56  if (paramsAlt_.get() == 0) paramsAlt_.reset(pdfAlt_->getParameters(data));
57 
58  // if the dataset is not empty, redirect pdf nodes to the dataset
59  std::auto_ptr<TIterator> iterDepObs(pdfDepObs_.createIterator());
60  bool nonEmpty = data.numEntries() > 0;
61  if (nonEmpty) {
62  const RooArgSet *entry = data.get(0);
63  for (RooAbsArg *a = (RooAbsArg *) iterDepObs->Next(); a != 0; a = (RooAbsArg *) iterDepObs->Next()) {
64  a->redirectServers(*entry);
65  }
66  }
67 
68  // evaluate null pdf
70  *paramsNull_ = nullPOI;
72 
73  // evaluate alt pdf
75  double altNLL = simPdfAlt_ ? evalSimNLL(data, simPdfAlt_, simPdfComponentsAlt_) : evalSimpleNLL(data, pdfAlt_);
76 
77  // put back links in pdf nodes, otherwise if the dataset goes out of scope they have dangling pointers
78  if (nonEmpty) {
79  iterDepObs->Reset();
80  for (RooAbsArg *a = (RooAbsArg *) iterDepObs->Next(); a != 0; a = (RooAbsArg *) iterDepObs->Next()) {
81  a->redirectServers(*obs_);
82  }
83  }
84 
85  return nullNLL-altNLL;
86 }
87 
88 void SimplerLikelihoodRatioTestStatOpt::unrollSimPdf(RooSimultaneous *simpdf, std::vector<RooAbsPdf *> &out) {
89  // get a clone of the pdf category, so that I can use it to enumerate the pdf states
90  std::auto_ptr<RooAbsCategoryLValue> catClone((RooAbsCategoryLValue*) simpdf->indexCat().Clone());
91  out.resize(catClone->numBins(NULL), 0);
92  //std::cout << "Pdf " << pdf->GetName() <<" is a SimPdf over category " << catClone->GetName() << ", with " << out.size() << " bins" << std::endl;
93 
94  // loop on the category state and fetch the pdfs
95  for (int ib = 0, nb = out.size(); ib < nb; ++ib) {
96  catClone->setBin(ib);
97  RooAbsPdf *pdfi = simpdf->getPdf(catClone->getLabel());
98  pdfi->optimizeCacheMode(*obs_);
99  if (pdfi != 0) {
100  //std::cout << " bin " << ib << " (label " << catClone->getLabel() << ") has pdf " << pdfi->GetName() << " of type " << pdfi->ClassName() << std::endl;
101  out[ib] = pdfi;
102  }
103  }
104 }
105 
106 double SimplerLikelihoodRatioTestStatOpt::evalSimNLL(RooAbsData &data, RooSimultaneous *pdf, std::vector<RooAbsPdf *> &components) {
107  data.setDirtyProp(false);
108 
109  double sum = 0.0;
110  int i, n = data.numEntries();
111 
112  // must fetch the category, from the dataset first, if it's not empty
113  RooAbsCategoryLValue *cat = 0;
114  if (n) {
115  const RooArgSet *entry = data.get(0);
116  cat = dynamic_cast<RooAbsCategoryLValue *>(entry->find(pdf->indexCat().GetName()));
117  assert(cat != 0 && "Didn't find category in dataset");
118  }
119 
120  // now loop on the dataset, and dispatch the request to the appropriate pdf
121  std::vector<double> sumw(components.size(), 0);
122  for (i = 0; i < n; ++i) {
123  data.get(i);
124  double w = data.weight(); if (w == 0) continue;
125  int bin = cat->getBin();
126  assert(bin < int(components.size()) && "Bin outside range");
127  if (components[bin] == 0) continue;
128  sum += -w*components[bin]->getLogVal(obs_);
129  sumw[bin] += w;
130  }
131 
132  // then compute extended term
133  for (i = 0, n = components.size(); i < n; ++i) {
134  if (components[i]) sum += components[i]->extendedTerm(UInt_t(sumw[i]), obs_);
135  }
136  return sum;
137 }
138 
139 double SimplerLikelihoodRatioTestStatOpt::evalSimpleNLL(RooAbsData &data, RooAbsPdf *pdf) {
140  data.setDirtyProp(false);
141  double sum = 0.0, sumw = 0.0;
142  int i, n = data.numEntries();
143  for (i = 0; i < n; ++i) {
144  data.get(i);
145  double w = data.weight(); if (w == 0) continue;
146  sum += -w*pdf->getLogVal(obs_);
147  sumw += w;
148  }
149  sum += pdf->extendedTerm(UInt_t(sumw), obs_);
150  return sum;
151 }
152 
153 // ===== This below is identical to the RooStats::SimpleLikelihoodRatioTestStat also in implementation
154 // I've made a copy here just to be able to put some debug hooks inside.
155 #if 0
156 
157 SimplerLikelihoodRatioTestStatExt::SimplerLikelihoodRatioTestStatExt(
158  const RooArgSet &obs,
159  RooAbsPdf &pdfNull, RooAbsPdf &pdfAlt,
160  const RooArgSet & paramsNull, const RooArgSet & paramsAlt)
161 {
162  RooArgList constraints;
163  pdfNull_ = &pdfNull;
164  pdfAlt_ = &pdfAlt;
165  paramsNull_.reset(pdfNull_->getVariables());
166  paramsAlt_.reset(pdfAlt_->getVariables());
167  snapNull_.addClone(paramsNull);
168  snapAlt_.addClone(paramsAlt);
169 }
170 
171 SimplerLikelihoodRatioTestStatExt::~SimplerLikelihoodRatioTestStatExt()
172 {
173 }
174 
175 Double_t
176 SimplerLikelihoodRatioTestStatExt::Evaluate(RooAbsData& data, RooArgSet& nullPOI)
177 {
178  std::auto_ptr<RooAbsReal> nllNull_(pdfNull_->createNLL(data));
179  std::auto_ptr<RooAbsReal> nllAlt_(pdfAlt_->createNLL(data));
180 
181  *paramsNull_ = snapNull_;
182  *paramsNull_ = nullPOI;
183  double nullNLL = nllNull_->getVal();
184 
185  *paramsAlt_ = snapAlt_;
186  double altNLL = nllAlt_->getVal();
187  return nullNLL-altNLL;
188 }
189 
190 #endif
int i
Definition: DBlmapReader.cc:9
RooArgList pdfDepObs_
nodes which depend directly on observables, on which one has to do redirectServers ...
double evalSimNLL(RooAbsData &data, RooSimultaneous *pdf, std::vector< RooAbsPdf * > &components)
std::vector< RooAbsPdf * > simPdfComponentsNull_
components of the sim pdfs after factorization, for each bin in sim. category. can contain nulls ...
std::auto_ptr< RooAbsPdf > pdfNullOwned_
owned copy of the pdfs after factorizing
#define NULL
Definition: scimark2.h:8
void getClients(const RooAbsCollection &values, const RooAbsCollection &allObjects, RooAbsCollection &clients)
add to &#39;clients&#39; all object within allObjects that directly depend on values
Definition: utils.cc:236
RooAbsPdf * pdfNull_
pdfs (cloned, and with constraints factorized away)
RooArgSet snapNull_
snapshots of parameters for the two pdfs
RooSimultaneous * simPdfNull_
pdfNull, pdfAlt cast to sympdf (may be null) after factorization
RooAbsPdf * fullClonePdf(const RooAbsPdf *pdf, RooArgSet &holder, bool cloneLeafNodes=false)
Definition: utils.cc:218
const RooArgSet * obs_
observables (global argset)
RooArgSet pdfCompNull_
snapshot with all branch nodes of the pdfs before factorization
RooAbsPdf * factorizePdf(const RooArgSet &observables, RooAbsPdf &pdf, RooArgList &constraints)
Definition: utils.cc:80
std::pair< std::string, MonitorElement * > entry
Definition: ME_MAP.h:8
virtual Double_t Evaluate(RooAbsData &data, RooArgSet &nullPOI)
SimplerLikelihoodRatioTestStatOpt(const RooArgSet &obs, RooAbsPdf &pdfNull, RooAbsPdf &pdfAlt, const RooArgSet &paramsNull=RooArgSet(), const RooArgSet &paramsAlt=RooArgSet(), bool factorize=true)
tuple out
Definition: dbtoconf.py:99
unsigned int UInt_t
Definition: FUTypes.h:12
std::auto_ptr< RooArgSet > paramsNull_
parameter sets to apply snapshots to
double evalSimpleNLL(RooAbsData &data, RooAbsPdf *pdf)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
double a
Definition: hdecay.h:121
void unrollSimPdf(RooSimultaneous *pdf, std::vector< RooAbsPdf * > &out)
T w() const