Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions roofit/roofitcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ROOT_STANDARD_LIBRARY_PACKAGE(RooFitCore
RooAbsIntegrator.h
RooAbsLValue.h
RooAbsMCStudyModule.h
RooAbsMinimizerFcn.h
RooAbsMoment.h
RooAbsNumGenerator.h
RooAbsOptTestStatistic.h
Expand Down Expand Up @@ -253,6 +254,7 @@ ROOT_STANDARD_LIBRARY_PACKAGE(RooFitCore
src/RooAbsIntegrator.cxx
src/RooAbsLValue.cxx
src/RooAbsMCStudyModule.cxx
src/RooAbsMinimizerFcn.cxx
src/RooAbsMoment.cxx
src/RooAbsNumGenerator.cxx
src/RooAbsOptTestStatistic.cxx
Expand Down
136 changes: 136 additions & 0 deletions roofit/roofitcore/inc/RooAbsMinimizerFcn.h
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> &parameters, 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> &parameters, 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);
Comment thread
egpbos marked this conversation as resolved.
/// 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
6 changes: 3 additions & 3 deletions roofit/roofitcore/inc/RooMinimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class RooMinimizer : public TObject {
void setProfile(Bool_t flag=kTRUE) { _profile = flag ; }
Bool_t setLogFile(const char* logf=0) { return fitterFcn()->SetLogFile(logf); }

Int_t getPrintLevel() const;

void setMinimizerType(const char* type) ;

static void cleanup() ;
Expand Down Expand Up @@ -109,9 +111,7 @@ class RooMinimizer : public TObject {

Int_t _printLevel ;
Int_t _status ;
Bool_t _optConst ;
Comment thread
egpbos marked this conversation as resolved.
Bool_t _profile ;
RooAbsReal* _func ;

Bool_t _verbose ;
TStopwatch _timer ;
Expand All @@ -129,7 +129,7 @@ class RooMinimizer : public TObject {

RooMinimizer(const RooMinimizer&) ;

ClassDef(RooMinimizer,0) // RooFit interface to ROOT::Fit::Fitter
ClassDef(RooMinimizer,1) // RooFit interface to ROOT::Fit::Fitter
} ;


Expand Down
87 changes: 20 additions & 67 deletions roofit/roofitcore/inc/RooMinimizerFcn.h
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, *
Expand All @@ -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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.
If possible, make AbsMinimizerFcn really abstract, i.e. no members.


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
Loading