Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 * *
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 * (see tmva/doc/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
45
46////////////////////////////////////////////////////////////////////////////////
47/// Default constructor for streamer
48
54
55////////////////////////////////////////////////////////////////////////////////
56/// Copy constructor
57
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
78Float_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 corresponding 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///
171/// \f[
172/// e^(\frac{-(\frac{d}{\sigma})^2}{2})
173/// \f]
174///
175/// 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/// - \f$ sigma = \frac{1}{VolFrac} \f$
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));
208
209 // weight with Gaus
210 return TMath::Gaus(distance, 0, fSigma, kFALSE);
211}
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
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.
Float_t Estimate(PDEFoam *, std::vector< Float_t > &, ECellValue) override
Gaussian kernel estimator.
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'.
Implementation of PDEFoam.
Definition PDEFoam.h:79
const Double_t sigma
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculates a gaussian function with mean and sigma.
Definition TMath.cxx:471
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:673