/*****************************************************************************
 * 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
// P.d.f describing decay time distribution of B meson including
// effects of standard model CP violation. This function can be analytically 
// convolved with any RooResolutionModel implementation
// END_HTML
//

#include "RooFit.h"

#include "Riostream.h"
#include "Riostream.h"
#include "RooRealVar.h"
#include "RooRandom.h"
#include "RooBCPEffDecay.h"
#include "RooRealIntegral.h"

using namespace std;

ClassImp(RooBCPEffDecay) 
;



//_____________________________________________________________________________
RooBCPEffDecay::RooBCPEffDecay(const char *name, const char *title, 
			       RooRealVar& t, RooAbsCategory& tag,
			       RooAbsReal& tau, RooAbsReal& dm,
			       RooAbsReal& avgMistag, RooAbsReal& CPeigenval,
			       RooAbsReal& a, RooAbsReal& b,
			       RooAbsReal& effRatio, RooAbsReal& delMistag,
			       const RooResolutionModel& model, DecayType type) :
  RooAbsAnaConvPdf(name,title,model,t), 
  _absLambda("absLambda","Absolute value of lambda",this,a),
  _argLambda("argLambda","Arg(Lambda)",this,b),
  _effRatio("effRatio","B0/B0bar efficiency ratio",this,effRatio),
  _CPeigenval("CPeigenval","CP eigen value",this,CPeigenval),
  _avgMistag("avgMistag","Average mistag rate",this,avgMistag),
  _delMistag("delMistag","Delta mistag rate",this,delMistag),  
  _t("t","time",this,t),
  _tau("tau","decay time",this,tau),
  _dm("dm","mixing frequency",this,dm),
  _tag("tag","CP state",this,tag),
  _genB0Frac(0),
  _type(type)
{
  // Constructor
  switch(type) {
  case SingleSided:
    _basisExp = declareBasis("exp(-@0/@1)",RooArgList(tau,dm)) ;
    _basisSin = declareBasis("exp(-@0/@1)*sin(@0*@2)",RooArgList(tau,dm)) ;
    _basisCos = declareBasis("exp(-@0/@1)*cos(@0*@2)",RooArgList(tau,dm)) ;
    break ;
  case Flipped:
    _basisExp = declareBasis("exp(@0)/@1)",RooArgList(tau,dm)) ;
    _basisSin = declareBasis("exp(@0/@1)*sin(@0*@2)",RooArgList(tau,dm)) ;
    _basisCos = declareBasis("exp(@0/@1)*cos(@0*@2)",RooArgList(tau,dm)) ;
    break ;
  case DoubleSided:
    _basisExp = declareBasis("exp(-abs(@0)/@1)",RooArgList(tau,dm)) ;
    _basisSin = declareBasis("exp(-abs(@0)/@1)*sin(@0*@2)",RooArgList(tau,dm)) ;
    _basisCos = declareBasis("exp(-abs(@0)/@1)*cos(@0*@2)",RooArgList(tau,dm)) ;
    break ;
  }
}



//_____________________________________________________________________________
RooBCPEffDecay::RooBCPEffDecay(const RooBCPEffDecay& other, const char* name) : 
  RooAbsAnaConvPdf(other,name), 
  _absLambda("absLambda",this,other._absLambda),
  _argLambda("argLambda",this,other._argLambda),
  _effRatio("effRatio",this,other._effRatio),
  _CPeigenval("CPeigenval",this,other._CPeigenval),
  _avgMistag("avgMistag",this,other._avgMistag),
  _delMistag("delMistag",this,other._delMistag),
  _t("t",this,other._t),
  _tau("tau",this,other._tau),
  _dm("dm",this,other._dm),
  _tag("tag",this,other._tag),
  _genB0Frac(other._genB0Frac),
  _type(other._type),
  _basisExp(other._basisExp),
  _basisSin(other._basisSin),
  _basisCos(other._basisCos)
{
  // Copy constructor
}



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



//_____________________________________________________________________________
Double_t RooBCPEffDecay::coefficient(Int_t basisIndex) const 
{
  // B0    : _tag = +1 
  // B0bar : _tag = -1 

  if (basisIndex==_basisExp) {
    //exp term: (1 -/+ dw)(1+a^2)/2 
    return (1 - _tag*_delMistag)*(1+_absLambda*_absLambda)/2 ;
    // =    1 + _tag*deltaDil/2
  }

  if (basisIndex==_basisSin) {
    //sin term: +/- (1-2w)*ImLambda(= -etaCP * |l| * sin2b)
    return -1*_tag*(1-2*_avgMistag)*_CPeigenval*_absLambda*_argLambda ;
    // =   _tag*avgDil * ...
  }
  
  if (basisIndex==_basisCos) {
    //cos term: +/- (1-2w)*(1-a^2)/2
    return -1*_tag*(1-2*_avgMistag)*(1-_absLambda*_absLambda)/2 ;
    // =   -_tag*avgDil * ...
  } 
  
  return 0 ;
}



//_____________________________________________________________________________
Int_t RooBCPEffDecay::getCoefAnalyticalIntegral(Int_t /*code*/, RooArgSet& allVars, RooArgSet& analVars, const char* rangeName) const 
{
  if (rangeName) return 0 ;

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



//_____________________________________________________________________________
Double_t RooBCPEffDecay::coefAnalyticalIntegral(Int_t basisIndex, Int_t code, const char* /*rangeName*/) const 
{
  switch(code) {
    // No integration
  case 0: return coefficient(basisIndex) ;

    // Integration over 'tag'
  case 1:
    if (basisIndex==_basisExp) {
      return (1+_absLambda*_absLambda) ;
    }
    
    if (basisIndex==_basisSin || basisIndex==_basisCos) {
      return 0 ;
    }
    break ;

  default:
    assert(0) ;
  }
    
  return 0 ;
}



//_____________________________________________________________________________
Int_t RooBCPEffDecay::getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, Bool_t staticInitOK) const
{
  if (staticInitOK) {
    if (matchArgs(directVars,generateVars,_t,_tag)) return 2 ;  
  }
  if (matchArgs(directVars,generateVars,_t)) return 1 ;  
  return 0 ;
}



//_____________________________________________________________________________
void RooBCPEffDecay::initGenerator(Int_t code)
{
  if (code==2) {
    // Calculate the fraction of mixed events to generate
    Double_t sumInt = RooRealIntegral("sumInt","sum integral",*this,RooArgSet(_t.arg(),_tag.arg())).getVal() ;
    _tag = 1 ;
    Double_t b0Int = RooRealIntegral("mixInt","mix integral",*this,RooArgSet(_t.arg())).getVal() ;
    _genB0Frac = b0Int/sumInt ;
  }  
}



//_____________________________________________________________________________
void RooBCPEffDecay::generateEvent(Int_t code)
{
  // Generate mix-state dependent
  if (code==2) {
    Double_t rand = RooRandom::uniform() ;
    _tag = (rand<=_genB0Frac) ? 1 : -1 ;
  }

  // Generate delta-t dependent
  while(1) {
    Double_t rand = RooRandom::uniform() ;
    Double_t tval(0) ;

    switch(_type) {
    case SingleSided:
      tval = -_tau*log(rand);
      break ;
    case Flipped:
      tval= +_tau*log(rand);
      break ;
    case DoubleSided:
      tval = (rand<=0.5) ? -_tau*log(2*rand) : +_tau*log(2*(rand-0.5)) ;
      break ;
    }

    // Accept event if T is in generated range
    Double_t maxDil = 1.0 ;
    Double_t al2 = _absLambda*_absLambda ;
    Double_t maxAcceptProb = (1+al2) + fabs(maxDil*_CPeigenval*_absLambda*_argLambda) + fabs(maxDil*(1-al2)/2);        
    Double_t acceptProb    = (1+al2)/2*(1-_tag*_delMistag) 
                           - (_tag*(1-2*_avgMistag))*(_CPeigenval*_absLambda*_argLambda)*sin(_dm*tval) 
                           - (_tag*(1-2*_avgMistag))*(1-al2)/2*cos(_dm*tval);

    Bool_t accept = maxAcceptProb*RooRandom::uniform() < acceptProb ? kTRUE : kFALSE ;
    
    if (tval<_t.max() && tval>_t.min() && accept) {
      _t = tval ;
      break ;
    }
  }
  
}

 RooBCPEffDecay.cxx:1
 RooBCPEffDecay.cxx:2
 RooBCPEffDecay.cxx:3
 RooBCPEffDecay.cxx:4
 RooBCPEffDecay.cxx:5
 RooBCPEffDecay.cxx:6
 RooBCPEffDecay.cxx:7
 RooBCPEffDecay.cxx:8
 RooBCPEffDecay.cxx:9
 RooBCPEffDecay.cxx:10
 RooBCPEffDecay.cxx:11
 RooBCPEffDecay.cxx:12
 RooBCPEffDecay.cxx:13
 RooBCPEffDecay.cxx:14
 RooBCPEffDecay.cxx:15
 RooBCPEffDecay.cxx:16
 RooBCPEffDecay.cxx:17
 RooBCPEffDecay.cxx:18
 RooBCPEffDecay.cxx:19
 RooBCPEffDecay.cxx:20
 RooBCPEffDecay.cxx:21
 RooBCPEffDecay.cxx:22
 RooBCPEffDecay.cxx:23
 RooBCPEffDecay.cxx:24
 RooBCPEffDecay.cxx:25
 RooBCPEffDecay.cxx:26
 RooBCPEffDecay.cxx:27
 RooBCPEffDecay.cxx:28
 RooBCPEffDecay.cxx:29
 RooBCPEffDecay.cxx:30
 RooBCPEffDecay.cxx:31
 RooBCPEffDecay.cxx:32
 RooBCPEffDecay.cxx:33
 RooBCPEffDecay.cxx:34
 RooBCPEffDecay.cxx:35
 RooBCPEffDecay.cxx:36
 RooBCPEffDecay.cxx:37
 RooBCPEffDecay.cxx:38
 RooBCPEffDecay.cxx:39
 RooBCPEffDecay.cxx:40
 RooBCPEffDecay.cxx:41
 RooBCPEffDecay.cxx:42
 RooBCPEffDecay.cxx:43
 RooBCPEffDecay.cxx:44
 RooBCPEffDecay.cxx:45
 RooBCPEffDecay.cxx:46
 RooBCPEffDecay.cxx:47
 RooBCPEffDecay.cxx:48
 RooBCPEffDecay.cxx:49
 RooBCPEffDecay.cxx:50
 RooBCPEffDecay.cxx:51
 RooBCPEffDecay.cxx:52
 RooBCPEffDecay.cxx:53
 RooBCPEffDecay.cxx:54
 RooBCPEffDecay.cxx:55
 RooBCPEffDecay.cxx:56
 RooBCPEffDecay.cxx:57
 RooBCPEffDecay.cxx:58
 RooBCPEffDecay.cxx:59
 RooBCPEffDecay.cxx:60
 RooBCPEffDecay.cxx:61
 RooBCPEffDecay.cxx:62
 RooBCPEffDecay.cxx:63
 RooBCPEffDecay.cxx:64
 RooBCPEffDecay.cxx:65
 RooBCPEffDecay.cxx:66
 RooBCPEffDecay.cxx:67
 RooBCPEffDecay.cxx:68
 RooBCPEffDecay.cxx:69
 RooBCPEffDecay.cxx:70
 RooBCPEffDecay.cxx:71
 RooBCPEffDecay.cxx:72
 RooBCPEffDecay.cxx:73
 RooBCPEffDecay.cxx:74
 RooBCPEffDecay.cxx:75
 RooBCPEffDecay.cxx:76
 RooBCPEffDecay.cxx:77
 RooBCPEffDecay.cxx:78
 RooBCPEffDecay.cxx:79
 RooBCPEffDecay.cxx:80
 RooBCPEffDecay.cxx:81
 RooBCPEffDecay.cxx:82
 RooBCPEffDecay.cxx:83
 RooBCPEffDecay.cxx:84
 RooBCPEffDecay.cxx:85
 RooBCPEffDecay.cxx:86
 RooBCPEffDecay.cxx:87
 RooBCPEffDecay.cxx:88
 RooBCPEffDecay.cxx:89
 RooBCPEffDecay.cxx:90
 RooBCPEffDecay.cxx:91
 RooBCPEffDecay.cxx:92
 RooBCPEffDecay.cxx:93
 RooBCPEffDecay.cxx:94
 RooBCPEffDecay.cxx:95
 RooBCPEffDecay.cxx:96
 RooBCPEffDecay.cxx:97
 RooBCPEffDecay.cxx:98
 RooBCPEffDecay.cxx:99
 RooBCPEffDecay.cxx:100
 RooBCPEffDecay.cxx:101
 RooBCPEffDecay.cxx:102
 RooBCPEffDecay.cxx:103
 RooBCPEffDecay.cxx:104
 RooBCPEffDecay.cxx:105
 RooBCPEffDecay.cxx:106
 RooBCPEffDecay.cxx:107
 RooBCPEffDecay.cxx:108
 RooBCPEffDecay.cxx:109
 RooBCPEffDecay.cxx:110
 RooBCPEffDecay.cxx:111
 RooBCPEffDecay.cxx:112
 RooBCPEffDecay.cxx:113
 RooBCPEffDecay.cxx:114
 RooBCPEffDecay.cxx:115
 RooBCPEffDecay.cxx:116
 RooBCPEffDecay.cxx:117
 RooBCPEffDecay.cxx:118
 RooBCPEffDecay.cxx:119
 RooBCPEffDecay.cxx:120
 RooBCPEffDecay.cxx:121
 RooBCPEffDecay.cxx:122
 RooBCPEffDecay.cxx:123
 RooBCPEffDecay.cxx:124
 RooBCPEffDecay.cxx:125
 RooBCPEffDecay.cxx:126
 RooBCPEffDecay.cxx:127
 RooBCPEffDecay.cxx:128
 RooBCPEffDecay.cxx:129
 RooBCPEffDecay.cxx:130
 RooBCPEffDecay.cxx:131
 RooBCPEffDecay.cxx:132
 RooBCPEffDecay.cxx:133
 RooBCPEffDecay.cxx:134
 RooBCPEffDecay.cxx:135
 RooBCPEffDecay.cxx:136
 RooBCPEffDecay.cxx:137
 RooBCPEffDecay.cxx:138
 RooBCPEffDecay.cxx:139
 RooBCPEffDecay.cxx:140
 RooBCPEffDecay.cxx:141
 RooBCPEffDecay.cxx:142
 RooBCPEffDecay.cxx:143
 RooBCPEffDecay.cxx:144
 RooBCPEffDecay.cxx:145
 RooBCPEffDecay.cxx:146
 RooBCPEffDecay.cxx:147
 RooBCPEffDecay.cxx:148
 RooBCPEffDecay.cxx:149
 RooBCPEffDecay.cxx:150
 RooBCPEffDecay.cxx:151
 RooBCPEffDecay.cxx:152
 RooBCPEffDecay.cxx:153
 RooBCPEffDecay.cxx:154
 RooBCPEffDecay.cxx:155
 RooBCPEffDecay.cxx:156
 RooBCPEffDecay.cxx:157
 RooBCPEffDecay.cxx:158
 RooBCPEffDecay.cxx:159
 RooBCPEffDecay.cxx:160
 RooBCPEffDecay.cxx:161
 RooBCPEffDecay.cxx:162
 RooBCPEffDecay.cxx:163
 RooBCPEffDecay.cxx:164
 RooBCPEffDecay.cxx:165
 RooBCPEffDecay.cxx:166
 RooBCPEffDecay.cxx:167
 RooBCPEffDecay.cxx:168
 RooBCPEffDecay.cxx:169
 RooBCPEffDecay.cxx:170
 RooBCPEffDecay.cxx:171
 RooBCPEffDecay.cxx:172
 RooBCPEffDecay.cxx:173
 RooBCPEffDecay.cxx:174
 RooBCPEffDecay.cxx:175
 RooBCPEffDecay.cxx:176
 RooBCPEffDecay.cxx:177
 RooBCPEffDecay.cxx:178
 RooBCPEffDecay.cxx:179
 RooBCPEffDecay.cxx:180
 RooBCPEffDecay.cxx:181
 RooBCPEffDecay.cxx:182
 RooBCPEffDecay.cxx:183
 RooBCPEffDecay.cxx:184
 RooBCPEffDecay.cxx:185
 RooBCPEffDecay.cxx:186
 RooBCPEffDecay.cxx:187
 RooBCPEffDecay.cxx:188
 RooBCPEffDecay.cxx:189
 RooBCPEffDecay.cxx:190
 RooBCPEffDecay.cxx:191
 RooBCPEffDecay.cxx:192
 RooBCPEffDecay.cxx:193
 RooBCPEffDecay.cxx:194
 RooBCPEffDecay.cxx:195
 RooBCPEffDecay.cxx:196
 RooBCPEffDecay.cxx:197
 RooBCPEffDecay.cxx:198
 RooBCPEffDecay.cxx:199
 RooBCPEffDecay.cxx:200
 RooBCPEffDecay.cxx:201
 RooBCPEffDecay.cxx:202
 RooBCPEffDecay.cxx:203
 RooBCPEffDecay.cxx:204
 RooBCPEffDecay.cxx:205
 RooBCPEffDecay.cxx:206
 RooBCPEffDecay.cxx:207
 RooBCPEffDecay.cxx:208
 RooBCPEffDecay.cxx:209
 RooBCPEffDecay.cxx:210
 RooBCPEffDecay.cxx:211
 RooBCPEffDecay.cxx:212
 RooBCPEffDecay.cxx:213
 RooBCPEffDecay.cxx:214
 RooBCPEffDecay.cxx:215
 RooBCPEffDecay.cxx:216
 RooBCPEffDecay.cxx:217
 RooBCPEffDecay.cxx:218
 RooBCPEffDecay.cxx:219
 RooBCPEffDecay.cxx:220
 RooBCPEffDecay.cxx:221
 RooBCPEffDecay.cxx:222
 RooBCPEffDecay.cxx:223
 RooBCPEffDecay.cxx:224
 RooBCPEffDecay.cxx:225
 RooBCPEffDecay.cxx:226
 RooBCPEffDecay.cxx:227
 RooBCPEffDecay.cxx:228
 RooBCPEffDecay.cxx:229
 RooBCPEffDecay.cxx:230
 RooBCPEffDecay.cxx:231
 RooBCPEffDecay.cxx:232
 RooBCPEffDecay.cxx:233
 RooBCPEffDecay.cxx:234
 RooBCPEffDecay.cxx:235
 RooBCPEffDecay.cxx:236
 RooBCPEffDecay.cxx:237
 RooBCPEffDecay.cxx:238
 RooBCPEffDecay.cxx:239
 RooBCPEffDecay.cxx:240
 RooBCPEffDecay.cxx:241
 RooBCPEffDecay.cxx:242
 RooBCPEffDecay.cxx:243
 RooBCPEffDecay.cxx:244
 RooBCPEffDecay.cxx:245
 RooBCPEffDecay.cxx:246
 RooBCPEffDecay.cxx:247
 RooBCPEffDecay.cxx:248
 RooBCPEffDecay.cxx:249
 RooBCPEffDecay.cxx:250
 RooBCPEffDecay.cxx:251
 RooBCPEffDecay.cxx:252
 RooBCPEffDecay.cxx:253
 RooBCPEffDecay.cxx:254