/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 *    File: $Id$
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
 *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
 *   AL, Alfio Lazzaro,   INFN Milan,        alfio.lazzaro@mi.infn.it        *
 *                                                                           *
 *                                                                           *
 * 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 __ROOFIT_NOROOMINIMIZER

#ifndef ROO_MINIMIZER
#define ROO_MINIMIZER

#include "TObject.h"
#include "TStopwatch.h"
#include <fstream>
#include "TMatrixDSymfwd.h"


#include "Fit/Fitter.h"
#include "RooMinimizerFcn.h"

class RooAbsReal ;
class RooFitResult ;
class RooArgList ;
class RooRealVar ;
class RooArgSet ;
class TH2F ;
class RooPlot ;

class RooMinimizer : public TObject {
public:

  RooMinimizer(RooAbsReal& function) ;
  virtual ~RooMinimizer() ;

  enum Strategy { Speed=0, Balance=1, Robustness=2 } ;
  enum PrintLevel { None=-1, Reduced=0, Normal=1, ExtraForProblem=2, Maximum=3 } ;
  void setStrategy(Int_t strat) ;
  void setErrorLevel(Double_t level) ;
  void setEps(Double_t eps) ;
  void optimizeConst(Int_t flag) ;
  void setEvalErrorWall(Bool_t flag) { fitterFcn()->SetEvalErrorWall(flag); }
  void setOffsetting(Bool_t flag) ;
  void setMaxIterations(Int_t n) ;
  void setMaxFunctionCalls(Int_t n) ; 

  RooFitResult* fit(const char* options) ;

  Int_t migrad() ;
  Int_t hesse() ;
  Int_t minos() ;
  Int_t minos(const RooArgSet& minosParamList) ;
  Int_t seek() ;
  Int_t simplex() ;
  Int_t improve() ;

  Int_t minimize(const char* type, const char* alg=0) ;

  RooFitResult* save(const char* name=0, const char* title=0) ;
  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) ;

  Int_t setPrintLevel(Int_t newLevel) ; 
  void setPrintEvalErrors(Int_t numEvalErrors) { fitterFcn()->SetPrintEvalErrors(numEvalErrors); }
  void setVerbose(Bool_t flag=kTRUE) { _verbose = flag ; fitterFcn()->SetVerbose(flag); }
  void setProfile(Bool_t flag=kTRUE) { _profile = flag ; }
  Bool_t setLogFile(const char* logf=0) { return fitterFcn()->SetLogFile(logf); }

  void setMinimizerType(const char* type) ;

  static void cleanup() ;
  static RooFitResult* lastMinuitFit(const RooArgList& varList=RooArgList()) ;

  void saveStatus(const char* label, Int_t status) { _statusHistory.push_back(std::pair<std::string,int>(label,status)) ; }

  Int_t evalCounter() const { return fitterFcn()->evalCounter() ; }
  void zeroEvalCount() { fitterFcn()->zeroEvalCount() ; }

  ROOT::Fit::Fitter* fitter() ;
  const ROOT::Fit::Fitter* fitter() const ;
  
protected:

  friend class RooAbsPdf ;
  void applyCovarianceMatrix(TMatrixDSym& V) ;

  void profileStart() ;
  void profileStop() ;

  inline Int_t getNPar() const { return fitterFcn()->NDim() ; }
  inline std::ofstream* logfile() { return fitterFcn()->GetLogFile(); }
  inline Double_t& maxFCN() { return fitterFcn()->GetMaxFCN() ; }
  
  const RooMinimizerFcn* fitterFcn() const {  return ( fitter()->GetFCN() ? ((RooMinimizerFcn*) fitter()->GetFCN()) : _fcn ) ; }
  RooMinimizerFcn* fitterFcn() { return ( fitter()->GetFCN() ? ((RooMinimizerFcn*) fitter()->GetFCN()) : _fcn ) ; }

private:

  Int_t       _printLevel ;
  Int_t       _status ;
  Bool_t      _optConst ;
  Bool_t      _profile ;
  RooAbsReal* _func ;

  Bool_t      _verbose ;
  TStopwatch  _timer ;
  TStopwatch  _cumulTimer ;
  Bool_t      _profileStart ;

  TMatrixDSym* _extV ;

  RooMinimizerFcn *_fcn;
  std::string _minimizerType;

  static ROOT::Fit::Fitter *_theFitter ;

  std::vector<std::pair<std::string,int> > _statusHistory ;

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


#endif

#endif
 RooMinimizer.h:1
 RooMinimizer.h:2
 RooMinimizer.h:3
 RooMinimizer.h:4
 RooMinimizer.h:5
 RooMinimizer.h:6
 RooMinimizer.h:7
 RooMinimizer.h:8
 RooMinimizer.h:9
 RooMinimizer.h:10
 RooMinimizer.h:11
 RooMinimizer.h:12
 RooMinimizer.h:13
 RooMinimizer.h:14
 RooMinimizer.h:15
 RooMinimizer.h:16
 RooMinimizer.h:17
 RooMinimizer.h:18
 RooMinimizer.h:19
 RooMinimizer.h:20
 RooMinimizer.h:21
 RooMinimizer.h:22
 RooMinimizer.h:23
 RooMinimizer.h:24
 RooMinimizer.h:25
 RooMinimizer.h:26
 RooMinimizer.h:27
 RooMinimizer.h:28
 RooMinimizer.h:29
 RooMinimizer.h:30
 RooMinimizer.h:31
 RooMinimizer.h:32
 RooMinimizer.h:33
 RooMinimizer.h:34
 RooMinimizer.h:35
 RooMinimizer.h:36
 RooMinimizer.h:37
 RooMinimizer.h:38
 RooMinimizer.h:39
 RooMinimizer.h:40
 RooMinimizer.h:41
 RooMinimizer.h:42
 RooMinimizer.h:43
 RooMinimizer.h:44
 RooMinimizer.h:45
 RooMinimizer.h:46
 RooMinimizer.h:47
 RooMinimizer.h:48
 RooMinimizer.h:49
 RooMinimizer.h:50
 RooMinimizer.h:51
 RooMinimizer.h:52
 RooMinimizer.h:53
 RooMinimizer.h:54
 RooMinimizer.h:55
 RooMinimizer.h:56
 RooMinimizer.h:57
 RooMinimizer.h:58
 RooMinimizer.h:59
 RooMinimizer.h:60
 RooMinimizer.h:61
 RooMinimizer.h:62
 RooMinimizer.h:63
 RooMinimizer.h:64
 RooMinimizer.h:65
 RooMinimizer.h:66
 RooMinimizer.h:67
 RooMinimizer.h:68
 RooMinimizer.h:69
 RooMinimizer.h:70
 RooMinimizer.h:71
 RooMinimizer.h:72
 RooMinimizer.h:73
 RooMinimizer.h:74
 RooMinimizer.h:75
 RooMinimizer.h:76
 RooMinimizer.h:77
 RooMinimizer.h:78
 RooMinimizer.h:79
 RooMinimizer.h:80
 RooMinimizer.h:81
 RooMinimizer.h:82
 RooMinimizer.h:83
 RooMinimizer.h:84
 RooMinimizer.h:85
 RooMinimizer.h:86
 RooMinimizer.h:87
 RooMinimizer.h:88
 RooMinimizer.h:89
 RooMinimizer.h:90
 RooMinimizer.h:91
 RooMinimizer.h:92
 RooMinimizer.h:93
 RooMinimizer.h:94
 RooMinimizer.h:95
 RooMinimizer.h:96
 RooMinimizer.h:97
 RooMinimizer.h:98
 RooMinimizer.h:99
 RooMinimizer.h:100
 RooMinimizer.h:101
 RooMinimizer.h:102
 RooMinimizer.h:103
 RooMinimizer.h:104
 RooMinimizer.h:105
 RooMinimizer.h:106
 RooMinimizer.h:107
 RooMinimizer.h:108
 RooMinimizer.h:109
 RooMinimizer.h:110
 RooMinimizer.h:111
 RooMinimizer.h:112
 RooMinimizer.h:113
 RooMinimizer.h:114
 RooMinimizer.h:115
 RooMinimizer.h:116
 RooMinimizer.h:117
 RooMinimizer.h:118
 RooMinimizer.h:119
 RooMinimizer.h:120
 RooMinimizer.h:121
 RooMinimizer.h:122
 RooMinimizer.h:123
 RooMinimizer.h:124
 RooMinimizer.h:125
 RooMinimizer.h:126
 RooMinimizer.h:127
 RooMinimizer.h:128
 RooMinimizer.h:129
 RooMinimizer.h:130
 RooMinimizer.h:131
 RooMinimizer.h:132
 RooMinimizer.h:133
 RooMinimizer.h:134
 RooMinimizer.h:135
 RooMinimizer.h:136