/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitModels                                                     *
 * @(#)root/roofit:$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 RooGaussModel implements a RooResolutionModel that models a Gaussian
// distribution. Object of class RooGaussModel can be used
// for analytical convolutions with classes inheriting from RooAbsAnaConvPdf
// END_HTML
//

#include "RooFit.h"

#include "TMath.h"
#include "Riostream.h"
#include "Riostream.h"
#include "RooGaussModel.h"
#include "RooRealConstant.h"
#include "RooRandom.h"

#include "TError.h"

using namespace std;

ClassImp(RooGaussModel) 
;



//_____________________________________________________________________________
RooGaussModel::RooGaussModel(const char *name, const char *title, RooRealVar& xIn, 
			     RooAbsReal& _mean, RooAbsReal& _sigma) :
  RooResolutionModel(name,title,xIn), 
  _flatSFInt(kFALSE),
  _asympInt(kFALSE),
  mean("mean","Mean",this,_mean),
  sigma("sigma","Width",this,_sigma),
  msf("msf","Mean Scale Factor",this,(RooRealVar&)RooRealConstant::value(1)),
  ssf("ssf","Sigma Scale Factor",this,(RooRealVar&)RooRealConstant::value(1))
{  
}



//_____________________________________________________________________________
RooGaussModel::RooGaussModel(const char *name, const char *title, RooRealVar& xIn, 
			     RooAbsReal& _mean, RooAbsReal& _sigma, 
			     RooAbsReal& _msSF) : 
  RooResolutionModel(name,title,xIn), 
  _flatSFInt(kFALSE),
  _asympInt(kFALSE),
  mean("mean","Mean",this,_mean),
  sigma("sigma","Width",this,_sigma),
  msf("msf","Mean Scale Factor",this,_msSF),
  ssf("ssf","Sigma Scale Factor",this,_msSF)
{  
}



//_____________________________________________________________________________
RooGaussModel::RooGaussModel(const char *name, const char *title, RooRealVar& xIn, 
			     RooAbsReal& _mean, RooAbsReal& _sigma, 
			     RooAbsReal& _meanSF, RooAbsReal& _sigmaSF) : 
  RooResolutionModel(name,title,xIn), 
  _flatSFInt(kFALSE),
  _asympInt(kFALSE),
  mean("mean","Mean",this,_mean),
  sigma("sigma","Width",this,_sigma),
  msf("msf","Mean Scale Factor",this,_meanSF),
  ssf("ssf","Sigma Scale Factor",this,_sigmaSF)
{  
}



//_____________________________________________________________________________
RooGaussModel::RooGaussModel(const RooGaussModel& other, const char* name) : 
  RooResolutionModel(other,name),
  _flatSFInt(other._flatSFInt),
  _asympInt(other._asympInt),
  mean("mean",this,other.mean),
  sigma("sigma",this,other.sigma),
  msf("msf",this,other.msf),
  ssf("ssf",this,other.ssf)
{
}



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



//_____________________________________________________________________________
Int_t RooGaussModel::basisCode(const char* name) const 
{
  if (!TString("exp(-@0/@1)").CompareTo(name)) return expBasisPlus ;
  if (!TString("exp(@0/@1)").CompareTo(name)) return expBasisMinus ;
  if (!TString("exp(-abs(@0)/@1)").CompareTo(name)) return expBasisSum ;
  if (!TString("exp(-@0/@1)*sin(@0*@2)").CompareTo(name)) return sinBasisPlus ;
  if (!TString("exp(@0/@1)*sin(@0*@2)").CompareTo(name)) return sinBasisMinus ;
  if (!TString("exp(-abs(@0)/@1)*sin(@0*@2)").CompareTo(name)) return sinBasisSum ;
  if (!TString("exp(-@0/@1)*cos(@0*@2)").CompareTo(name)) return cosBasisPlus ;
  if (!TString("exp(@0/@1)*cos(@0*@2)").CompareTo(name)) return cosBasisMinus ;
  if (!TString("exp(-abs(@0)/@1)*cos(@0*@2)").CompareTo(name)) return cosBasisSum ;
  if (!TString("(@0/@1)*exp(-@0/@1)").CompareTo(name)) return linBasisPlus ;
  if (!TString("(@0/@1)*(@0/@1)*exp(-@0/@1)").CompareTo(name)) return quadBasisPlus ;
  if (!TString("exp(-@0/@1)*cosh(@0*@2/2)").CompareTo(name)) return coshBasisPlus;
  if (!TString("exp(@0/@1)*cosh(@0*@2/2)").CompareTo(name)) return coshBasisMinus;
  if (!TString("exp(-abs(@0)/@1)*cosh(@0*@2/2)").CompareTo(name)) return coshBasisSum;
  if (!TString("exp(-@0/@1)*sinh(@0*@2/2)").CompareTo(name)) return sinhBasisPlus;
  if (!TString("exp(@0/@1)*sinh(@0*@2/2)").CompareTo(name)) return sinhBasisMinus;
  if (!TString("exp(-abs(@0)/@1)*sinh(@0*@2/2)").CompareTo(name)) return sinhBasisSum;
  return 0 ;
} 



//_____________________________________________________________________________
Double_t RooGaussModel::evaluate() const 
{  
  //cout << "RooGaussModel::evaluate(" << GetName() << ") basisCode = " << _basisCode << endl ;
  
  // *** 1st form: Straight Gaussian, used for unconvoluted PDF or expBasis with 0 lifetime ***
  static Double_t root2(std::sqrt(2.)) ;
  static Double_t root2pi(std::sqrt(2.*std::atan2(0.,-1.))) ;
  static Double_t rootpi(std::sqrt(std::atan2(0.,-1.))) ;

  BasisType basisType = (BasisType)( (_basisCode == 0) ? 0 : (_basisCode/10) + 1 );
  BasisSign basisSign = (BasisSign)( _basisCode - 10*(basisType-1) - 2 ) ;

  Double_t tau = (_basisCode!=noBasis)?((RooAbsReal*)basis().getParameter(1))->getVal():0 ;
  if (basisType == coshBasis && _basisCode!=noBasis ) {
     Double_t dGamma = ((RooAbsReal*)basis().getParameter(2))->getVal();
     if (dGamma==0) basisType = expBasis;
  }

  if (basisType==none || ((basisType==expBasis || basisType==cosBasis) && tau==0.)) {
    Double_t xprime = (x-(mean*msf))/(sigma*ssf) ;
    if (verboseEval()>2) cout << "RooGaussModel::evaluate(" << GetName() << ") 1st form" << endl ;
    
    Double_t result = std::exp(-0.5*xprime*xprime)/(sigma*ssf*root2pi) ;
    if (_basisCode!=0 && basisSign==Both) result *= 2 ;
    return result ;
  }

  // *** 2nd form: 0, used for sinBasis, linBasis, and quadBasis with tau=0 ***
  if (tau==0) {
    if (verboseEval()>2) cout << "RooGaussModel::evaluate(" << GetName() << ") 2nd form" << endl ;
    return 0. ;
  }

  // *** 3nd form: Convolution with exp(-t/tau), used for expBasis and cosBasis(omega=0) ***
  Double_t omega =  (basisType==sinBasis  || basisType==cosBasis)  ? ((RooAbsReal*)basis().getParameter(2))->getVal() : 0 ;
  Double_t dgamma = (basisType==sinhBasis || basisType==coshBasis) ? ((RooAbsReal*)basis().getParameter(2))->getVal() : 0 ;
  Double_t _x = omega *tau ;
  Double_t _y = tau*dgamma/2;
  Double_t xprime = (x-(mean*msf))/tau ;
  Double_t c = (sigma*ssf)/(root2*tau) ; 
  Double_t u = xprime/(2*c) ;

  if (basisType==expBasis || (basisType==cosBasis && _x==0.)) {
    if (verboseEval()>2) cout << "RooGaussModel::evaluate(" << GetName() << ") 3d form tau=" << tau << endl ;
    Double_t result(0) ;
    if (basisSign!=Minus) result += evalCerf(0,-u,c).real(); 
    if (basisSign!=Plus)  result += evalCerf(0, u,c).real(); 
    if (TMath::IsNaN(result)) { cxcoutE(Tracing) << "RooGaussModel::getVal(" << GetName() << ") got nan during case 1 " << endl; }
    return result ;
  }

  // *** 4th form: Convolution with exp(-t/tau)*sin(omega*t), used for sinBasis(omega<>0,tau<>0) ***
  if (basisType==sinBasis) {
    if (verboseEval()>2) cout << "RooGaussModel::evaluate(" << GetName() << ") 4th form omega = " << omega << ", tau = " << tau << endl ;
    Double_t result(0) ;
    if (_x==0.) return result ;
    if (basisSign!=Minus) result += -evalCerf(-_x,-u,c).imag(); 
    if (basisSign!=Plus)  result += -evalCerf( _x, u,c).imag(); 
    if (TMath::IsNaN(result)) cxcoutE(Tracing) << "RooGaussModel::getVal(" << GetName() << ") got nan during case 3 " << endl; 
    return result ;
  }

  // *** 5th form: Convolution with exp(-t/tau)*cos(omega*t), used for cosBasis(omega<>0) ***
  if (basisType==cosBasis) {
    if (verboseEval()>2) cout << "RooGaussModel::evaluate(" << GetName() << ") 5th form omega = " << omega << ", tau = " << tau << endl ;
    Double_t result(0) ;
    if (basisSign!=Minus) result += evalCerf(-_x,-u,c).real(); 
    if (basisSign!=Plus)  result += evalCerf( _x, u,c).real(); 
    if (TMath::IsNaN(result)) cxcoutE(Tracing) << "RooGaussModel::getVal(" << GetName() << ") got nan during case 4 " << endl; 
    return result ;  
  }

  // ***8th form: Convolution with exp(-|t|/tau)*cosh(dgamma*t/2), used for         coshBasisSum ***
  if (basisType==coshBasis || basisType ==sinhBasis) {
    if (verboseEval()>2) cout << "RooGaussModel::evaluate(" << GetName() << ") 8th form tau = " << tau << endl ;
    Double_t result(0);   
    int sgn = ( basisType == coshBasis ? +1 : -1 );
    if (basisSign!=Minus) result += 0.5*(    evalCerf(0,-u,c*(1-_y)).real()+sgn*evalCerf(0,-u,c*(1+_y)).real()) ; 
    if (basisSign!=Plus)  result += 0.5*(sgn*evalCerf(0, u,c*(1-_y)).real()+    evalCerf(0, u,c*(1+_y)).real()) ; 
    if (TMath::IsNaN(result)) cxcoutE(Tracing) << "RooGaussModel::getVal(" << GetName() << ") got nan during case 8 " << endl; 
    return result ;
  }

  // *** 6th form: Convolution with (t/tau)*exp(-t/tau), used for linBasis ***
  if (basisType==linBasis) {
    if (verboseEval()>2) cout << "RooGaussModel::evaluate(" << GetName() << ") 6th form tau = " << tau << endl ;
    R__ASSERT(basisSign==Plus);  // This should only be for positive times

    Double_t f0 = std::exp(-xprime+c*c) * RooMath::erfc(-u+c);
    Double_t f1 = std::exp(-u*u);
    return (xprime - 2*c*c)*f0 + (2*c/rootpi)*f1 ; 
  }

  // *** 7th form: Convolution with (t/tau)^2*exp(-t/tau), used for quadBasis ***
  if (basisType==quadBasis) {
    if (verboseEval()>2) cout << "RooGaussModel::evaluate(" << GetName() << ") 7th form tau = " << tau << endl ;
    R__ASSERT(basisSign==Plus);  // This should only be for positive times

    Double_t f0 = std::exp(-xprime+c*c) * RooMath::erfc(-u+c);
    Double_t f1 = std::exp(-u*u);
    Double_t x2c2 = xprime - 2*c*c; 
    return ( x2c2*x2c2*f0 + (2*c/rootpi)*x2c2*f1 + 2*c*c*f0 );
  }

  R__ASSERT(0) ;
  return 0 ;
}



//_____________________________________________________________________________
Int_t RooGaussModel::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const 
{
  switch(_basisCode) {    

  // Analytical integration capability of raw PDF
  case noBasis:
 
    // Optionally advertise flat integral over sigma scale factor
    if (_flatSFInt) {
      if (matchArgs(allVars,analVars,RooArgSet(convVar(),ssf.arg()))) return 2 ;
    }

    if (matchArgs(allVars,analVars,convVar())) return 1 ;
    break ;

  // Analytical integration capability of convoluted PDF
  case expBasisPlus:
  case expBasisMinus:
  case expBasisSum:
  case sinBasisPlus:
  case sinBasisMinus:
  case sinBasisSum:
  case cosBasisPlus:
  case cosBasisMinus:
  case cosBasisSum:
  case linBasisPlus:
  case quadBasisPlus:
  case coshBasisMinus:
  case coshBasisPlus:
  case coshBasisSum:
  case sinhBasisMinus:
  case sinhBasisPlus:
  case sinhBasisSum:

    // Optionally advertise flat integral over sigma scale factor
    if (_flatSFInt) {

      if (matchArgs(allVars,analVars,RooArgSet(convVar(),ssf.arg()))) {
	return 2 ;
      }
    }
    
    if (matchArgs(allVars,analVars,convVar())) return 1 ;
    break ;
  }
  
  return 0 ;
}



//_____________________________________________________________________________
Double_t RooGaussModel::analyticalIntegral(Int_t code, const char* rangeName) const 
{
  static const Double_t root2 = std::sqrt(2.) ;
  //static Double_t rootPiBy2 = std::sqrt(std::atan2(0.0,-1.0)/2.0);
  static const Double_t rootpi = std::sqrt(std::atan2(0.0,-1.0));
  Double_t ssfInt(1.0) ;

  // Code must be 1 or 2
  R__ASSERT(code==1||code==2) ;
  if (code==2) ssfInt = (ssf.max(rangeName)-ssf.min(rangeName)) ;

  BasisType basisType = (BasisType)( (_basisCode == 0) ? 0 : (_basisCode/10) + 1 );
  BasisSign basisSign = (BasisSign)( _basisCode - 10*(basisType-1) - 2 ) ;

  // *** 1st form: Straight Gaussian, used for unconvoluted PDF or expBasis with 0 lifetime ***
  Double_t tau = (_basisCode!=noBasis)?((RooAbsReal*)basis().getParameter(1))->getVal():0 ;
  if (basisType == coshBasis && _basisCode!=noBasis ) {
     Double_t dGamma = ((RooAbsReal*)basis().getParameter(2))->getVal();
     if (dGamma==0) basisType = expBasis;
  }
  if (basisType==none || ((basisType==expBasis || basisType==cosBasis) && tau==0.)) {
    Double_t xscale = root2*(sigma*ssf);
    if (verboseEval()>0) cout << "RooGaussModel::analyticalIntegral(" << GetName() << ") 1st form" << endl ;
    
    Double_t xpmin = (x.min(rangeName)-(mean*msf))/xscale ;
    Double_t xpmax = (x.max(rangeName)-(mean*msf))/xscale ;
 
    Double_t result ;
    if (_asympInt) { // modified FMV, 07/24/03
        result = 1.0 ;
    } else {
	    result = 0.5*(RooMath::erf(xpmax)-RooMath::erf(xpmin)) ;
    }

    if (_basisCode!=0 && basisSign==Both) result *= 2 ;    
    //cout << "Integral 1st form " << " result= " << result*ssfInt << endl;
    if (TMath::IsNaN(result)) { cxcoutE(Tracing) << "RooGaussModel::analyticalIntegral(" << GetName() << ") got nan during case 1 " << endl; }
    return result*ssfInt ;
  }


  Double_t omega = ((basisType==sinBasis)||(basisType==cosBasis)) ?  ((RooAbsReal*)basis().getParameter(2))->getVal() : 0 ;
  Double_t dgamma =((basisType==sinhBasis)||(basisType==coshBasis)) ?  ((RooAbsReal*)basis().getParameter(2))->getVal() : 0 ;

  // *** 2nd form: unity, used for sinBasis and linBasis with tau=0 (PDF is zero) ***
  if (tau==0) {
    if (verboseEval()>0) cout << "RooGaussModel::analyticalIntegral(" << GetName() << ") 2nd form" << endl ;
    return 0. ;
  }

  // *** 3rd form: Convolution with exp(-t/tau), used for expBasis and cosBasis(omega=0) ***
  Double_t c = (sigma*ssf)/(root2*tau) ; 
  Double_t xpmin = (x.min(rangeName)-(mean*msf))/tau ;
  Double_t xpmax = (x.max(rangeName)-(mean*msf))/tau ;
  Double_t umin = xpmin/(2*c) ;
  Double_t umax = xpmax/(2*c) ;

  if (basisType==expBasis || (basisType==cosBasis && omega==0.)) {
    if (verboseEval()>0) cout << "RooGaussModel::analyticalIntegral(" << GetName() << ") 3d form tau=" << tau << endl ;
    Double_t result(0) ;
    if (basisSign!=Minus) result += evalCerfInt(+1,0,tau,-umin,-umax,c).real();   
    if (basisSign!=Plus)  result += evalCerfInt(-1,0,tau, umin, umax,c).real();
    if (TMath::IsNaN(result)) { cxcoutE(Tracing) << "RooGaussModel::analyticalIntegral(" << GetName() << ") got nan during case 3 " << endl; }
    return result*ssfInt ;
  }

  // *** 4th form: Convolution with exp(-t/tau)*sin(omega*t), used for sinBasis(omega<>0,tau<>0) ***
  Double_t _x = omega * tau ;
  Double_t _y = tau*dgamma/2;
    
  if (basisType==sinBasis) {    
    if (verboseEval()>0) cout << "RooGaussModel::analyticalIntegral(" << GetName() << ") 4th form omega = " << omega << ", tau = " << tau << endl ;
    Double_t result(0) ;
    if (_x==0) return result*ssfInt ;
    if (basisSign!=Minus) result += -1*evalCerfInt(+1,-_x,tau,-umin,-umax,c).imag();
    if (basisSign!=Plus)  result += -1*evalCerfInt(-1, _x,tau, umin, umax,c).imag();      
    if (TMath::IsNaN(result)) { cxcoutE(Tracing) << "RooGaussModel::analyticalIntegral(" << GetName() << ") got nan during case 4 " << endl; }
    return result*ssfInt ;
  }

  // *** 5th form: Convolution with exp(-t/tau)*cos(omega*t), used for cosBasis(omega<>0) ***
  if (basisType==cosBasis) {
    if (verboseEval()>0) cout << "RooGaussModel::analyticalIntegral(" << GetName() << ") 5th form omega = " << omega << ", tau = " << tau << endl ;
    Double_t result(0) ;
    if (basisSign!=Minus) result += evalCerfInt(+1,-_x,tau,-umin,-umax,c).real();
    if (basisSign!=Plus)  result += evalCerfInt(-1, _x,tau, umin, umax,c).real();      
    if (TMath::IsNaN(result)) { cxcoutE(Tracing) << "RooGaussModel::analyticalIntegral(" << GetName() << ") got nan during case 5 " << endl; }
    return result*ssfInt ;
  }

  // *** 8th form: Convolution with exp(-|t|/tau)*cosh(dgamma*t/2), used for coshBasis ***
  // *** 9th form: Convolution with exp(-|t|/tau)*sinh(dgamma*t/2), used for sinhBasis ***
  if (basisType==coshBasis || basisType == sinhBasis) {
    if (verboseEval()>0) {cout << "RooGaussModel::analyticalIntegral(" << GetName()                             << ") 8th form tau=" << tau << endl ; }
    Double_t result(0) ;
    int sgn = ( basisType == coshBasis ? +1 : -1 );
    if (basisSign!=Minus) result += 0.5*(    evalCerfInt(+1,0,tau/(1-_y),-umin,-umax,c*(1-_y)).real()+ sgn*evalCerfInt(+1,0,tau/(1+_y),-umin,-umax,c*(1+_y)).real());
    if (basisSign!=Plus)  result += 0.5*(sgn*evalCerfInt(-1,0,tau/(1-_y), umin, umax,c*(1-_y)).real()+     evalCerfInt(-1,0,tau/(1+_y), umin, umax,c*(1+_y)).real());
    if (TMath::IsNaN(result)) { cxcoutE(Tracing) << "RooGaussModel::analyticalIntegral(" << GetName() << ") got nan during case 6 " << endl; }
    return result*ssfInt ;
  }

  // *** 6th form: Convolution with (t/tau)*exp(-t/tau), used for linBasis ***
  if (basisType==linBasis) {
    if (verboseEval()>0) cout << "RooGaussModel::analyticalIntegral(" << GetName() << ") 6th form tau=" << tau << endl ;

    Double_t f0 = RooMath::erf(-umax) - RooMath::erf(-umin);
    Double_t f1 = std::exp(-umax*umax) - std::exp(-umin*umin);

    Double_t tmp1 = std::exp(-xpmax)*RooMath::erfc(-umax + c);
    Double_t tmp2 = std::exp(-xpmin)*RooMath::erfc(-umin + c);

    Double_t f2 = tmp1 - tmp2;
    Double_t f3 = xpmax*tmp1 - xpmin*tmp2;

    Double_t expc2 = std::exp(c*c);

    return -tau*(              f0 +
		  (2*c/rootpi)*f1 +
	     (1 - 2*c*c)*expc2*f2 +
			 expc2*f3
		)*ssfInt;
  }

  // *** 7th form: Convolution with (t/tau)*(t/tau)*exp(-t/tau), used for quadBasis ***
  if (basisType==quadBasis) {
    if (verboseEval()>0) cout << "RooGaussModel::analyticalIntegral(" << GetName() << ") 7th form tau=" << tau << endl ;

    Double_t f0 = RooMath::erf(-umax) - RooMath::erf(-umin);

    Double_t tmpA1 = std::exp(-umax*umax);
    Double_t tmpA2 = std::exp(-umin*umin);

    Double_t f1 = tmpA1 - tmpA2;
    Double_t f2 = umax*tmpA1 - umin*tmpA2;

    Double_t tmpB1 = std::exp(-xpmax)*RooMath::erfc(-umax + c);
    Double_t tmpB2 = std::exp(-xpmin)*RooMath::erfc(-umin + c);

    Double_t f3 = tmpB1 - tmpB2;
    Double_t f4 = xpmax*tmpB1 - xpmin*tmpB2;
    Double_t f5 = xpmax*xpmax*tmpB1 - xpmin*xpmin*tmpB2;

    Double_t expc2 = std::exp(c*c);

    return -tau*( 2*f0 +
		  (4*c/rootpi)*((1-c*c)*f1 + c*f2) +
		  (2*c*c*(2*c*c-1) + 2)*expc2*f3 - (4*c*c-2)*expc2*f4 + expc2*f5
                )*ssfInt;
  }
  R__ASSERT(0) ;
  return 0 ;
}

//_____________________________________________________________________________
std::complex<Double_t> RooGaussModel::evalCerfApprox(Double_t _x, Double_t u, Double_t c)
{
  // use the approximation: erf(z) = exp(-z*z)/(std::sqrt(pi)*z)
  // to explicitly cancel the divergent exp(y*y) behaviour of
  // CWERF for z = x + i y with large negative y

  static const Double_t rootpi= std::sqrt(std::atan2(0.,-1.));
  const std::complex<Double_t> z(_x * c, u + c);  
  const std::complex<Double_t> zc(u + c, - _x * c);
  const std::complex<Double_t> zsq((z.real() + z.imag()) * (z.real() - z.imag()),
	  2. * z.real() * z.imag());
  const std::complex<Double_t> v(-zsq.real() - u*u, -zsq.imag());
  const std::complex<Double_t> ev = std::exp(v);
  const std::complex<Double_t> mez2zcrootpi = -std::exp(zsq)/(zc*rootpi);

  return 2. * (ev * (mez2zcrootpi + 1.));
}

//_____________________________________________________________________________
std::complex<Double_t> RooGaussModel::evalCerfInt(Double_t sign, Double_t _x, Double_t tau, Double_t umin, Double_t umax, Double_t c) const
{
  std::complex<Double_t> diff(2., 0.);
  if (!_asympInt) {
    diff = evalCerf(_x,umin,c);
    diff -= evalCerf(_x,umax,c);
    diff += RooMath::erf(umin) - RooMath::erf(umax);
    diff *= sign;
  }
  diff *= std::complex<Double_t>(1., _x);
  diff *= tau / (1.+_x*_x);
  return diff;
}

//_____________________________________________________________________________
Int_t RooGaussModel::getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, Bool_t /*staticInitOK*/) const
{
  if (matchArgs(directVars,generateVars,x)) return 1 ;  
  return 0 ;
}

//_____________________________________________________________________________
void RooGaussModel::generateEvent(Int_t code)
{
  R__ASSERT(code==1) ;
  Double_t xmin = x.min();
  Double_t xmax = x.max();
  TRandom *generator = RooRandom::randomGenerator();
  while(true) {    
    Double_t xgen = generator->Gaus(mean*msf,sigma*ssf);
    if (xgen<xmax && xgen>xmin) {
      x = xgen ;
      return ;
    }
  }
}
 RooGaussModel.cxx:1
 RooGaussModel.cxx:2
 RooGaussModel.cxx:3
 RooGaussModel.cxx:4
 RooGaussModel.cxx:5
 RooGaussModel.cxx:6
 RooGaussModel.cxx:7
 RooGaussModel.cxx:8
 RooGaussModel.cxx:9
 RooGaussModel.cxx:10
 RooGaussModel.cxx:11
 RooGaussModel.cxx:12
 RooGaussModel.cxx:13
 RooGaussModel.cxx:14
 RooGaussModel.cxx:15
 RooGaussModel.cxx:16
 RooGaussModel.cxx:17
 RooGaussModel.cxx:18
 RooGaussModel.cxx:19
 RooGaussModel.cxx:20
 RooGaussModel.cxx:21
 RooGaussModel.cxx:22
 RooGaussModel.cxx:23
 RooGaussModel.cxx:24
 RooGaussModel.cxx:25
 RooGaussModel.cxx:26
 RooGaussModel.cxx:27
 RooGaussModel.cxx:28
 RooGaussModel.cxx:29
 RooGaussModel.cxx:30
 RooGaussModel.cxx:31
 RooGaussModel.cxx:32
 RooGaussModel.cxx:33
 RooGaussModel.cxx:34
 RooGaussModel.cxx:35
 RooGaussModel.cxx:36
 RooGaussModel.cxx:37
 RooGaussModel.cxx:38
 RooGaussModel.cxx:39
 RooGaussModel.cxx:40
 RooGaussModel.cxx:41
 RooGaussModel.cxx:42
 RooGaussModel.cxx:43
 RooGaussModel.cxx:44
 RooGaussModel.cxx:45
 RooGaussModel.cxx:46
 RooGaussModel.cxx:47
 RooGaussModel.cxx:48
 RooGaussModel.cxx:49
 RooGaussModel.cxx:50
 RooGaussModel.cxx:51
 RooGaussModel.cxx:52
 RooGaussModel.cxx:53
 RooGaussModel.cxx:54
 RooGaussModel.cxx:55
 RooGaussModel.cxx:56
 RooGaussModel.cxx:57
 RooGaussModel.cxx:58
 RooGaussModel.cxx:59
 RooGaussModel.cxx:60
 RooGaussModel.cxx:61
 RooGaussModel.cxx:62
 RooGaussModel.cxx:63
 RooGaussModel.cxx:64
 RooGaussModel.cxx:65
 RooGaussModel.cxx:66
 RooGaussModel.cxx:67
 RooGaussModel.cxx:68
 RooGaussModel.cxx:69
 RooGaussModel.cxx:70
 RooGaussModel.cxx:71
 RooGaussModel.cxx:72
 RooGaussModel.cxx:73
 RooGaussModel.cxx:74
 RooGaussModel.cxx:75
 RooGaussModel.cxx:76
 RooGaussModel.cxx:77
 RooGaussModel.cxx:78
 RooGaussModel.cxx:79
 RooGaussModel.cxx:80
 RooGaussModel.cxx:81
 RooGaussModel.cxx:82
 RooGaussModel.cxx:83
 RooGaussModel.cxx:84
 RooGaussModel.cxx:85
 RooGaussModel.cxx:86
 RooGaussModel.cxx:87
 RooGaussModel.cxx:88
 RooGaussModel.cxx:89
 RooGaussModel.cxx:90
 RooGaussModel.cxx:91
 RooGaussModel.cxx:92
 RooGaussModel.cxx:93
 RooGaussModel.cxx:94
 RooGaussModel.cxx:95
 RooGaussModel.cxx:96
 RooGaussModel.cxx:97
 RooGaussModel.cxx:98
 RooGaussModel.cxx:99
 RooGaussModel.cxx:100
 RooGaussModel.cxx:101
 RooGaussModel.cxx:102
 RooGaussModel.cxx:103
 RooGaussModel.cxx:104
 RooGaussModel.cxx:105
 RooGaussModel.cxx:106
 RooGaussModel.cxx:107
 RooGaussModel.cxx:108
 RooGaussModel.cxx:109
 RooGaussModel.cxx:110
 RooGaussModel.cxx:111
 RooGaussModel.cxx:112
 RooGaussModel.cxx:113
 RooGaussModel.cxx:114
 RooGaussModel.cxx:115
 RooGaussModel.cxx:116
 RooGaussModel.cxx:117
 RooGaussModel.cxx:118
 RooGaussModel.cxx:119
 RooGaussModel.cxx:120
 RooGaussModel.cxx:121
 RooGaussModel.cxx:122
 RooGaussModel.cxx:123
 RooGaussModel.cxx:124
 RooGaussModel.cxx:125
 RooGaussModel.cxx:126
 RooGaussModel.cxx:127
 RooGaussModel.cxx:128
 RooGaussModel.cxx:129
 RooGaussModel.cxx:130
 RooGaussModel.cxx:131
 RooGaussModel.cxx:132
 RooGaussModel.cxx:133
 RooGaussModel.cxx:134
 RooGaussModel.cxx:135
 RooGaussModel.cxx:136
 RooGaussModel.cxx:137
 RooGaussModel.cxx:138
 RooGaussModel.cxx:139
 RooGaussModel.cxx:140
 RooGaussModel.cxx:141
 RooGaussModel.cxx:142
 RooGaussModel.cxx:143
 RooGaussModel.cxx:144
 RooGaussModel.cxx:145
 RooGaussModel.cxx:146
 RooGaussModel.cxx:147
 RooGaussModel.cxx:148
 RooGaussModel.cxx:149
 RooGaussModel.cxx:150
 RooGaussModel.cxx:151
 RooGaussModel.cxx:152
 RooGaussModel.cxx:153
 RooGaussModel.cxx:154
 RooGaussModel.cxx:155
 RooGaussModel.cxx:156
 RooGaussModel.cxx:157
 RooGaussModel.cxx:158
 RooGaussModel.cxx:159
 RooGaussModel.cxx:160
 RooGaussModel.cxx:161
 RooGaussModel.cxx:162
 RooGaussModel.cxx:163
 RooGaussModel.cxx:164
 RooGaussModel.cxx:165
 RooGaussModel.cxx:166
 RooGaussModel.cxx:167
 RooGaussModel.cxx:168
 RooGaussModel.cxx:169
 RooGaussModel.cxx:170
 RooGaussModel.cxx:171
 RooGaussModel.cxx:172
 RooGaussModel.cxx:173
 RooGaussModel.cxx:174
 RooGaussModel.cxx:175
 RooGaussModel.cxx:176
 RooGaussModel.cxx:177
 RooGaussModel.cxx:178
 RooGaussModel.cxx:179
 RooGaussModel.cxx:180
 RooGaussModel.cxx:181
 RooGaussModel.cxx:182
 RooGaussModel.cxx:183
 RooGaussModel.cxx:184
 RooGaussModel.cxx:185
 RooGaussModel.cxx:186
 RooGaussModel.cxx:187
 RooGaussModel.cxx:188
 RooGaussModel.cxx:189
 RooGaussModel.cxx:190
 RooGaussModel.cxx:191
 RooGaussModel.cxx:192
 RooGaussModel.cxx:193
 RooGaussModel.cxx:194
 RooGaussModel.cxx:195
 RooGaussModel.cxx:196
 RooGaussModel.cxx:197
 RooGaussModel.cxx:198
 RooGaussModel.cxx:199
 RooGaussModel.cxx:200
 RooGaussModel.cxx:201
 RooGaussModel.cxx:202
 RooGaussModel.cxx:203
 RooGaussModel.cxx:204
 RooGaussModel.cxx:205
 RooGaussModel.cxx:206
 RooGaussModel.cxx:207
 RooGaussModel.cxx:208
 RooGaussModel.cxx:209
 RooGaussModel.cxx:210
 RooGaussModel.cxx:211
 RooGaussModel.cxx:212
 RooGaussModel.cxx:213
 RooGaussModel.cxx:214
 RooGaussModel.cxx:215
 RooGaussModel.cxx:216
 RooGaussModel.cxx:217
 RooGaussModel.cxx:218
 RooGaussModel.cxx:219
 RooGaussModel.cxx:220
 RooGaussModel.cxx:221
 RooGaussModel.cxx:222
 RooGaussModel.cxx:223
 RooGaussModel.cxx:224
 RooGaussModel.cxx:225
 RooGaussModel.cxx:226
 RooGaussModel.cxx:227
 RooGaussModel.cxx:228
 RooGaussModel.cxx:229
 RooGaussModel.cxx:230
 RooGaussModel.cxx:231
 RooGaussModel.cxx:232
 RooGaussModel.cxx:233
 RooGaussModel.cxx:234
 RooGaussModel.cxx:235
 RooGaussModel.cxx:236
 RooGaussModel.cxx:237
 RooGaussModel.cxx:238
 RooGaussModel.cxx:239
 RooGaussModel.cxx:240
 RooGaussModel.cxx:241
 RooGaussModel.cxx:242
 RooGaussModel.cxx:243
 RooGaussModel.cxx:244
 RooGaussModel.cxx:245
 RooGaussModel.cxx:246
 RooGaussModel.cxx:247
 RooGaussModel.cxx:248
 RooGaussModel.cxx:249
 RooGaussModel.cxx:250
 RooGaussModel.cxx:251
 RooGaussModel.cxx:252
 RooGaussModel.cxx:253
 RooGaussModel.cxx:254
 RooGaussModel.cxx:255
 RooGaussModel.cxx:256
 RooGaussModel.cxx:257
 RooGaussModel.cxx:258
 RooGaussModel.cxx:259
 RooGaussModel.cxx:260
 RooGaussModel.cxx:261
 RooGaussModel.cxx:262
 RooGaussModel.cxx:263
 RooGaussModel.cxx:264
 RooGaussModel.cxx:265
 RooGaussModel.cxx:266
 RooGaussModel.cxx:267
 RooGaussModel.cxx:268
 RooGaussModel.cxx:269
 RooGaussModel.cxx:270
 RooGaussModel.cxx:271
 RooGaussModel.cxx:272
 RooGaussModel.cxx:273
 RooGaussModel.cxx:274
 RooGaussModel.cxx:275
 RooGaussModel.cxx:276
 RooGaussModel.cxx:277
 RooGaussModel.cxx:278
 RooGaussModel.cxx:279
 RooGaussModel.cxx:280
 RooGaussModel.cxx:281
 RooGaussModel.cxx:282
 RooGaussModel.cxx:283
 RooGaussModel.cxx:284
 RooGaussModel.cxx:285
 RooGaussModel.cxx:286
 RooGaussModel.cxx:287
 RooGaussModel.cxx:288
 RooGaussModel.cxx:289
 RooGaussModel.cxx:290
 RooGaussModel.cxx:291
 RooGaussModel.cxx:292
 RooGaussModel.cxx:293
 RooGaussModel.cxx:294
 RooGaussModel.cxx:295
 RooGaussModel.cxx:296
 RooGaussModel.cxx:297
 RooGaussModel.cxx:298
 RooGaussModel.cxx:299
 RooGaussModel.cxx:300
 RooGaussModel.cxx:301
 RooGaussModel.cxx:302
 RooGaussModel.cxx:303
 RooGaussModel.cxx:304
 RooGaussModel.cxx:305
 RooGaussModel.cxx:306
 RooGaussModel.cxx:307
 RooGaussModel.cxx:308
 RooGaussModel.cxx:309
 RooGaussModel.cxx:310
 RooGaussModel.cxx:311
 RooGaussModel.cxx:312
 RooGaussModel.cxx:313
 RooGaussModel.cxx:314
 RooGaussModel.cxx:315
 RooGaussModel.cxx:316
 RooGaussModel.cxx:317
 RooGaussModel.cxx:318
 RooGaussModel.cxx:319
 RooGaussModel.cxx:320
 RooGaussModel.cxx:321
 RooGaussModel.cxx:322
 RooGaussModel.cxx:323
 RooGaussModel.cxx:324
 RooGaussModel.cxx:325
 RooGaussModel.cxx:326
 RooGaussModel.cxx:327
 RooGaussModel.cxx:328
 RooGaussModel.cxx:329
 RooGaussModel.cxx:330
 RooGaussModel.cxx:331
 RooGaussModel.cxx:332
 RooGaussModel.cxx:333
 RooGaussModel.cxx:334
 RooGaussModel.cxx:335
 RooGaussModel.cxx:336
 RooGaussModel.cxx:337
 RooGaussModel.cxx:338
 RooGaussModel.cxx:339
 RooGaussModel.cxx:340
 RooGaussModel.cxx:341
 RooGaussModel.cxx:342
 RooGaussModel.cxx:343
 RooGaussModel.cxx:344
 RooGaussModel.cxx:345
 RooGaussModel.cxx:346
 RooGaussModel.cxx:347
 RooGaussModel.cxx:348
 RooGaussModel.cxx:349
 RooGaussModel.cxx:350
 RooGaussModel.cxx:351
 RooGaussModel.cxx:352
 RooGaussModel.cxx:353
 RooGaussModel.cxx:354
 RooGaussModel.cxx:355
 RooGaussModel.cxx:356
 RooGaussModel.cxx:357
 RooGaussModel.cxx:358
 RooGaussModel.cxx:359
 RooGaussModel.cxx:360
 RooGaussModel.cxx:361
 RooGaussModel.cxx:362
 RooGaussModel.cxx:363
 RooGaussModel.cxx:364
 RooGaussModel.cxx:365
 RooGaussModel.cxx:366
 RooGaussModel.cxx:367
 RooGaussModel.cxx:368
 RooGaussModel.cxx:369
 RooGaussModel.cxx:370
 RooGaussModel.cxx:371
 RooGaussModel.cxx:372
 RooGaussModel.cxx:373
 RooGaussModel.cxx:374
 RooGaussModel.cxx:375
 RooGaussModel.cxx:376
 RooGaussModel.cxx:377
 RooGaussModel.cxx:378
 RooGaussModel.cxx:379
 RooGaussModel.cxx:380
 RooGaussModel.cxx:381
 RooGaussModel.cxx:382
 RooGaussModel.cxx:383
 RooGaussModel.cxx:384
 RooGaussModel.cxx:385
 RooGaussModel.cxx:386
 RooGaussModel.cxx:387
 RooGaussModel.cxx:388
 RooGaussModel.cxx:389
 RooGaussModel.cxx:390
 RooGaussModel.cxx:391
 RooGaussModel.cxx:392
 RooGaussModel.cxx:393
 RooGaussModel.cxx:394
 RooGaussModel.cxx:395
 RooGaussModel.cxx:396
 RooGaussModel.cxx:397
 RooGaussModel.cxx:398
 RooGaussModel.cxx:399
 RooGaussModel.cxx:400
 RooGaussModel.cxx:401
 RooGaussModel.cxx:402
 RooGaussModel.cxx:403
 RooGaussModel.cxx:404
 RooGaussModel.cxx:405
 RooGaussModel.cxx:406
 RooGaussModel.cxx:407
 RooGaussModel.cxx:408
 RooGaussModel.cxx:409
 RooGaussModel.cxx:410
 RooGaussModel.cxx:411
 RooGaussModel.cxx:412
 RooGaussModel.cxx:413
 RooGaussModel.cxx:414
 RooGaussModel.cxx:415
 RooGaussModel.cxx:416
 RooGaussModel.cxx:417
 RooGaussModel.cxx:418
 RooGaussModel.cxx:419
 RooGaussModel.cxx:420
 RooGaussModel.cxx:421
 RooGaussModel.cxx:422
 RooGaussModel.cxx:423
 RooGaussModel.cxx:424
 RooGaussModel.cxx:425
 RooGaussModel.cxx:426
 RooGaussModel.cxx:427
 RooGaussModel.cxx:428
 RooGaussModel.cxx:429
 RooGaussModel.cxx:430
 RooGaussModel.cxx:431
 RooGaussModel.cxx:432
 RooGaussModel.cxx:433
 RooGaussModel.cxx:434
 RooGaussModel.cxx:435
 RooGaussModel.cxx:436
 RooGaussModel.cxx:437
 RooGaussModel.cxx:438
 RooGaussModel.cxx:439
 RooGaussModel.cxx:440
 RooGaussModel.cxx:441
 RooGaussModel.cxx:442
 RooGaussModel.cxx:443
 RooGaussModel.cxx:444
 RooGaussModel.cxx:445
 RooGaussModel.cxx:446
 RooGaussModel.cxx:447
 RooGaussModel.cxx:448
 RooGaussModel.cxx:449
 RooGaussModel.cxx:450
 RooGaussModel.cxx:451
 RooGaussModel.cxx:452
 RooGaussModel.cxx:453
 RooGaussModel.cxx:454
 RooGaussModel.cxx:455
 RooGaussModel.cxx:456
 RooGaussModel.cxx:457
 RooGaussModel.cxx:458
 RooGaussModel.cxx:459
 RooGaussModel.cxx:460
 RooGaussModel.cxx:461
 RooGaussModel.cxx:462
 RooGaussModel.cxx:463
 RooGaussModel.cxx:464
 RooGaussModel.cxx:465
 RooGaussModel.cxx:466
 RooGaussModel.cxx:467
 RooGaussModel.cxx:468
 RooGaussModel.cxx:469
 RooGaussModel.cxx:470
 RooGaussModel.cxx:471
 RooGaussModel.cxx:472
 RooGaussModel.cxx:473
 RooGaussModel.cxx:474
 RooGaussModel.cxx:475
 RooGaussModel.cxx:476
 RooGaussModel.cxx:477
 RooGaussModel.cxx:478
 RooGaussModel.cxx:479
 RooGaussModel.cxx:480
 RooGaussModel.cxx:481
 RooGaussModel.cxx:482
 RooGaussModel.cxx:483
 RooGaussModel.cxx:484
 RooGaussModel.cxx:485
 RooGaussModel.cxx:486
 RooGaussModel.cxx:487
 RooGaussModel.cxx:488
 RooGaussModel.cxx:489
 RooGaussModel.cxx:490
 RooGaussModel.cxx:491
 RooGaussModel.cxx:492
 RooGaussModel.cxx:493
 RooGaussModel.cxx:494
 RooGaussModel.cxx:495
 RooGaussModel.cxx:496
 RooGaussModel.cxx:497
 RooGaussModel.cxx:498
 RooGaussModel.cxx:499
 RooGaussModel.cxx:500
 RooGaussModel.cxx:501
 RooGaussModel.cxx:502
 RooGaussModel.cxx:503
 RooGaussModel.cxx:504
 RooGaussModel.cxx:505
 RooGaussModel.cxx:506
 RooGaussModel.cxx:507
 RooGaussModel.cxx:508
 RooGaussModel.cxx:509
 RooGaussModel.cxx:510
 RooGaussModel.cxx:511