1 /*******************************************************************************
2 * Project: BaBar detector at the SLAC PEP-II B-factory
4 * File: $Id: EvtPdf.hh,v 1.2 2009-03-16 16:40:15 robbep Exp $
5 * Author: Alexei Dvoretskii, dvoretsk@slac.stanford.edu, 2001-2002
7 * Copyright (C) 2002 Caltech
8 *******************************************************************************/
11 * All classes are templated on the point type T
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.
22 * Generator adaptor. Can be used to generate random points
23 * distributed according to the PDF for analytic PDFs.
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").
32 * Adapter for generic algorithms. Evaluates the PDF and returns the value
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.
47 #include "EvtGenBase/EvtValError.hh"
48 #include "EvtGenBase/EvtPredGen.hh"
49 #include "EvtGenBase/EvtStreamInputIterator.hh"
50 #include "EvtGenBase/EvtPdfMax.hh"
51 #include "EvtGenBase/EvtMacros.hh"
52 #include "EvtGenBase/EvtRandom.hh"
54 template <class T> class EvtPdfPred;
55 template <class T> class EvtPdfGen;
57 template <class T> class EvtPdf {
61 EvtPdf(const EvtPdf& other) : _itg(other._itg) {}
63 virtual EvtPdf<T>* clone() const = 0;
65 double evaluate(const T& p) const {
66 if(p.isValid()) return pdf(p);
70 // Find PDF maximum. Points are sampled according to pc
72 EvtPdfMax<T> findMax(const EvtPdf<T>& pc, int N);
74 // Find generation efficiency.
76 EvtValError findGenEff(const EvtPdf<T>& pc, int N, int nFindMax);
78 // Analytic integration. Calls cascade down until an overridden
81 void setItg(EvtValError itg) {_itg = itg; }
83 EvtValError getItg() const {
84 if(!_itg.valueKnown()) _itg = compute_integral();
87 EvtValError getItg(int N) const {
88 if(!_itg.valueKnown()) _itg = compute_integral(N);
92 virtual EvtValError compute_integral() const
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) const { return compute_integral(); }
97 // Monte Carlo integration.
99 EvtValError compute_mc_integral(const EvtPdf<T>& pc, int N);
101 // Generation. Create predicate accept-reject generators.
102 // nMax iterations will be used to find the maximum of the accept-reject predicate
104 EvtPredGen<EvtPdfGen<T>,EvtPdfPred<T> > accRejGen(const EvtPdf<T>& pc, int nMax, double factor = 1.);
106 virtual T randomPoint();
110 virtual double pdf(const T&) const = 0;
111 mutable EvtValError _itg;
115 template <class T> class EvtPdfGen {
117 typedef T result_type;
119 EvtPdfGen() : _pdf(0) {}
120 EvtPdfGen(const EvtPdfGen<T>& other) :
121 _pdf(other._pdf ? other._pdf->clone() : 0)
123 EvtPdfGen(const EvtPdf<T>& pdf) :
126 ~EvtPdfGen() { delete _pdf;}
128 result_type operator()() {return _pdf->randomPoint();}
136 template <class T> class EvtPdfPred {
138 typedef T argument_type;
139 typedef bool result_type;
142 EvtPdfPred(const EvtPdf<T>& thePdf) : itsPdf(thePdf.clone()) {}
143 EvtPdfPred(const EvtPdfPred& other) : COPY_PTR(itsPdf), COPY_MEM(itsPdfMax) {}
144 ~EvtPdfPred() { delete itsPdf; }
146 result_type operator()(argument_type p)
149 assert(itsPdfMax.valueKnown());
151 double random = EvtRandom::Flat(0.,itsPdfMax.value());
152 return (random <= itsPdf->evaluate(p));
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,
161 itsPdfMax = EvtPdfMax<T>(p,itsPdf->evaluate(p)*factor);
163 while(!(it == end)) {
165 double val = itsPdf->evaluate(p)*factor;
166 if(val > itsPdfMax.value()) itsPdfMax = EvtPdfMax<T>(p,val);
173 EvtPdfMax<T> itsPdfMax;
177 template <class T> class EvtPdfUnary {
179 typedef double result_type;
180 typedef T argument_type;
183 EvtPdfUnary(const EvtPdf<T>& thePdf) : itsPdf(thePdf.clone()) {}
184 EvtPdfUnary(const EvtPdfUnary& other) : COPY_PTR(itsPdf) {}
185 ~EvtPdfUnary() { delete itsPdf; }
187 result_type operator()(argument_type p)
190 double ret = itsPdf->evaluate(p);
200 template <class T> class EvtPdfDiv : public EvtPdf<T> {
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())
207 EvtPdfDiv(const EvtPdfDiv<T>& other)
208 : EvtPdf<T>(other), COPY_PTR(itsNum), COPY_PTR(itsDen)
210 virtual ~EvtPdfDiv() { delete itsNum; delete itsDen; }
211 virtual EvtPdf<T>* clone() const
212 { return new EvtPdfDiv(*this); }
214 virtual double pdf(const T& p) const
216 double num = itsNum->evaluate(p);
217 double den = itsDen->evaluate(p);
224 EvtPdf<T>* itsNum; // numerator
225 EvtPdf<T>* itsDen; // denominator
230 EvtPdfMax<T> EvtPdf<T>::findMax(const EvtPdf<T>& pc, int N)
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();
241 EvtValError EvtPdf<T>::findGenEff(const EvtPdf<T>& pc, int N, int nFindMax)
243 assert(N > 0 || nFindMax > 0);
244 EvtPredGen<EvtPdfGen<T>,EvtPdfPred<T> > gen = accRejGen(pc,nFindMax);
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);
253 EvtValError EvtPdf<T>::compute_mc_integral(const EvtPdf<T>& pc, int N)
257 EvtValError otherItg = pc.getItg();
258 EvtPdfDiv<T> pdfdiv(*this,pc);
259 EvtPdfUnary<T> unary(pdfdiv);
261 EvtPdfGen<T> gen(pc);
262 EvtStreamInputIterator<T> begin = iter(gen,N);
263 EvtStreamInputIterator<T> end;
267 while(!(begin == end)) {
269 double value = pdfdiv.evaluate(*begin++);
276 double av = sum/((double) N);
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);
284 else x = EvtValError(av);
286 _itg = x * pc.getItg();
291 T EvtPdf<T>::randomPoint()
293 printf("Function defined for analytic PDFs only\n");
300 EvtPredGen<EvtPdfGen<T>,EvtPdfPred<T> >
301 EvtPdf<T>::accRejGen(const EvtPdf<T>& pc, int nMax, double factor)
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);