Logo ROOT   6.08/07
Reference Guide
RooGaussian.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitModels *
4  * @(#)root/roofit:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 /**
18 \file RooGaussian.cxx
19 \class RooGaussian
20 \ingroup Roofit
21 
22 Plain Gaussian p.d.f
23 **/
24 
25 #include "RooFit.h"
26 
27 #include "Riostream.h"
28 #include "Riostream.h"
29 #include <math.h>
30 
31 #include "RooGaussian.h"
32 #include "RooAbsReal.h"
33 #include "RooRealVar.h"
34 #include "RooRandom.h"
35 #include "RooMath.h"
36 
37 using namespace std;
38 
40 
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 
44 RooGaussian::RooGaussian(const char *name, const char *title,
45  RooAbsReal& _x, RooAbsReal& _mean,
46  RooAbsReal& _sigma) :
47  RooAbsPdf(name,title),
48  x("x","Observable",this,_x),
49  mean("mean","Mean",this,_mean),
50  sigma("sigma","Width",this,_sigma)
51 {
52 }
53 
54 
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 
58 RooGaussian::RooGaussian(const RooGaussian& other, const char* name) :
59  RooAbsPdf(other,name), x("x",this,other.x), mean("mean",this,other.mean),
60  sigma("sigma",this,other.sigma)
61 {
62 }
63 
64 
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 
69 {
70  Double_t arg= x - mean;
71  Double_t sig = sigma ;
72  Double_t ret =exp(-0.5*arg*arg/(sig*sig)) ;
73 // if (gDebug>2) {
74 // cout << "gauss(" << GetName() << ") x = " << x << " mean = " << mean << " sigma = " << sigma << " ret = " << ret << endl ;
75 // }
76  return ret ;
77 }
78 
79 
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// calculate and return the negative log-likelihood of the Poisson
83 
85 {
86  return RooAbsPdf::getLogVal(set) ;
87 // Double_t prob = getVal(set) ;
88 // return log(prob) ;
89 
90  Double_t arg= x - mean;
91  Double_t sig = sigma ;
92 
93  //static const Double_t rootPiBy2 = sqrt(atan2(0.0,-1.0)/2.0);
94  //Double_t extra = -0.5*arg*arg/(sig*sig) - log(2*rootPiBy2*sig) ;
95  Double_t extra = -0.5*arg*arg/(sig*sig) - log(analyticalIntegral(1,0)) ;
96 
97  return extra ;
98 
99 }
100 
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 
104 Int_t RooGaussian::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
105 {
106  if (matchArgs(allVars,analVars,x)) return 1 ;
107  if (matchArgs(allVars,analVars,mean)) return 2 ;
108  return 0 ;
109 }
110 
111 
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 
115 Double_t RooGaussian::analyticalIntegral(Int_t code, const char* rangeName) const
116 {
117  assert(code==1 || code==2) ;
118 
119  static const Double_t root2 = sqrt(2.) ;
120  static const Double_t rootPiBy2 = sqrt(atan2(0.0,-1.0)/2.0);
121  Double_t xscale = root2*sigma;
122  Double_t ret = 0;
123  if(code==1){
124  ret = rootPiBy2*sigma*(RooMath::erf((x.max(rangeName)-mean)/xscale)-RooMath::erf((x.min(rangeName)-mean)/xscale));
125 // if (gDebug>2) {
126 // cout << "Int_gauss_dx(mean=" << mean << ",sigma=" << sigma << ", xmin=" << x.min(rangeName) << ", xmax=" << x.max(rangeName) << ")=" << ret << endl ;
127 // }
128  } else if(code==2) {
129  ret = rootPiBy2*sigma*(RooMath::erf((mean.max(rangeName)-x)/xscale)-RooMath::erf((mean.min(rangeName)-x)/xscale));
130  } else{
131  cout << "Error in RooGaussian::analyticalIntegral" << endl;
132  }
133  return ret ;
134 
135 }
136 
137 
138 
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 
142 Int_t RooGaussian::getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, Bool_t /*staticInitOK*/) const
143 {
144  if (matchArgs(directVars,generateVars,x)) return 1 ;
145  if (matchArgs(directVars,generateVars,mean)) return 2 ;
146  return 0 ;
147 }
148 
149 
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 
154 {
155  assert(code==1 || code==2) ;
156  Double_t xgen ;
157  if(code==1){
158  while(1) {
160  if (xgen<x.max() && xgen>x.min()) {
161  x = xgen ;
162  break;
163  }
164  }
165  } else if(code==2){
166  while(1) {
168  if (xgen<mean.max() && xgen>mean.min()) {
169  mean = xgen ;
170  break;
171  }
172  }
173  } else {
174  cout << "error in RooGaussian generateEvent"<< endl;
175  }
176 
177  return;
178 }
179 
RooRealProxy x
Definition: RooGaussian.h:44
Double_t evaluate() const
Definition: RooGaussian.cxx:68
Int_t getGenerator(const RooArgSet &directVars, RooArgSet &generateVars, Bool_t staticInitOK=kTRUE) const
Load generatedVars with the subset of directVars that we can generate events for, and return a code t...
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
Bool_t matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
virtual Double_t getLogVal(const RooArgSet *set=0) const
Return the log of the current value with given normalization An error message is printed if the argum...
Definition: RooAbsPdf.cxx:606
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
STL namespace.
double sqrt(double)
Double_t x[n]
Definition: legend1.C:17
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:54
const Double_t sigma
Double_t getLogVal(const RooArgSet *set) const
calculate and return the negative log-likelihood of the Poisson
Definition: RooGaussian.cxx:84
RooRealProxy sigma
Definition: RooGaussian.h:46
Double_t Mean(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:811
#define ClassImp(name)
Definition: Rtypes.h:279
Double_t min(const char *rname=0) const
Definition: RooRealProxy.h:56
double Double_t
Definition: RtypesCore.h:55
RooRealProxy mean
Definition: RooGaussian.h:45
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
double atan2(double, double)
Double_t max(const char *rname=0) const
Definition: RooRealProxy.h:57
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=0) const
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported...
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
static std::complex< double > erf(const std::complex< double > z)
complex erf function
Definition: RooMath.cxx:584
Double_t analyticalIntegral(Int_t code, const char *rangeName=0) const
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral. ...
double exp(double)
void generateEvent(Int_t code)
Interface for generation of anan event using the algorithm corresponding to the specified code...
char name[80]
Definition: TGX11.cxx:109
double log(double)