BOSS 8.0.0
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, dvoretsk@slac.stanford.edu, 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 "EvtPdf.hh"
19
20template <class T> class EvtPdfSum : public EvtPdf<T> {
21public:
23 EvtPdfSum( const EvtPdfSum<T>& other );
24 virtual ~EvtPdfSum();
25 virtual EvtPdf<T>* clone() const { return new EvtPdfSum( *this ); }
26
27 // Manipulate terms and coefficients
28
29 void addTerm( double c, const EvtPdf<T>& pdf ) {
30 assert( c >= 0. );
31 _c.push_back( c );
32 _term.push_back( pdf.clone() );
33 }
34
35 void addOwnedTerm( double c, EvtPdf<T>* pdf ) {
36 _c.push_back( c );
37 _term.push_back( pdf );
38 }
39
40 int nTerms() const { return _term.size(); } // number of terms
41
42 inline double c( int i ) const { return _c[i]; }
43 inline EvtPdf<T>* getPdf( int i ) const { return _term[i]; }
44
45 // Integrals
46
48 virtual EvtValError compute_integral( int N ) const;
49 virtual T randomPoint();
50
51protected:
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
59template <class T> EvtPdfSum<T>::EvtPdfSum( const EvtPdfSum<T>& other ) : EvtPdf<T>( other ) {
60 int i;
61 for ( i = 0; i < other.nTerms(); i++ )
62 {
63 _c.push_back( other._c[i] );
64 _term.push_back( other._term[i]->clone() );
65 }
66}
67
68template <class T> EvtPdfSum<T>::~EvtPdfSum() {
69 int i;
70 for ( i = 0; i < _c.size(); i++ ) delete _term[i];
71}
72
73template <class T> double EvtPdfSum<T>::pdf( const T& p ) const {
74 double ret = 0.;
75 unsigned i;
76 for ( i = 0; i < _c.size(); i++ ) ret += _c[i] * _term[i]->evaluate( p );
77 return ret;
78}
79
80/*
81 * Compute the sum integral by summing all term integrals.
82 */
83
84template <class T> EvtValError EvtPdfSum<T>::compute_integral() const {
85 int i;
86 EvtValError itg( 0.0, 0.0 );
87 for ( i = 0; i < nTerms(); i++ ) itg += _c[i] * _term[i]->getItg();
88 return itg;
89}
90
91template <class T> EvtValError EvtPdfSum<T>::compute_integral( int N ) const {
92 int i;
93 EvtValError itg( 0.0, 0.0 );
94 for ( i = 0; i < nTerms(); i++ ) itg += _c[i] * _term[i]->getItg( N );
95 return itg;
96}
97
98/*
99 * Sample points randomly according to the sum of PDFs. First throw a random number uniformly
100 * between zero and the value of the sum integral. Using this random number select one
101 * of the PDFs. The generate a random point according to that PDF.
102 */
103
104template <class T> T EvtPdfSum<T>::randomPoint() {
105 if ( !_itg.valueKnown() ) _itg = compute_integral();
106
107 double max = _itg.value();
108 double rnd = EvtRandom::Flat( 0, max );
109
110 double sum = 0.;
111 int i;
112 for ( i = 0; i < nTerms(); i++ )
113 {
114 double itg = _term[i]->getItg().value();
115 sum += _c[i] * itg;
116 if ( sum > rnd ) break;
117 }
118
119 return _term[i]->randomPoint();
120}
121
122#endif
#define max(a, b)
vector< double > _c
Definition EvtPdfSum.hh:54
EvtValError _itg
Definition EvtPdfSum.hh:56
virtual EvtValError compute_integral() const
Definition EvtPdfSum.hh:84
virtual EvtPdf< T > * clone() const
Definition EvtPdfSum.hh:25
virtual ~EvtPdfSum()
Definition EvtPdfSum.hh:68
int nTerms() const
Definition EvtPdfSum.hh:40
EvtPdf< T > * getPdf(int i) const
Definition EvtPdfSum.hh:43
void addTerm(double c, const EvtPdf< T > &pdf)
Definition EvtPdfSum.hh:29
virtual double pdf(const T &p) const
Definition EvtPdfSum.hh:73
double c(int i) const
Definition EvtPdfSum.hh:42
vector< EvtPdf< T > * > _term
Definition EvtPdfSum.hh:55
virtual T randomPoint()
Definition EvtPdfSum.hh:104
void addOwnedTerm(double c, EvtPdf< T > *pdf)
Definition EvtPdfSum.hh:35
double evaluate(const T &p) const
Definition EvtPdf.hh:64
EvtValError getItg() const
Definition EvtPdf.hh:82
EvtPdf()
Definition EvtPdf.hh:59
static double Flat()
Definition EvtRandom.cc:69