-
Notifications
You must be signed in to change notification settings - Fork 1.5k
RooFit::MultiProcess & TestStatistics part 4: RooMinimizerFcn refactoring #8569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
guitargeek
merged 3 commits into
root-project:master
from
roofit-dev:RooFit_MultiProcess_PR_4_RooMinimizerFcn_refactoring
Jul 8, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /***************************************************************************** | ||
| * Project: RooFit * | ||
| * Package: RooFitCore * | ||
| * @(#)root/roofitcore:$Id$ | ||
| * Authors: * | ||
| * AL, Alfio Lazzaro, INFN Milan, alfio.lazzaro@mi.infn.it * | ||
| * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl * | ||
| * * | ||
| * * | ||
| * Redistribution and use in source and binary forms, * | ||
| * with or without modification, are permitted according to the terms * | ||
| * listed in LICENSE (http://roofit.sourceforge.net/license.txt) * | ||
| *****************************************************************************/ | ||
|
|
||
| #ifndef ROO_ABS_MINIMIZER_FCN | ||
| #define ROO_ABS_MINIMIZER_FCN | ||
|
|
||
| #include "Math/IFunction.h" | ||
| #include "Fit/ParameterSettings.h" | ||
| #include "Fit/FitResult.h" | ||
|
|
||
| #include "TMatrixDSym.h" | ||
|
|
||
| #include "RooAbsReal.h" | ||
| #include "RooArgList.h" | ||
| #include "RooRealVar.h" | ||
|
|
||
| #include <iostream> | ||
| #include <fstream> | ||
| #include <string> | ||
| #include <memory> // unique_ptr | ||
|
|
||
| // forward declaration | ||
| class RooMinimizer; | ||
|
|
||
| class RooAbsMinimizerFcn { | ||
|
|
||
| public: | ||
| RooAbsMinimizerFcn(RooArgList paramList, RooMinimizer *context, bool verbose = false); | ||
| RooAbsMinimizerFcn(const RooAbsMinimizerFcn &other); | ||
| virtual ~RooAbsMinimizerFcn() = default; | ||
|
|
||
| /// Informs Minuit through its parameter_settings vector of RooFit parameter properties. | ||
| Bool_t synchronizeParameterSettings(std::vector<ROOT::Fit::ParameterSettings> ¶meters, Bool_t optConst, Bool_t verbose); | ||
| /// Like synchronizeParameterSettings, Synchronize informs Minuit through its parameter_settings vector of RooFit parameter properties, | ||
| /// but Synchronize can be overridden to e.g. also include gradient strategy synchronization in subclasses. | ||
| virtual Bool_t Synchronize(std::vector<ROOT::Fit::ParameterSettings> ¶meters, Bool_t optConst, Bool_t verbose); | ||
|
|
||
| RooArgList *GetFloatParamList(); | ||
| RooArgList *GetConstParamList(); | ||
| RooArgList *GetInitFloatParamList(); | ||
| RooArgList *GetInitConstParamList(); | ||
| Int_t GetNumInvalidNLL() const; | ||
|
|
||
| // need access from Minimizer: | ||
| void SetEvalErrorWall(Bool_t flag); | ||
| /// Try to recover from invalid function values. When invalid function values are encountered, | ||
| /// a penalty term is returned to the minimiser to make it back off. This sets the strength of this penalty. | ||
| /// \note A strength of zero is equivalent to a constant penalty (= the gradient vanishes, ROOT < 6.24). | ||
| /// Positive values lead to a gradient pointing away from the undefined regions. Use ~10 to force the minimiser | ||
| /// away from invalid function values. | ||
| void SetRecoverFromNaNStrength(double strength) { _recoverFromNaNStrength = strength; } | ||
| void SetPrintEvalErrors(Int_t numEvalErrors); | ||
| Double_t &GetMaxFCN(); | ||
| Int_t evalCounter() const; | ||
| void zeroEvalCount(); | ||
| /// Return a possible offset that's applied to the function to separate invalid function values from valid ones. | ||
| double getOffset() const { return _funcOffset; } | ||
| void SetVerbose(Bool_t flag = kTRUE); | ||
|
|
||
| /// Put Minuit results back into RooFit objects. | ||
| void BackProp(const ROOT::Fit::FitResult &results); | ||
|
|
||
| /// RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class. | ||
| virtual std::string getFunctionName() const = 0; | ||
| /// RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class. | ||
| virtual std::string getFunctionTitle() const = 0; | ||
|
|
||
| /// Set different external covariance matrix | ||
| void ApplyCovarianceMatrix(TMatrixDSym &V); | ||
|
|
||
| Bool_t SetLogFile(const char *inLogfile); | ||
| std::ofstream *GetLogFile() { return _logfile; } | ||
|
|
||
| unsigned int getNDim() const { return _nDim; } | ||
|
|
||
| void setOptimizeConst(Int_t flag); | ||
|
|
||
| bool getOptConst(); | ||
| std::vector<double> getParameterValues() const; | ||
|
|
||
| Bool_t SetPdfParamVal(int index, double value) const; | ||
|
|
||
| /// Enable or disable offsetting on the function to be minimized, which enhances numerical precision. | ||
| virtual void setOffsetting(Bool_t flag) = 0; | ||
|
|
||
| protected: | ||
| void optimizeConstantTerms(bool constStatChange, bool constValChange); | ||
| /// This function must be overridden in the derived class to pass on constant term optimization configuration | ||
| /// to the function to be minimized. For a RooAbsArg, this would be RooAbsArg::constOptimizeTestStatistic. | ||
| virtual void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, Bool_t doAlsoTrackingOpt) = 0; | ||
|
|
||
| // used in BackProp (Minuit results -> RooFit) and ApplyCovarianceMatrix | ||
| void SetPdfParamErr(Int_t index, Double_t value); | ||
| void ClearPdfParamAsymErr(Int_t index); | ||
| void SetPdfParamErr(Int_t index, Double_t loVal, Double_t hiVal); | ||
|
|
||
| void printEvalErrors() const; | ||
|
|
||
| // members | ||
| RooMinimizer *_context; | ||
|
|
||
| // the following four are mutable because DoEval is const (in child classes) | ||
| // Reset the *largest* negative log-likelihood value we have seen so far: | ||
| mutable double _maxFCN = -std::numeric_limits<double>::infinity(); | ||
| mutable double _funcOffset{0.}; | ||
| double _recoverFromNaNStrength{10.}; | ||
| mutable int _numBadNLL = 0; | ||
| mutable int _printEvalErrors = 10; | ||
| mutable int _evalCounter{0}; | ||
|
|
||
| unsigned int _nDim = 0; | ||
|
|
||
| Bool_t _optConst = kFALSE; | ||
|
|
||
| std::unique_ptr<RooArgList> _floatParamList; | ||
| std::unique_ptr<RooArgList> _constParamList; | ||
| std::unique_ptr<RooArgList> _initFloatParamList; | ||
| std::unique_ptr<RooArgList> _initConstParamList; | ||
|
|
||
| std::ofstream *_logfile = nullptr; | ||
| bool _doEvalErrorWall{true}; | ||
| bool _verbose; | ||
| }; | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| /***************************************************************************** | ||
| * Project: RooFit * | ||
| * Package: RooFitCore * | ||
| * Package: RooFitCore * | ||
| * @(#)root/roofitcore:$Id$ | ||
| * Authors: * | ||
| * AL, Alfio Lazzaro, INFN Milan, alfio.lazzaro@mi.infn.it * | ||
| * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl * | ||
| * * | ||
| * * | ||
| * Redistribution and use in source and binary forms, * | ||
|
|
@@ -24,83 +25,35 @@ | |
| #include <fstream> | ||
| #include <vector> | ||
|
|
||
| class RooMinimizer; | ||
| #include <RooAbsMinimizerFcn.h> | ||
|
|
||
| template<typename T> class TMatrixTSym; | ||
| using TMatrixDSym = TMatrixTSym<double>; | ||
|
|
||
| class RooMinimizerFcn : public ROOT::Math::IBaseFunctionMultiDim { | ||
|
|
||
| public: | ||
|
|
||
| RooMinimizerFcn(RooAbsReal *funct, RooMinimizer *context, | ||
| bool verbose = false); | ||
| RooMinimizerFcn(const RooMinimizerFcn& other); | ||
| virtual ~RooMinimizerFcn(); | ||
|
|
||
| virtual ROOT::Math::IBaseFunctionMultiDim* Clone() const; | ||
| virtual unsigned int NDim() const { return _nDim; } | ||
|
|
||
| RooArgList* GetFloatParamList() { return _floatParamList; } | ||
| RooArgList* GetConstParamList() { return _constParamList; } | ||
| RooArgList* GetInitFloatParamList() { return _initFloatParamList; } | ||
| RooArgList* GetInitConstParamList() { return _initConstParamList; } | ||
|
|
||
| void SetEvalErrorWall(Bool_t flag) { _doEvalErrorWall = flag ; } | ||
| /// Try to recover from invalid function values. When invalid function values are encountered, | ||
| /// a penalty term is returned to the minimiser to make it back off. This sets the strength of this penalty. | ||
| /// \note A strength of zero is equivalent to a constant penalty (= the gradient vanishes, ROOT < 6.24). | ||
| /// Positive values lead to a gradient pointing away from the undefined regions. Use ~10 to force the minimiser | ||
| /// away from invalid function values. | ||
| void SetRecoverFromNaNStrength(double strength) { _recoverFromNaNStrength = strength; } | ||
| void SetPrintEvalErrors(Int_t numEvalErrors) { _printEvalErrors = numEvalErrors ; } | ||
| Bool_t SetLogFile(const char* inLogfile); | ||
| std::ofstream* GetLogFile() { return _logfile; } | ||
| void SetVerbose(Bool_t flag=kTRUE) { _verbose = flag ; } | ||
|
|
||
| Double_t& GetMaxFCN() { return _maxFCN; } | ||
| Int_t GetNumInvalidNLL() const { return _numBadNLL; } | ||
|
|
||
| Bool_t Synchronize(std::vector<ROOT::Fit::ParameterSettings>& parameters, | ||
| Bool_t optConst, Bool_t verbose); | ||
| void BackProp(const ROOT::Fit::FitResult &results); | ||
| void ApplyCovarianceMatrix(TMatrixDSym& V); | ||
|
|
||
| Int_t evalCounter() const { return _evalCounter ; } | ||
| void zeroEvalCount() { _evalCounter = 0 ; } | ||
| /// Return a possible offset that's applied to the function to separate invalid function values from valid ones. | ||
| double getOffset() const { return _funcOffset; } | ||
|
|
||
| private: | ||
| void SetPdfParamErr(Int_t index, Double_t value); | ||
| void ClearPdfParamAsymErr(Int_t index); | ||
| void SetPdfParamErr(Int_t index, Double_t loVal, Double_t hiVal); | ||
| // forward declaration | ||
| class RooMinimizer; | ||
|
|
||
| Bool_t SetPdfParamVal(int index, double value) const; | ||
| void printEvalErrors() const; | ||
| class RooMinimizerFcn : public RooAbsMinimizerFcn, public ROOT::Math::IBaseFunctionMultiDim { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multiple inheritance from classes that are not abstract isn't nice in C++. Now all the stuff such as having two separate data sections, pointer changes on casting etc are going to happen. |
||
|
|
||
| virtual double DoEval(const double * x) const; | ||
| public: | ||
| RooMinimizerFcn(RooAbsReal *funct, RooMinimizer *context, bool verbose = false); | ||
| RooMinimizerFcn(const RooMinimizerFcn &other); | ||
| virtual ~RooMinimizerFcn(); | ||
|
|
||
| ROOT::Math::IBaseFunctionMultiDim *Clone() const override; | ||
| unsigned int NDim() const override { return getNDim(); } | ||
|
|
||
| RooAbsReal *_funct; | ||
| const RooMinimizer *_context; | ||
| std::string getFunctionName() const override; | ||
| std::string getFunctionTitle() const override; | ||
|
|
||
| mutable double _maxFCN; | ||
| mutable double _funcOffset{0.}; | ||
| double _recoverFromNaNStrength{10.}; | ||
| mutable int _numBadNLL; | ||
| mutable int _printEvalErrors; | ||
| mutable int _evalCounter{0}; | ||
| int _nDim; | ||
| void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, Bool_t doAlsoTrackingOpt) override; | ||
|
|
||
| RooArgList* _floatParamList; | ||
| RooArgList* _constParamList; | ||
| RooArgList* _initFloatParamList; | ||
| RooArgList* _initConstParamList; | ||
| void setOffsetting(Bool_t flag) override; | ||
|
|
||
| std::ofstream *_logfile; | ||
| bool _doEvalErrorWall{true}; | ||
| bool _verbose; | ||
| private: | ||
| double DoEval(const double *x) const override; | ||
|
|
||
| RooAbsReal *_funct; | ||
| }; | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.