ROOT  6.06/09
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 //
19 // BEGIN_HTML
20 // Plain Gaussian p.d.f
21 // END_HTML
22 //
23 
24 #include "RooFit.h"
25 
26 #include "Riostream.h"
27 #include "Riostream.h"
28 #include <math.h>
29 
30 #include "RooGaussian.h"
31 #include "RooAbsReal.h"
32 #include "RooRealVar.h"
33 #include "RooRandom.h"
34 #include "RooMath.h"
35 
36 using namespace std;
37 
39 
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 
43 RooGaussian::RooGaussian(const char *name, const char *title,
44  RooAbsReal& _x, RooAbsReal& _mean,
45  RooAbsReal& _sigma) :
46  RooAbsPdf(name,title),
47  x("x","Observable",this,_x),
48  mean("mean","Mean",this,_mean),
49  sigma("sigma","Width",this,_sigma)
50 {
51 }
52 
53 
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 RooGaussian::RooGaussian(const RooGaussian& other, const char* name) :
58  RooAbsPdf(other,name), x("x",this,other.x), mean("mean",this,other.mean),
59  sigma("sigma",this,other.sigma)
60 {
61 }
62 
63 
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 
68 {
69  Double_t arg= x - mean;
70  Double_t sig = sigma ;
71  Double_t ret =exp(-0.5*arg*arg/(sig*sig)) ;
72 // if (gDebug>2) {
73 // cout << "gauss(" << GetName() << ") x = " << x << " mean = " << mean << " sigma = " << sigma << " ret = " << ret << endl ;
74 // }
75  return ret ;
76 }
77 
78 
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// calculate and return the negative log-likelihood of the Poisson
82 
84 {
85  return RooAbsPdf::getLogVal(set) ;
86 // Double_t prob = getVal(set) ;
87 // return log(prob) ;
88 
89  Double_t arg= x - mean;
90  Double_t sig = sigma ;
91 
92  //static const Double_t rootPiBy2 = sqrt(atan2(0.0,-1.0)/2.0);
93  //Double_t extra = -0.5*arg*arg/(sig*sig) - log(2*rootPiBy2*sig) ;
94  Double_t extra = -0.5*arg*arg/(sig*sig) - log(analyticalIntegral(1,0)) ;
95 
96  return extra ;
97 
98 }
99 
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 
103 Int_t RooGaussian::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
104 {
105  if (matchArgs(allVars,analVars,x)) return 1 ;
106  if (matchArgs(allVars,analVars,mean)) return 2 ;
107  return 0 ;
108 }
109 
110 
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 
114 Double_t RooGaussian::analyticalIntegral(Int_t code, const char* rangeName) const
115 {
116  assert(code==1 || code==2) ;
117 
118  static const Double_t root2 = sqrt(2.) ;
119  static const Double_t rootPiBy2 = sqrt(atan2(0.0,-1.0)/2.0);
120  Double_t xscale = root2*sigma;
121  Double_t ret = 0;
122  if(code==1){
123  ret = rootPiBy2*sigma*(RooMath::erf((x.max(rangeName)-mean)/xscale)-RooMath::erf((x.min(rangeName)-mean)/xscale));
124 // if (gDebug>2) {
125 // cout << "Int_gauss_dx(mean=" << mean << ",sigma=" << sigma << ", xmin=" << x.min(rangeName) << ", xmax=" << x.max(rangeName) << ")=" << ret << endl ;
126 // }
127  } else if(code==2) {
128  ret = rootPiBy2*sigma*(RooMath::erf((mean.max(rangeName)-x)/xscale)-RooMath::erf((mean.min(rangeName)-x)/xscale));
129  } else{
130  cout << "Error in RooGaussian::analyticalIntegral" << endl;
131  }
132  return ret ;
133 
134 }
135 
136 
137 
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 
141 Int_t RooGaussian::getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, Bool_t /*staticInitOK*/) const
142 {
143  if (matchArgs(directVars,generateVars,x)) return 1 ;
144  if (matchArgs(directVars,generateVars,mean)) return 2 ;
145  return 0 ;
146 }
147 
148 
149 
150 ////////////////////////////////////////////////////////////////////////////////
151 
153 {
154  assert(code==1 || code==2) ;
155  Double_t xgen ;
156  if(code==1){
157  while(1) {
159  if (xgen<x.max() && xgen>x.min()) {
160  x = xgen ;
161  break;
162  }
163  }
164  } else if(code==2){
165  while(1) {
167  if (xgen<mean.max() && xgen>mean.min()) {
168  mean = xgen ;
169  break;
170  }
171  }
172  } else {
173  cout << "error in RooGaussian generateEvent"<< endl;
174  }
175 
176  return;
177 }
178 
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=0) const
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported...
RooRealProxy x
Definition: RooGaussian.h:44
Double_t analyticalIntegral(Int_t code, const char *rangeName=0) const
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral. ...
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
#define assert(cond)
Definition: unittest.h:542
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
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:53
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
RooRealProxy sigma
Definition: RooGaussian.h:46
Double_t getLogVal(const RooArgSet *set) const
calculate and return the negative log-likelihood of the Poisson
Definition: RooGaussian.cxx:83
Double_t Mean(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:824
Double_t evaluate() const
Definition: RooGaussian.cxx:67
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)
#define name(a, b)
Definition: linkTestLib0.cpp:5
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
Double_t min(const char *rname=0) const
Definition: RooRealProxy.h:56
static std::complex< double > erf(const std::complex< double > z)
complex erf function
Definition: RooMath.cxx:584
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...
Bool_t matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
ClassImp(RooGaussian) RooGaussian
Definition: RooGaussian.cxx:38
double exp(double)
void generateEvent(Int_t code)
Interface for generation of anan event using the algorithm corresponding to the specified code...
Double_t max(const char *rname=0) const
Definition: RooRealProxy.h:57
double log(double)