/*****************************************************************************
 * Project: RooFit                                                           *
 *                                                                           *
 * Copyright (c) 2000-2005, Regents of the University of California          *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 * 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 ROOPROFILELL
#define ROOPROFILELL

#include "RooAbsReal.h"
#include "RooRealProxy.h"
#include "RooSetProxy.h"
#include <map>
#include <string>

class RooMinimizer ;
class RooMinuit ; 

#define MINIMIZER RooMinimizer

class RooProfileLL : public RooAbsReal {
public:

  RooProfileLL() ;
  RooProfileLL(const char *name, const char *title, RooAbsReal& nll, const RooArgSet& observables);
  RooProfileLL(const RooProfileLL& other, const char* name=0) ;
  virtual TObject* clone(const char* newname) const { return new RooProfileLL(*this,newname); }
  virtual ~RooProfileLL() ;

  void setAlwaysStartFromMin(Bool_t flag) { _startFromMin = flag ; }
  Bool_t alwaysStartFromMin() const { return _startFromMin ; }

  MINIMIZER* minimizer() { return _minimizer ; }
  RooAbsReal& nll() { return const_cast<RooAbsReal&>(_nll.arg()) ; }
  const RooArgSet& bestFitParams() const ;
  const RooArgSet& bestFitObs() const ;

  virtual RooAbsReal* createProfile(const RooArgSet& paramsOfInterest) ;
  
  virtual Bool_t redirectServersHook(const RooAbsCollection& /*newServerList*/, Bool_t /*mustReplaceAll*/, Bool_t /*nameChange*/, Bool_t /*isRecursive*/) ;

  void clearAbsMin() { _absMinValid = kFALSE ; }

  Int_t numEval() const { return _neval ; }


protected:

  void validateAbsMin() const ;
  void initializeMinimizer() const ;

  RooRealProxy _nll ;    // Input -log(L) function
  RooSetProxy _obs ;     // Parameters of profile likelihood
  RooSetProxy _par ;     // Marginialized parameters of likelihood
  Bool_t _startFromMin ; // Always start minimization for global minimum?

  TIterator* _piter ; //! Iterator over profile likelihood parameters to be minimized 
  TIterator* _oiter ; //! Iterator of profile likelihood output parameter(s)

  mutable MINIMIZER* _minimizer ; //! Internal minuit instance

  mutable Bool_t _absMinValid ; // flag if absmin is up-to-date
  mutable Double_t _absMin ; // absolute minimum of -log(L)
  mutable RooArgSet _paramAbsMin ; // Parameter values at absolute minimum
  mutable RooArgSet _obsAbsMin ; // Observable values at absolute minimum
  mutable std::map<std::string,bool> _paramFixed ; // Parameter constant status at last time of use
  mutable Int_t _neval ; // Number evaluations used in last minimization
  Double_t evaluate() const ;


private:

  ClassDef(RooProfileLL,0) // Real-valued function representing profile likelihood of external (likelihood) function
};
 
#endif
 RooProfileLL.h:1
 RooProfileLL.h:2
 RooProfileLL.h:3
 RooProfileLL.h:4
 RooProfileLL.h:5
 RooProfileLL.h:6
 RooProfileLL.h:7
 RooProfileLL.h:8
 RooProfileLL.h:9
 RooProfileLL.h:10
 RooProfileLL.h:11
 RooProfileLL.h:12
 RooProfileLL.h:13
 RooProfileLL.h:14
 RooProfileLL.h:15
 RooProfileLL.h:16
 RooProfileLL.h:17
 RooProfileLL.h:18
 RooProfileLL.h:19
 RooProfileLL.h:20
 RooProfileLL.h:21
 RooProfileLL.h:22
 RooProfileLL.h:23
 RooProfileLL.h:24
 RooProfileLL.h:25
 RooProfileLL.h:26
 RooProfileLL.h:27
 RooProfileLL.h:28
 RooProfileLL.h:29
 RooProfileLL.h:30
 RooProfileLL.h:31
 RooProfileLL.h:32
 RooProfileLL.h:33
 RooProfileLL.h:34
 RooProfileLL.h:35
 RooProfileLL.h:36
 RooProfileLL.h:37
 RooProfileLL.h:38
 RooProfileLL.h:39
 RooProfileLL.h:40
 RooProfileLL.h:41
 RooProfileLL.h:42
 RooProfileLL.h:43
 RooProfileLL.h:44
 RooProfileLL.h:45
 RooProfileLL.h:46
 RooProfileLL.h:47
 RooProfileLL.h:48
 RooProfileLL.h:49
 RooProfileLL.h:50
 RooProfileLL.h:51
 RooProfileLL.h:52
 RooProfileLL.h:53
 RooProfileLL.h:54
 RooProfileLL.h:55
 RooProfileLL.h:56
 RooProfileLL.h:57
 RooProfileLL.h:58
 RooProfileLL.h:59
 RooProfileLL.h:60
 RooProfileLL.h:61
 RooProfileLL.h:62
 RooProfileLL.h:63
 RooProfileLL.h:64
 RooProfileLL.h:65
 RooProfileLL.h:66
 RooProfileLL.h:67
 RooProfileLL.h:68
 RooProfileLL.h:69
 RooProfileLL.h:70
 RooProfileLL.h:71
 RooProfileLL.h:72
 RooProfileLL.h:73
 RooProfileLL.h:74
 RooProfileLL.h:75
 RooProfileLL.h:76
 RooProfileLL.h:77
 RooProfileLL.h:78
 RooProfileLL.h:79
 RooProfileLL.h:80
 RooProfileLL.h:81