-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathBestFitSigmaTestStat.cc
More file actions
114 lines (98 loc) · 3.93 KB
/
BestFitSigmaTestStat.cc
File metadata and controls
114 lines (98 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "../interface/ProfiledLikelihoodRatioTestStatExt.h"
#include "../interface/Combine.h"
#include "../interface/BestFitSigmaTestStat.h"
#include "../interface/CascadeMinimizer.h"
#include "../interface/CloseCoutSentry.h"
#include "../interface/utils.h"
#include <stdexcept>
#include <RooRealVar.h>
#include <RooMinimizer.h>
#include <RooFitResult.h>
#include <RooSimultaneous.h>
#include <RooCategory.h>
#include <RooRandom.h>
#include <Math/MinimizerOptions.h>
#include <RooStats/RooStatsUtils.h>
#include "../interface/ProfilingTools.h"
//---- Uncomment this and run with --perfCounters to get statistics of successful and failed fits
#define DEBUG_FIT_STATUS
#ifdef DEBUG_FIT_STATUS
#define COUNT_ONE(x) PerfCounter::add(x);
#else
#define COUNT_ONE(X)
#endif
//---- Uncomment this and set some of these to 1 to get more debugging
#if 0
#define DBG(X,Z) if (X) { Z; }
#define DBGV(X,Z) if (X>1) { Z; }
#define DBG_TestStat_params 0 // ProfiledLikelihoodRatioTestStatOpt::Evaluate; 1 = dump nlls; 2 = dump params at each eval
#define DBG_TestStat_NOFIT 0 // FIXME HACK: if set, don't profile the likelihood, just evaluate it
#define DBG_PLTestStat_ctor 0 // dump parameters in c-tor
#define DBG_PLTestStat_pars 0 // dump parameters in eval
#define DBG_PLTestStat_fit 0 // dump fit result
#define DBG_PLTestStat_main 1 // limited debugging (final NLLs, failed fits)
#else
#define DBG(X,Z)
#define DBGV(X,Z)
#endif
//============================================================================================================================================
BestFitSigmaTestStat::BestFitSigmaTestStat(
const RooArgSet & observables,
RooAbsPdf &pdf,
const RooArgSet *nuisances,
const RooArgSet & params,
int verbosity)
:
pdf_(&pdf),
verbosity_(verbosity)
{
params.snapshot(snap_,false);
((RooRealVar*)snap_.find(params.first()->GetName()))->setConstant(false);
poi_.add(snap_);
if (nuisances) { nuisances_.add(*nuisances); snap_.addClone(*nuisances, /*silent=*/true); }
params_.reset(pdf_->getParameters(observables));
}
Double_t BestFitSigmaTestStat::Evaluate(RooAbsData& data, RooArgSet& /*nullPOI*/)
{
// Take snapshot of initial state, to restore it at the end
RooArgSet initialState; params_->snapshot(initialState);
// Initialize parameters
*params_ = snap_;
// Initialize signal strength
RooRealVar *rIn = (RooRealVar *) poi_.first();
RooRealVar *r = (RooRealVar *) params_->find(rIn->GetName());
bool canKeepNLL = createNLLWrapper(*pdf_, data);
double initialR = rIn->getVal();
// Perform unconstrained minimization
r->setVal(initialR == 0 ? 0.5 : 0.5*initialR); //best guess
r->setConstant(false);
std::cout << "Doing a fit for with " << r->GetName() << " in range [ " << r->getMin() << " , " << r->getMax() << "]" << std::endl;
std::cout << "Starting point is " << r->GetName() << " = " << r->getVal() << std::endl;
minNLL(/*constrained=*/false, r);
double bestFitR = r->getVal();
std::cout << "Fit result was " << r->GetName() << " = " << r->getVal() << std::endl;
//Restore initial state, to avoid issues with ToyMCSampler
*params_ = initialState;
if (!canKeepNLL) nll_.reset();
return bestFitR;
}
bool BestFitSigmaTestStat::createNLLWrapper(RooAbsPdf &pdf, RooAbsData &data)
{
if (typeid(pdf) == typeid(RooSimultaneousOpt)) {
if (nll_.get() == 0)
nll_ = Combine::combineCreateNLL(pdf, data, &nuisances_, /*offset=*/false);
else
((cacheutils::CachingSimNLL &)(*nll_)).setData(data);
return true;
} else {
nll_ = Combine::combineCreateNLL(pdf, data, &nuisances_, /*offset=*/false);
return false;
}
}
double BestFitSigmaTestStat::minNLL(bool constrained, RooRealVar *r)
{
CascadeMinimizer::Mode mode(constrained ? CascadeMinimizer::Constrained : CascadeMinimizer::Unconstrained);
CascadeMinimizer minim(*nll_, mode, r);
minim.minimize(verbosity_-2);
return nll_->getVal();
}