/*****************************************************************************
 * 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
// RooMoment 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 "RooMoment.h"
#include "RooAbsReal.h"
#include "RooAbsPdf.h"
#include "RooErrorHandler.h"
#include "RooArgSet.h"
#include "RooMsgService.h"
#include "RooRealVar.h"
#include "RooFunctor.h"
#include "RooFormulaVar.h"
#include "RooGlobalFunc.h"
#include "RooConstVar.h"
#include "RooRealIntegral.h"
#include "RooNumIntConfig.h"
#include <string>
using namespace std ;


ClassImp(RooMoment)
;


//_____________________________________________________________________________
RooMoment::RooMoment() 
{
  // Default constructor
}



//_____________________________________________________________________________
RooMoment::RooMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, Int_t orderIn, Bool_t centr, Bool_t takeRoot) :
  RooAbsMoment(name, title,func,x,orderIn,takeRoot),
  _xf("!xf","xf",this,kFALSE,kFALSE),
  _ixf("!ixf","ixf",this),
  _if("!if","if",this)
{
  setExpensiveObjectCache(func.expensiveObjectCache()) ;
  
  string pname=Form("%s_product",name) ;

  RooFormulaVar* XF ;
  if (centr) {
    string formula=Form("pow((@0-@1),%d)*@2",_order) ;
    string m1name=Form("%s_moment1",GetName()) ;
    RooAbsReal* mom1 = func.mean(x) ;
    XF = new RooFormulaVar(pname.c_str(),formula.c_str(),RooArgList(x,*mom1,func)) ;
    XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
    addOwnedComponents(*mom1) ;
    _mean.setArg(*mom1) ;
  } else {
    string formula=Form("pow(@0,%d)*@1",_order) ;
    XF = new RooFormulaVar(pname.c_str(),formula.c_str(),RooArgSet(x,func)) ;
    XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
  }

  if (func.isBinnedDistribution(x)) {
    XF->specialIntegratorConfig(kTRUE)->method1D().setLabel("RooBinIntegrator");
  }

  RooRealIntegral* intXF = (RooRealIntegral*) XF->createIntegral(x) ;
  RooRealIntegral* intF =  (RooRealIntegral*) func.createIntegral(x) ;
  intXF->setCacheNumeric(kTRUE) ;
  intF->setCacheNumeric(kTRUE) ;

  _xf.setArg(*XF) ;
  _ixf.setArg(*intXF) ;
  _if.setArg(*intF) ;
  addOwnedComponents(RooArgSet(*XF,*intXF,*intF)) ;
}

//_____________________________________________________________________________
RooMoment::RooMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset, 
		     Int_t orderIn, Bool_t centr, Bool_t takeRoot, Bool_t intNSet) :
  RooAbsMoment(name, title,func,x,orderIn,takeRoot),
  _xf("!xf","xf",this,kFALSE,kFALSE),
  _ixf("!ixf","ixf",this),
  _if("!if","if",this)
{

  setExpensiveObjectCache(func.expensiveObjectCache()) ;

  _nset.add(nset) ;

  string pname=Form("%s_product",name) ;
  RooFormulaVar* XF ;
  if (centr) {
    string formula=Form("pow((@0-@1),%d)*@2",_order) ;
    string m1name=Form("%s_moment1",GetName()) ;
    RooAbsReal* mom1 = func.mean(x,nset) ;
    XF = new RooFormulaVar(pname.c_str(),formula.c_str(),RooArgList(x,*mom1,func)) ;
    XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
    addOwnedComponents(*mom1) ;
    _mean.setArg(*mom1) ;
  } else {
    string formula=Form("pow(@0,%d)*@1",_order) ;
    XF = new RooFormulaVar(pname.c_str(),formula.c_str(),RooArgSet(x,func)) ;
    XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
  }

  if (func.isBinnedDistribution(x)) {
    XF->specialIntegratorConfig(kTRUE)->method1D().setLabel("RooBinIntegrator");
  }

  RooArgSet intSet(x) ;
  if (intNSet) intSet.add(_nset,kTRUE) ;

  RooRealIntegral* intXF = (RooRealIntegral*) XF->createIntegral(intSet,&_nset) ;
  RooRealIntegral* intF =  (RooRealIntegral*) func.createIntegral(intSet,&_nset) ;
  intXF->setCacheNumeric(kTRUE) ;
  intF->setCacheNumeric(kTRUE) ;

  _xf.setArg(*XF) ;
  _ixf.setArg(*intXF) ;
  _if.setArg(*intF) ;
  addOwnedComponents(RooArgSet(*XF,*intXF,*intF)) ;
}



//_____________________________________________________________________________
RooMoment::RooMoment(const RooMoment& other, const char* name) :
  RooAbsMoment(other, name), 
  _xf("xf",this,other._xf),
  _ixf("ixf",this,other._ixf),
  _if("if",this,other._if)
{
}



//_____________________________________________________________________________
RooMoment::~RooMoment() 
{
  // Destructor
}



//_____________________________________________________________________________
Double_t RooMoment::evaluate() const 
{
  // Calculate value  
  Double_t ratio = _ixf / _if ;
  Double_t ret =  _takeRoot ? pow(ratio,1.0/_order) : ratio ;
  return ret ;
}


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