/***************************************************************************** 
  * 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)             * 
  *****************************************************************************/ 

//////////////////////////////////////////////////////////////////////////////
// 
// BEGIN_HTML
// RooAbsSelfCachedPdf is an abstract base class for probability
// density functions whose output is cached in terms of a histogram in
// all observables between getVal() and evaluate(). For certain
// p.d.f.s that are very expensive to calculate it may be beneficial
// to implement them as a RooAbsSelfCachedPdf rather than a
// RooAbsPdf. Class RooAbsSelfCachedPdf is designed to have its
// interface identical to that of RooAbsPdf, so any p.d.f can make use
// of its caching functionality by merely switching its base class.
// Existing RooAbsPdf objects can also be cached a posteriori with the
// RooCachedPdf wrapper p.d.f. that takes any RooAbsPdf object as
// input.
// END_HTML
//
//

#include "Riostream.h" 

#include "RooFit.h"
#include "RooAbsSelfCachedPdf.h" 
#include "RooAbsReal.h" 
#include "RooMsgService.h"
#include "RooDataHist.h"
#include "RooHistPdf.h"

using namespace std ;

ClassImp(RooAbsSelfCachedPdf) 



//_____________________________________________________________________________
RooAbsSelfCachedPdf::RooAbsSelfCachedPdf(const char *name, const char *title, Int_t ipOrder) :
  RooAbsCachedPdf(name,title,ipOrder)
 { 
   // Constructor
 } 



//_____________________________________________________________________________
RooAbsSelfCachedPdf::RooAbsSelfCachedPdf(const RooAbsSelfCachedPdf& other, const char* name) :  
   RooAbsCachedPdf(other,name)
 { 
   // Copy constructor
 } 



//_____________________________________________________________________________
RooAbsSelfCachedPdf::~RooAbsSelfCachedPdf() 
{
  // Destructor
}



//_____________________________________________________________________________
void RooAbsSelfCachedPdf::fillCacheObject(RooAbsCachedPdf::PdfCacheElem& cache) const 
{
  // Fill cache with sampling of p.d.f as defined by the evaluate() implementation

  RooDataHist& cacheHist = *cache.hist() ;

  // Make deep clone of self in non-caching mde and attach to dataset observables
  RooArgSet* cloneSet = (RooArgSet*) RooArgSet(*this).snapshot(kTRUE) ;
  RooAbsSelfCachedPdf* clone2 = (RooAbsSelfCachedPdf*) cloneSet->find(GetName()) ;
  clone2->disableCache(kTRUE) ;
  clone2->attachDataSet(cacheHist) ;

  // Iterator over all bins of RooDataHist and fill weights
  for (Int_t i=0 ; i<cacheHist.numEntries() ; i++) {
    const RooArgSet* obs = cacheHist.get(i) ;
    Double_t wgt = clone2->getVal(obs) ;
    cacheHist.set(wgt) ;
  }

  cache.pdf()->setUnitNorm(kTRUE) ;

  delete cloneSet ;
}



//_____________________________________________________________________________
RooArgSet* RooAbsSelfCachedPdf::actualObservables(const RooArgSet& /*nset*/) const 
{
  // Defines observables to be cached, given a set of user defined observables
  // Returns the subset of nset that are observables this p.d.f

  // Make list of servers
  RooArgSet servers ;

  TIterator* siter = serverIterator() ;
  siter->Reset() ;
  RooAbsArg* server ;
  while((server=(RooAbsArg*)siter->Next())) {
    servers.add(*server) ;
  }
  
  // Return servers that are in common with given normalization set
  return new RooArgSet(servers) ;
  //return (RooArgSet*) servers.selectCommon(nset) ;
  
}



//_____________________________________________________________________________
RooArgSet* RooAbsSelfCachedPdf::actualParameters(const RooArgSet& nset) const 
{  
  // Defines parameters on which cache contents depends. Returns
  // subset of variables of self that is not contained in the
  // supplied nset

  RooArgSet *servers = new RooArgSet ;

  TIterator* siter = serverIterator() ;
  siter->Reset() ;
  RooAbsArg* server ;
  while((server=(RooAbsArg*)siter->Next())) {
    servers->add(*server) ;
  }
  
  // Remove all given observables from server list
  servers->remove(nset,kTRUE,kTRUE) ;

  return servers ;
}







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