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