// @(#)root/roostats:$Id$
// Authors: Kevin Belasco        17/06/2009
// Authors: Kyle Cranmer         17/06/2009
/*************************************************************************
 * 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 ROOSTATS_PdfProposal
#define ROOSTATS_PdfProposal

#ifndef ROOT_Rtypes
#include "Rtypes.h"
#endif

#ifndef ROOSTATS_ProposalFunction
#include "RooStats/ProposalFunction.h"
#endif

#ifndef ROO_ARG_SET
#include "RooArgSet.h"
#endif
#ifndef ROO_MSG_SERVICE
#include "RooMsgService.h"
#endif
#ifndef ROO_REAL_VAR
#include "RooRealVar.h"
#endif
#ifndef ROO_DATA_SET
#include "RooDataSet.h"
#endif
#ifndef ROO_ABS_PDF
#include "RooAbsPdf.h"
#endif

#include <map>


namespace RooStats {

   class PdfProposal : public ProposalFunction {

   public:
      PdfProposal();
      PdfProposal(RooAbsPdf& pdf);

      // Populate xPrime with a new proposed point
      virtual void Propose(RooArgSet& xPrime, RooArgSet& x);

      // Determine whether or not the proposal density is symmetric for
      // points x1 and x2 - that is, whether the probabilty of reaching x2
      // from x1 is equal to the probability of reaching x1 from x2
      virtual Bool_t IsSymmetric(RooArgSet& x1, RooArgSet& x2);

      // Return the probability of proposing the point x1 given the starting
      // point x2
      virtual Double_t GetProposalDensity(RooArgSet& x1, RooArgSet& x2);

      // Set the PDF to be the proposal density function
      virtual void SetPdf(RooAbsPdf& pdf) { fPdf = &pdf; }

      // Get the PDF is the proposal density function
      virtual const RooAbsPdf* GetPdf() const { return fPdf; }

      // specify a mapping between a parameter of the proposal function and
      // a parameter of interest.  this mapping is used to set the value of
      // proposalParam equal to the value of update to determine the
      // proposal function.
      // proposalParam is a parameter of the proposal function that must
      // be set to the value of update (from the current point) in order to
      // propose a new point.
      virtual void AddMapping(RooRealVar& proposalParam, RooAbsReal& update);

      virtual void Reset()
      {
         delete fCache;
         fCache = NULL;
         fCachePosition = 0;
         fLastX.removeAll();
      }

      virtual void printMappings()
      {
         std::map<RooRealVar*, RooAbsReal*>::iterator it;
         for (it = fMap.begin(); it != fMap.end(); it++)
            std::cout << it->first->GetName() << " => " << it->second->GetName() << std::endl;
      }

      // Set how many points to generate each time we propose from a new point
      // Default (and minimum) is 1
      virtual void SetCacheSize(Int_t size)
      {
         if (size > 0)
            fCacheSize = size;
         else
            coutE(Eval) << "Warning: Requested non-positive cache size: " <<
               size << ". Cache size unchanged." << std::endl;
      }

      // set whether we own the PDF that serves as the proposal density function
      // By default, when constructed, PdfProposal does NOT own the PDF.
      virtual void SetOwnsPdf(Bool_t ownsPdf) { fOwnsPdf = ownsPdf; }

      //virtual void SetIsAlwaysSymmetric(Bool_t isAlwaysSymmetric)
      //{ fIsAlwaysSymmetric = isAlwaysSymmetric; }

      virtual ~PdfProposal()
      {
         delete fCache;
         if (fOwnsPdf)
            delete fPdf;
      }

   protected:
      RooAbsPdf* fPdf; // the proposal density function
      std::map<RooRealVar*, RooAbsReal*> fMap; // map of values in pdf to update
      std::map<RooRealVar*, RooAbsReal*>::iterator fIt; // pdf iterator
      RooArgSet fLastX; // the last point we were at
      Int_t fCacheSize; // how many points to generate each time
      Int_t fCachePosition; // our position in the cached proposal data set
      RooDataSet* fCache; // the cached proposal data set
      RooArgSet fMaster; // pointers to master variables needed for updates
      Bool_t fOwnsPdf; // whether we own the proposal density function
      //Bool_t fIsAlwaysSymmetric; // does Q(x1 | x2) == Q(x2 | x1) for all x1, x2

      // determine whether these two RooArgSets represent the same point
      virtual Bool_t Equals(RooArgSet& x1, RooArgSet& x2);

      // Interface for tools setting limits (producing confidence intervals)
      ClassDef(PdfProposal,1)
   };
}

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