/*****************************************************************************
 * 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
// Class RooPolyVar is a RooAbsReal implementing a polynomial in terms
// of a list of RooAbsReal coefficients
// <pre>
// f(x) = sum_i a_i * x
// </pre>
// Class RooPolyvar implements analytical integrals of all polynomials
// it can define.
// END_HTML
//

#include "RooFit.h"

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

#include "RooPolyVar.h"
#include "RooAbsReal.h"
#include "RooRealVar.h"
#include "RooArgList.h"
#include "RooMsgService.h"
#include "TMath.h"

#include "TError.h"

using namespace std;

ClassImp(RooPolyVar)
;


//_____________________________________________________________________________
RooPolyVar::RooPolyVar() : _lowestOrder(0)
{
  // Default constructor
  _coefIter = _coefList.createIterator() ;
}


//_____________________________________________________________________________
RooPolyVar::RooPolyVar(const char* name, const char* title, 
			     RooAbsReal& x, const RooArgList& coefList, Int_t lowestOrder) :
  RooAbsReal(name, title),
  _x("x", "Dependent", this, x),
  _coefList("coefList","List of coefficients",this),
  _lowestOrder(lowestOrder) 
{
  // Construct polynomial in x with coefficients in coefList. If
  // lowestOrder is not zero, then the first element in coefList is
  // interpreted as as the 'lowestOrder' coefficients and all
  // subsequent coeffient elements are shifted by a similar amount.


  _coefIter = _coefList.createIterator() ;

  // Check lowest order
  if (_lowestOrder<0) {
    coutE(InputArguments) << "RooPolyVar::ctor(" << GetName() 
			  << ") WARNING: lowestOrder must be >=0, setting value to 0" << endl ;
    _lowestOrder=0 ;
  }

  TIterator* coefIter = coefList.createIterator() ;
  RooAbsArg* coef ;
  while((coef = (RooAbsArg*)coefIter->Next())) {
    if (!dynamic_cast<RooAbsReal*>(coef)) {
      coutE(InputArguments) << "RooPolyVar::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName() 
			    << " is not of type RooAbsReal" << endl ;
      assert(0) ;
    }
    _coefList.add(*coef) ;
  }
  delete coefIter ;
}



//_____________________________________________________________________________
RooPolyVar::RooPolyVar(const char* name, const char* title,
                           RooAbsReal& x) :
  RooAbsReal(name, title),
  _x("x", "Dependent", this, x),
  _coefList("coefList","List of coefficients",this),
  _lowestOrder(1)
{
  // Constructor of flat polynomial function

  _coefIter = _coefList.createIterator() ;
}                                                                                                                                 



//_____________________________________________________________________________
RooPolyVar::RooPolyVar(const RooPolyVar& other, const char* name) :
  RooAbsReal(other, name), 
  _x("x", this, other._x), 
  _coefList("coefList",this,other._coefList),
  _lowestOrder(other._lowestOrder) 
{
  // Copy constructor
  _coefIter = _coefList.createIterator() ;
}




//_____________________________________________________________________________
RooPolyVar::~RooPolyVar() 
{
  // Destructor
  delete _coefIter ;
}




//_____________________________________________________________________________
Double_t RooPolyVar::evaluate() const 
{
  // Calculate and return value of polynomial

  Double_t sum(0) ;
  Int_t order(_lowestOrder) ;
  _coefIter->Reset() ;

  RooAbsReal* coef ;
  const RooArgSet* nset = _coefList.nset() ;
  while((coef=(RooAbsReal*)_coefIter->Next())) {
    sum += coef->getVal(nset)*TMath::Power(_x,order++) ;
  }

  return sum;
}



//_____________________________________________________________________________
Int_t RooPolyVar::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const 
{
  // Advertise that we can internally integrate over x

  if (matchArgs(allVars, analVars, _x)) return 1;
  return 0;
}



//_____________________________________________________________________________
Double_t RooPolyVar::analyticalIntegral(Int_t code, const char* rangeName) const 
{
  // Calculate and return analytical integral over x

  R__ASSERT(code==1) ;

  Double_t sum(0) ;

  const RooArgSet* nset = _coefList.nset() ;
  Int_t order(_lowestOrder) ;
  _coefIter->Reset() ;
  RooAbsReal* coef ;

  // Primitive = sum(k) coef_k * 1/(k+1) x^(k+1)
  while((coef=(RooAbsReal*)_coefIter->Next())) {
    sum += coef->getVal(nset)*(TMath::Power(_x.max(rangeName),order+1)-TMath::Power(_x.min(rangeName),order+1))/(order+1) ; 
    order++ ;
  }

  return sum;  
  
}
 RooPolyVar.cxx:1
 RooPolyVar.cxx:2
 RooPolyVar.cxx:3
 RooPolyVar.cxx:4
 RooPolyVar.cxx:5
 RooPolyVar.cxx:6
 RooPolyVar.cxx:7
 RooPolyVar.cxx:8
 RooPolyVar.cxx:9
 RooPolyVar.cxx:10
 RooPolyVar.cxx:11
 RooPolyVar.cxx:12
 RooPolyVar.cxx:13
 RooPolyVar.cxx:14
 RooPolyVar.cxx:15
 RooPolyVar.cxx:16
 RooPolyVar.cxx:17
 RooPolyVar.cxx:18
 RooPolyVar.cxx:19
 RooPolyVar.cxx:20
 RooPolyVar.cxx:21
 RooPolyVar.cxx:22
 RooPolyVar.cxx:23
 RooPolyVar.cxx:24
 RooPolyVar.cxx:25
 RooPolyVar.cxx:26
 RooPolyVar.cxx:27
 RooPolyVar.cxx:28
 RooPolyVar.cxx:29
 RooPolyVar.cxx:30
 RooPolyVar.cxx:31
 RooPolyVar.cxx:32
 RooPolyVar.cxx:33
 RooPolyVar.cxx:34
 RooPolyVar.cxx:35
 RooPolyVar.cxx:36
 RooPolyVar.cxx:37
 RooPolyVar.cxx:38
 RooPolyVar.cxx:39
 RooPolyVar.cxx:40
 RooPolyVar.cxx:41
 RooPolyVar.cxx:42
 RooPolyVar.cxx:43
 RooPolyVar.cxx:44
 RooPolyVar.cxx:45
 RooPolyVar.cxx:46
 RooPolyVar.cxx:47
 RooPolyVar.cxx:48
 RooPolyVar.cxx:49
 RooPolyVar.cxx:50
 RooPolyVar.cxx:51
 RooPolyVar.cxx:52
 RooPolyVar.cxx:53
 RooPolyVar.cxx:54
 RooPolyVar.cxx:55
 RooPolyVar.cxx:56
 RooPolyVar.cxx:57
 RooPolyVar.cxx:58
 RooPolyVar.cxx:59
 RooPolyVar.cxx:60
 RooPolyVar.cxx:61
 RooPolyVar.cxx:62
 RooPolyVar.cxx:63
 RooPolyVar.cxx:64
 RooPolyVar.cxx:65
 RooPolyVar.cxx:66
 RooPolyVar.cxx:67
 RooPolyVar.cxx:68
 RooPolyVar.cxx:69
 RooPolyVar.cxx:70
 RooPolyVar.cxx:71
 RooPolyVar.cxx:72
 RooPolyVar.cxx:73
 RooPolyVar.cxx:74
 RooPolyVar.cxx:75
 RooPolyVar.cxx:76
 RooPolyVar.cxx:77
 RooPolyVar.cxx:78
 RooPolyVar.cxx:79
 RooPolyVar.cxx:80
 RooPolyVar.cxx:81
 RooPolyVar.cxx:82
 RooPolyVar.cxx:83
 RooPolyVar.cxx:84
 RooPolyVar.cxx:85
 RooPolyVar.cxx:86
 RooPolyVar.cxx:87
 RooPolyVar.cxx:88
 RooPolyVar.cxx:89
 RooPolyVar.cxx:90
 RooPolyVar.cxx:91
 RooPolyVar.cxx:92
 RooPolyVar.cxx:93
 RooPolyVar.cxx:94
 RooPolyVar.cxx:95
 RooPolyVar.cxx:96
 RooPolyVar.cxx:97
 RooPolyVar.cxx:98
 RooPolyVar.cxx:99
 RooPolyVar.cxx:100
 RooPolyVar.cxx:101
 RooPolyVar.cxx:102
 RooPolyVar.cxx:103
 RooPolyVar.cxx:104
 RooPolyVar.cxx:105
 RooPolyVar.cxx:106
 RooPolyVar.cxx:107
 RooPolyVar.cxx:108
 RooPolyVar.cxx:109
 RooPolyVar.cxx:110
 RooPolyVar.cxx:111
 RooPolyVar.cxx:112
 RooPolyVar.cxx:113
 RooPolyVar.cxx:114
 RooPolyVar.cxx:115
 RooPolyVar.cxx:116
 RooPolyVar.cxx:117
 RooPolyVar.cxx:118
 RooPolyVar.cxx:119
 RooPolyVar.cxx:120
 RooPolyVar.cxx:121
 RooPolyVar.cxx:122
 RooPolyVar.cxx:123
 RooPolyVar.cxx:124
 RooPolyVar.cxx:125
 RooPolyVar.cxx:126
 RooPolyVar.cxx:127
 RooPolyVar.cxx:128
 RooPolyVar.cxx:129
 RooPolyVar.cxx:130
 RooPolyVar.cxx:131
 RooPolyVar.cxx:132
 RooPolyVar.cxx:133
 RooPolyVar.cxx:134
 RooPolyVar.cxx:135
 RooPolyVar.cxx:136
 RooPolyVar.cxx:137
 RooPolyVar.cxx:138
 RooPolyVar.cxx:139
 RooPolyVar.cxx:140
 RooPolyVar.cxx:141
 RooPolyVar.cxx:142
 RooPolyVar.cxx:143
 RooPolyVar.cxx:144
 RooPolyVar.cxx:145
 RooPolyVar.cxx:146
 RooPolyVar.cxx:147
 RooPolyVar.cxx:148
 RooPolyVar.cxx:149
 RooPolyVar.cxx:150
 RooPolyVar.cxx:151
 RooPolyVar.cxx:152
 RooPolyVar.cxx:153
 RooPolyVar.cxx:154
 RooPolyVar.cxx:155
 RooPolyVar.cxx:156
 RooPolyVar.cxx:157
 RooPolyVar.cxx:158
 RooPolyVar.cxx:159
 RooPolyVar.cxx:160
 RooPolyVar.cxx:161
 RooPolyVar.cxx:162
 RooPolyVar.cxx:163
 RooPolyVar.cxx:164
 RooPolyVar.cxx:165
 RooPolyVar.cxx:166
 RooPolyVar.cxx:167
 RooPolyVar.cxx:168
 RooPolyVar.cxx:169
 RooPolyVar.cxx:170
 RooPolyVar.cxx:171
 RooPolyVar.cxx:172
 RooPolyVar.cxx:173
 RooPolyVar.cxx:174
 RooPolyVar.cxx:175
 RooPolyVar.cxx:176
 RooPolyVar.cxx:177
 RooPolyVar.cxx:178
 RooPolyVar.cxx:179
 RooPolyVar.cxx:180
 RooPolyVar.cxx:181
 RooPolyVar.cxx:182
 RooPolyVar.cxx:183
 RooPolyVar.cxx:184
 RooPolyVar.cxx:185
 RooPolyVar.cxx:186
 RooPolyVar.cxx:187
 RooPolyVar.cxx:188
 RooPolyVar.cxx:189
 RooPolyVar.cxx:190