/***************************************************************************** 
  * 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
// RooCachedReal is an implementation of RooAbsCachedReal that can cache
// any external RooAbsReal input function provided in the constructor. 
// END_HTML
//

#include "Riostream.h" 

#include "RooAbsPdf.h"
#include "RooCachedReal.h" 
#include "RooAbsReal.h" 
#include "RooMsgService.h"
#include "RooDataHist.h"
#include "RooHistPdf.h"
#include "RooChangeTracker.h"

using namespace std;

ClassImp(RooCachedReal) 
  ;


//_____________________________________________________________________________
RooCachedReal::RooCachedReal(const char *name, const char *title, RooAbsReal& _func) :
   RooAbsCachedReal(name,title), 
   func("func","func",this,_func),
   _useCdfBoundaries(kFALSE),
   _cacheSource(kFALSE)
 { 
   // Constructor taking name, title and function to be cached. To control
   // granularity of the binning of the cache histogram set the desired properties
   // in the binning named "cache" in the observables of the function
   
   // Choose same expensive object cache as input function
   setExpensiveObjectCache(_func.expensiveObjectCache()) ;
 } 




//_____________________________________________________________________________
RooCachedReal::RooCachedReal(const char *name, const char *title, RooAbsReal& _func, const RooArgSet& cacheObs) :
   RooAbsCachedReal(name,title), 
   func("func","func",this,_func),
   _cacheObs("cacheObs","cacheObs",this,kFALSE,kFALSE),
   _useCdfBoundaries(kFALSE),
   _cacheSource(kFALSE)
 { 
   // Constructor taking name, title and function to be cached and
   // fixed choice of variable to cache. To control granularity of the
   // binning of the cache histogram set the desired properties in the
   // binning named "cache" in the observables of the function.
   // If the fixed set of cache observables does not match the observables
   // defined in the use context of the p.d.f the cache is still filled
   // completely. Ee.g. when it is specified to cache x and p and only x 
   // is a observable in the given use context the cache histogram will
   // store sampled values for all values of observable x and parameter p.
   // In such a mode of operation the cache will also not be recalculated
   // if the observable p changes

   _cacheObs.add(cacheObs) ;

   // Choose same expensive object cache as input function
   setExpensiveObjectCache(_func.expensiveObjectCache()) ;
 } 




//_____________________________________________________________________________
RooCachedReal::RooCachedReal(const RooCachedReal& other, const char* name) :  
   RooAbsCachedReal(other,name), 
   func("func",this,other.func),
   _cacheObs("cacheObs",this,other._cacheObs),
   _useCdfBoundaries(other._useCdfBoundaries),
   _cacheSource(other._cacheSource)
 { 
   // Copy constructor
 } 



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


//_____________________________________________________________________________
RooAbsCachedReal::FuncCacheElem* RooCachedReal::createCache(const RooArgSet* nset) const
{
  // Interface function to create an internal cache object that represent
  // each cached function configuration. This interface allows to create and
  // return a class derived from RooAbsCachedReal::FuncCacheElem so that
  // a derived class fillCacheObject implementation can utilize extra functionality
  // defined in such a derived cache class
  
  FuncCacheElem* ret = RooAbsCachedReal::createCache(nset) ;
  if (_cacheSource) {
    ret->setCacheSource(kTRUE) ;
  }
  return ret ;
}



//_____________________________________________________________________________
void RooCachedReal::fillCacheObject(RooAbsCachedReal::FuncCacheElem& cache) const 
{
  // Update contents of cache histogram by resampling the input function

  if (cache.hist()->get()->getSize()>1) {
    coutP(Eval) << "RooCachedReal::fillCacheObject(" << GetName() << ") filling multi-dimensional cache (" << cache.hist()->numEntries() << " points)" ;
  }

  // Make deep clone of self and attach to dataset observables
  if (!cache.sourceClone()) {
    RooAbsArg* sourceClone = func.arg().cloneTree() ;
    cache.setSourceClone((RooAbsReal*)sourceClone) ;
    cache.sourceClone()->recursiveRedirectServers(*cache.hist()->get()) ;
    cache.sourceClone()->recursiveRedirectServers(cache.paramTracker()->parameters());  
  }

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

  // Delete source clone if we don't cache it
  if (!cache.cacheSource()) {
    cache.setSourceClone(0) ;
  }

  cache.func()->setCdfBoundaries(_useCdfBoundaries) ;
  if (cache.hist()->get()->getSize()>1) {
    ccoutP(Eval) << endl ;
  }

}



//_____________________________________________________________________________
RooArgSet* RooCachedReal::actualObservables(const RooArgSet& nset) const 
{ 
  // If this pdf is operated with a fixed set of observables, return
  // the subset of the fixed observables that are actual dependents
  // of the external input p.d.f. If this p.d.f is operated without
  // a fixed set of cache observables, return the actual observables
  // of the external input p.d.f given the choice of observables defined
  // in nset

  if (_cacheObs.getSize()>0) {
    return func.arg().getObservables(_cacheObs) ;
  } 

  return func.arg().getObservables(nset) ; 
}



//_____________________________________________________________________________
RooArgSet* RooCachedReal::actualParameters(const RooArgSet& nset) const 
{ 
  // If this p.d.f is operated with a fixed set of observables, return
  // all variables of the external input p.d.f that are not one of
  // the cache observables. If this p.d.f is operated in automatic mode,
  // return the parameters of the external input p.d.f

  if (_cacheObs.getSize()>0) {
    return func.arg().getParameters(_cacheObs) ;
  } 
  return func.arg().getParameters(nset) ; 
}


void RooCachedReal::operModeHook() 
{
  if (operMode()==ADirty) {
    ((RooAbsArg*)func.absArg())->setOperMode(ADirty) ;
  }
}




 RooCachedReal.cxx:1
 RooCachedReal.cxx:2
 RooCachedReal.cxx:3
 RooCachedReal.cxx:4
 RooCachedReal.cxx:5
 RooCachedReal.cxx:6
 RooCachedReal.cxx:7
 RooCachedReal.cxx:8
 RooCachedReal.cxx:9
 RooCachedReal.cxx:10
 RooCachedReal.cxx:11
 RooCachedReal.cxx:12
 RooCachedReal.cxx:13
 RooCachedReal.cxx:14
 RooCachedReal.cxx:15
 RooCachedReal.cxx:16
 RooCachedReal.cxx:17
 RooCachedReal.cxx:18
 RooCachedReal.cxx:19
 RooCachedReal.cxx:20
 RooCachedReal.cxx:21
 RooCachedReal.cxx:22
 RooCachedReal.cxx:23
 RooCachedReal.cxx:24
 RooCachedReal.cxx:25
 RooCachedReal.cxx:26
 RooCachedReal.cxx:27
 RooCachedReal.cxx:28
 RooCachedReal.cxx:29
 RooCachedReal.cxx:30
 RooCachedReal.cxx:31
 RooCachedReal.cxx:32
 RooCachedReal.cxx:33
 RooCachedReal.cxx:34
 RooCachedReal.cxx:35
 RooCachedReal.cxx:36
 RooCachedReal.cxx:37
 RooCachedReal.cxx:38
 RooCachedReal.cxx:39
 RooCachedReal.cxx:40
 RooCachedReal.cxx:41
 RooCachedReal.cxx:42
 RooCachedReal.cxx:43
 RooCachedReal.cxx:44
 RooCachedReal.cxx:45
 RooCachedReal.cxx:46
 RooCachedReal.cxx:47
 RooCachedReal.cxx:48
 RooCachedReal.cxx:49
 RooCachedReal.cxx:50
 RooCachedReal.cxx:51
 RooCachedReal.cxx:52
 RooCachedReal.cxx:53
 RooCachedReal.cxx:54
 RooCachedReal.cxx:55
 RooCachedReal.cxx:56
 RooCachedReal.cxx:57
 RooCachedReal.cxx:58
 RooCachedReal.cxx:59
 RooCachedReal.cxx:60
 RooCachedReal.cxx:61
 RooCachedReal.cxx:62
 RooCachedReal.cxx:63
 RooCachedReal.cxx:64
 RooCachedReal.cxx:65
 RooCachedReal.cxx:66
 RooCachedReal.cxx:67
 RooCachedReal.cxx:68
 RooCachedReal.cxx:69
 RooCachedReal.cxx:70
 RooCachedReal.cxx:71
 RooCachedReal.cxx:72
 RooCachedReal.cxx:73
 RooCachedReal.cxx:74
 RooCachedReal.cxx:75
 RooCachedReal.cxx:76
 RooCachedReal.cxx:77
 RooCachedReal.cxx:78
 RooCachedReal.cxx:79
 RooCachedReal.cxx:80
 RooCachedReal.cxx:81
 RooCachedReal.cxx:82
 RooCachedReal.cxx:83
 RooCachedReal.cxx:84
 RooCachedReal.cxx:85
 RooCachedReal.cxx:86
 RooCachedReal.cxx:87
 RooCachedReal.cxx:88
 RooCachedReal.cxx:89
 RooCachedReal.cxx:90
 RooCachedReal.cxx:91
 RooCachedReal.cxx:92
 RooCachedReal.cxx:93
 RooCachedReal.cxx:94
 RooCachedReal.cxx:95
 RooCachedReal.cxx:96
 RooCachedReal.cxx:97
 RooCachedReal.cxx:98
 RooCachedReal.cxx:99
 RooCachedReal.cxx:100
 RooCachedReal.cxx:101
 RooCachedReal.cxx:102
 RooCachedReal.cxx:103
 RooCachedReal.cxx:104
 RooCachedReal.cxx:105
 RooCachedReal.cxx:106
 RooCachedReal.cxx:107
 RooCachedReal.cxx:108
 RooCachedReal.cxx:109
 RooCachedReal.cxx:110
 RooCachedReal.cxx:111
 RooCachedReal.cxx:112
 RooCachedReal.cxx:113
 RooCachedReal.cxx:114
 RooCachedReal.cxx:115
 RooCachedReal.cxx:116
 RooCachedReal.cxx:117
 RooCachedReal.cxx:118
 RooCachedReal.cxx:119
 RooCachedReal.cxx:120
 RooCachedReal.cxx:121
 RooCachedReal.cxx:122
 RooCachedReal.cxx:123
 RooCachedReal.cxx:124
 RooCachedReal.cxx:125
 RooCachedReal.cxx:126
 RooCachedReal.cxx:127
 RooCachedReal.cxx:128
 RooCachedReal.cxx:129
 RooCachedReal.cxx:130
 RooCachedReal.cxx:131
 RooCachedReal.cxx:132
 RooCachedReal.cxx:133
 RooCachedReal.cxx:134
 RooCachedReal.cxx:135
 RooCachedReal.cxx:136
 RooCachedReal.cxx:137
 RooCachedReal.cxx:138
 RooCachedReal.cxx:139
 RooCachedReal.cxx:140
 RooCachedReal.cxx:141
 RooCachedReal.cxx:142
 RooCachedReal.cxx:143
 RooCachedReal.cxx:144
 RooCachedReal.cxx:145
 RooCachedReal.cxx:146
 RooCachedReal.cxx:147
 RooCachedReal.cxx:148
 RooCachedReal.cxx:149
 RooCachedReal.cxx:150
 RooCachedReal.cxx:151
 RooCachedReal.cxx:152
 RooCachedReal.cxx:153
 RooCachedReal.cxx:154
 RooCachedReal.cxx:155
 RooCachedReal.cxx:156
 RooCachedReal.cxx:157
 RooCachedReal.cxx:158
 RooCachedReal.cxx:159
 RooCachedReal.cxx:160
 RooCachedReal.cxx:161
 RooCachedReal.cxx:162
 RooCachedReal.cxx:163
 RooCachedReal.cxx:164
 RooCachedReal.cxx:165
 RooCachedReal.cxx:166
 RooCachedReal.cxx:167
 RooCachedReal.cxx:168
 RooCachedReal.cxx:169
 RooCachedReal.cxx:170
 RooCachedReal.cxx:171
 RooCachedReal.cxx:172
 RooCachedReal.cxx:173
 RooCachedReal.cxx:174
 RooCachedReal.cxx:175
 RooCachedReal.cxx:176
 RooCachedReal.cxx:177
 RooCachedReal.cxx:178
 RooCachedReal.cxx:179
 RooCachedReal.cxx:180
 RooCachedReal.cxx:181
 RooCachedReal.cxx:182
 RooCachedReal.cxx:183
 RooCachedReal.cxx:184
 RooCachedReal.cxx:185
 RooCachedReal.cxx:186
 RooCachedReal.cxx:187
 RooCachedReal.cxx:188
 RooCachedReal.cxx:189
 RooCachedReal.cxx:190
 RooCachedReal.cxx:191
 RooCachedReal.cxx:192
 RooCachedReal.cxx:193
 RooCachedReal.cxx:194
 RooCachedReal.cxx:195
 RooCachedReal.cxx:196
 RooCachedReal.cxx:197
 RooCachedReal.cxx:198
 RooCachedReal.cxx:199
 RooCachedReal.cxx:200
 RooCachedReal.cxx:201
 RooCachedReal.cxx:202