```/*****************************************************************************
* 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          *
*                                                                           *
* Redistribution and use in source and binary forms,                        *
* with or without modification, are permitted according to the terms        *
*****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//
// BEGIN_HTML
// RooDerivative represents the first, second, or third order derivative
// of any RooAbsReal as calculated (numerically) by the MathCore Richardson
// derivator class.
// END_HTML
//

#include "RooFit.h"

#include "Riostream.h"
#include "Riostream.h"
#include <math.h>
#include <string>

#include "RooDerivative.h"
#include "RooAbsReal.h"
#include "RooAbsPdf.h"
#include "RooErrorHandler.h"
#include "RooArgSet.h"
#include "RooMsgService.h"
#include "RooRealVar.h"
#include "RooFunctor.h"

#include "Math/WrappedFunction.h"
#include "Math/RichardsonDerivator.h"

using namespace std;

ClassImp(RooDerivative)
;

//_____________________________________________________________________________
RooDerivative::RooDerivative() : _order(1), _eps(1e-7), _ftor(0), _rd(0)
{
// Default constructor
}

//_____________________________________________________________________________
RooDerivative::RooDerivative(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, Int_t orderIn, Double_t epsIn) :
RooAbsReal(name, title),
_order(orderIn),
_eps(epsIn),
_nset("nset","nset",this,kFALSE,kFALSE),
_func("function","function",this,func),
_x("x","x",this,x),
_ftor(0),
_rd(0)
{
if (_order<0 || _order>3 ) {
throw std::string(Form("RooDerivative::ctor(%s) ERROR, derivation order must be 1,2 or 3",name)) ;
}
}

//_____________________________________________________________________________
RooDerivative::RooDerivative(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset, Int_t orderIn, Double_t epsIn) :
RooAbsReal(name, title),
_order(orderIn),
_eps(epsIn),
_nset("nset","nset",this,kFALSE,kFALSE),
_func("function","function",this,func),
_x("x","x",this,x),
_ftor(0),
_rd(0)
{
if (_order<0 || _order>3) {
throw std::string(Form("RooDerivative::ctor(%s) ERROR, derivation order must be 1,2 or 3",name)) ;
}
}

//_____________________________________________________________________________
RooDerivative::RooDerivative(const RooDerivative& other, const char* name) :
RooAbsReal(other, name),
_order(other._order),
_eps(other._eps),
_nset("nset",this,other._nset),
_func("function",this,other._func),
_x("x",this,other._x),
_ftor(0),
_rd(0)
{
}

//_____________________________________________________________________________
RooDerivative::~RooDerivative()
{
// Destructor
if (_rd) delete _rd ;
if (_ftor) delete _ftor ;
}

//_____________________________________________________________________________
Double_t RooDerivative::evaluate() const
{
// Calculate value
if (!_ftor) {
_ftor = _func.arg().functor(_x.arg(),RooArgSet(),_nset)  ;
ROOT::Math::WrappedFunction<RooFunctor&> wf(*_ftor);
_rd = new ROOT::Math::RichardsonDerivator(wf,_eps*(_x.max()-_x.min()),kTRUE) ;
}

switch (_order) {
case 1: return _rd->Derivative1(_x);
case 2: return _rd->Derivative2(_x);
case 3: return _rd->Derivative3(_x);
}
return 0 ;
}

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