Logo ROOT  
Reference Guide
PDEFoamKernelGauss.cxx
Go to the documentation of this file.
1// @(#)root/tmva $Id$
2// Author: Dominik Dannheim, Alexander Voigt
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Classes: PDEFoamKernelGauss *
8 * Web : http://tmva.sourceforge.net *
9 * *
10 * Description: *
11 * Implementation of gauss PDEFoam kernel *
12 * *
13 * Authors (alphabetical): *
14 * S. Jadach - Institute of Nuclear Physics, Cracow, Poland *
15 * Tancredi Carli - CERN, Switzerland *
16 * Dominik Dannheim - CERN, Switzerland *
17 * Alexander Voigt - TU Dresden, Germany *
18 * *
19 * Copyright (c) 2008, 2010: *
20 * CERN, Switzerland *
21 * MPI-K Heidelberg, Germany *
22 * *
23 * Redistribution and use in source and binary forms, with or without *
24 * modification, are permitted according to the terms listed in LICENSE *
25 * (http://tmva.sourceforge.net/LICENSE) *
26 **********************************************************************************/
27
28/*! \class TMVA::PDEFoamKernelGauss
29\ingroup TMVA
30This PDEFoam kernel estimates a cell value for a given event by
31weighting all cell values with a gauss function.
32*/
33
35
36#include "TMVA/MsgLogger.h"
37#include "TMVA/PDEFoam.h"
39#include "TMVA/Types.h"
40
41#include "TMath.h"
42
43#include "Rtypes.h"
44
46
47////////////////////////////////////////////////////////////////////////////////
48/// Default constructor for streamer
49
52 , fSigma(sigma)
53{
54}
55
56////////////////////////////////////////////////////////////////////////////////
57/// Copy constructor
58
60 : PDEFoamKernelBase(other)
61 , fSigma(other.fSigma)
62{
63}
64
65////////////////////////////////////////////////////////////////////////////////
66/// Gaussian kernel estimator. It returns the cell value 'cv',
67/// corresponding to the event vector 'txvec' (in foam coordinates)
68/// weighted by the cell values of all other cells, where the weight
69/// is a gaussian function.
70///
71/// Parameters:
72///
73/// - foam - the pdefoam to search in
74///
75/// - txvec - event vector in foam coordinates [0,1]
76///
77/// - cv - cell value to estimate
78
79Float_t TMVA::PDEFoamKernelGauss::Estimate(PDEFoam *foam, std::vector<Float_t> &txvec, ECellValue cv)
80{
81 if (foam == NULL)
82 Log() << kFATAL << "<PDEFoamKernelGauss::Estimate>: PDEFoam not set!" << Endl;
83
84 Float_t result = 0, norm = 0;
85
86 for (Long_t iCell = 0; iCell <= foam->fLastCe; iCell++) {
87 if (!(foam->fCells[iCell]->GetStat())) continue;
88
89 // calc cell density
90 Float_t cell_val = 0;
91 if (!foam->CellValueIsUndefined(foam->fCells[iCell]))
92 // cell is not empty
93 cell_val = foam->GetCellValue(foam->fCells[iCell], cv);
94 else
95 // cell is empty -> calc average target of neighbor cells
96 cell_val = GetAverageNeighborsValue(foam, txvec, cv);
97
98 // calculate gaussian weight between txvec and fCells[iCell]
99 Float_t gau = WeightGaus(foam, foam->fCells[iCell], txvec);
100
101 result += gau * cell_val;
102 norm += gau;
103 }
104
105 return (norm != 0 ? result / norm : 0);
106}
107
108////////////////////////////////////////////////////////////////////////////////
109/// This function returns the average value 'cv' of only nearest
110/// neighbor cells. It is used in cases when a cell value is
111/// undefined and the cell value shall be estimated by the
112/// (well-defined) cell values of the neighbor cells.
113///
114/// Parameters:
115/// - foam - the foam to search in
116/// - txvec - event vector, transformed into foam coordinates [0, 1]
117/// - cv - cell value, see definition of ECellValue
118
120 std::vector<Float_t> &txvec,
121 ECellValue cv)
122{
123 const Float_t xoffset = 1.e-6;
124 Float_t norm = 0; // normalisation
125 Float_t result = 0; // return value
126
127 PDEFoamCell *cell = foam->FindCell(txvec); // find corresponding cell
128 PDEFoamVect cellSize(foam->GetTotDim());
129 PDEFoamVect cellPosi(foam->GetTotDim());
130 cell->GetHcub(cellPosi, cellSize); // get cell coordinates
131
132 // loop over all dimensions and find neighbor cells
133 for (Int_t dim = 0; dim < foam->GetTotDim(); dim++) {
134 std::vector<Float_t> ntxvec(txvec);
135 PDEFoamCell* left_cell = 0; // left cell
136 PDEFoamCell* right_cell = 0; // right cell
137
138 // get left cell
139 ntxvec[dim] = cellPosi[dim] - xoffset;
140 left_cell = foam->FindCell(ntxvec);
141 if (!foam->CellValueIsUndefined(left_cell)) {
142 // if left cell is not empty, take its value
143 result += foam->GetCellValue(left_cell, cv);
144 norm++;
145 }
146 // get right cell
147 ntxvec[dim] = cellPosi[dim] + cellSize[dim] + xoffset;
148 right_cell = foam->FindCell(ntxvec);
149 if (!foam->CellValueIsUndefined(right_cell)) {
150 // if right cell is not empty, take its value
151 result += foam->GetCellValue(right_cell, cv);
152 norm++;
153 }
154 }
155 if (norm > 0) result /= norm; // calc average target
156 else result = 0; // return null if all neighbors are empty
157
158 return result;
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Returns the gauss weight between the 'cell' and a given coordinate 'txvec'.
163///
164/// Parameters:
165/// - cell - the cell
166///
167/// - txvec - the transformed event variables (in [0,1]) (coordinates <0 are
168/// set to 0, >1 are set to 1)
169///
170/// Returns:
171///
172/// \f[
173/// e^(\frac{-(\frac{d}{\sigma})^2}{2})
174/// \f]
175///
176/// where:
177/// - d - is the euclidean distance between 'txvec' and the point of the 'cell'
178/// which is most close to 'txvec' (in order to avoid artefacts because of the
179/// form of the cells).
180/// - \f$ sigma = \frac{1}{VolFrac} \f$
181
183 std::vector<Float_t> &txvec)
184{
185 // get cell coordinates
186 PDEFoamVect cellSize(foam->GetTotDim());
187 PDEFoamVect cellPosi(foam->GetTotDim());
188 cell->GetHcub(cellPosi, cellSize);
189
190 // calc position of nearest edge of cell
191 std::vector<Float_t> cell_center;
192 cell_center.reserve(foam->GetTotDim());
193 for (Int_t i = 0; i < foam->GetTotDim(); ++i) {
194 if (txvec[i] < 0.) txvec[i] = 0.;
195 if (txvec[i] > 1.) txvec[i] = 1.;
196 //cell_center.push_back(cellPosi[i] + (0.5*cellSize[i]));
197 if (cellPosi[i] > txvec.at(i))
198 cell_center.push_back(cellPosi[i]);
199 else if (cellPosi[i] + cellSize[i] < txvec.at(i))
200 cell_center.push_back(cellPosi[i] + cellSize[i]);
201 else
202 cell_center.push_back(txvec.at(i));
203 }
204
205 Float_t distance = 0; // euclidean distance for weighting
206 for (Int_t i = 0; i < foam->GetTotDim(); ++i)
207 distance += Sqr(txvec.at(i) - cell_center.at(i));
208 distance = TMath::Sqrt(distance);
209
210 // weight with Gaus
211 return TMath::Gaus(distance, 0, fSigma, kFALSE);
212}
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
long Long_t
Definition: RtypesCore.h:50
float Float_t
Definition: RtypesCore.h:53
#define ClassImp(name)
Definition: Rtypes.h:365
Int_t GetStat() const
Definition: PDEFoamCell.h:91
void GetHcub(PDEFoamVect &, PDEFoamVect &) const
Provides size and position of the cell These parameter are calculated by analyzing information in all...
This class is the abstract kernel interface for PDEFoam.
This PDEFoam kernel estimates a cell value for a given event by weighting all cell values with a gaus...
Float_t GetAverageNeighborsValue(PDEFoam *, std::vector< Float_t > &, ECellValue)
This function returns the average value 'cv' of only nearest neighbor cells.
PDEFoamKernelGauss(Float_t sigma)
Default constructor for streamer.
Float_t WeightGaus(PDEFoam *, PDEFoamCell *, std::vector< Float_t > &)
Returns the gauss weight between the 'cell' and a given coordinate 'txvec'.
virtual Float_t Estimate(PDEFoam *, std::vector< Float_t > &, ECellValue)
Gaussian kernel estimator.
Implementation of PDEFoam.
Definition: PDEFoam.h:77
virtual Float_t GetCellValue(const std::vector< Float_t > &xvec, ECellValue cv, PDEFoamKernelBase *)
This function finds the cell, which corresponds to the given untransformed event vector 'xvec' and re...
Definition: PDEFoam.cxx:1017
virtual Bool_t CellValueIsUndefined(PDEFoamCell *)
Returns true, if the value of the given cell is undefined.
Definition: PDEFoam.cxx:991
Int_t GetTotDim() const
Definition: PDEFoam.h:195
PDEFoamCell * FindCell(const std::vector< Float_t > &) const
Find cell that contains 'xvec' (in foam coordinates [0,1]).
Definition: PDEFoam.cxx:1081
PDEFoamCell ** fCells
Definition: PDEFoam.h:94
Int_t fLastCe
Definition: PDEFoam.h:93
const Double_t sigma
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculate a gaussian function with mean and sigma.
Definition: TMath.cxx:448
Double_t Log(Double_t x)
Definition: TMath.h:750
Double_t Sqrt(Double_t x)
Definition: TMath.h:681