Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooDerivative.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 /**
18 \file RooDerivative.cxx
19 \class RooDerivative
20 \ingroup Roofitcore
21 
22 RooDerivative represents the first, second, or third order derivative
23 of any RooAbsReal as calculated (numerically) by the MathCore Richardson
24 derivator class.
25 **/
26 
27 
28 #include "RooFit.h"
29 
30 #include "Riostream.h"
31 #include "Riostream.h"
32 #include <math.h>
33 
34 #include "RooDerivative.h"
35 #include "RooAbsReal.h"
36 #include "RooAbsPdf.h"
37 #include "RooErrorHandler.h"
38 #include "RooArgSet.h"
39 #include "RooMsgService.h"
40 #include "RooRealVar.h"
41 #include "RooFunctor.h"
42 
43 #include "Math/WrappedFunction.h"
45 
46 using namespace std;
47 
48 ClassImp(RooDerivative);
49 ;
50 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Default constructor
54 
55 RooDerivative::RooDerivative() : _order(1), _eps(1e-7), _ftor(0), _rd(0)
56 {
57 }
58 
59 
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 
63 RooDerivative::RooDerivative(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, Int_t orderIn, Double_t epsIn) :
64  RooAbsReal(name, title),
65  _order(orderIn),
66  _eps(epsIn),
67  _nset("nset","nset",this,kFALSE,kFALSE),
68  _func("function","function",this,func),
69  _x("x","x",this,x),
70  _ftor(0),
71  _rd(0)
72 {
73  if (_order<0 || _order>3 ) {
74  throw std::string(Form("RooDerivative::ctor(%s) ERROR, derivation order must be 1,2 or 3",name)) ;
75  }
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 
80 RooDerivative::RooDerivative(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset, Int_t orderIn, Double_t epsIn) :
81  RooAbsReal(name, title),
82  _order(orderIn),
83  _eps(epsIn),
84  _nset("nset","nset",this,kFALSE,kFALSE),
85  _func("function","function",this,func),
86  _x("x","x",this,x),
87  _ftor(0),
88  _rd(0)
89 {
90  if (_order<0 || _order>3) {
91  throw std::string(Form("RooDerivative::ctor(%s) ERROR, derivation order must be 1,2 or 3",name)) ;
92  }
93  _nset.add(nset) ;
94 }
95 
96 
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 
100 RooDerivative::RooDerivative(const RooDerivative& other, const char* name) :
101  RooAbsReal(other, name),
102  _order(other._order),
103  _eps(other._eps),
104  _nset("nset",this,other._nset),
105  _func("function",this,other._func),
106  _x("x",this,other._x),
107  _ftor(0),
108  _rd(0)
109 {
110 }
111 
112 
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// Destructor
116 
117 RooDerivative::~RooDerivative()
118 {
119  if (_rd) delete _rd ;
120  if (_ftor) delete _ftor ;
121 }
122 
123 
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Calculate value
127 
128 Double_t RooDerivative::evaluate() const
129 {
130  if (!_ftor) {
131  _ftor = _func.arg().functor(_x.arg(),RooArgSet(),_nset) ;
132  ROOT::Math::WrappedFunction<RooFunctor&> wf(*_ftor);
133  _rd = new ROOT::Math::RichardsonDerivator(wf,_eps*(_x.max()-_x.min()),kTRUE) ;
134  }
135 
136  switch (_order) {
137  case 1: return _rd->Derivative1(_x);
138  case 2: return _rd->Derivative2(_x);
139  case 3: return _rd->Derivative3(_x);
140  }
141  return 0 ;
142 }
143 
144 
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Zap functor and derivator ;
148 
149 Bool_t RooDerivative::redirectServersHook(const RooAbsCollection& /*newServerList*/, Bool_t /*mustReplaceAll*/, Bool_t /*nameChange*/, Bool_t /*isRecursive*/)
150 {
151  delete _ftor ;
152  delete _rd ;
153  _ftor = 0 ;
154  _rd = 0 ;
155  return kFALSE ;
156 }