meerkat is hosted by Hepforge, IPPP Durham
Meerkat  v1r3
Multidimensional kernel density estimation package
SumDensity.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <vector>
3 #include <stdlib.h>
4 
5 #include "TMath.h"
6 
7 #include "AbsPhaseSpace.hh"
8 #include "AbsDensity.hh"
9 #include "SumDensity.hh"
10 
11 #include "Logger.hh"
12 
13 SumDensity::SumDensity(const char* pdfName,
14  AbsPhaseSpace* thePhaseSpace,
15  std::vector<AbsDensity*> &densityComponents,
16  std::vector<Double_t> &weights) : AbsDensity(pdfName) {
17 
18  init(thePhaseSpace, densityComponents, weights);
19 }
20 
21 SumDensity::SumDensity(const char* pdfName,
22  AbsPhaseSpace* thePhaseSpace,
23  AbsDensity* d1, AbsDensity* d2,
24  Double_t w1, Double_t w2) : AbsDensity(pdfName) {
25 
26  std::vector<AbsDensity*> densityVector(2);
27  densityVector[0] = d1;
28  densityVector[1] = d2;
29 
30  std::vector<Double_t> weightVector(2);
31  weightVector[0] = w1;
32  weightVector[1] = w2;
33 
34  init(thePhaseSpace, densityVector, weightVector);
35 }
36 
37 SumDensity::SumDensity(const char* pdfName,
38  AbsPhaseSpace* thePhaseSpace,
39  AbsDensity* d1, AbsDensity* d2, AbsDensity* d3,
40  Double_t w1, Double_t w2, Double_t w3) : AbsDensity(pdfName) {
41 
42  std::vector<AbsDensity*> densityVector(3);
43  densityVector[0] = d1;
44  densityVector[1] = d2;
45  densityVector[2] = d3;
46 
47  std::vector<Double_t> weightVector(3);
48  weightVector[0] = w1;
49  weightVector[1] = w2;
50  weightVector[2] = w3;
51 
52  init(thePhaseSpace, densityVector, weightVector);
53 }
54 
55 SumDensity::SumDensity(const char* pdfName,
56  AbsPhaseSpace* thePhaseSpace,
57  AbsDensity* d1, AbsDensity* d2, AbsDensity* d3, AbsDensity* d4,
58  Double_t w1, Double_t w2, Double_t w3, Double_t w4) : AbsDensity(pdfName) {
59 
60  std::vector<AbsDensity*> densityVector(4);
61  densityVector[0] = d1;
62  densityVector[1] = d2;
63  densityVector[2] = d3;
64  densityVector[3] = d4;
65 
66  std::vector<Double_t> weightVector(4);
67  weightVector[0] = w1;
68  weightVector[1] = w2;
69  weightVector[2] = w3;
70  weightVector[3] = w4;
71 
72  init(thePhaseSpace, densityVector, weightVector);
73 }
74 
76 
77 }
78 
79 void SumDensity::init(AbsPhaseSpace* thePhaseSpace,
80  std::vector<AbsDensity*> &densityComponents,
81  std::vector<Double_t> &weights) {
82 
83  m_phaseSpace = thePhaseSpace;
84  m_densityComponents = densityComponents;
85  m_weights = weights;
86 
87  if (densityComponents.size() != weights.size() ) {
88  Logger::print(2,"%20.20s ERROR: Dimensionalities of the phase space and weight vectors differ\n", m_name);
89  abort();
90  }
91 
92  Logger::print(0,"%20.20s INFO: Creating sum density of %d components\n", m_name, (UInt_t)densityComponents.size() );
93 
95 
96  std::vector<AbsDensity*>::iterator i;
97  for (i=densityComponents.begin(); i != densityComponents.end(); i++) {
98  UInt_t dim = (*i)->phaseSpace()->dimensionality();
99  if (dim != m_dim) {
100  Logger::print(2,"%20.20s ERROR: Dimensionalities of the sum phase space and its component %s differ\n", m_name, (*i)->name() );
101  abort();
102  }
103  }
104 
105 }
106 
107 Double_t SumDensity::density(std::vector<Double_t> &x) {
108 
109  if (x.size() != m_dim) {
110  Logger::print(2,"%20.20s ERROR: Dimensionality of vector (%d) does not correspond to phase space definition (%d)\n",
111  m_name, (UInt_t)x.size(), m_dim);
112  abort();
113  }
114 
115  Double_t sum = 0.;
116  UInt_t i;
117  for (i=0; i < m_densityComponents.size(); i++) {
118  AbsDensity* dens = m_densityComponents[i];
119  Double_t w = m_weights[i];
120  Double_t d = dens->density(x);
121  sum += w*d;
122 // Logger::print(0,"Component %d, density=%f, prod=%f\n", n, d, prod);
123  }
124  return sum;
125 }
Double_t density(std::vector< Double_t > &x)
Calculate PDF value at the given point.
Definition: SumDensity.cpp:107
Abstract class which defines probability density interface.
Definition: AbsDensity.hh:16
void print(int level, const char *format,...)
Definition: Logger.cpp:27
char m_name[256]
PDF name.
Definition: AbsDensity.hh:118
virtual ~SumDensity()
Destructor.
Definition: SumDensity.cpp:75
AbsPhaseSpace * m_phaseSpace
Reference to phase space.
Definition: SumDensity.hh:118
std::vector< Double_t > m_weights
Vector of density component weights.
Definition: SumDensity.hh:124
SumDensity(const char *pdfName, AbsPhaseSpace *thePhaseSpace, std::vector< AbsDensity * > &densityComponents, std::vector< Double_t > &weights)
Constructor of Sum density of an arbitrary number of density components.
Definition: SumDensity.cpp:13
void init(AbsPhaseSpace *thePhaseSpace, std::vector< AbsDensity * > &densityComponents, std::vector< Double_t > &weights)
Common initialise function used by all constructors.
Definition: SumDensity.cpp:79
Abstract class which defines phase space interface.
UInt_t m_dim
Cached dimensionality of the phase space.
Definition: SumDensity.hh:127
virtual UInt_t dimensionality()=0
Get dimensionality of phase space.
std::vector< AbsDensity * > m_densityComponents
Vector of density components.
Definition: SumDensity.hh:121
virtual Double_t density(std::vector< Double_t > &x)=0
Calculate PDF value at the given point.