/***************************************************************************** 
 * Project: RooFit                                                           * 
 *                                                                           * 
 * This code was autogenerated by RooClassFactory                            * 
 *****************************************************************************/ 

// Your description goes here... 

#include "Riostream.h" 
#include "RooParamHistFunc.h" 
#include "RooAbsReal.h" 
#include "RooAbsCategory.h" 
#include "RooRealVar.h"
#include <math.h> 
#include "TMath.h" 


using namespace std ;


ClassImp(RooParamHistFunc) 
  ;  


//_____________________________________________________________________________
RooParamHistFunc::RooParamHistFunc(const char *name, const char *title, RooDataHist& dh, Bool_t paramRelative) :
  RooAbsReal(name,title), 
  _x("x","x",this),
  _p("p","p",this),
  _dh(dh),
  _relParam(paramRelative)
{ 
  // Populate x with observables
  _x.add(*_dh.get()) ;

  // Now populate p with parameters 
  RooArgSet allVars ;
  for (Int_t i=0 ; i<_dh.numEntries() ; i++) {
    _dh.get(i) ;
    const char* vname = Form("%s_gamma_bin_%i",GetName(),i) ;
    RooRealVar* var = new RooRealVar(vname,vname,0,1000) ;    
    var->setVal(_relParam ? 1 : _dh.weight()) ;
    var->setConstant(kTRUE) ;
    allVars.add(*var) ;
    _p.add(*var) ;
  }
  addOwnedComponents(allVars) ;
} 



//_____________________________________________________________________________
RooParamHistFunc::RooParamHistFunc(const char *name, const char *title, const RooAbsArg& /*x*/, RooDataHist& dh, Bool_t paramRelative) :
  RooAbsReal(name,title), 
  _x("x","x",this),
  _p("p","p",this),
  _dh(dh),
  _relParam(paramRelative)
{ 
  // Populate x with observables
  _x.add(*_dh.get()) ;

  // Now populate p with parameters 
  RooArgSet allVars ;
  for (Int_t i=0 ; i<_dh.numEntries() ; i++) {
    _dh.get(i) ;
    const char* vname = Form("%s_gamma_bin_%i",GetName(),i) ;
    RooRealVar* var = new RooRealVar(vname,vname,0,1000) ;    
    var->setVal(_relParam ? 1 : _dh.weight()) ;
    var->setConstant(kTRUE) ;
    allVars.add(*var) ;
    _p.add(*var) ;
  }
  addOwnedComponents(allVars) ;
} 



//_____________________________________________________________________________
RooParamHistFunc::RooParamHistFunc(const char *name, const char *title, RooDataHist& dh, const RooParamHistFunc& paramSource, Bool_t paramRelative) :
  RooAbsReal(name,title), 
  _x("x","x",this),
  _p("p","p",this),
  _dh(dh),
  _relParam(paramRelative)
{ 

  // Populate x with observables
  _x.add(*_dh.get()) ;
  
  // Now populate p with existing parameters  
  _p.add(paramSource._p) ;
} 



//_____________________________________________________________________________
RooParamHistFunc::RooParamHistFunc(const RooParamHistFunc& other, const char* name) :  
  RooAbsReal(other,name), 
  _x("x",this,other._x),
  _p("p",this,other._p),
  _dh(other._dh),
  _relParam(other._relParam)
{ 
} 



//_____________________________________________________________________________
Double_t RooParamHistFunc::evaluate() const 
{ 
  Int_t idx = ((RooDataHist&)_dh).getIndex(_x,kTRUE) ;
  Double_t ret = ((RooAbsReal*)_p.at(idx))->getVal() ;
  if (_relParam) {
    Double_t nom = getNominal(idx) ;
    ret *= nom ;
  }
  return  ret ;
} 



//_____________________________________________________________________________
Double_t RooParamHistFunc::getActual(Int_t ibin) 
{
  return ((RooAbsReal&)_p[ibin]).getVal() ;
}



//_____________________________________________________________________________
void RooParamHistFunc::setActual(Int_t ibin, Double_t newVal) 
{
  ((RooRealVar&)_p[ibin]).setVal(newVal) ;
}



//_____________________________________________________________________________
Double_t RooParamHistFunc::getNominal(Int_t ibin) const
{
  _dh.get(ibin) ;
  return _dh.weight() ;
}
  

//_____________________________________________________________________________
Double_t RooParamHistFunc::getNominalError(Int_t ibin) const
{
  _dh.get(ibin) ;
  return _dh.weightError() ;
}
  


//_____________________________________________________________________________
list<Double_t>* RooParamHistFunc::plotSamplingHint(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const
{
  // Return sampling hint for making curves of (projections) of this function
  // as the recursive division strategy of RooCurve cannot deal efficiently
  // with the vertical lines that occur in a non-interpolated histogram

  // Check that observable is in dataset, if not no hint is generated
  RooAbsLValue* lvarg = dynamic_cast<RooAbsLValue*>(_dh.get()->find(obs.GetName())) ;
  if (!lvarg) {
    return 0 ;
  }

  // Retrieve position of all bin boundaries
  const RooAbsBinning* binning = lvarg->getBinningPtr(0) ;
  Double_t* boundaries = binning->array() ;

  list<Double_t>* hint = new list<Double_t> ;

  // Widen range slighty
  xlo = xlo - 0.01*(xhi-xlo) ;
  xhi = xhi + 0.01*(xhi-xlo) ;

  Double_t delta = (xhi-xlo)*1e-8 ;
 
  // Construct array with pairs of points positioned epsilon to the left and
  // right of the bin boundaries
  for (Int_t i=0 ; i<binning->numBoundaries() ; i++) {
    if (boundaries[i]>=xlo && boundaries[i]<=xhi) {
      hint->push_back(boundaries[i]-delta) ;
      hint->push_back(boundaries[i]+delta) ;
    }
  }

  return hint ;
}


//______________________________________________________________________________
std::list<Double_t>* RooParamHistFunc::binBoundaries(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const 
{
  // Return sampling hint for making curves of (projections) of this function
  // as the recursive division strategy of RooCurve cannot deal efficiently
  // with the vertical lines that occur in a non-interpolated histogram

  // Check that observable is in dataset, if not no hint is generated
  RooAbsLValue* lvarg = dynamic_cast<RooAbsLValue*>(_dh.get()->find(obs.GetName())) ;
  if (!lvarg) {
    return 0 ;
  }

  // Retrieve position of all bin boundaries
  const RooAbsBinning* binning = lvarg->getBinningPtr(0) ;
  Double_t* boundaries = binning->array() ;

  list<Double_t>* hint = new list<Double_t> ;

  // Construct array with pairs of points positioned epsilon to the left and
  // right of the bin boundaries
  for (Int_t i=0 ; i<binning->numBoundaries() ; i++) {
    if (boundaries[i]>=xlo && boundaries[i]<=xhi) {
      hint->push_back(boundaries[i]) ;
    }
  }

  return hint ;
}



//_____________________________________________________________________________
Int_t RooParamHistFunc::getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, 
						const RooArgSet* /*normSet*/, const char* /*rangeName*/) const 
{
  // Advertise that all integrals can be handled internally.

  // Simplest scenario, integrate over all dependents
  RooAbsCollection *allVarsCommon = allVars.selectCommon(_x) ;  
  Bool_t intAllObs = (allVarsCommon->getSize()==_x.getSize()) ;  
  delete allVarsCommon ;
  if (intAllObs && matchArgs(allVars,analVars,_x)) {
    return 1 ;
  }

  return 0 ; 
}




//_____________________________________________________________________________
Double_t RooParamHistFunc::analyticalIntegralWN(Int_t code, const RooArgSet* /*normSet2*/,const char* /*rangeName*/) const 
{
  // Implement analytical integrations by doing appropriate weighting from  component integrals
  // functions to integrators of components

  R__ASSERT(code==1) ;

  RooFIter iter = _p.fwdIterator() ;
  RooAbsReal* p ;
  Double_t ret(0) ;
  Int_t i(0) ;
  while((p=(RooAbsReal*)iter.next())) {
    Double_t bin = p->getVal() ; 
    if (_relParam) bin *= getNominal(i++) ;
    ret += bin ;
  }

  // WVE fix this!!! Assume uniform binning for now!
  RooFIter xiter = _x.fwdIterator() ;
  RooAbsArg* obs ;
  Double_t binV(1) ;
  while((obs=xiter.next())) {
    RooRealVar* xx = (RooRealVar*) obs ;
    binV *= (xx->getMax()-xx->getMin())/xx->numBins() ;
  }  
  
  return ret*binV ;
}


 RooParamHistFunc.cxx:1
 RooParamHistFunc.cxx:2
 RooParamHistFunc.cxx:3
 RooParamHistFunc.cxx:4
 RooParamHistFunc.cxx:5
 RooParamHistFunc.cxx:6
 RooParamHistFunc.cxx:7
 RooParamHistFunc.cxx:8
 RooParamHistFunc.cxx:9
 RooParamHistFunc.cxx:10
 RooParamHistFunc.cxx:11
 RooParamHistFunc.cxx:12
 RooParamHistFunc.cxx:13
 RooParamHistFunc.cxx:14
 RooParamHistFunc.cxx:15
 RooParamHistFunc.cxx:16
 RooParamHistFunc.cxx:17
 RooParamHistFunc.cxx:18
 RooParamHistFunc.cxx:19
 RooParamHistFunc.cxx:20
 RooParamHistFunc.cxx:21
 RooParamHistFunc.cxx:22
 RooParamHistFunc.cxx:23
 RooParamHistFunc.cxx:24
 RooParamHistFunc.cxx:25
 RooParamHistFunc.cxx:26
 RooParamHistFunc.cxx:27
 RooParamHistFunc.cxx:28
 RooParamHistFunc.cxx:29
 RooParamHistFunc.cxx:30
 RooParamHistFunc.cxx:31
 RooParamHistFunc.cxx:32
 RooParamHistFunc.cxx:33
 RooParamHistFunc.cxx:34
 RooParamHistFunc.cxx:35
 RooParamHistFunc.cxx:36
 RooParamHistFunc.cxx:37
 RooParamHistFunc.cxx:38
 RooParamHistFunc.cxx:39
 RooParamHistFunc.cxx:40
 RooParamHistFunc.cxx:41
 RooParamHistFunc.cxx:42
 RooParamHistFunc.cxx:43
 RooParamHistFunc.cxx:44
 RooParamHistFunc.cxx:45
 RooParamHistFunc.cxx:46
 RooParamHistFunc.cxx:47
 RooParamHistFunc.cxx:48
 RooParamHistFunc.cxx:49
 RooParamHistFunc.cxx:50
 RooParamHistFunc.cxx:51
 RooParamHistFunc.cxx:52
 RooParamHistFunc.cxx:53
 RooParamHistFunc.cxx:54
 RooParamHistFunc.cxx:55
 RooParamHistFunc.cxx:56
 RooParamHistFunc.cxx:57
 RooParamHistFunc.cxx:58
 RooParamHistFunc.cxx:59
 RooParamHistFunc.cxx:60
 RooParamHistFunc.cxx:61
 RooParamHistFunc.cxx:62
 RooParamHistFunc.cxx:63
 RooParamHistFunc.cxx:64
 RooParamHistFunc.cxx:65
 RooParamHistFunc.cxx:66
 RooParamHistFunc.cxx:67
 RooParamHistFunc.cxx:68
 RooParamHistFunc.cxx:69
 RooParamHistFunc.cxx:70
 RooParamHistFunc.cxx:71
 RooParamHistFunc.cxx:72
 RooParamHistFunc.cxx:73
 RooParamHistFunc.cxx:74
 RooParamHistFunc.cxx:75
 RooParamHistFunc.cxx:76
 RooParamHistFunc.cxx:77
 RooParamHistFunc.cxx:78
 RooParamHistFunc.cxx:79
 RooParamHistFunc.cxx:80
 RooParamHistFunc.cxx:81
 RooParamHistFunc.cxx:82
 RooParamHistFunc.cxx:83
 RooParamHistFunc.cxx:84
 RooParamHistFunc.cxx:85
 RooParamHistFunc.cxx:86
 RooParamHistFunc.cxx:87
 RooParamHistFunc.cxx:88
 RooParamHistFunc.cxx:89
 RooParamHistFunc.cxx:90
 RooParamHistFunc.cxx:91
 RooParamHistFunc.cxx:92
 RooParamHistFunc.cxx:93
 RooParamHistFunc.cxx:94
 RooParamHistFunc.cxx:95
 RooParamHistFunc.cxx:96
 RooParamHistFunc.cxx:97
 RooParamHistFunc.cxx:98
 RooParamHistFunc.cxx:99
 RooParamHistFunc.cxx:100
 RooParamHistFunc.cxx:101
 RooParamHistFunc.cxx:102
 RooParamHistFunc.cxx:103
 RooParamHistFunc.cxx:104
 RooParamHistFunc.cxx:105
 RooParamHistFunc.cxx:106
 RooParamHistFunc.cxx:107
 RooParamHistFunc.cxx:108
 RooParamHistFunc.cxx:109
 RooParamHistFunc.cxx:110
 RooParamHistFunc.cxx:111
 RooParamHistFunc.cxx:112
 RooParamHistFunc.cxx:113
 RooParamHistFunc.cxx:114
 RooParamHistFunc.cxx:115
 RooParamHistFunc.cxx:116
 RooParamHistFunc.cxx:117
 RooParamHistFunc.cxx:118
 RooParamHistFunc.cxx:119
 RooParamHistFunc.cxx:120
 RooParamHistFunc.cxx:121
 RooParamHistFunc.cxx:122
 RooParamHistFunc.cxx:123
 RooParamHistFunc.cxx:124
 RooParamHistFunc.cxx:125
 RooParamHistFunc.cxx:126
 RooParamHistFunc.cxx:127
 RooParamHistFunc.cxx:128
 RooParamHistFunc.cxx:129
 RooParamHistFunc.cxx:130
 RooParamHistFunc.cxx:131
 RooParamHistFunc.cxx:132
 RooParamHistFunc.cxx:133
 RooParamHistFunc.cxx:134
 RooParamHistFunc.cxx:135
 RooParamHistFunc.cxx:136
 RooParamHistFunc.cxx:137
 RooParamHistFunc.cxx:138
 RooParamHistFunc.cxx:139
 RooParamHistFunc.cxx:140
 RooParamHistFunc.cxx:141
 RooParamHistFunc.cxx:142
 RooParamHistFunc.cxx:143
 RooParamHistFunc.cxx:144
 RooParamHistFunc.cxx:145
 RooParamHistFunc.cxx:146
 RooParamHistFunc.cxx:147
 RooParamHistFunc.cxx:148
 RooParamHistFunc.cxx:149
 RooParamHistFunc.cxx:150
 RooParamHistFunc.cxx:151
 RooParamHistFunc.cxx:152
 RooParamHistFunc.cxx:153
 RooParamHistFunc.cxx:154
 RooParamHistFunc.cxx:155
 RooParamHistFunc.cxx:156
 RooParamHistFunc.cxx:157
 RooParamHistFunc.cxx:158
 RooParamHistFunc.cxx:159
 RooParamHistFunc.cxx:160
 RooParamHistFunc.cxx:161
 RooParamHistFunc.cxx:162
 RooParamHistFunc.cxx:163
 RooParamHistFunc.cxx:164
 RooParamHistFunc.cxx:165
 RooParamHistFunc.cxx:166
 RooParamHistFunc.cxx:167
 RooParamHistFunc.cxx:168
 RooParamHistFunc.cxx:169
 RooParamHistFunc.cxx:170
 RooParamHistFunc.cxx:171
 RooParamHistFunc.cxx:172
 RooParamHistFunc.cxx:173
 RooParamHistFunc.cxx:174
 RooParamHistFunc.cxx:175
 RooParamHistFunc.cxx:176
 RooParamHistFunc.cxx:177
 RooParamHistFunc.cxx:178
 RooParamHistFunc.cxx:179
 RooParamHistFunc.cxx:180
 RooParamHistFunc.cxx:181
 RooParamHistFunc.cxx:182
 RooParamHistFunc.cxx:183
 RooParamHistFunc.cxx:184
 RooParamHistFunc.cxx:185
 RooParamHistFunc.cxx:186
 RooParamHistFunc.cxx:187
 RooParamHistFunc.cxx:188
 RooParamHistFunc.cxx:189
 RooParamHistFunc.cxx:190
 RooParamHistFunc.cxx:191
 RooParamHistFunc.cxx:192
 RooParamHistFunc.cxx:193
 RooParamHistFunc.cxx:194
 RooParamHistFunc.cxx:195
 RooParamHistFunc.cxx:196
 RooParamHistFunc.cxx:197
 RooParamHistFunc.cxx:198
 RooParamHistFunc.cxx:199
 RooParamHistFunc.cxx:200
 RooParamHistFunc.cxx:201
 RooParamHistFunc.cxx:202
 RooParamHistFunc.cxx:203
 RooParamHistFunc.cxx:204
 RooParamHistFunc.cxx:205
 RooParamHistFunc.cxx:206
 RooParamHistFunc.cxx:207
 RooParamHistFunc.cxx:208
 RooParamHistFunc.cxx:209
 RooParamHistFunc.cxx:210
 RooParamHistFunc.cxx:211
 RooParamHistFunc.cxx:212
 RooParamHistFunc.cxx:213
 RooParamHistFunc.cxx:214
 RooParamHistFunc.cxx:215
 RooParamHistFunc.cxx:216
 RooParamHistFunc.cxx:217
 RooParamHistFunc.cxx:218
 RooParamHistFunc.cxx:219
 RooParamHistFunc.cxx:220
 RooParamHistFunc.cxx:221
 RooParamHistFunc.cxx:222
 RooParamHistFunc.cxx:223
 RooParamHistFunc.cxx:224
 RooParamHistFunc.cxx:225
 RooParamHistFunc.cxx:226
 RooParamHistFunc.cxx:227
 RooParamHistFunc.cxx:228
 RooParamHistFunc.cxx:229
 RooParamHistFunc.cxx:230
 RooParamHistFunc.cxx:231
 RooParamHistFunc.cxx:232
 RooParamHistFunc.cxx:233
 RooParamHistFunc.cxx:234
 RooParamHistFunc.cxx:235
 RooParamHistFunc.cxx:236
 RooParamHistFunc.cxx:237
 RooParamHistFunc.cxx:238
 RooParamHistFunc.cxx:239
 RooParamHistFunc.cxx:240
 RooParamHistFunc.cxx:241
 RooParamHistFunc.cxx:242
 RooParamHistFunc.cxx:243
 RooParamHistFunc.cxx:244
 RooParamHistFunc.cxx:245
 RooParamHistFunc.cxx:246
 RooParamHistFunc.cxx:247
 RooParamHistFunc.cxx:248
 RooParamHistFunc.cxx:249
 RooParamHistFunc.cxx:250
 RooParamHistFunc.cxx:251
 RooParamHistFunc.cxx:252
 RooParamHistFunc.cxx:253
 RooParamHistFunc.cxx:254
 RooParamHistFunc.cxx:255
 RooParamHistFunc.cxx:256
 RooParamHistFunc.cxx:257
 RooParamHistFunc.cxx:258
 RooParamHistFunc.cxx:259
 RooParamHistFunc.cxx:260
 RooParamHistFunc.cxx:261
 RooParamHistFunc.cxx:262
 RooParamHistFunc.cxx:263
 RooParamHistFunc.cxx:264
 RooParamHistFunc.cxx:265
 RooParamHistFunc.cxx:266
 RooParamHistFunc.cxx:267
 RooParamHistFunc.cxx:268
 RooParamHistFunc.cxx:269
 RooParamHistFunc.cxx:270
 RooParamHistFunc.cxx:271
 RooParamHistFunc.cxx:272
 RooParamHistFunc.cxx:273
 RooParamHistFunc.cxx:274
 RooParamHistFunc.cxx:275
 RooParamHistFunc.cxx:276