Logo ROOT  
Reference Guide
RooProfileLL.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2 * Project: RooFit *
3 * *
4 * Copyright (c) 2000-2005, Regents of the University of California *
5 * and Stanford University. All rights reserved. *
6 * *
7 * Redistribution and use in source and binary forms, *
8 * with or without modification, are permitted according to the terms *
9 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
10 *****************************************************************************/
11
12/**
13\file RooProfileLL.cxx
14\class RooProfileLL
15\ingroup Roofitcore
16
17Class RooProfileLL implements the profile likelihood estimator for
18a given likelihood and set of parameters of interest. The value return by
19RooProfileLL is the input likelihood nll minimized w.r.t all nuisance parameters
20(which are all parameters except for those listed in the constructor) minus
21the -log(L) of the best fit. Note that this function is slow to evaluate
22as a MIGRAD minimization step is executed for each function evaluation
23**/
24
25#include "Riostream.h"
26
27#include "RooFit.h"
28#include "RooProfileLL.h"
29#include "RooAbsReal.h"
30#include "RooMinimizer.h"
31#include "RooMsgService.h"
32#include "RooRealVar.h"
33
34using namespace std ;
35
37
38
39////////////////////////////////////////////////////////////////////////////////
40/// Default constructor
41/// Should only be used by proof.
42
44 RooAbsReal("RooProfileLL","RooProfileLL"),
45 _nll(),
46 _obs("paramOfInterest","Parameters of interest",this),
47 _par("nuisanceParam","Nuisance parameters",this,kFALSE,kFALSE),
48 _startFromMin(kTRUE),
49 _absMinValid(kFALSE),
50 _absMin(0),
51 _neval(0)
52{
53}
54
55
56////////////////////////////////////////////////////////////////////////////////
57/// Constructor of profile likelihood given input likelihood nll w.r.t
58/// the given set of variables. The input log likelihood is minimized w.r.t
59/// to all other variables of the likelihood at each evaluation and the
60/// value of the global log likelihood minimum is always subtracted.
61
62RooProfileLL::RooProfileLL(const char *name, const char *title,
63 RooAbsReal& nllIn, const RooArgSet& observables) :
64 RooAbsReal(name,title),
65 _nll("input","-log(L) function",this,nllIn),
66 _obs("paramOfInterest","Parameters of interest",this),
67 _par("nuisanceParam","Nuisance parameters",this,kFALSE,kFALSE),
68 _startFromMin(kTRUE),
69 _absMinValid(kFALSE),
70 _absMin(0),
71 _neval(0)
72{
73 // Determine actual parameters and observables
74 nllIn.getObservables(&observables, _obs) ;
75 nllIn.getParameters(&observables, _par) ;
76}
77
78
79
80////////////////////////////////////////////////////////////////////////////////
81/// Copy constructor
82
83RooProfileLL::RooProfileLL(const RooProfileLL& other, const char* name) :
84 RooAbsReal(other,name),
85 _nll("nll",this,other._nll),
86 _obs("obs",this,other._obs),
87 _par("par",this,other._par),
88 _startFromMin(other._startFromMin),
89 _absMinValid(kFALSE),
90 _absMin(0),
91 _paramFixed(other._paramFixed),
92 _neval(0)
93{
96
97}
98
99
100////////////////////////////////////////////////////////////////////////////////
101
103{
105 return _paramAbsMin ;
106}
107
108
109////////////////////////////////////////////////////////////////////////////////
110
112{
114 return _obsAbsMin ;
115}
116
117
118
119
120////////////////////////////////////////////////////////////////////////////////
121/// Optimized implementation of createProfile for profile likelihoods.
122/// Return profile of original function in terms of stated parameters
123/// of interest rather than profiling recursively.
124
126{
127 return nll().createProfile(paramsOfInterest) ;
128}
129
130
131
132
133////////////////////////////////////////////////////////////////////////////////
134
136{
137 coutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") Creating instance of MINUIT" << endl ;
138
141 _minimizer = std::make_unique<RooMinimizer>(const_cast<RooAbsReal&>(_nll.arg())) ;
143
144}
145
146
147
148////////////////////////////////////////////////////////////////////////////////
149/// Evaluate profile likelihood by minimizing likelihood w.r.t. all
150/// parameters that are not considered observables of this profile
151/// likelihood object.
152
154{
155 // Instantiate minimizer if we haven't done that already
156 if (!_minimizer) {
158 }
159
160 // Save current value of observables
161 RooArgSet obsSetOrig;
162 _obs.snapshot(obsSetOrig) ;
163
165
166
167 // Set all observables constant in the minimization
168 const_cast<RooSetProxy&>(_obs).setAttribAll("Constant",kTRUE) ;
169 ccoutP(Eval) << "." ; ccoutP(Eval).flush() ;
170
171 // If requested set initial parameters to those corresponding to absolute minimum
172 if (_startFromMin) {
174 }
175
176 _minimizer->zeroEvalCount() ;
177
178 //TString minim=::ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
179 //TString algorithm = ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str();
180 //if (algorithm == "Migrad") algorithm = "Minimize"; // prefer to use Minimize instead of Migrad
181 //_minimizer->minimize(minim.Data(),algorithm.Data());
182
183 _minimizer->migrad() ;
184 _neval = _minimizer->evalCounter() ;
185
186 // Restore original values and constant status of observables
187 for(auto const& arg : obsSetOrig) {
188 assert(dynamic_cast<RooRealVar*>(arg));
189 auto var = static_cast<RooRealVar*>(arg);
190 auto target = static_cast<RooRealVar*>(_obs.find(var->GetName())) ;
191 target->setVal(var->getVal()) ;
192 target->setConstant(var->isConstant()) ;
193 }
194
195 return _nll - _absMin ;
196}
197
198
199
200////////////////////////////////////////////////////////////////////////////////
201/// Check that parameters and likelihood value for 'best fit' are still valid. If not,
202/// because the best fit has never been calculated, or because constant parameters have
203/// changed value or parameters have changed const/float status, the minimum is recalculated
204
206{
207 // Check if constant status of any of the parameters have changed
208 if (_absMinValid) {
209 for(auto const& par : _par) {
210 if (_paramFixed[par->GetName()] != par->isConstant()) {
211 cxcoutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") constant status of parameter " << par->GetName() << " has changed from "
212 << (_paramFixed[par->GetName()]?"fixed":"floating") << " to " << (par->isConstant()?"fixed":"floating")
213 << ", recalculating absolute minimum" << endl ;
215 break ;
216 }
217 }
218 }
219
220
221 // If we don't have the absolute minimum w.r.t all observables, calculate that first
222 if (!_absMinValid) {
223
224 cxcoutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") determining minimum likelihood for current configurations w.r.t all observable" << endl ;
225
226
227 if (!_minimizer) {
229 }
230
231 // Save current values of non-marginalized parameters
232 RooArgSet obsStart;
233 _obs.snapshot(obsStart, false) ;
234
235 // Start from previous global minimum
236 if (_paramAbsMin.getSize()>0) {
237 const_cast<RooSetProxy&>(_par).assignValueOnly(_paramAbsMin) ;
238 }
239 if (_obsAbsMin.getSize()>0) {
240 const_cast<RooSetProxy&>(_obs).assignValueOnly(_obsAbsMin) ;
241 }
242
243 // Find minimum with all observables floating
244 const_cast<RooSetProxy&>(_obs).setAttribAll("Constant",kFALSE) ;
245
246 //TString minim=::ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
247 //TString algorithm = ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str();
248 //if (algorithm == "Migrad") algorithm = "Minimize"; // prefer to use Minimize instead of Migrad
249 //_minimizer->minimize(minim.Data(),algorithm.Data());
250 _minimizer->migrad() ;
251
252 // Save value and remember
253 _absMin = _nll ;
255
256 // Save parameter values at abs minimum as well
258
259 // Only store non-constant parameters here!
260 _paramAbsMin.addClone(*std::unique_ptr<RooArgSet>{static_cast<RooArgSet*>(_par.selectByAttrib("Constant",false))});
261
263
264 // Save constant status of all parameters
265 for(auto const& par : _par) {
266 _paramFixed[par->GetName()] = par->isConstant() ;
267 }
268
269 if (dologI(Minimization)) {
270 cxcoutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") minimum found at (" ;
271
273 for(auto const& arg : _obs) {
274 ccxcoutI(Minimization) << (first?"":", ") << arg->GetName() << "="
275 << static_cast<RooAbsReal const*>(arg)->getVal() ;
276 first=kFALSE ;
277 }
278 ccxcoutI(Minimization) << ")" << endl ;
279 }
280
281 // Restore original parameter values
282 _obs.assign(obsStart) ;
283
284 }
285}
286
287
288
289////////////////////////////////////////////////////////////////////////////////
290
291Bool_t RooProfileLL::redirectServersHook(const RooAbsCollection& /*newServerList*/, Bool_t /*mustReplaceAll*/,
292 Bool_t /*nameChange*/, Bool_t /*isRecursive*/)
293{
294 _minimizer.reset(nullptr);
295 return kFALSE ;
296}
297
298
#define coutI(a)
Definition: RooMsgService.h:30
#define cxcoutI(a)
Definition: RooMsgService.h:85
#define ccoutP(a)
Definition: RooMsgService.h:39
#define dologI(a)
Definition: RooMsgService.h:66
#define ccxcoutI(a)
Definition: RooMsgService.h:86
const Bool_t kFALSE
Definition: RtypesCore.h:101
bool Bool_t
Definition: RtypesCore.h:63
double Double_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:364
char name[80]
Definition: TGX11.cxx:110
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Given a set of possible observables, return the observables that this PDF depends on.
Definition: RooAbsArg.h:295
RooArgSet * getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
Definition: RooAbsArg.cxx:578
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
Int_t getSize() const
virtual RooAbsArg * addClone(const RooAbsArg &var, Bool_t silent=kFALSE)
Add a clone of the specified argument to list.
void assign(const RooAbsCollection &other) const
Sets the value, cache and constant attribute of any argument in our set that also appears in the othe...
RooAbsCollection * selectByAttrib(const char *name, Bool_t value) const
Create a subset of the current collection, consisting only of those elements with the specified attri...
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:61
virtual RooAbsReal * createProfile(const RooArgSet &paramsOfInterest)
Create a RooProfileLL object that eliminates all nuisance parameters in the present function.
Definition: RooAbsReal.cxx:505
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:35
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:154
static RooMsgService & instance()
Return reference to singleton instance.
Bool_t silentMode() const
void setSilentMode(Bool_t flag)
Class RooProfileLL implements the profile likelihood estimator for a given likelihood and set of para...
Definition: RooProfileLL.h:22
const RooArgSet & bestFitObs() const
RooArgSet _paramAbsMin
Definition: RooProfileLL.h:64
RooSetProxy _obs
Definition: RooProfileLL.h:53
RooProfileLL()
Default constructor Should only be used by proof.
void initializeMinimizer() const
RooAbsReal & nll()
Definition: RooProfileLL.h:34
std::map< std::string, bool > _paramFixed
Definition: RooProfileLL.h:66
Double_t evaluate() const
Evaluate profile likelihood by minimizing likelihood w.r.t.
std::unique_ptr< RooMinimizer > _minimizer
Iterator of profile likelihood output parameter(s)
Definition: RooProfileLL.h:60
RooArgSet _obsAbsMin
Definition: RooProfileLL.h:65
Bool_t _startFromMin
Definition: RooProfileLL.h:55
void validateAbsMin() const
Check that parameters and likelihood value for 'best fit' are still valid.
RooSetProxy _par
Definition: RooProfileLL.h:54
virtual RooAbsReal * createProfile(const RooArgSet &paramsOfInterest)
Optimized implementation of createProfile for profile likelihoods.
virtual Bool_t redirectServersHook(const RooAbsCollection &, Bool_t, Bool_t, Bool_t)
RooRealProxy _nll
Definition: RooProfileLL.h:52
Double_t _absMin
Definition: RooProfileLL.h:63
const RooArgSet & bestFitParams() const
Bool_t _absMinValid
Internal minimizer instance.
Definition: RooProfileLL.h:62
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:39
virtual void setVal(Double_t value)
Set value of variable to 'value'.
Definition: RooRealVar.cxx:282
RooSetProxy is the concrete proxy for RooArgSet objects.
Definition: RooSetProxy.h:23
const T & arg() const
Return reference to object held in proxy.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
@ Minimization
Definition: RooGlobalFunc.h:60
Definition: first.py:1