/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id$
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
 *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
 *                                                                           *
 * 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
// Lightweight interface adaptor that exports a RooAbsPdf as a functor
// END_HTML
//


#include "RooFit.h"
#include "Riostream.h"

#include "RooFunctor.h"
#include "RooRealBinding.h"
#include "RooAbsReal.h"
#include "RooAbsPdf.h"
#include "RooArgSet.h"

#include <assert.h>



using namespace std;

ClassImp(RooFunctor)
;


//_____________________________________________________________________________
RooFunctor::RooFunctor(const RooAbsFunc& func)
{
  _ownBinding = kFALSE ;

  _x = new Double_t[func.getDimension()] ; 

  _nobs = func.getDimension() ;
  _npar = 0 ;
  _binding = (RooAbsFunc*) &func ;
}



//_____________________________________________________________________________
RooFunctor::RooFunctor(const RooAbsReal& func, const RooArgList& observables, const RooArgList& parameters) 
{
  // Store list of observables
  _nset.add(observables) ;

  // Make list of all variables to be bound
  RooArgList allVars(observables) ;
  allVars.add(parameters) ;

  // Create RooFit function binding
  _binding = new RooRealBinding(func,allVars,&_nset,kFALSE,0) ;
  _ownBinding = kTRUE ;

  // Allocate transfer array
  _x = new Double_t[allVars.getSize()] ; 
  _nobs = observables.getSize() ;
  _npar = parameters.getSize() ;
}


//_____________________________________________________________________________
RooFunctor::RooFunctor(const RooAbsReal& func, const RooArgList& observables, const RooArgList& parameters, const RooArgSet& nset) 
{
  // Store normalization set
  _nset.add(nset) ;

  // Make list of all variables to be bound
  RooArgList allVars(observables) ;
  allVars.add(parameters) ;

  // Create RooFit function binding
  _binding = new RooRealBinding(func,allVars,&_nset,kFALSE,0) ;
  _ownBinding = kTRUE ;

  // Allocate transfer array
  _x = new Double_t[allVars.getSize()] ; 
  _nobs = observables.getSize() ;
  _npar = parameters.getSize() ;
}



//_____________________________________________________________________________
RooFunctor::RooFunctor(const RooFunctor& other) :
  _ownBinding(other._ownBinding),
  _nset(other._nset),
  _binding(0),
  _npar(other._npar),
  _nobs(other._nobs)
{
  if (other._ownBinding) {
    _binding = new RooRealBinding((RooRealBinding&)*other._binding,&_nset) ;
  } else {
    _binding = other._binding ;
  }
  _x = new Double_t[_nobs+_npar] ;
}




//_____________________________________________________________________________
RooFunctor::~RooFunctor() 
{
  // Destructor
  if (_ownBinding) delete _binding ; 
  delete[] _x ;
}



//_____________________________________________________________________________
Double_t RooFunctor::eval(const Double_t *x) const
{
  return (*_binding)(x) ;
}

//_____________________________________________________________________________
Double_t RooFunctor::eval(Double_t x) const
{
  return (*_binding)(&x) ;
}

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