/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitModels                                                     *
 * @(#)root/roofit:$Id$
 * Authors:                                                                  *
 *   RW, Ruddick William  UC Colorado        wor@slac.stanford.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
// RooBukinPdf implements the NovosibirskA function 
// END_HTML
//

// Original Fortran Header below
/*****************************************************************************
 * Fitting function for asymmetric peaks with 6 free parameters:	     *
 *     Ap   - peak value						     *
 *     Xp   - peak position						     *
 *     sigp - FWHM divided by 2*sqrt(2*log(2))=2.35			     *
 *     xi   - peak asymmetry parameter					     *
 *     rho1 - parameter of the "left tail"				     *
 *     rho2 - parameter of the "right tail"				     *
 *   ---------------------------------------------			     *
 *       May 26, 2003							     *
 *       A.Bukin, Budker INP, Novosibirsk				     *
 *       Documentation:							     *
 *       http://www.slac.stanford.edu/BFROOT/www/Organization/CollabMtgs/2003/detJuly2003/Tues3a/bukin.ps 
 *   -------------------------------------------			     *
 *****************************************************************************/

#include "RooFit.h"

#include <math.h>


#include "RooBukinPdf.h"
#include "RooRealVar.h"
#include "TMath.h"

using namespace std;

ClassImp(RooBukinPdf)



//_____________________________________________________________________________
RooBukinPdf::RooBukinPdf(const char *name, const char *title,
			 RooAbsReal& _x,    RooAbsReal& _Xp,
			 RooAbsReal& _sigp, RooAbsReal& _xi,
			 RooAbsReal& _rho1, RooAbsReal& _rho2) :
  // The two addresses refer to our first dependent variable and
  // parameter, respectively, as declared in the rdl file
  RooAbsPdf(name, title),
  x("x","x",this,_x),
  Xp("Xp","Xp",this,_Xp),
  sigp("sigp","sigp",this,_sigp),
  xi("xi","xi",this,_xi),
  rho1("rho1","rho1",this,_rho1),
  rho2("rho2","rho2",this,_rho2)
{
  // Constructor
  consts = 2*sqrt(2*log(2.));
}



//_____________________________________________________________________________
RooBukinPdf::RooBukinPdf(const RooBukinPdf& other, const char *name):
  RooAbsPdf(other,name),
  x("x",this,other.x),
  Xp("Xp",this,other.Xp),
  sigp("sigp",this,other.sigp),
  xi("xi",this,other.xi),
  rho1("rho1",this,other.rho1),
  rho2("rho2",this,other.rho2)

{
  // Copy constructor
  consts = 2*sqrt(2*log(2.));
}



//_____________________________________________________________________________
Double_t RooBukinPdf::evaluate() const 
{
  // Implementation 

  double r1=0,r2=0,r3=0,r4=0,r5=0,hp=0;
  double x1 = 0,x2 = 0;
  double fit_result = 0;
  
  hp=sigp*consts;
  r3=log(2.);
  r4=sqrt(TMath::Power(xi,2)+1);
  r1=xi/r4;  

  if(TMath::Abs(xi) > exp(-6.)){
    r5=xi/log(r4+xi);
  }
  else
    r5=1;
    
  x1 = Xp + (hp / 2) * (r1-1);
  x2 = Xp + (hp / 2) * (r1+1);
  
  //--- Left Side
  if(x < x1){
    r2=rho1*TMath::Power((x-x1)/(Xp-x1),2)-r3 + 4 * r3 * (x-x1)/hp * r5 * r4/TMath::Power((r4-xi),2);
  }


  //--- Center
  else if(x < x2) {
    if(TMath::Abs(xi) > exp(-6.)) {
      r2=log(1 + 4 * xi * r4 * (x-Xp)/hp)/log(1+2*xi*(xi-r4));
      r2=-r3*(TMath::Power(r2,2));
    }
    else{
      r2=-4*r3*TMath::Power(((x-Xp)/hp),2);  
    }
  }
  

  //--- Right Side
  else {
    r2=rho2*TMath::Power((x-x2)/(Xp-x2),2)-r3 - 4 * r3 * (x-x2)/hp * r5 * r4/TMath::Power((r4+xi),2);
  }

  if(TMath::Abs(r2) > 100){
    fit_result = 0;  
  }
  else{
    //---- Normalize the result
    fit_result = exp(r2);
  }
  
  return fit_result;
  
}
 RooBukinPdf.cxx:1
 RooBukinPdf.cxx:2
 RooBukinPdf.cxx:3
 RooBukinPdf.cxx:4
 RooBukinPdf.cxx:5
 RooBukinPdf.cxx:6
 RooBukinPdf.cxx:7
 RooBukinPdf.cxx:8
 RooBukinPdf.cxx:9
 RooBukinPdf.cxx:10
 RooBukinPdf.cxx:11
 RooBukinPdf.cxx:12
 RooBukinPdf.cxx:13
 RooBukinPdf.cxx:14
 RooBukinPdf.cxx:15
 RooBukinPdf.cxx:16
 RooBukinPdf.cxx:17
 RooBukinPdf.cxx:18
 RooBukinPdf.cxx:19
 RooBukinPdf.cxx:20
 RooBukinPdf.cxx:21
 RooBukinPdf.cxx:22
 RooBukinPdf.cxx:23
 RooBukinPdf.cxx:24
 RooBukinPdf.cxx:25
 RooBukinPdf.cxx:26
 RooBukinPdf.cxx:27
 RooBukinPdf.cxx:28
 RooBukinPdf.cxx:29
 RooBukinPdf.cxx:30
 RooBukinPdf.cxx:31
 RooBukinPdf.cxx:32
 RooBukinPdf.cxx:33
 RooBukinPdf.cxx:34
 RooBukinPdf.cxx:35
 RooBukinPdf.cxx:36
 RooBukinPdf.cxx:37
 RooBukinPdf.cxx:38
 RooBukinPdf.cxx:39
 RooBukinPdf.cxx:40
 RooBukinPdf.cxx:41
 RooBukinPdf.cxx:42
 RooBukinPdf.cxx:43
 RooBukinPdf.cxx:44
 RooBukinPdf.cxx:45
 RooBukinPdf.cxx:46
 RooBukinPdf.cxx:47
 RooBukinPdf.cxx:48
 RooBukinPdf.cxx:49
 RooBukinPdf.cxx:50
 RooBukinPdf.cxx:51
 RooBukinPdf.cxx:52
 RooBukinPdf.cxx:53
 RooBukinPdf.cxx:54
 RooBukinPdf.cxx:55
 RooBukinPdf.cxx:56
 RooBukinPdf.cxx:57
 RooBukinPdf.cxx:58
 RooBukinPdf.cxx:59
 RooBukinPdf.cxx:60
 RooBukinPdf.cxx:61
 RooBukinPdf.cxx:62
 RooBukinPdf.cxx:63
 RooBukinPdf.cxx:64
 RooBukinPdf.cxx:65
 RooBukinPdf.cxx:66
 RooBukinPdf.cxx:67
 RooBukinPdf.cxx:68
 RooBukinPdf.cxx:69
 RooBukinPdf.cxx:70
 RooBukinPdf.cxx:71
 RooBukinPdf.cxx:72
 RooBukinPdf.cxx:73
 RooBukinPdf.cxx:74
 RooBukinPdf.cxx:75
 RooBukinPdf.cxx:76
 RooBukinPdf.cxx:77
 RooBukinPdf.cxx:78
 RooBukinPdf.cxx:79
 RooBukinPdf.cxx:80
 RooBukinPdf.cxx:81
 RooBukinPdf.cxx:82
 RooBukinPdf.cxx:83
 RooBukinPdf.cxx:84
 RooBukinPdf.cxx:85
 RooBukinPdf.cxx:86
 RooBukinPdf.cxx:87
 RooBukinPdf.cxx:88
 RooBukinPdf.cxx:89
 RooBukinPdf.cxx:90
 RooBukinPdf.cxx:91
 RooBukinPdf.cxx:92
 RooBukinPdf.cxx:93
 RooBukinPdf.cxx:94
 RooBukinPdf.cxx:95
 RooBukinPdf.cxx:96
 RooBukinPdf.cxx:97
 RooBukinPdf.cxx:98
 RooBukinPdf.cxx:99
 RooBukinPdf.cxx:100
 RooBukinPdf.cxx:101
 RooBukinPdf.cxx:102
 RooBukinPdf.cxx:103
 RooBukinPdf.cxx:104
 RooBukinPdf.cxx:105
 RooBukinPdf.cxx:106
 RooBukinPdf.cxx:107
 RooBukinPdf.cxx:108
 RooBukinPdf.cxx:109
 RooBukinPdf.cxx:110
 RooBukinPdf.cxx:111
 RooBukinPdf.cxx:112
 RooBukinPdf.cxx:113
 RooBukinPdf.cxx:114
 RooBukinPdf.cxx:115
 RooBukinPdf.cxx:116
 RooBukinPdf.cxx:117
 RooBukinPdf.cxx:118
 RooBukinPdf.cxx:119
 RooBukinPdf.cxx:120
 RooBukinPdf.cxx:121
 RooBukinPdf.cxx:122
 RooBukinPdf.cxx:123
 RooBukinPdf.cxx:124
 RooBukinPdf.cxx:125
 RooBukinPdf.cxx:126
 RooBukinPdf.cxx:127
 RooBukinPdf.cxx:128
 RooBukinPdf.cxx:129
 RooBukinPdf.cxx:130
 RooBukinPdf.cxx:131
 RooBukinPdf.cxx:132
 RooBukinPdf.cxx:133
 RooBukinPdf.cxx:134
 RooBukinPdf.cxx:135
 RooBukinPdf.cxx:136
 RooBukinPdf.cxx:137
 RooBukinPdf.cxx:138
 RooBukinPdf.cxx:139
 RooBukinPdf.cxx:140
 RooBukinPdf.cxx:141
 RooBukinPdf.cxx:142
 RooBukinPdf.cxx:143
 RooBukinPdf.cxx:144
 RooBukinPdf.cxx:145
 RooBukinPdf.cxx:146
 RooBukinPdf.cxx:147
 RooBukinPdf.cxx:148
 RooBukinPdf.cxx:149