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