Skip to content

Commit 3956794

Browse files
guitargeekanigamova
authored andcommitted
Do analytical Barlow Beeston before likelihood evaluation
Doing the analytical Barlow Beeston before evaluating the likelihood has two big advantages, instead of doing it while evaluating the CMSHistSum/CMSHistErrorPropagator: 1. We don't have to hack the dirty state propagation. This was necessary before, because it is not allowed to change a RooRealVar server during the evaluation of the client. Also, the hack relies on the implementation details of the CachingSimNLL (evaluating main pdf before constraints), so it's also a problem for using RooFit regular NLL code path. 2. If the analytical minimization and the likelihood evaluation factorize, we don't need to worry how to differentiate through the analytical minimization and the likelihood evaluation together.
1 parent 45d8e74 commit 3956794

File tree

6 files changed

+32
-34
lines changed

6 files changed

+32
-34
lines changed

interface/CMSHistErrorPropagator.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class CMSHistErrorPropagator : public RooAbsReal {
3030
std::vector<double> x2;
3131
std::vector<double> res;
3232
std::vector<double> gobs;
33-
std::set<RooAbsArg*> dirty_prop;
3433
std::vector<RooRealVar*> push_res;
3534
};
3635
public:
@@ -77,7 +76,9 @@ class CMSHistErrorPropagator : public RooAbsReal {
7776

7877
friend class CMSHistV<CMSHistErrorPropagator>;
7978

80-
protected:
79+
void runBarlowBeeston() const;
80+
81+
protected:
8182
RooRealProxy x_;
8283
RooListProxy funcs_;
8384
RooListProxy coeffs_;
@@ -109,8 +110,6 @@ class CMSHistErrorPropagator : public RooAbsReal {
109110
void initialize() const;
110111
void updateCache(int eval = 1) const;
111112

112-
void runBarlowBeeston() const;
113-
114113

115114
private:
116115
ClassDefOverride(CMSHistErrorPropagator,1)

interface/CMSHistSum.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class CMSHistSum : public RooAbsReal {
3030
std::vector<double> x2;
3131
std::vector<double> res;
3232
std::vector<double> gobs;
33-
std::set<RooAbsArg*> dirty_prop;
3433
std::vector<RooRealVar*> push_res;
3534
};
3635
public:
@@ -80,7 +79,9 @@ class CMSHistSum : public RooAbsReal {
8079

8180
void injectExternalMorph(int idx, CMSExternalMorph& morph);
8281

83-
protected:
82+
void runBarlowBeeston() const;
83+
84+
protected:
8485
RooRealProxy x_;
8586

8687
RooListProxy morphpars_;
@@ -143,9 +144,6 @@ class CMSHistSum : public RooAbsReal {
143144
void updateCache() const;
144145
inline double smoothStepFunc(double x, int const& ip) const;
145146

146-
147-
void runBarlowBeeston() const;
148-
149147
void updateMorphs() const;
150148

151149

interface/CachingNLL.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class CachingAddNLL : public RooAbsReal {
129129
void updateZeroPoint() { clearZeroPoint(); setZeroPoint(); }
130130
void propagateData();
131131
void setAnalyticBarlowBeeston(bool flag);
132+
void runAnalyticBarlowBeeston();
132133
/// note: setIncludeZeroWeights(true) won't have effect unless you also re-call setData
133134
virtual void setIncludeZeroWeights(bool includeZeroWeights) ;
134135
RooSetProxy & params() { return params_; }

src/CMSHistErrorPropagator.cc

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ void CMSHistErrorPropagator::initialize() const {
9999
initialized_ = true;
100100
}
101101

102-
103102
void CMSHistErrorPropagator::updateCache(int eval) const {
104103
initialize();
105104

@@ -153,7 +152,6 @@ void CMSHistErrorPropagator::updateCache(int eval) const {
153152

154153

155154
if (!binsentry_.good() || eval != last_eval_) {
156-
runBarlowBeeston();
157155
// bintypes might have size == 0 if we never ran setupBinPars()
158156
for (unsigned j = 0; j < bintypes_.size(); ++j) {
159157
cache_[j] = valsum_[j];
@@ -192,9 +190,7 @@ void CMSHistErrorPropagator::updateCache(int eval) const {
192190
}
193191

194192
void CMSHistErrorPropagator::runBarlowBeeston() const {
195-
if (!bb_.init) return;
196-
RooAbsArg::setDirtyInhibit(true);
197-
193+
updateCache();
198194
const unsigned n = bb_.use.size();
199195
for (unsigned j = 0; j < n; ++j) {
200196
bb_.dat[j] = data_[bb_.use[j]];
@@ -215,10 +211,6 @@ void CMSHistErrorPropagator::runBarlowBeeston() const {
215211
for (unsigned j = 0; j < n; ++j) {
216212
if (toterr_[bb_.use[j]] > 0.) bb_.push_res[j]->setVal(bb_.res[j]);
217213
}
218-
RooAbsArg::setDirtyInhibit(false);
219-
for (RooAbsArg *arg : bb_.dirty_prop) {
220-
arg->setValueDirty();
221-
}
222214
}
223215

224216
void CMSHistErrorPropagator::setAnalyticBarlowBeeston(bool flag) const {
@@ -240,7 +232,6 @@ void CMSHistErrorPropagator::setAnalyticBarlowBeeston(bool flag) const {
240232
bb_.x2.clear();
241233
bb_.res.clear();
242234
bb_.gobs.clear();
243-
bb_.dirty_prop.clear();
244235
bb_.push_res.clear();
245236
bb_.init = false;
246237
}
@@ -254,7 +245,6 @@ void CMSHistErrorPropagator::setAnalyticBarlowBeeston(bool flag) const {
254245
// std::cout << "Skipping " << this << " " << this->GetName() << "\n";
255246
} else {
256247
// std::cout << "Adding " << arg << " " << arg->GetName() << "\n";
257-
bb_.dirty_prop.insert(arg);
258248
auto as_gauss = dynamic_cast<RooGaussian*>(arg);
259249
if (as_gauss) {
260250
auto gobs = dynamic_cast<RooAbsReal*>(as_gauss->findServer(TString(vbinpars_[j][0]->GetName())+"_In"));

src/CMSHistSum.cc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ inline double CMSHistSum::smoothStepFunc(double x, int const& ip) const {
336336
return 0.125 * xnorm * (xnorm2 * (3. * xnorm2 - 10.) + 15);
337337
}
338338

339-
340339
void CMSHistSum::updateCache() const {
341340
initialize();
342341

@@ -379,10 +378,6 @@ void CMSHistSum::updateCache() const {
379378

380379

381380
if (!binsentry_.good()) {
382-
#if HFVERBOSE > 0
383-
std::cout << "Calling runBarlowBeeston\n";
384-
#endif
385-
runBarlowBeeston();
386381
// bintypes might have size == 0 if we never ran setupBinPars()
387382
#if HFVERBOSE > 0
388383
std::cout << "Assigning bin shifts\n";
@@ -418,9 +413,7 @@ void CMSHistSum::updateCache() const {
418413
}
419414

420415
void CMSHistSum::runBarlowBeeston() const {
421-
if (!bb_.init) return;
422-
RooAbsArg::setDirtyInhibit(true);
423-
416+
updateCache();
424417
const unsigned n = bb_.use.size();
425418
for (unsigned j = 0; j < n; ++j) {
426419
bb_.dat[j] = data_[bb_.use[j]];
@@ -441,10 +434,6 @@ void CMSHistSum::runBarlowBeeston() const {
441434
for (unsigned j = 0; j < n; ++j) {
442435
if (toterr_[bb_.use[j]] > 0.) bb_.push_res[j]->setVal(bb_.res[j]);
443436
}
444-
RooAbsArg::setDirtyInhibit(false);
445-
for (RooAbsArg *arg : bb_.dirty_prop) {
446-
arg->setValueDirty();
447-
}
448437
}
449438

450439
void CMSHistSum::setAnalyticBarlowBeeston(bool flag) const {
@@ -466,7 +455,6 @@ void CMSHistSum::setAnalyticBarlowBeeston(bool flag) const {
466455
bb_.x2.clear();
467456
bb_.res.clear();
468457
bb_.gobs.clear();
469-
bb_.dirty_prop.clear();
470458
bb_.push_res.clear();
471459
bb_.init = false;
472460
}
@@ -480,7 +468,6 @@ void CMSHistSum::setAnalyticBarlowBeeston(bool flag) const {
480468
// std::cout << "Skipping " << this << " " << this->GetName() << "\n";
481469
} else {
482470
// std::cout << "Adding " << arg << " " << arg->GetName() << "\n";
483-
bb_.dirty_prop.insert(arg);
484471
auto as_gauss = dynamic_cast<RooGaussian*>(arg);
485472
if (as_gauss) {
486473
auto gobs = dynamic_cast<RooAbsReal*>(as_gauss->findServer(TString(vbinpars_[j][0]->GetName())+"_In"));

src/CachingNLL.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@ cacheutils::CachingAddNLL::evaluate() const
595595
#ifdef DEBUG_CACHE
596596
PerfCounter::add("CachingAddNLL::evaluate called");
597597
#endif
598+
// The very first thing we do before any evaluation: run the analytical
599+
// minimization of Barlow-Beeston nuisance parameters.
600+
const_cast<CachingAddNLL&>(*this).runAnalyticBarlowBeeston();
598601

599602
std::fill( partialSum_.begin(), partialSum_.end(), 0.0 );
600603

@@ -809,6 +812,15 @@ void cacheutils::CachingAddNLL::setAnalyticBarlowBeeston(bool flag) {
809812
}
810813
}
811814

815+
void cacheutils::CachingAddNLL::runAnalyticBarlowBeeston() {
816+
for (auto* hist : histErrorPropagators_) {
817+
hist->runBarlowBeeston();
818+
}
819+
for (auto* hist : histSums_) {
820+
hist->runBarlowBeeston();
821+
}
822+
}
823+
812824
cacheutils::CachingSimNLL::CachingSimNLL(RooSimultaneous *pdf, RooAbsData *data, const RooArgSet *nuis) :
813825
pdfOriginal_(pdf),
814826
dataOriginal_(data),
@@ -1007,6 +1019,17 @@ cacheutils::CachingSimNLL::evaluate() const
10071019
#ifdef DEBUG_CACHE
10081020
PerfCounter::add("CachingSimNLL::evaluate called");
10091021
#endif
1022+
1023+
// The very first thing we do before any evaluation: run the analytical
1024+
// minimization of Barlow-Beeston nuisance parameters.
1025+
for (size_t i = 0; i < pdfs_.size(); ++i) {
1026+
if (!pdfs_[i])
1027+
continue;
1028+
if (!channelMasks_.empty() && channelMasks_[i]->getVal() != 0.)
1029+
continue;
1030+
pdfs_[i]->runAnalyticBarlowBeeston();
1031+
}
1032+
10101033
static bool gentleNegativePenalty_ = runtimedef::get("GENTLE_LEE");
10111034
DefaultAccumulator<double> ret = 0;
10121035
for (std::size_t idx = 0; idx < pdfs_.size(); ++idx) {

0 commit comments

Comments
 (0)