Logo ROOT   6.12/07
Reference Guide
RooPolyVar.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 RooPolyVar.cxx
19 \class RooPolyVar
20 \ingroup Roofitcore
21 
22 Class RooPolyVar is a RooAbsReal implementing a polynomial in terms
23 of a list of RooAbsReal coefficients
24 \f[f(x) = \sum_{i} a_{i}x \f]
25 Class RooPolyvar implements analytical integrals of all polynomials
26 it can define.
27 **/
28 
29 #include <cmath>
30 
31 #include "RooPolyVar.h"
32 #include "RooArgList.h"
33 #include "RooMsgService.h"
34 //#include "Riostream.h"
35 
36 #include "TError.h"
37 
38 using namespace std;
39 
41 ;
42 
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// Default constructor
46 
47 RooPolyVar::RooPolyVar() : _lowestOrder(0)
48 { }
49 
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Construct polynomial in x with coefficients in coefList. If
53 /// lowestOrder is not zero, then the first element in coefList is
54 /// interpreted as as the 'lowestOrder' coefficients and all
55 /// subsequent coeffient elements are shifted by a similar amount.
56 RooPolyVar::RooPolyVar(const char* name, const char* title,
57  RooAbsReal& x, const RooArgList& coefList, Int_t lowestOrder) :
58  RooAbsReal(name, title),
59  _x("x", "Dependent", this, x),
60  _coefList("coefList","List of coefficients",this),
61  _lowestOrder(lowestOrder)
62 {
63  // Check lowest order
64  if (_lowestOrder<0) {
65  coutE(InputArguments) << "RooPolyVar::ctor(" << GetName()
66  << ") WARNING: lowestOrder must be >=0, setting value to 0" << endl ;
67  _lowestOrder=0 ;
68  }
69 
70  RooFIter coefIter = coefList.fwdIterator() ;
71  RooAbsArg* coef ;
72  while((coef = (RooAbsArg*)coefIter.next())) {
73  if (!dynamic_cast<RooAbsReal*>(coef)) {
74  coutE(InputArguments) << "RooPolyVar::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
75  << " is not of type RooAbsReal" << endl ;
76  R__ASSERT(0) ;
77  }
78  _coefList.add(*coef) ;
79  }
80 }
81 
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// Constructor of flat polynomial function
85 
86 RooPolyVar::RooPolyVar(const char* name, const char* title,
87  RooAbsReal& x) :
88  RooAbsReal(name, title),
89  _x("x", "Dependent", this, x),
90  _coefList("coefList","List of coefficients",this),
91  _lowestOrder(1)
92 { }
93 
94 
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Copy constructor
98 
99 RooPolyVar::RooPolyVar(const RooPolyVar& other, const char* name) :
100  RooAbsReal(other, name),
101  _x("x", this, other._x),
102  _coefList("coefList",this,other._coefList),
104 { }
105 
106 
107 
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Destructor
111 
113 { }
114 
115 
116 
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Calculate and return value of polynomial
120 
122 {
123  const unsigned sz = _coefList.getSize();
124  const int lowestOrder = _lowestOrder;
125  if (!sz) return lowestOrder ? 1. : 0.;
126  _wksp.clear();
127  _wksp.reserve(sz);
128  {
129  const RooArgSet* nset = _coefList.nset();
131  RooAbsReal* c;
132  while ((c = (RooAbsReal*) it.next())) _wksp.push_back(c->getVal(nset));
133  }
134  const Double_t x = _x;
135  Double_t retVal = _wksp[sz - 1];
136  for (unsigned i = sz - 1; i--; ) retVal = _wksp[i] + x * retVal;
137  return retVal * std::pow(x, lowestOrder);
138 }
139 
140 
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 /// Advertise that we can internally integrate over x
144 
145 Int_t RooPolyVar::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
146 {
147  if (matchArgs(allVars, analVars, _x)) return 1;
148  return 0;
149 }
150 
151 
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Calculate and return analytical integral over x
155 
156 Double_t RooPolyVar::analyticalIntegral(Int_t code, const char* rangeName) const
157 {
158  R__ASSERT(code==1) ;
159 
160  const Double_t xmin = _x.min(rangeName), xmax = _x.max(rangeName);
161  const int lowestOrder = _lowestOrder;
162  const unsigned sz = _coefList.getSize();
163  if (!sz) return xmax - xmin;
164  _wksp.clear();
165  _wksp.reserve(sz);
166  {
167  const RooArgSet* nset = _coefList.nset();
169  unsigned i = 1 + lowestOrder;
170  RooAbsReal* c;
171  while ((c = (RooAbsReal*) it.next())) {
172  _wksp.push_back(c->getVal(nset) / Double_t(i));
173  ++i;
174  }
175  }
176  Double_t min = _wksp[sz - 1], max = _wksp[sz - 1];
177  for (unsigned i = sz - 1; i--; )
178  min = _wksp[i] + xmin * min, max = _wksp[i] + xmax * max;
179  return max * std::pow(xmax, 1 + lowestOrder) - min * std::pow(xmin, 1 + lowestOrder);
180 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
#define coutE(a)
Definition: RooMsgService.h:34
float xmin
Definition: THbookFile.cxx:93
virtual ~RooPolyVar()
Destructor.
Definition: RooPolyVar.cxx:112
Bool_t matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
#define R__ASSERT(e)
Definition: TError.h:96
int Int_t
Definition: RtypesCore.h:41
STL namespace.
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=0) const
Advertise that we can internally integrate over x.
Definition: RooPolyVar.cxx:145
Double_t x[n]
Definition: legend1.C:17
double pow(double, double)
Class RooPolyVar is a RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficie...
Definition: RooPolyVar.h:28
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
const RooArgSet * nset() const
Definition: RooAbsProxy.h:46
Int_t getSize() const
float xmax
Definition: THbookFile.cxx:93
RooAbsArg * next()
Int_t _lowestOrder
Definition: RooPolyVar.h:47
#define ClassImp(name)
Definition: Rtypes.h:359
Double_t min(const char *rname=0) const
Definition: RooRealProxy.h:56
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
RooFIter fwdIterator() const
Double_t max(const char *rname=0) const
Definition: RooRealProxy.h:57
Double_t evaluate() const
do not persist
Definition: RooPolyVar.cxx:121
RooListProxy _coefList
Definition: RooPolyVar.h:46
Double_t analyticalIntegral(Int_t code, const char *rangeName=0) const
Calculate and return analytical integral over x.
Definition: RooPolyVar.cxx:156
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
std::vector< Double_t > _wksp
Definition: RooPolyVar.h:49
RooRealProxy _x
Definition: RooPolyVar.h:45
RooPolyVar()
Default constructor.
Definition: RooPolyVar.cxx:47
char name[80]
Definition: TGX11.cxx:109