BOSS 6.6.4.p01
BESIII Offline Software System
Loading...
Searching...
No Matches
EvtPdfSum.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: EvtPdfSum.hh,v 1.2 2007/11/20 08:36:27 pingrg Exp $
5 * Author: Alexei Dvoretskii, [email protected], 2001-2002
6 *
7 * Copyright (C) 2002 Caltech
8 *******************************************************************************/
9
10// Sum of PDF functions.
11
12#ifndef EVT_PDF_SUM_HH
13#define EVT_PDF_SUM_HH
14
15#include <stdio.h>
16#include <vector>
17using std::vector;
18#include "EvtGenBase/EvtPdf.hh"
19
20template <class T>
21class EvtPdfSum : public EvtPdf<T> {
22public:
23
25 EvtPdfSum(const EvtPdfSum<T>& other);
26 virtual ~EvtPdfSum();
27 virtual EvtPdf<T>* clone() const { return new EvtPdfSum(*this); }
28
29
30 // Manipulate terms and coefficients
31
32 void addTerm(double c,const EvtPdf<T>& pdf)
33 { assert(c >= 0.); _c.push_back(c); _term.push_back(pdf.clone()); }
34
35 void addOwnedTerm(double c, EvtPdf<T>* pdf)
36 { _c.push_back(c); _term.push_back(pdf); }
37
38 int nTerms() const { return _term.size(); } // number of terms
39
40 inline double c(int i) const { return _c[i]; }
41 inline EvtPdf<T>* getPdf(int i) const { return _term[i]; }
42
43
44 // Integrals
45
47 virtual EvtValError compute_integral(int N) const;
48 virtual T randomPoint();
49
50protected:
51
52 virtual double pdf(const T& p) const;
53
54 vector<double> _c; // coefficients
55 vector<EvtPdf<T>*> _term; // pointers to pdfs
56 mutable EvtValError _itg; // pingrg, 2007,Nov.19
57};
58
59
60template <class T>
62 : EvtPdf<T>(other)
63{
64 int i;
65 for(i = 0; i < other.nTerms(); i++) {
66 _c.push_back(other._c[i]);
67 _term.push_back(other._term[i]->clone());
68 }
69}
70
71template <class T>
73{
74 int i;
75 for(i = 0; i < _c.size(); i++) delete _term[i];
76}
77
78
79template <class T>
80double EvtPdfSum<T>::pdf(const T& p) const
81{
82 double ret = 0.;
83 unsigned i;
84 for(i=0; i < _c.size(); i++) ret += _c[i] * _term[i]->evaluate(p);
85 return ret;
86}
87
88/*
89 * Compute the sum integral by summing all term integrals.
90 */
91
92template <class T>
94{
95 int i;
96 EvtValError itg(0.0,0.0);
97 for(i=0;i<nTerms();i++) itg += _c[i]*_term[i]->getItg();
98 return itg;
99}
100
101template <class T>
103{
104 int i;
105 EvtValError itg(0.0,0.0);
106 for(i=0;i<nTerms();i++) itg += _c[i]*_term[i]->getItg(N);
107 return itg;
108}
109
110
111/*
112 * Sample points randomly according to the sum of PDFs. First throw a random number uniformly
113 * between zero and the value of the sum integral. Using this random number select one
114 * of the PDFs. The generate a random point according to that PDF.
115 */
116
117template <class T>
119{
120 if(!_itg.valueKnown()) _itg = compute_integral();
121
122 double max = _itg.value();
123 double rnd = EvtRandom::Flat(0,max);
124
125 double sum = 0.;
126 int i;
127 for(i = 0; i < nTerms(); i++) {
128 double itg = _term[i]->getItg().value();
129 sum += _c[i] * itg;
130 if(sum > rnd) break;
131 }
132
133 return _term[i]->randomPoint();
134}
135
136#endif
137
138
vector< double > _c
Definition: EvtPdfSum.hh:54
EvtValError _itg
Definition: EvtPdfSum.hh:56
virtual EvtValError compute_integral() const
Definition: EvtPdfSum.hh:93
virtual EvtPdf< T > * clone() const
Definition: EvtPdfSum.hh:27
virtual ~EvtPdfSum()
Definition: EvtPdfSum.hh:72
int nTerms() const
Definition: EvtPdfSum.hh:38
EvtPdf< T > * getPdf(int i) const
Definition: EvtPdfSum.hh:41
void addTerm(double c, const EvtPdf< T > &pdf)
Definition: EvtPdfSum.hh:32
virtual double pdf(const T &p) const
Definition: EvtPdfSum.hh:80
double c(int i) const
Definition: EvtPdfSum.hh:40
virtual EvtValError compute_integral(int N) const
Definition: EvtPdfSum.hh:102
vector< EvtPdf< T > * > _term
Definition: EvtPdfSum.hh:55
virtual T randomPoint()
Definition: EvtPdfSum.hh:118
void addOwnedTerm(double c, EvtPdf< T > *pdf)
Definition: EvtPdfSum.hh:35
EvtPdfSum(const EvtPdfSum< T > &other)
Definition: EvtPdfSum.hh:61
Definition: EvtPdf.hh:57
static double Flat()
Definition: EvtRandom.cc:73