meerkat is hosted by Hepforge, IPPP Durham
Meerkat  v1r3
Multidimensional kernel density estimation package
FactorisedDensity.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 "FactorisedDensity.hh"
10 
11 #include "Logger.hh"
12 
14  AbsPhaseSpace* thePhaseSpace,
15  std::vector<AbsDensity*> &densityComponents) : AbsDensity(pdfName) {
16 
17  init(thePhaseSpace, densityComponents);
18 }
19 
21  AbsPhaseSpace* thePhaseSpace,
22  AbsDensity* d1, AbsDensity* d2,
23  AbsDensity* d3, AbsDensity* d4) : AbsDensity(pdfName) {
24 
25  std::vector<AbsDensity*> densityVector(2);
26  densityVector[0] = d1;
27  densityVector[1] = d2;
28  if (d3) {
29  densityVector.push_back(d3);
30  }
31  if (d4) {
32  densityVector.push_back(d4);
33  }
34  init(thePhaseSpace, densityVector);
35 }
36 
38 
39 }
40 
42  std::vector<AbsDensity*> &densityComponents) {
43 
44  m_phaseSpace = thePhaseSpace;
45  m_densityComponents = densityComponents;
46 
47  Logger::print(0,"%20.20s INFO: Creating factorised density of %d components\n", m_name, (UInt_t)densityComponents.size() );
48 
49  UInt_t sumDim = 0;
50  std::vector<AbsDensity*>::iterator i;
51  for (i=densityComponents.begin(); i != densityComponents.end(); i++) {
52  sumDim += (*i)->phaseSpace()->dimensionality();
53  }
54 
56  if (sumDim != m_dim) {
57  Logger::print(2,"%20.20s ERROR: Dimensionalities of the factorised phase space and sum of its components differ\n", m_name);
58  abort();
59  }
60 
61 }
62 
63 Double_t FactorisedDensity::density(std::vector<Double_t> &x) {
64 
65  if (x.size() != m_dim) {
66  Logger::print(2,"%20.20s ERROR: Dimensionality of vector (%d) does not correspond to phase space definition (%d)\n",
67  m_name, (UInt_t)x.size(), m_dim);
68  abort();
69  }
70 
71  Double_t prod = 1.;
72  UInt_t n=0;
73  std::vector<AbsDensity*>::iterator i;
74  for (i=m_densityComponents.begin(); i != m_densityComponents.end(); i++) {
75  UInt_t dim = (*i)->phaseSpace()->dimensionality();
76  std::vector<Double_t> subset(dim);
77  UInt_t j;
78  for (j=0; j<dim; j++) {
79  subset[j] = x[n];
80  n++;
81  }
82  Double_t d = (*i)->density(subset);
83 
84  prod *= d;
85 
86 // Logger::print(0,"Component %d, density=%f, prod=%f\n", n, d, prod);
87  }
88  return prod;
89 
90 }
Abstract class which defines probability density interface.
Definition: AbsDensity.hh:16
FactorisedDensity(const char *pdfName, AbsPhaseSpace *thePhaseSpace, std::vector< AbsDensity * > &densityComponents)
Constructor of factorised density of an arbitrary number of density components.
void print(int level, const char *format,...)
Definition: Logger.cpp:27
char m_name[256]
PDF name.
Definition: AbsDensity.hh:118
void init(AbsPhaseSpace *thePhaseSpace, std::vector< AbsDensity * > &densityComponents)
Common initialise function used by all constructors.
Abstract class which defines phase space interface.
virtual UInt_t dimensionality()=0
Get dimensionality of phase space.
UInt_t m_dim
Cached dimensionality of the phase space.
std::vector< AbsDensity * > m_densityComponents
Vector of density components.
AbsPhaseSpace * m_phaseSpace
Reference to phase space.
virtual ~FactorisedDensity()
Destructor.
Double_t density(std::vector< Double_t > &x)
Calculate PDF value at the given point.