```/*****************************************************************************
* 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          *
*                                                                           *
* Redistribution and use in source and binary forms,                        *
* with or without modification, are permitted according to the terms        *
*****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//
// BEGIN_HTML
// Plain Gaussian p.d.f
// END_HTML
//

#include "RooFit.h"

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

#include "RooGaussian.h"
#include "RooAbsReal.h"
#include "RooRealVar.h"
#include "RooRandom.h"
#include "RooMath.h"

using namespace std;

ClassImp(RooGaussian)

//_____________________________________________________________________________
RooGaussian::RooGaussian(const char *name, const char *title,
RooAbsReal& _x, RooAbsReal& _mean,
RooAbsReal& _sigma) :
RooAbsPdf(name,title),
x("x","Observable",this,_x),
mean("mean","Mean",this,_mean),
sigma("sigma","Width",this,_sigma)
{
}

//_____________________________________________________________________________
RooGaussian::RooGaussian(const RooGaussian& other, const char* name) :
RooAbsPdf(other,name), x("x",this,other.x), mean("mean",this,other.mean),
sigma("sigma",this,other.sigma)
{
}

//_____________________________________________________________________________
Double_t RooGaussian::evaluate() const
{
Double_t arg= x - mean;
Double_t sig = sigma ;
Double_t ret =exp(-0.5*arg*arg/(sig*sig)) ;
//   if (gDebug>2) {
//     cout << "gauss(" << GetName() << ") x = " << x << " mean = " << mean << " sigma = " << sigma << " ret = " << ret << endl ;
//   }
return ret ;
}

//_____________________________________________________________________________
Double_t RooGaussian::getLogVal(const RooArgSet* set) const
{
// calculate and return the negative log-likelihood of the Poisson
return RooAbsPdf::getLogVal(set) ;
//   Double_t prob = getVal(set) ;
//   return log(prob) ;

Double_t arg= x - mean;
Double_t sig = sigma ;

//static const Double_t rootPiBy2 = sqrt(atan2(0.0,-1.0)/2.0);
//Double_t extra = -0.5*arg*arg/(sig*sig) - log(2*rootPiBy2*sig) ;
Double_t extra = -0.5*arg*arg/(sig*sig) - log(analyticalIntegral(1,0)) ;

return extra ;

}

//_____________________________________________________________________________
Int_t RooGaussian::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
{
if (matchArgs(allVars,analVars,x)) return 1 ;
if (matchArgs(allVars,analVars,mean)) return 2 ;
return 0 ;
}

//_____________________________________________________________________________
Double_t RooGaussian::analyticalIntegral(Int_t code, const char* rangeName) const
{
assert(code==1 || code==2) ;

static const Double_t root2 = sqrt(2.) ;
static const Double_t rootPiBy2 = sqrt(atan2(0.0,-1.0)/2.0);
Double_t xscale = root2*sigma;
Double_t ret = 0;
if(code==1){
ret = rootPiBy2*sigma*(RooMath::erf((x.max(rangeName)-mean)/xscale)-RooMath::erf((x.min(rangeName)-mean)/xscale));
//     if (gDebug>2) {
//       cout << "Int_gauss_dx(mean=" << mean << ",sigma=" << sigma << ", xmin=" << x.min(rangeName) << ", xmax=" << x.max(rangeName) << ")=" << ret << endl ;
//     }
} else if(code==2) {
ret = rootPiBy2*sigma*(RooMath::erf((mean.max(rangeName)-x)/xscale)-RooMath::erf((mean.min(rangeName)-x)/xscale));
} else{
cout << "Error in RooGaussian::analyticalIntegral" << endl;
}
return ret ;

}

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

//_____________________________________________________________________________
void RooGaussian::generateEvent(Int_t code)
{
assert(code==1 || code==2) ;
Double_t xgen ;
if(code==1){
while(1) {
xgen = RooRandom::randomGenerator()->Gaus(mean,sigma);
if (xgen<x.max() && xgen>x.min()) {
x = xgen ;
break;
}
}
} else if(code==2){
while(1) {
xgen = RooRandom::randomGenerator()->Gaus(x,sigma);
if (xgen<mean.max() && xgen>mean.min()) {
mean = xgen ;
break;
}
}
} else {
cout << "error in RooGaussian generateEvent"<< endl;
}

return;
}

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