BOSS 7.0.8
BESIII Offline Software System
Loading...
Searching...
No Matches
EvtPdf.hh
Go to the documentation of this file.
1/*******************************************************************************
2 * Project: BaBar detector at the SLAC PEP-II B-factory
3 * Package: EvtGenBase
4 * File: $Id: EvtPdf.hh,v 1.1.1.2 2007/10/26 05:03:14 pingrg Exp $
5 * Author: Alexei Dvoretskii, [email protected], 2001-2002
6 *
7 * Copyright (C) 2002 Caltech
8 *******************************************************************************/
9
10/*
11 * All classes are templated on the point type T
12 *
13 * EvtPdf:
14 *
15 * Probability density function defined on an interval of phase-space.
16 * Integral over the interval can be calculated by Monte Carlo integration.
17 * Some (but not all) PDFs are analytic in the sense that they can be integrated
18 * by numeric quadrature and distributions can be generated according to them.
19 *
20 * EvtPdfGen:
21 *
22 * Generator adaptor. Can be used to generate random points
23 * distributed according to the PDF for analytic PDFs.
24 *
25 * EvtPdfPred:
26 *
27 * Predicate adaptor for PDFs. Can be used for generating random points distributed
28 * according to the PDF for any PDF using rejection method. (See "Numerical Recipes").
29 *
30 * EvtPdfUnary:
31 *
32 * Adapter for generic algorithms. Evaluates the PDF and returns the value
33 *
34 * EvtPdfDiv:
35 *
36 * PDF obtained by division of one PDF by another. Because the two PDFs are
37 * arbitrary this PDF is not analytic. When importance sampling is used the
38 * original PDF is divided by the analytic comparison function. EvtPdfDiv is
39 * used to represent the modified PDF.
40 */
41
42#ifndef EVT_PDF_HH
43#define EVT_PDF_HH
44
45#include <assert.h>
46#include <stdio.h>
53
54template <class T> class EvtPdfPred;
55template <class T> class EvtPdfGen;
56
57template <class T> class EvtPdf {
58public:
59
60 EvtPdf() {}
61 EvtPdf(const EvtPdf& other) : _itg(other._itg) {}
62 virtual ~EvtPdf() {}
63 virtual EvtPdf<T>* clone() const = 0;
64
65 double evaluate(const T& p) const {
66 if(p.isValid()) return pdf(p);
67 else return 0.;
68 }
69
70 // Find PDF maximum. Points are sampled according to pc
71
72 EvtPdfMax<T> findMax(const EvtPdf<T>& pc, int N);
73
74 // Find generation efficiency.
75
76 EvtValError findGenEff(const EvtPdf<T>& pc, int N, int nFindMax);
77
78 // Analytic integration. Calls cascade down until an overridden
79 // method is called.
80
81 void setItg(EvtValError itg) {_itg = itg; }
82
85 return _itg;
86 }
87 EvtValError getItg(int N) const {
89 return _itg;
90 }
91
93 //make sun happy - return something
94 { printf("Analytic integration of PDF is not defined\n"); assert(0); return compute_integral();}
95 virtual EvtValError compute_integral(int N) const { return compute_integral(); }
96
97 // Monte Carlo integration.
98
100
101 // Generation. Create predicate accept-reject generators.
102 // nMax iterations will be used to find the maximum of the accept-reject predicate
103
104 EvtPredGen<EvtPdfGen<T>,EvtPdfPred<T> > accRejGen(const EvtPdf<T>& pc, int nMax, double factor = 1.);
105
106 virtual T randomPoint();
107
108protected:
109
110 virtual double pdf(const T&) const = 0;
112};
113
114
115template <class T> class EvtPdfGen {
116public:
117 typedef T result_type;
118
119 EvtPdfGen() : _pdf(0) {}
120 EvtPdfGen(const EvtPdfGen<T>& other) :
121 _pdf(other._pdf ? other._pdf->clone() : 0)
122 {}
123 EvtPdfGen(const EvtPdf<T>& pdf) :
124 _pdf(pdf.clone())
125 {}
126 ~EvtPdfGen() { delete _pdf;}
127
128 result_type operator()() {return _pdf->randomPoint();}
129
130private:
131
132 EvtPdf<T>* _pdf;
133};
134
135
136template <class T> class EvtPdfPred {
137public:
138 typedef T argument_type;
139 typedef bool result_type;
140
142 EvtPdfPred(const EvtPdf<T>& thePdf) : itsPdf(thePdf.clone()) {}
143 EvtPdfPred(const EvtPdfPred& other) : COPY_PTR(itsPdf), COPY_MEM(itsPdfMax) {}
144 ~EvtPdfPred() { delete itsPdf; }
145
147 {
148 assert(itsPdf);
149 assert(itsPdfMax.valueKnown());
150
151 double random = EvtRandom::Flat(0.,itsPdfMax.value());
152 return (random <= itsPdf->evaluate(p));
153 }
154
155 EvtPdfMax<T> getMax() const { return itsPdfMax; }
156 void setMax(const EvtPdfMax<T>& max) { itsPdfMax = max; }
157 template <class InputIterator> void compute_max(InputIterator it, InputIterator end,
158 double factor = 1.)
159 {
160 T p = *it++;
161 itsPdfMax = EvtPdfMax<T>(p,itsPdf->evaluate(p)*factor);
162
163 while(!(it == end)) {
164 T p = *it++;
165 double val = itsPdf->evaluate(p)*factor;
166 if(val > itsPdfMax.value()) itsPdfMax = EvtPdfMax<T>(p,val);
167 }
168 }
169
170private:
171
172 EvtPdf<T>* itsPdf;
173 EvtPdfMax<T> itsPdfMax;
174};
175
176
177template <class T> class EvtPdfUnary {
178public:
179 typedef double result_type;
180 typedef T argument_type;
181
183 EvtPdfUnary(const EvtPdf<T>& thePdf) : itsPdf(thePdf.clone()) {}
184 EvtPdfUnary(const EvtPdfUnary& other) : COPY_PTR(itsPdf) {}
185 ~EvtPdfUnary() { delete itsPdf; }
186
188 {
189 assert(itsPdf);
190 double ret = itsPdf->evaluate(p);
191 return ret;
192 }
193
194private:
195
196 EvtPdf<T>* itsPdf;
197};
198
199
200template <class T> class EvtPdfDiv : public EvtPdf<T> {
201public:
202
203 EvtPdfDiv() : itsNum(0), itsDen(0) {}
204 EvtPdfDiv(const EvtPdf<T>& theNum, const EvtPdf<T>& theDen)
205 : EvtPdf<T>(), itsNum(theNum.clone()), itsDen(theDen.clone())
206 {}
208 : EvtPdf<T>(other), COPY_PTR(itsNum), COPY_PTR(itsDen)
209 {}
210 virtual ~EvtPdfDiv() { delete itsNum; delete itsDen; }
211 virtual EvtPdf<T>* clone() const
212 { return new EvtPdfDiv(*this); }
213
214 virtual double pdf(const T& p) const
215 {
216 double num = itsNum->evaluate(p);
217 double den = itsDen->evaluate(p);
218 assert(den != 0);
219 return num/den;
220 }
221
222private:
223
224 EvtPdf<T>* itsNum; // numerator
225 EvtPdf<T>* itsDen; // denominator
226};
227
228
229template <class T>
231{
232 EvtPdfPred<T> pred(*this);
233 EvtPdfGen<T> gen(pc);
234 pred.compute_max(iter(gen,N),iter(gen));
235 EvtPdfMax<T> p = pred.getMax();
236 return p;
237}
238
239
240template <class T>
241EvtValError EvtPdf<T>::findGenEff(const EvtPdf<T>& pc, int N, int nFindMax)
242{
243 assert(N > 0 || nFindMax > 0);
244 EvtPredGen<EvtPdfGen<T>,EvtPdfPred<T> > gen = accRejGen(pc,nFindMax);
245 int i;
246 for(i=0;i<N;i++) gen();
247 double eff = double(gen.getPassed())/double(gen.getTried());
248 double err = sqrt(double(gen.getPassed()))/double(gen.getTried());
249 return EvtValError(eff,err);
250}
251
252template <class T>
254{
255 assert(N > 0);
256
257 EvtValError otherItg = pc.getItg();
258 EvtPdfDiv<T> pdfdiv(*this,pc);
259 EvtPdfUnary<T> unary(pdfdiv);
260
261 EvtPdfGen<T> gen(pc);
262 EvtStreamInputIterator<T> begin = iter(gen,N);
264
265 double sum = 0.;
266 double sum2 = 0.;
267 while(!(begin == end)) {
268
269 double value = pdfdiv.evaluate(*begin++);
270 sum += value;
271 sum2 += value*value;
272 }
273
275 if(N > 0) {
276 double av = sum/((double) N);
277 if(N > 1) {
278 double dev2 = (sum2 - av*av*N)/((double) (N - 1));
279 // Due to numerical precision dev2 may sometimes be negative
280 if(dev2 < 0.) dev2 = 0.;
281 double error = sqrt(dev2/((double) N));
282 x = EvtValError(av,error);
283 }
284 else x = EvtValError(av);
285 }
286 _itg = x * pc.getItg();
287 return _itg;
288}
289
290template <class T>
292{
293 printf("Function defined for analytic PDFs only\n");
294 assert(0);
295 T temp;
296 return temp;
297}
298
299template <class T>
301EvtPdf<T>::accRejGen(const EvtPdf<T>& pc, int nMax, double factor)
302{
303 EvtPdfGen<T> gen(pc);
304 EvtPdfDiv<T> pdfdiv(*this,pc);
305 EvtPdfPred<T> pred(pdfdiv);
306 pred.compute_max(iter(gen,nMax),iter(gen),factor);
307 return EvtPredGen<EvtPdfGen<T>,EvtPdfPred<T> >(gen,pred);
308}
309
310#endif
311
312
313
314
315
Double_t x[10]
#define COPY_PTR(X)
Definition: EvtMacros.hh:15
#define COPY_MEM(X)
Definition: EvtMacros.hh:16
EvtStreamInputIterator< typename Generator::result_type > iter(Generator gen, int N=0)
virtual double pdf(const T &p) const
Definition: EvtPdf.hh:214
virtual ~EvtPdfDiv()
Definition: EvtPdf.hh:210
EvtPdfDiv(const EvtPdfDiv< T > &other)
Definition: EvtPdf.hh:207
EvtPdfDiv()
Definition: EvtPdf.hh:203
EvtPdfDiv(const EvtPdf< T > &theNum, const EvtPdf< T > &theDen)
Definition: EvtPdf.hh:204
virtual EvtPdf< T > * clone() const
Definition: EvtPdf.hh:211
~EvtPdfGen()
Definition: EvtPdf.hh:126
EvtPdfGen(const EvtPdf< T > &pdf)
Definition: EvtPdf.hh:123
T result_type
Definition: EvtPdf.hh:117
result_type operator()()
Definition: EvtPdf.hh:128
EvtPdfGen()
Definition: EvtPdf.hh:119
EvtPdfGen(const EvtPdfGen< T > &other)
Definition: EvtPdf.hh:120
bool valueKnown() const
Definition: EvtPdfMax.hh:38
double value() const
Definition: EvtPdfMax.hh:39
result_type operator()(argument_type p)
Definition: EvtPdf.hh:146
T argument_type
Definition: EvtPdf.hh:138
void compute_max(InputIterator it, InputIterator end, double factor=1.)
Definition: EvtPdf.hh:157
EvtPdfPred()
Definition: EvtPdf.hh:141
void setMax(const EvtPdfMax< T > &max)
Definition: EvtPdf.hh:156
~EvtPdfPred()
Definition: EvtPdf.hh:144
EvtPdfPred(const EvtPdf< T > &thePdf)
Definition: EvtPdf.hh:142
bool result_type
Definition: EvtPdf.hh:139
EvtPdfMax< T > getMax() const
Definition: EvtPdf.hh:155
EvtPdfPred(const EvtPdfPred &other)
Definition: EvtPdf.hh:143
EvtPdfUnary(const EvtPdf< T > &thePdf)
Definition: EvtPdf.hh:183
EvtPdfUnary()
Definition: EvtPdf.hh:182
result_type operator()(argument_type p)
Definition: EvtPdf.hh:187
T argument_type
Definition: EvtPdf.hh:180
~EvtPdfUnary()
Definition: EvtPdf.hh:185
EvtPdfUnary(const EvtPdfUnary &other)
Definition: EvtPdf.hh:184
double result_type
Definition: EvtPdf.hh:179
Definition: EvtPdf.hh:57
virtual ~EvtPdf()
Definition: EvtPdf.hh:62
virtual EvtPdf< T > * clone() const =0
virtual EvtValError compute_integral(int N) const
Definition: EvtPdf.hh:95
EvtPredGen< EvtPdfGen< T >, EvtPdfPred< T > > accRejGen(const EvtPdf< T > &pc, int nMax, double factor=1.)
Definition: EvtPdf.hh:301
virtual EvtValError compute_integral() const
Definition: EvtPdf.hh:92
void setItg(EvtValError itg)
Definition: EvtPdf.hh:81
EvtValError _itg
Definition: EvtPdf.hh:111
EvtValError getItg(int N) const
Definition: EvtPdf.hh:87
virtual double pdf(const T &) const =0
EvtValError findGenEff(const EvtPdf< T > &pc, int N, int nFindMax)
Definition: EvtPdf.hh:241
virtual T randomPoint()
Definition: EvtPdf.hh:291
EvtValError compute_mc_integral(const EvtPdf< T > &pc, int N)
Definition: EvtPdf.hh:253
double evaluate(const T &p) const
Definition: EvtPdf.hh:65
EvtPdfMax< T > findMax(const EvtPdf< T > &pc, int N)
Definition: EvtPdf.hh:230
EvtPdf(const EvtPdf &other)
Definition: EvtPdf.hh:61
EvtValError getItg() const
Definition: EvtPdf.hh:83
EvtPdf()
Definition: EvtPdf.hh:60
int getPassed() const
Definition: EvtPredGen.hh:65
int getTried() const
Definition: EvtPredGen.hh:64
static double Flat()
Definition: EvtRandom.cc:74
int valueKnown() const
Definition: EvtValError.hh:30