ROOT  6.06/09
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 //_____________________________________________________________________
29 //
30 // PDEFoamKernelGauss
31 //
32 // This PDEFoam kernel estimates a cell value for a given event by
33 // weighting all cell values with a gauss function.
34 // _____________________________________________________________________
35 
36 #ifndef ROOT_TMath
37 #include "TMath.h"
38 #endif
39 
40 #ifndef ROOT_TMVA_PDEFoamKernelGauss
42 #endif
43 
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// Default constructor for streamer
48 
49 TMVA::PDEFoamKernelGauss::PDEFoamKernelGauss(Float_t sigma)
50  : PDEFoamKernelBase()
51  , fSigma(sigma)
52 {
53 }
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// Copy constructor
57 
59  : PDEFoamKernelBase(other)
60  , fSigma(other.fSigma)
61 {
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Gaussian kernel estimator. It returns the cell value 'cv',
66 /// corresponding to the event vector 'txvec' (in foam coordinates)
67 /// weighted by the cell values of all other cells, where the weight
68 /// is a gaussian function.
69 ///
70 /// Parameters:
71 ///
72 /// - foam - the pdefoam to search in
73 ///
74 /// - txvec - event vector in foam coordinates [0,1]
75 ///
76 /// - cv - cell value to estimate
77 
78 Float_t TMVA::PDEFoamKernelGauss::Estimate(PDEFoam *foam, std::vector<Float_t> &txvec, ECellValue cv)
79 {
80  if (foam == NULL)
81  Log() << kFATAL << "<PDEFoamKernelGauss::Estimate>: PDEFoam not set!" << Endl;
82 
83  Float_t result = 0, norm = 0;
84 
85  for (Long_t iCell = 0; iCell <= foam->fLastCe; iCell++) {
86  if (!(foam->fCells[iCell]->GetStat())) continue;
87 
88  // calc cell density
89  Float_t cell_val = 0;
90  if (!foam->CellValueIsUndefined(foam->fCells[iCell]))
91  // cell is not empty
92  cell_val = foam->GetCellValue(foam->fCells[iCell], cv);
93  else
94  // cell is empty -> calc average target of neighbor cells
95  cell_val = GetAverageNeighborsValue(foam, txvec, cv);
96 
97  // calculate gaussian weight between txvec and fCells[iCell]
98  Float_t gau = WeightGaus(foam, foam->fCells[iCell], txvec);
99 
100  result += gau * cell_val;
101  norm += gau;
102  }
103 
104  return (norm != 0 ? result / norm : 0);
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// This function returns the average value 'cv' of only nearest
109 /// neighbor cells. It is used in cases when a cell value is
110 /// undefined and the cell value shall be estimated by the
111 /// (well-defined) cell values of the neighbor cells.
112 ///
113 /// Parameters:
114 /// - foam - the foam to search in
115 /// - txvec - event vector, transformed into foam coordinates [0, 1]
116 /// - cv - cell value, see definition of ECellValue
117 
119  std::vector<Float_t> &txvec,
120  ECellValue cv)
121 {
122  const Float_t xoffset = 1.e-6;
123  Float_t norm = 0; // normalisation
124  Float_t result = 0; // return value
125 
126  PDEFoamCell *cell = foam->FindCell(txvec); // find cooresponding cell
127  PDEFoamVect cellSize(foam->GetTotDim());
128  PDEFoamVect cellPosi(foam->GetTotDim());
129  cell->GetHcub(cellPosi, cellSize); // get cell coordinates
130 
131  // loop over all dimensions and find neighbor cells
132  for (Int_t dim = 0; dim < foam->GetTotDim(); dim++) {
133  std::vector<Float_t> ntxvec(txvec);
134  PDEFoamCell* left_cell = 0; // left cell
135  PDEFoamCell* right_cell = 0; // right cell
136 
137  // get left cell
138  ntxvec[dim] = cellPosi[dim] - xoffset;
139  left_cell = foam->FindCell(ntxvec);
140  if (!foam->CellValueIsUndefined(left_cell)) {
141  // if left cell is not empty, take its value
142  result += foam->GetCellValue(left_cell, cv);
143  norm++;
144  }
145  // get right cell
146  ntxvec[dim] = cellPosi[dim] + cellSize[dim] + xoffset;
147  right_cell = foam->FindCell(ntxvec);
148  if (!foam->CellValueIsUndefined(right_cell)) {
149  // if right cell is not empty, take its value
150  result += foam->GetCellValue(right_cell, cv);
151  norm++;
152  }
153  }
154  if (norm > 0) result /= norm; // calc average target
155  else result = 0; // return null if all neighbors are empty
156 
157  return result;
158 }
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 /// Returns the gauss weight between the 'cell' and a given coordinate 'txvec'.
162 ///
163 /// Parameters:
164 /// - cell - the cell
165 ///
166 /// - txvec - the transformed event variables (in [0,1]) (coordinates <0 are
167 /// set to 0, >1 are set to 1)
168 ///
169 /// Returns:
170 /// exp(-(d/sigma)^2/2), where
171 /// - d - is the euclidean distance between 'txvec' and the point of the 'cell'
172 /// which is most close to 'txvec' (in order to avoid artefacts because of the
173 /// form of the cells).
174 /// - sigma = 1/VolFrac
175 
177  std::vector<Float_t> &txvec)
178 {
179  // get cell coordinates
180  PDEFoamVect cellSize(foam->GetTotDim());
181  PDEFoamVect cellPosi(foam->GetTotDim());
182  cell->GetHcub(cellPosi, cellSize);
183 
184  // calc position of nearest edge of cell
185  std::vector<Float_t> cell_center;
186  cell_center.reserve(foam->GetTotDim());
187  for (Int_t i = 0; i < foam->GetTotDim(); ++i) {
188  if (txvec[i] < 0.) txvec[i] = 0.;
189  if (txvec[i] > 1.) txvec[i] = 1.;
190  //cell_center.push_back(cellPosi[i] + (0.5*cellSize[i]));
191  if (cellPosi[i] > txvec.at(i))
192  cell_center.push_back(cellPosi[i]);
193  else if (cellPosi[i] + cellSize[i] < txvec.at(i))
194  cell_center.push_back(cellPosi[i] + cellSize[i]);
195  else
196  cell_center.push_back(txvec.at(i));
197  }
198 
199  Float_t distance = 0; // euclidean distance for weighting
200  for (Int_t i = 0; i < foam->GetTotDim(); ++i)
201  distance += Sqr(txvec.at(i) - cell_center.at(i));
202  distance = TMath::Sqrt(distance);
203 
204  // weight with Gaus
205  return TMath::Gaus(distance, 0, fSigma, kFALSE);
206 }
ClassImp(TMVA::PDEFoamKernelGauss) TMVA
Default constructor for streamer.
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
float Float_t
Definition: RtypesCore.h:53
Double_t distance(const TPoint2 &p1, const TPoint2 &p2)
Definition: CsgOps.cxx:467
int Int_t
Definition: RtypesCore.h:41
Float_t WeightGaus(PDEFoam *, PDEFoamCell *, std::vector< Float_t > &)
Returns the gauss weight between the 'cell' and a given coordinate 'txvec'.
const Bool_t kFALSE
Definition: Rtypes.h:92
Int_t GetStat() const
Definition: PDEFoamCell.h:97
virtual Bool_t CellValueIsUndefined(PDEFoamCell *)
Returns true, if the value of the given cell is undefined.
Definition: PDEFoam.cxx:1000
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)
Int_t GetTotDim() const
Definition: PDEFoam.h:222
virtual Float_t Estimate(PDEFoam *, std::vector< Float_t > &, ECellValue)
Gaussian kernel estimator.
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:453
long Long_t
Definition: RtypesCore.h:50
ECellValue
Definition: PDEFoam.h:85
PDEFoamCell ** fCells
Definition: PDEFoam.h:121
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:1026
Abstract ClassifierFactory template that handles arbitrary types.
#define NULL
Definition: Rtypes.h:82
Int_t fLastCe
Definition: PDEFoam.h:120
PDEFoamCell * FindCell(const std::vector< Float_t > &) const
Find cell that contains 'xvec' (in foam coordinates [0,1]).
Definition: PDEFoam.cxx:1090
double result[121]
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
void GetHcub(PDEFoamVect &, PDEFoamVect &) const
Provides size and position of the cell These parameter are calculated by analyzing information in all...
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
Definition: math.cpp:60