Logo ROOT   6.18/05
Reference Guide
RooMinimizer.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * AL, Alfio Lazzaro, INFN Milan, alfio.lazzaro@mi.infn.it *
9 * *
10 * Redistribution and use in source and binary forms, *
11 * with or without modification, are permitted according to the terms *
12 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
13 *****************************************************************************/
14
15/**
16\file RooMinimizer.cxx
17\class RooMinimizer
18\ingroup Roofitcore
19
20RooMinimizer is a wrapper class around ROOT::Fit:Fitter that
21provides a seamless interface between the minimizer functionality
22and the native RooFit interface.
23By default the Minimizer is MINUIT.
24RooMinimizer can minimize any RooAbsReal function with respect to
25its parameters. Usual choices for minimization are RooNLLVar
26and RooChi2Var
27RooMinimizer has methods corresponding to MINUIT functions like
28hesse(), migrad(), minos() etc. In each of these function calls
29the state of the MINUIT engine is synchronized with the state
30of the RooFit variables: any change in variables, change
31in the constant status etc is forwarded to MINUIT prior to
32execution of the MINUIT call. Afterwards the RooFit objects
33are resynchronized with the output state of MINUIT: changes
34parameter values, errors are propagated.
35Various methods are available to control verbosity, profiling,
36automatic PDF optimization.
37**/
38
39#ifndef __ROOFIT_NOROOMINIMIZER
40
41#include "RooFit.h"
42#include "Riostream.h"
43
44#include "TClass.h"
45
46#include <fstream>
47
48#include "TH1.h"
49#include "TH2.h"
50#include "TMarker.h"
51#include "TGraph.h"
52#include "Fit/FitConfig.h"
53#include "TStopwatch.h"
54#include "TDirectory.h"
55#include "TMatrixDSym.h"
56
57#include "RooArgSet.h"
58#include "RooArgList.h"
59#include "RooAbsReal.h"
60#include "RooAbsRealLValue.h"
61#include "RooRealVar.h"
62#include "RooAbsPdf.h"
63#include "RooSentinel.h"
64#include "RooMsgService.h"
65#include "RooPlot.h"
66
67
68#include "RooMinimizer.h"
69#include "RooFitResult.h"
70
71#include "Math/Minimizer.h"
72
73#if (__GNUC__==3&&__GNUC_MINOR__==2&&__GNUC_PATCHLEVEL__==3)
74char* operator+( streampos&, char* );
75#endif
76
77using namespace std;
78
80;
81
83
84
85
86////////////////////////////////////////////////////////////////////////////////
87/// Cleanup method called by atexit handler installed by RooSentinel
88/// to delete all global heap objects when the program is terminated
89
91{
92 if (_theFitter) {
93 delete _theFitter ;
94 _theFitter =0 ;
95 }
96}
97
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Construct MINUIT interface to given function. Function can be anything,
102/// but is typically a -log(likelihood) implemented by RooNLLVar or a chi^2
103/// (implemented by RooChi2Var). Other frequent use cases are a RooAddition
104/// of a RooNLLVar plus a penalty or constraint term. This class propagates
105/// all RooFit information (floating parameters, their values and errors)
106/// to MINUIT before each MINUIT call and propagates all MINUIT information
107/// back to the RooFit object at the end of each call (updated parameter
108/// values, their (asymmetric errors) etc. The default MINUIT error level
109/// for HESSE and MINOS error analysis is taken from the defaultErrorLevel()
110/// value of the input function.
111
113{
115
116 // Store function reference
117 _extV = 0 ;
118 _func = &function ;
119 _optConst = kFALSE ;
120 _verbose = kFALSE ;
121 _profile = kFALSE ;
123 _printLevel = 1 ;
124 _minimizerType = "Minuit"; // default minimizer
125
126 if (_theFitter) delete _theFitter ;
130 setEps(1.0); // default tolerance
131 // default max number of calls
134
135 // Shut up for now
136 setPrintLevel(-1) ;
137
138 // Use +0.5 for 1-sigma errors
140
141 // Declare our parameters to MINUIT
144
145 // Now set default verbosity
146 if (RooMsgService::instance().silentMode()) {
147 setPrintLevel(-1) ;
148 } else {
149 setPrintLevel(1) ;
150 }
151}
152
153
154
155////////////////////////////////////////////////////////////////////////////////
156/// Destructor
157
159{
160 if (_extV) {
161 delete _extV ;
162 }
163
164 if (_fcn) {
165 delete _fcn;
166 }
167
168}
169
170
171
172////////////////////////////////////////////////////////////////////////////////
173/// Change MINUIT strategy to istrat. Accepted codes
174/// are 0,1,2 and represent MINUIT strategies for dealing
175/// most efficiently with fast FCNs (0), expensive FCNs (2)
176/// and 'intermediate' FCNs (1)
177
179{
181
182}
183
184
185
186////////////////////////////////////////////////////////////////////////////////
187/// Change maximum number of MINUIT iterations
188/// (RooMinimizer default 500 * #parameters)
189
191{
193}
194
195
196
197
198////////////////////////////////////////////////////////////////////////////////
199/// Change maximum number of likelihood function calss from MINUIT
200/// (RooMinimizer default 500 * #parameters)
201
203{
205}
206
207
208
209
210////////////////////////////////////////////////////////////////////////////////
211/// Set the level for MINUIT error analysis to the given
212/// value. This function overrides the default value
213/// that is taken in the RooMinimizer constructor from
214/// the defaultErrorLevel() method of the input function
215
217{
219
220}
221
222
223
224////////////////////////////////////////////////////////////////////////////////
225/// Change MINUIT epsilon
226
228{
230
231}
232
233
234////////////////////////////////////////////////////////////////////////////////
235/// Enable internal likelihood offsetting for enhanced numeric precision
236
238{
239 _func->enableOffsetting(flag) ;
240}
241
242
243
244
245////////////////////////////////////////////////////////////////////////////////
246/// Choose the minimzer algorithm.
247
249{
251}
252
253
254
255
256////////////////////////////////////////////////////////////////////////////////
257/// Return underlying ROOT fitter object
258
260{
261 return _theFitter ;
262}
263
264
265////////////////////////////////////////////////////////////////////////////////
266/// Return underlying ROOT fitter object
267
269{
270 return _theFitter ;
271}
272
273
274
275////////////////////////////////////////////////////////////////////////////////
276/// Parse traditional RooAbsPdf::fitTo driver options
277///
278/// m - Run Migrad only
279/// h - Run Hesse to estimate errors
280/// v - Verbose mode
281/// l - Log parameters after each Minuit steps to file
282/// t - Activate profile timer
283/// r - Save fit result
284/// 0 - Run Migrad with strategy 0
285
286RooFitResult* RooMinimizer::fit(const char* options)
287{
288 TString opts(options) ;
289 opts.ToLower() ;
290
291 // Initial configuration
292 if (opts.Contains("v")) setVerbose(1) ;
293 if (opts.Contains("t")) setProfile(1) ;
294 if (opts.Contains("l")) setLogFile(Form("%s.log",_func->GetName())) ;
295 if (opts.Contains("c")) optimizeConst(1) ;
296
297 // Fitting steps
298 if (opts.Contains("0")) setStrategy(0) ;
299 migrad() ;
300 if (opts.Contains("0")) setStrategy(1) ;
301 if (opts.Contains("h")||!opts.Contains("m")) hesse() ;
302 if (!opts.Contains("m")) minos() ;
303
304 return (opts.Contains("r")) ? save() : 0 ;
305}
306
307
308
309
310////////////////////////////////////////////////////////////////////////////////
311
312Int_t RooMinimizer::minimize(const char* type, const char* alg)
313{
316
318
319 profileStart() ;
322
323 bool ret = _theFitter->FitFCN(*_fcn);
324 _status = ((ret) ? _theFitter->Result().Status() : -1);
325
327 profileStop() ;
329
330 saveStatus("MINIMIZE",_status) ;
331
332 return _status ;
333}
334
335
336
337////////////////////////////////////////////////////////////////////////////////
338/// Execute MIGRAD. Changes in parameter values
339/// and calculated errors are automatically
340/// propagated back the RooRealVars representing
341/// the floating parameters in the MINUIT operation
342
344{
347 profileStart() ;
350
351 _theFitter->Config().SetMinimizer(_minimizerType.c_str(),"migrad");
352 bool ret = _theFitter->FitFCN(*_fcn);
353 _status = ((ret) ? _theFitter->Result().Status() : -1);
354
356 profileStop() ;
358
359 saveStatus("MIGRAD",_status) ;
360
361 return _status ;
362}
363
364
365
366////////////////////////////////////////////////////////////////////////////////
367/// Execute HESSE. Changes in parameter values
368/// and calculated errors are automatically
369/// propagated back the RooRealVars representing
370/// the floating parameters in the MINUIT operation
371
373{
374 if (_theFitter->GetMinimizer()==0) {
375 coutW(Minimization) << "RooMinimizer::hesse: Error, run Migrad before Hesse!"
376 << endl ;
377 _status = -1;
378 }
379 else {
380
383 profileStart() ;
386
388 bool ret = _theFitter->CalculateHessErrors();
389 _status = ((ret) ? _theFitter->Result().Status() : -1);
390
392 profileStop() ;
394
395 saveStatus("HESSE",_status) ;
396
397 }
398
399 return _status ;
400
401}
402
403////////////////////////////////////////////////////////////////////////////////
404/// Execute MINOS. Changes in parameter values
405/// and calculated errors are automatically
406/// propagated back the RooRealVars representing
407/// the floating parameters in the MINUIT operation
408
410{
411 if (_theFitter->GetMinimizer()==0) {
412 coutW(Minimization) << "RooMinimizer::minos: Error, run Migrad before Minos!"
413 << endl ;
414 _status = -1;
415 }
416 else {
417
420 profileStart() ;
423
425 bool ret = _theFitter->CalculateMinosErrors();
426 _status = ((ret) ? _theFitter->Result().Status() : -1);
427
429 profileStop() ;
431
432 saveStatus("MINOS",_status) ;
433
434 }
435
436 return _status ;
437
438}
439
440
441////////////////////////////////////////////////////////////////////////////////
442/// Execute MINOS for given list of parameters. Changes in parameter values
443/// and calculated errors are automatically
444/// propagated back the RooRealVars representing
445/// the floating parameters in the MINUIT operation
446
447Int_t RooMinimizer::minos(const RooArgSet& minosParamList)
448{
449 if (_theFitter->GetMinimizer()==0) {
450 coutW(Minimization) << "RooMinimizer::minos: Error, run Migrad before Minos!"
451 << endl ;
452 _status = -1;
453 }
454 else if (minosParamList.getSize()>0) {
455
458 profileStart() ;
461
462 // get list of parameters for Minos
463 TIterator* aIter = minosParamList.createIterator() ;
464 RooAbsArg* arg ;
465 std::vector<unsigned int> paramInd;
466 while((arg=(RooAbsArg*)aIter->Next())) {
467 RooAbsArg* par = _fcn->GetFloatParamList()->find(arg->GetName());
468 if (par && !par->isConstant()) {
469 Int_t index = _fcn->GetFloatParamList()->index(par);
470 paramInd.push_back(index);
471 }
472 }
473 delete aIter ;
474
475 if (paramInd.size()) {
476 // set the parameter indeces
477 _theFitter->Config().SetMinosErrors(paramInd);
478
480 bool ret = _theFitter->CalculateMinosErrors();
481 _status = ((ret) ? _theFitter->Result().Status() : -1);
482 // to avoid that following minimization computes automatically the Minos errors
484
485 }
486
488 profileStop() ;
490
491 saveStatus("MINOS",_status) ;
492
493 }
494
495 return _status ;
496}
497
498
499
500////////////////////////////////////////////////////////////////////////////////
501/// Execute SEEK. Changes in parameter values
502/// and calculated errors are automatically
503/// propagated back the RooRealVars representing
504/// the floating parameters in the MINUIT operation
505
507{
510 profileStart() ;
513
514 _theFitter->Config().SetMinimizer(_minimizerType.c_str(),"seek");
515 bool ret = _theFitter->FitFCN(*_fcn);
516 _status = ((ret) ? _theFitter->Result().Status() : -1);
517
519 profileStop() ;
521
522 saveStatus("SEEK",_status) ;
523
524 return _status ;
525}
526
527
528
529////////////////////////////////////////////////////////////////////////////////
530/// Execute SIMPLEX. Changes in parameter values
531/// and calculated errors are automatically
532/// propagated back the RooRealVars representing
533/// the floating parameters in the MINUIT operation
534
536{
539 profileStart() ;
542
543 _theFitter->Config().SetMinimizer(_minimizerType.c_str(),"simplex");
544 bool ret = _theFitter->FitFCN(*_fcn);
545 _status = ((ret) ? _theFitter->Result().Status() : -1);
546
548 profileStop() ;
550
551 saveStatus("SEEK",_status) ;
552
553 return _status ;
554}
555
556
557
558////////////////////////////////////////////////////////////////////////////////
559/// Execute IMPROVE. Changes in parameter values
560/// and calculated errors are automatically
561/// propagated back the RooRealVars representing
562/// the floating parameters in the MINUIT operation
563
565{
568 profileStart() ;
571
572 _theFitter->Config().SetMinimizer(_minimizerType.c_str(),"migradimproved");
573 bool ret = _theFitter->FitFCN(*_fcn);
574 _status = ((ret) ? _theFitter->Result().Status() : -1);
575
577 profileStop() ;
579
580 saveStatus("IMPROVE",_status) ;
581
582 return _status ;
583}
584
585
586
587////////////////////////////////////////////////////////////////////////////////
588/// Change the MINUIT internal printing level
589
591{
592 Int_t ret = _printLevel ;
594 _printLevel = newLevel+1 ;
595 return ret ;
596}
597
598////////////////////////////////////////////////////////////////////////////////
599/// If flag is true, perform constant term optimization on
600/// function being minimized.
601
603{
605
606 if (_optConst && !flag){
607 if (_printLevel>-1) coutI(Minimization) << "RooMinimizer::optimizeConst: deactivating const optimization" << endl ;
609 _optConst = flag ;
610 } else if (!_optConst && flag) {
611 if (_printLevel>-1) coutI(Minimization) << "RooMinimizer::optimizeConst: activating const optimization" << endl ;
613 _optConst = flag ;
614 } else if (_optConst && flag) {
615 if (_printLevel>-1) coutI(Minimization) << "RooMinimizer::optimizeConst: const optimization already active" << endl ;
616 } else {
617 if (_printLevel>-1) coutI(Minimization) << "RooMinimizer::optimizeConst: const optimization wasn't active" << endl ;
618 }
619
621
622}
623
624
625
626////////////////////////////////////////////////////////////////////////////////
627/// Save and return a RooFitResult snaphot of current minimizer status.
628/// This snapshot contains the values of all constant parameters,
629/// the value of all floating parameters at RooMinimizer construction and
630/// after the last MINUIT operation, the MINUIT status, variance quality,
631/// EDM setting, number of calls with evaluation problems, the minimized
632/// function value and the full correlation matrix
633
634RooFitResult* RooMinimizer::save(const char* userName, const char* userTitle)
635{
636 if (_theFitter->GetMinimizer()==0) {
637 coutW(Minimization) << "RooMinimizer::save: Error, run minimization before!"
638 << endl ;
639 return 0;
640 }
641
642 TString name,title ;
643 name = userName ? userName : Form("%s", _func->GetName()) ;
644 title = userTitle ? userTitle : Form("%s", _func->GetTitle()) ;
645 RooFitResult* fitRes = new RooFitResult(name,title) ;
646
647 // Move eventual fixed parameters in floatList to constList
648 Int_t i ;
649 RooArgList saveConstList(*(_fcn->GetConstParamList())) ;
650 RooArgList saveFloatInitList(*(_fcn->GetInitFloatParamList())) ;
651 RooArgList saveFloatFinalList(*(_fcn->GetFloatParamList())) ;
652 for (i=0 ; i<_fcn->GetFloatParamList()->getSize() ; i++) {
653 RooAbsArg* par = _fcn->GetFloatParamList()->at(i) ;
654 if (par->isConstant()) {
655 saveFloatInitList.remove(*saveFloatInitList.find(par->GetName()),kTRUE) ;
656 saveFloatFinalList.remove(*par) ;
657 saveConstList.add(*par) ;
658 }
659 }
660 saveConstList.sort() ;
661
662 fitRes->setConstParList(saveConstList) ;
663 fitRes->setInitParList(saveFloatInitList) ;
664
665 fitRes->setStatus(_status) ;
667 fitRes->setMinNLL(_theFitter->Result().MinFcnValue()) ;
669 fitRes->setEDM(_theFitter->Result().Edm()) ;
670 fitRes->setFinalParList(saveFloatFinalList) ;
671 if (!_extV) {
672 std::vector<double> globalCC;
673 TMatrixDSym corrs(_theFitter->Result().Parameters().size()) ;
674 TMatrixDSym covs(_theFitter->Result().Parameters().size()) ;
675 for (UInt_t ic=0; ic<_theFitter->Result().Parameters().size(); ic++) {
676 globalCC.push_back(_theFitter->Result().GlobalCC(ic));
677 for (UInt_t ii=0; ii<_theFitter->Result().Parameters().size(); ii++) {
678 corrs(ic,ii) = _theFitter->Result().Correlation(ic,ii);
679 covs(ic,ii) = _theFitter->Result().CovMatrix(ic,ii);
680 }
681 }
682 fitRes->fillCorrMatrix(globalCC,corrs,covs) ;
683 } else {
684 fitRes->setCovarianceMatrix(*_extV) ;
685 }
686
688
689 return fitRes ;
690
691}
692
693////////////////////////////////////////////////////////////////////////////////
694/// Create and draw a TH2 with the error contours in the parameters `var1` and `var2`.
695/// \param[in] var1 The first parameter (x axis).
696/// \param[in] var2 The second parameter (y axis).
697/// \param[in] n1 First contour.
698/// \param[in] n2 Optional contour. 0 means don't draw.
699/// \param[in] n3 Optional contour. 0 means don't draw.
700/// \param[in] n4 Optional contour. 0 means don't draw.
701/// \param[in] n5 Optional contour. 0 means don't draw.
702/// \param[in] n6 Optional contour. 0 means don't draw.
703/// \param[in] npoints Number of points for evaluating the contour.
704///
705/// Up to six contours can be drawn using the arguments `n1` to `n6` to request the desired
706/// coverage in units of \f$ \sigma = n^2 \cdot \mathrm{ErrorDef} \f$.
707/// See ROOT::Math::Minimizer::ErrorDef().
708
710 Double_t n1, Double_t n2, Double_t n3,
711 Double_t n4, Double_t n5, Double_t n6, unsigned int npoints)
712{
713
714
715 RooArgList* params = _fcn->GetFloatParamList() ;
716 RooArgList* paramSave = (RooArgList*) params->snapshot() ;
717
718 // Verify that both variables are floating parameters of PDF
719 Int_t index1= _fcn->GetFloatParamList()->index(&var1);
720 if(index1 < 0) {
721 coutE(Minimization) << "RooMinimizer::contour(" << GetName()
722 << ") ERROR: " << var1.GetName()
723 << " is not a floating parameter of "
724 << _func->GetName() << endl ;
725 return 0;
726 }
727
728 Int_t index2= _fcn->GetFloatParamList()->index(&var2);
729 if(index2 < 0) {
730 coutE(Minimization) << "RooMinimizer::contour(" << GetName()
731 << ") ERROR: " << var2.GetName()
732 << " is not a floating parameter of PDF "
733 << _func->GetName() << endl ;
734 return 0;
735 }
736
737 // create and draw a frame
738 RooPlot* frame = new RooPlot(var1,var2) ;
739
740 // draw a point at the current parameter values
741 TMarker *point= new TMarker(var1.getVal(), var2.getVal(), 8);
742 frame->addObject(point) ;
743
744 // check first if a inimizer is available. If not means
745 // the minimization is not done , so do it
746 if (_theFitter->GetMinimizer()==0) {
747 coutW(Minimization) << "RooMinimizer::contour: Error, run Migrad before contours!"
748 << endl ;
749 return frame;
750 }
751
752
753 // remember our original value of ERRDEF
755
756 Double_t n[6] ;
757 n[0] = n1 ; n[1] = n2 ; n[2] = n3 ; n[3] = n4 ; n[4] = n5 ; n[5] = n6 ;
758
759 for (Int_t ic = 0 ; ic<6 ; ic++) {
760 if(n[ic] > 0) {
761
762 // set the value corresponding to an n1-sigma contour
763 _theFitter->GetMinimizer()->SetErrorDef(n[ic]*n[ic]*errdef);
764
765 // calculate and draw the contour
766 Double_t *xcoor = new Double_t[npoints+1];
767 Double_t *ycoor = new Double_t[npoints+1];
768 bool ret = _theFitter->GetMinimizer()->Contour(index1,index2,npoints,xcoor,ycoor);
769
770 if (!ret) {
771 coutE(Minimization) << "RooMinimizer::contour("
772 << GetName()
773 << ") ERROR: MINUIT did not return a contour graph for n="
774 << n[ic] << endl ;
775 } else {
776 xcoor[npoints] = xcoor[0];
777 ycoor[npoints] = ycoor[0];
778 TGraph* graph = new TGraph(npoints+1,xcoor,ycoor);
779
780 graph->SetName(Form("contour_%s_n%f",_func->GetName(),n[ic])) ;
781 graph->SetLineStyle(ic+1) ;
782 graph->SetLineWidth(2) ;
783 graph->SetLineColor(kBlue) ;
784 frame->addObject(graph,"L") ;
785 }
786
787 delete [] xcoor;
788 delete [] ycoor;
789 }
790 }
791
792
793 // restore the original ERRDEF
795
796 // restore parameter values
797 *params = *paramSave ;
798 delete paramSave ;
799
800 return frame ;
801
802}
803
804
805////////////////////////////////////////////////////////////////////////////////
806/// Start profiling timer
807
809{
810 if (_profile) {
811 _timer.Start() ;
814 }
815}
816
817
818////////////////////////////////////////////////////////////////////////////////
819/// Stop profiling timer and report results of last session
820
822{
823 if (_profile) {
824 _timer.Stop() ;
825 _cumulTimer.Stop() ;
826 coutI(Minimization) << "Command timer: " ; _timer.Print() ;
827 coutI(Minimization) << "Session timer: " ; _cumulTimer.Print() ;
828 }
829}
830
831
832
833
834
835////////////////////////////////////////////////////////////////////////////////
836/// Apply results of given external covariance matrix. i.e. propagate its errors
837/// to all RRV parameter representations and give this matrix instead of the
838/// HESSE matrix at the next save() call
839
841{
842 _extV = (TMatrixDSym*) V.Clone() ;
844
845}
846
847
848
850{
851 // Import the results of the last fit performed, interpreting
852 // the fit parameters as the given varList of parameters.
853
854 if (_theFitter==0 || _theFitter->GetMinimizer()==0) {
855 oocoutE((TObject*)0,InputArguments) << "RooMinimizer::save: Error, run minimization before!"
856 << endl ;
857 return 0;
858 }
859
860 // Verify length of supplied varList
861 if (varList.getSize()>0 && varList.getSize()!=Int_t(_theFitter->Result().NTotalParameters())) {
863 << "RooMinimizer::lastMinuitFit: ERROR: supplied variable list must be either empty " << endl
864 << " or match the number of variables of the last fit ("
865 << _theFitter->Result().NTotalParameters() << ")" << endl ;
866 return 0 ;
867 }
868
869
870 // Verify that all members of varList are of type RooRealVar
871 TIterator* iter = varList.createIterator() ;
872 RooAbsArg* arg ;
873 while((arg=(RooAbsArg*)iter->Next())) {
874 if (!dynamic_cast<RooRealVar*>(arg)) {
875 oocoutE((TObject*)0,InputArguments) << "RooMinimizer::lastMinuitFit: ERROR: variable '"
876 << arg->GetName() << "' is not of type RooRealVar" << endl ;
877 return 0 ;
878 }
879 }
880 delete iter ;
881
882 RooFitResult* res = new RooFitResult("lastMinuitFit","Last MINUIT fit") ;
883
884 // Extract names of fit parameters
885 // and construct corresponding RooRealVars
886 RooArgList constPars("constPars") ;
887 RooArgList floatPars("floatPars") ;
888
889 UInt_t i ;
890 for (i = 0; i < _theFitter->Result().NTotalParameters(); ++i) {
891
894
897 Double_t xerr = _theFitter->Result().Error(i);
898 Double_t xval = _theFitter->Result().Value(i);
899
900 RooRealVar* var ;
901 if (varList.getSize()==0) {
902
903 if ((xlo<xhi) && !isConst) {
904 var = new RooRealVar(varName,varName,xval,xlo,xhi) ;
905 } else {
906 var = new RooRealVar(varName,varName,xval) ;
907 }
908 var->setConstant(isConst) ;
909 } else {
910
911 var = (RooRealVar*) varList.at(i)->Clone() ;
912 var->setConstant(isConst) ;
913 var->setVal(xval) ;
914 if (xlo<xhi) {
915 var->setRange(xlo,xhi) ;
916 }
917
918 if (varName.CompareTo(var->GetName())) {
919 oocoutI((TObject*)0,Eval) << "RooMinimizer::lastMinuitFit: fit parameter '" << varName
920 << "' stored in variable '" << var->GetName() << "'" << endl ;
921 }
922
923 }
924
925 if (isConst) {
926 constPars.addOwned(*var) ;
927 } else {
928 var->setError(xerr) ;
929 floatPars.addOwned(*var) ;
930 }
931 }
932
933 res->setConstParList(constPars) ;
934 res->setInitParList(floatPars) ;
935 res->setFinalParList(floatPars) ;
937 res->setEDM(_theFitter->Result().Edm()) ;
939 res->setStatus(_theFitter->Result().Status()) ;
940 std::vector<double> globalCC;
941 TMatrixDSym corrs(_theFitter->Result().Parameters().size()) ;
942 TMatrixDSym covs(_theFitter->Result().Parameters().size()) ;
943 for (UInt_t ic=0; ic<_theFitter->Result().Parameters().size(); ic++) {
944 globalCC.push_back(_theFitter->Result().GlobalCC(ic));
945 for (UInt_t ii=0; ii<_theFitter->Result().Parameters().size(); ii++) {
946 corrs(ic,ii) = _theFitter->Result().Correlation(ic,ii);
947 covs(ic,ii) = _theFitter->Result().CovMatrix(ic,ii);
948 }
949 }
950 res->fillCorrMatrix(globalCC,corrs,covs) ;
951
952 return res;
953
954}
955
956#endif
#define coutI(a)
Definition: RooMsgService.h:31
#define coutW(a)
Definition: RooMsgService.h:33
#define oocoutE(o, a)
Definition: RooMsgService.h:47
#define oocoutI(o, a)
Definition: RooMsgService.h:44
#define coutE(a)
Definition: RooMsgService.h:34
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
@ kBlue
Definition: Rtypes.h:64
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
char * Form(const char *fmt,...)
TString operator+(const TString &s1, const TString &s2)
Use the special concatenation constructor.
Definition: TString.cxx:1474
void SetMinosErrors(bool on=true)
set Minos erros computation to be performed after fitting
Definition: FitConfig.h:226
void SetMinimizer(const char *type, const char *algo=0)
set minimizer type
Definition: FitConfig.h:180
const std::vector< ROOT::Fit::ParameterSettings > & ParamsSettings() const
get the vector of parameter settings (const method)
Definition: FitConfig.h:85
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
Definition: FitConfig.h:75
ROOT::Math::MinimizerOptions & MinimizerOptions()
access to the minimizer control parameter (non const method)
Definition: FitConfig.h:166
bool IsParameterFixed(unsigned int ipar) const
query if a parameter is fixed
Definition: FitResult.cxx:422
double Error(unsigned int i) const
parameter error by index
Definition: FitResult.h:187
double CovMatrix(unsigned int i, unsigned int j) const
retrieve covariance matrix element
Definition: FitResult.h:217
double Value(unsigned int i) const
parameter value by index
Definition: FitResult.h:180
const std::vector< double > & Parameters() const
parameter values (return std::vector)
Definition: FitResult.h:175
std::string GetParameterName(unsigned int ipar) const
get name of parameter (deprecated)
Definition: FitResult.h:329
double MinFcnValue() const
Return value of the objective function (chi2 or likelihood) used in the fit.
Definition: FitResult.h:121
double Edm() const
Expected distance from minimum.
Definition: FitResult.h:127
double Correlation(unsigned int i, unsigned int j) const
retrieve correlation elements
Definition: FitResult.h:227
unsigned int NTotalParameters() const
get total number of parameters
Definition: FitResult.h:130
int Status() const
minimizer status code
Definition: FitResult.h:138
double GlobalCC(unsigned int i) const
parameter global correlation coefficient
Definition: FitResult.h:211
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:77
bool FitFCN(unsigned int npar, Function &fcn, const double *params=0, unsigned int dataSize=0, bool chi2fit=false)
Fit using the a generic FCN function as a C++ callable object implementing double () (const double *)...
Definition: Fitter.h:590
ROOT::Math::Minimizer * GetMinimizer() const
return pointer to last used minimizer (is NULL in case fit is not yet done) This pointer is guranteed...
Definition: Fitter.h:415
const FitResult & Result() const
get fit result
Definition: Fitter.h:365
const FitConfig & Config() const
access to the fit configuration (const method)
Definition: Fitter.h:393
bool CalculateMinosErrors()
perform an error analysis on the result using MINOS To be called only after fitting and when a minimi...
Definition: Fitter.cxx:696
bool CalculateHessErrors()
perform an error analysis on the result using the Hessian Errors are obtaied from the inverse of the ...
Definition: Fitter.cxx:620
double LowerLimit() const
return lower limit value
double UpperLimit() const
return upper limit value
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
void SetStrategy(int stra)
set the strategy
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
void SetErrorDef(double err)
set error def
void SetPrintLevel(int level)
set print level
void SetTolerance(double tol)
set the tolerance
void SetErrorDef(double up)
set scale for calculating the errors
Definition: Minimizer.h:464
virtual int CovMatrixStatus() const
return status of covariance matrix using Minuit convention {0 not calculated 1 approximated 2 made po...
Definition: Minimizer.h:318
double ErrorDef() const
return the statistical scale used for calculate the error is typically 1 for Chi2 and 0....
Definition: Minimizer.h:434
virtual bool Contour(unsigned int ivar, unsigned int jvar, unsigned int &npoints, double *xi, double *xj)
find the contour points (xi, xj) of the function for parameter ivar and jvar around the minimum The c...
Definition: Minimizer.h:379
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:70
virtual TObject * Clone(const char *newname=0) const
Make a clone of an object using the Streamer facility.
Definition: RooAbsArg.h:82
virtual void constOptimizeTestStatistic(ConstOpCode opcode, Bool_t doAlsoTrackingOpt=kTRUE)
Interface function signaling a request to perform constant term optimization.
Definition: RooAbsArg.cxx:1706
@ DeActivate
Definition: RooAbsArg.h:344
Bool_t isConstant() const
Definition: RooAbsArg.h:311
Int_t getSize() const
void sort(Bool_t reverse=false)
Sort collection using std::sort and name comparison.
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
TIterator * createIterator(Bool_t dir=kIterForward) const R__SUGGEST_ALTERNATIVE("begin()
TIterator-style iteration over contained elements.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
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:53
virtual Double_t defaultErrorLevel() const
Definition: RooAbsReal.h:207
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:81
virtual void enableOffsetting(Bool_t)
Definition: RooAbsReal.h:325
static void setEvalErrorLoggingMode(ErrorLoggingMode m)
Set evaluation error logging mode.
static void clearEvalErrorLog()
Clear the stack of evaluation error messages.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition: RooArgList.h:88
Int_t index(const RooAbsArg *arg) const
Returns index of given arg, or -1 if arg is not in list.
Definition: RooArgList.h:73
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Definition: RooFitResult.h:40
void fillCorrMatrix()
Internal utility method to extract the correlation matrix and the global correlation coefficients fro...
void setCovQual(Int_t val)
Definition: RooFitResult.h:170
void setMinNLL(Double_t val)
Definition: RooFitResult.h:167
void setNumInvalidNLL(Int_t val)
Definition: RooFitResult.h:171
void setStatus(Int_t val)
Definition: RooFitResult.h:169
void setConstParList(const RooArgList &list)
Fill the list of constant parameters.
void setCovarianceMatrix(TMatrixDSym &V)
Store externally provided correlation matrix in this RooFitResult ;.
void setEDM(Double_t val)
Definition: RooFitResult.h:168
void setStatusHistory(std::vector< std::pair< std::string, int > > &hist)
Definition: RooFitResult.h:176
void setInitParList(const RooArgList &list)
Fill the list of initial values of the floating parameters.
void setFinalParList(const RooArgList &list)
Fill the list of final values of the floating parameters.
Int_t GetNumInvalidNLL()
RooArgList * GetInitFloatParamList()
void ApplyCovarianceMatrix(TMatrixDSym &V)
virtual unsigned int NDim() const
Retrieve the dimension of the function.
Bool_t Synchronize(std::vector< ROOT::Fit::ParameterSettings > &parameters, Bool_t optConst, Bool_t verbose)
void BackProp(const ROOT::Fit::FitResult &results)
RooArgList * GetFloatParamList()
RooArgList * GetConstParamList()
RooMinimizer is a wrapper class around ROOT::Fit:Fitter that provides a seamless interface between th...
Definition: RooMinimizer.h:38
Int_t _printLevel
Definition: RooMinimizer.h:108
RooMinimizerFcn * _fcn
Definition: RooMinimizer.h:121
std::vector< std::pair< std::string, int > > _statusHistory
Definition: RooMinimizer.h:126
RooMinimizer(RooAbsReal &function)
Construct MINUIT interface to given function.
Int_t hesse()
Execute HESSE.
void setMaxIterations(Int_t n)
Change maximum number of MINUIT iterations (RooMinimizer default 500 * #parameters)
RooFitResult * save(const char *name=0, const char *title=0)
Save and return a RooFitResult snaphot of current minimizer status.
void setMinimizerType(const char *type)
Choose the minimzer algorithm.
Bool_t _verbose
Definition: RooMinimizer.h:114
void saveStatus(const char *label, Int_t status)
Definition: RooMinimizer.h:83
Int_t improve()
Execute IMPROVE.
void profileStart()
Start profiling timer.
RooPlot * contour(RooRealVar &var1, RooRealVar &var2, Double_t n1=1, Double_t n2=2, Double_t n3=0, Double_t n4=0, Double_t n5=0, Double_t n6=0, unsigned int npoints=50)
Create and draw a TH2 with the error contours in the parameters var1 and var2.
Int_t migrad()
Execute MIGRAD.
Int_t minimize(const char *type, const char *alg=0)
void profileStop()
Stop profiling timer and report results of last session.
static RooFitResult * lastMinuitFit(const RooArgList &varList=RooArgList())
RooFitResult * fit(const char *options)
Parse traditional RooAbsPdf::fitTo driver options.
void setOffsetting(Bool_t flag)
Enable internal likelihood offsetting for enhanced numeric precision.
TMatrixDSym * _extV
Definition: RooMinimizer.h:119
Int_t seek()
Execute SEEK.
Bool_t setLogFile(const char *logf=0)
Definition: RooMinimizer.h:76
void setProfile(Bool_t flag=kTRUE)
Definition: RooMinimizer.h:75
void setEps(Double_t eps)
Change MINUIT epsilon.
void setErrorLevel(Double_t level)
Set the level for MINUIT error analysis to the given value.
static ROOT::Fit::Fitter * _theFitter
Definition: RooMinimizer.h:124
static void cleanup()
Cleanup method called by atexit handler installed by RooSentinel to delete all global heap objects wh...
Int_t setPrintLevel(Int_t newLevel)
Change the MINUIT internal printing level.
TStopwatch _timer
Definition: RooMinimizer.h:115
void setMaxFunctionCalls(Int_t n)
Change maximum number of likelihood function calss from MINUIT (RooMinimizer default 500 * #parameter...
Int_t minos()
Execute MINOS.
RooAbsReal * _func
Definition: RooMinimizer.h:112
void applyCovarianceMatrix(TMatrixDSym &V)
Apply results of given external covariance matrix.
void optimizeConst(Int_t flag)
If flag is true, perform constant term optimization on function being minimized.
Int_t simplex()
Execute SIMPLEX.
Bool_t _optConst
Definition: RooMinimizer.h:110
Bool_t _profileStart
Definition: RooMinimizer.h:117
ROOT::Fit::Fitter * fitter()
Return underlying ROOT fitter object.
void setStrategy(Int_t strat)
Change MINUIT strategy to istrat.
std::string _minimizerType
Definition: RooMinimizer.h:122
TStopwatch _cumulTimer
Definition: RooMinimizer.h:116
Bool_t _profile
Definition: RooMinimizer.h:111
void setVerbose(Bool_t flag=kTRUE)
Definition: RooMinimizer.h:74
virtual ~RooMinimizer()
Destructor.
static RooMsgService & instance()
Return reference to singleton instance.
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:41
void addObject(TObject *obj, Option_t *drawOptions="", Bool_t invisible=kFALSE)
Add a generic object to this plot.
Definition: RooPlot.cxx:391
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
void setError(Double_t value)
Definition: RooRealVar.h:56
void setRange(const char *name, Double_t min, Double_t max)
Set range named 'name to [min,max].
Definition: RooRealVar.cxx:477
virtual void setVal(Double_t value)
Set value of variable to 'value'.
Definition: RooRealVar.cxx:233
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:73
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
Iterator abstract base class.
Definition: TIterator.h:30
virtual TObject * Next()=0
Manages Markers.
Definition: TMarker.h:23
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TObject.cxx:144
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:219
Basic string class.
Definition: TString.h:131
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1125
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:418
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
const Int_t n
Definition: legend1.C:16
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:151
RooCmdArg Parameters(const RooArgSet &params)
@ Minimization
Definition: RooGlobalFunc.h:57
@ InputArguments
Definition: RooGlobalFunc.h:58
Definition: graph.py:1