// @(#)root/roostats:$Id$
// Author: George Lewis, Kyle Cranmer
/*************************************************************************
 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOBARLOWBEESTONLL
#define ROOBARLOWBEESTONLL

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

class RooMinuit ;

namespace RooStats{
  namespace HistFactory{

class RooBarlowBeestonLL : public RooAbsReal {
public:

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

  // A simple class to store the 
  // necessary objects for a 
  // single gamma in a single channel
  class BarlowCache {
  public:
    BarlowCache() : hasStatUncert(false), gamma(NULL), 
		    observables(NULL), bin_center(NULL), 
		    tau(NULL), nom_pois_mean(NULL),
		    sumPdf(NULL),  nData(-1) {}
    bool hasStatUncert;
    RooRealVar* gamma;
    RooArgSet* observables;
    RooArgSet* bin_center; // Snapshot
    RooRealVar* tau;
    RooAbsReal* nom_pois_mean;
    RooAbsReal* sumPdf;
    double nData;
    double binVolume;
    void SetBinCenter() const;
    /*
    // Restore original values and constant status of observables
    TIterator* iter = obsSetOrig->createIterator() ;
    RooRealVar* var ;
    while((var=(RooRealVar*)iter->Next())) {
    RooRealVar* target = (RooRealVar*) _obs.find(var->GetName()) ;
    target->setVal(var->getVal()) ;
    target->setConstant(var->isConstant()) ;
    }
     */

  };
  

  void initializeBarlowCache();

  RooArgSet* getParameters(const RooArgSet* depList, Bool_t stripDisconnected=kTRUE) const;

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

  //RooMinuit* minuit() { return _minuit ; }
  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 ; }

  void setPdf(RooAbsPdf* pdf) { _pdf = pdf; }
  void setDataset(RooAbsData* data) { _data = data; }
  
  //void FactorizePdf(const RooArgSet &observables, RooAbsPdf &pdf, 
  //	    RooArgList &obsTerms, RooArgList &constraints) const;


protected:

  // void validateAbsMin() const ;


  RooRealProxy _nll ;    // Input -log(L) function
  /*
  RooSetProxy _obs ;     // Parameters of profile likelihood
  RooSetProxy _par ;     // Marginialized parameters of likelihood
  */
  RooAbsPdf* _pdf;
  RooAbsData* _data;
  mutable std::map< std::string, std::vector< BarlowCache > > _barlowCache;
  mutable std::set< std::string > _statUncertParams;
  // 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 RooMinuit* _minuit ; //! 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 ;
  //Double_t evaluate_bad() const ;


private:

  ClassDef(RooStats::HistFactory::RooBarlowBeestonLL,0) // Real-valued function representing a Barlow-Beeston minimized profile likelihood of external (likelihood) function
};

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