Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "RooMinuit.h"
31#include "RooMinimizer.h"
32#include "RooMsgService.h"
33#include "RooRealVar.h"
34
35using namespace std ;
36
38
39
40////////////////////////////////////////////////////////////////////////////////
41/// Default constructor
42/// Should only be used by proof.
43
45 RooAbsReal("RooProfileLL","RooProfileLL"),
46 _nll(),
47 _obs("paramOfInterest","Parameters of interest",this),
48 _par("nuisanceParam","Nuisance parameters",this,kFALSE,kFALSE),
49 _startFromMin(kTRUE),
50 _minimizer(0),
51 _absMinValid(kFALSE),
52 _absMin(0),
53 _neval(0)
54{
57}
58
59
60////////////////////////////////////////////////////////////////////////////////
61/// Constructor of profile likelihood given input likelihood nll w.r.t
62/// the given set of variables. The input log likelihood is minimized w.r.t
63/// to all other variables of the likelihood at each evaluation and the
64/// value of the global log likelihood minimum is always subtracted.
65
66RooProfileLL::RooProfileLL(const char *name, const char *title,
67 RooAbsReal& nllIn, const RooArgSet& observables) :
68 RooAbsReal(name,title),
69 _nll("input","-log(L) function",this,nllIn),
70 _obs("paramOfInterest","Parameters of interest",this),
71 _par("nuisanceParam","Nuisance parameters",this,kFALSE,kFALSE),
72 _startFromMin(kTRUE),
73 _minimizer(0),
74 _absMinValid(kFALSE),
75 _absMin(0),
76 _neval(0)
77{
78 // Determine actual parameters and observables
79 RooArgSet* actualObs = nllIn.getObservables(observables) ;
80 RooArgSet* actualPars = nllIn.getParameters(observables) ;
81
82 _obs.add(*actualObs) ;
83 _par.add(*actualPars) ;
84
85 delete actualObs ;
86 delete actualPars ;
87
90}
91
92
93
94////////////////////////////////////////////////////////////////////////////////
95/// Copy constructor
96
97RooProfileLL::RooProfileLL(const RooProfileLL& other, const char* name) :
98 RooAbsReal(other,name),
99 _nll("nll",this,other._nll),
100 _obs("obs",this,other._obs),
101 _par("par",this,other._par),
102 _startFromMin(other._startFromMin),
103 _minimizer(0),
104 _absMinValid(kFALSE),
105 _absMin(0),
106 _paramFixed(other._paramFixed),
107 _neval(0)
108{
111
114
115}
116
117
118
119////////////////////////////////////////////////////////////////////////////////
120/// Destructor
121
123{
124 // Delete instance of minuit if it was ever instantiated
125 if (_minimizer) {
126 delete _minimizer ;
127 }
128
129 delete _piter ;
130 delete _oiter ;
131}
132
133
134
135
136////////////////////////////////////////////////////////////////////////////////
137
139{
141 return _paramAbsMin ;
142}
143
144
145////////////////////////////////////////////////////////////////////////////////
146
148{
150 return _obsAbsMin ;
151}
152
153
154
155
156////////////////////////////////////////////////////////////////////////////////
157/// Optimized implementation of createProfile for profile likelihoods.
158/// Return profile of original function in terms of stated parameters
159/// of interest rather than profiling recursively.
160
162{
163 return nll().createProfile(paramsOfInterest) ;
164}
165
166
167
168
169////////////////////////////////////////////////////////////////////////////////
170
172{
173 coutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") Creating instance of MINUIT" << endl ;
174
177 _minimizer = new MINIMIZER(const_cast<RooAbsReal&>(_nll.arg())) ;
179
180 //_minimizer->setPrintLevel(-999) ;
181 //_minimizer->setNoWarn() ;
182 //_minimizer->setVerbose(1) ;
183}
184
185
186
187////////////////////////////////////////////////////////////////////////////////
188/// Evaluate profile likelihood by minimizing likelihood w.r.t. all
189/// parameters that are not considered observables of this profile
190/// likelihood object.
191
193{
194 // Instantiate minimizer if we haven't done that already
195 if (!_minimizer) {
197 }
198
199 // Save current value of observables
200 RooArgSet* obsSetOrig = (RooArgSet*) _obs.snapshot() ;
201
203
204
205 // Set all observables constant in the minimization
206 const_cast<RooSetProxy&>(_obs).setAttribAll("Constant",kTRUE) ;
207 ccoutP(Eval) << "." ; ccoutP(Eval).flush() ;
208
209 // If requested set initial parameters to those corresponding to absolute minimum
210 if (_startFromMin) {
211 const_cast<RooProfileLL&>(*this)._par = _paramAbsMin ;
212 }
213
214 _minimizer->zeroEvalCount() ;
215
216 //TString minim=::ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
217 //TString algorithm = ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str();
218 //if (algorithm == "Migrad") algorithm = "Minimize"; // prefer to use Minimize instead of Migrad
219 //_minimizer->minimize(minim.Data(),algorithm.Data());
220
221 _minimizer->migrad() ;
222 _neval = _minimizer->evalCounter() ;
223
224 // Restore original values and constant status of observables
225 TIterator* iter = obsSetOrig->createIterator() ;
226 RooRealVar* var ;
227 while((var=dynamic_cast<RooRealVar*>(iter->Next()) ) ) {
228 RooRealVar* target = (RooRealVar*) _obs.find(var->GetName()) ;
229 target->setVal(var->getVal()) ;
230 target->setConstant(var->isConstant()) ;
231 }
232 delete iter ;
233 delete obsSetOrig ;
234
235 return _nll - _absMin ;
236}
237
238
239
240////////////////////////////////////////////////////////////////////////////////
241/// Check that parameters and likelihood value for 'best fit' are still valid. If not,
242/// because the best fit has never been calculated, or because constant parameters have
243/// changed value or parameters have changed const/float status, the minimum is recalculated
244
246{
247 // Check if constant status of any of the parameters have changed
248 if (_absMinValid) {
249 _piter->Reset() ;
250 RooAbsArg* par ;
251 while((par=(RooAbsArg*)_piter->Next())) {
252 if (_paramFixed[par->GetName()] != par->isConstant()) {
253 cxcoutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") constant status of parameter " << par->GetName() << " has changed from "
254 << (_paramFixed[par->GetName()]?"fixed":"floating") << " to " << (par->isConstant()?"fixed":"floating")
255 << ", recalculating absolute minimum" << endl ;
257 break ;
258 }
259 }
260 }
261
262
263 // If we don't have the absolute minimum w.r.t all observables, calculate that first
264 if (!_absMinValid) {
265
266 cxcoutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") determining minimum likelihood for current configurations w.r.t all observable" << endl ;
267
268
269 if (!_minimizer) {
271 }
272
273 // Save current values of non-marginalized parameters
274 RooArgSet* obsStart = (RooArgSet*) _obs.snapshot(kFALSE) ;
275
276 // Start from previous global minimum
277 if (_paramAbsMin.getSize()>0) {
278 const_cast<RooSetProxy&>(_par).assignValueOnly(_paramAbsMin) ;
279 }
280 if (_obsAbsMin.getSize()>0) {
281 const_cast<RooSetProxy&>(_obs).assignValueOnly(_obsAbsMin) ;
282 }
283
284 // Find minimum with all observables floating
285 const_cast<RooSetProxy&>(_obs).setAttribAll("Constant",kFALSE) ;
286
287 //TString minim=::ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
288 //TString algorithm = ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str();
289 //if (algorithm == "Migrad") algorithm = "Minimize"; // prefer to use Minimize instead of Migrad
290 //_minimizer->minimize(minim.Data(),algorithm.Data());
291 _minimizer->migrad() ;
292
293 // Save value and remember
294 _absMin = _nll ;
296
297 // Save parameter values at abs minimum as well
299
300 // Only store non-constant parameters here!
301 RooArgSet* tmp = (RooArgSet*) _par.selectByAttrib("Constant",kFALSE) ;
302 _paramAbsMin.addClone(*tmp) ;
303 delete tmp ;
304
306
307 // Save constant status of all parameters
308 _piter->Reset() ;
309 RooAbsArg* par ;
310 while((par=(RooAbsArg*)_piter->Next())) {
311 _paramFixed[par->GetName()] = par->isConstant() ;
312 }
313
314 if (dologI(Minimization)) {
315 cxcoutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") minimum found at (" ;
316
317 RooAbsReal* arg ;
319 _oiter->Reset() ;
320 while ((arg=(RooAbsReal*)_oiter->Next())) {
321 ccxcoutI(Minimization) << (first?"":", ") << arg->GetName() << "=" << arg->getVal() ;
322 first=kFALSE ;
323 }
324 ccxcoutI(Minimization) << ")" << endl ;
325 }
326
327 // Restore original parameter values
328 const_cast<RooSetProxy&>(_obs) = *obsStart ;
329 delete obsStart ;
330
331 }
332}
333
334
335
336////////////////////////////////////////////////////////////////////////////////
337
338Bool_t RooProfileLL::redirectServersHook(const RooAbsCollection& /*newServerList*/, Bool_t /*mustReplaceAll*/,
339 Bool_t /*nameChange*/, Bool_t /*isRecursive*/)
340{
341 if (_minimizer) {
342 delete _minimizer ;
343 _minimizer = 0 ;
344 }
345 return kFALSE ;
346}
347
348
#define coutI(a)
#define cxcoutI(a)
#define ccoutP(a)
#define dologI(a)
#define ccxcoutI(a)
#define MINIMIZER
const Bool_t kFALSE
Definition RtypesCore.h:92
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:72
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:313
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...
Bool_t isConstant() const
Check if the "Constant" attribute is set.
Definition RooAbsArg.h:380
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
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...
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
RooAbsArg * find(const char *name) const
Find object with given name in list.
void setConstant(Bool_t value=kTRUE)
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.
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:29
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:118
RooAbsArg * addClone(const RooAbsArg &var, Bool_t silent=kFALSE) override
Add clone of specified element to an owning set.
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...
virtual ~RooProfileLL()
Destructor.
const RooArgSet & bestFitObs() const
RooArgSet _paramAbsMin
RooSetProxy _obs
RooProfileLL()
Default constructor Should only be used by proof.
void initializeMinimizer() const
TIterator * _piter
MINIMIZER * _minimizer
Iterator of profile likelihood output parameter(s)
RooAbsReal & nll()
std::map< std::string, bool > _paramFixed
Double_t evaluate() const
Evaluate profile likelihood by minimizing likelihood w.r.t.
RooArgSet _obsAbsMin
Bool_t _startFromMin
TIterator * _oiter
Iterator over profile likelihood parameters to be minimized.
void validateAbsMin() const
Check that parameters and likelihood value for 'best fit' are still valid.
RooSetProxy _par
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)
Function that is called at the end of redirectServers().
RooRealProxy _nll
Double_t _absMin
const RooArgSet & bestFitParams() const
Bool_t _absMinValid
Internal minuit instance.
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'.
RooSetProxy is the concrete proxy for RooArgSet objects.
Definition RooSetProxy.h:23
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE) override
Overloaded RooArgSet::add() method inserts 'var' into set and registers 'var' as server to owner with...
const T & arg() const
Return reference to object held in proxy.
Iterator abstract base class.
Definition TIterator.h:30
virtual void Reset()=0
virtual TObject * Next()=0
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Definition first.py:1