Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
PDEFoamKernelLinN.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: PDEFoamKernelLinN *
8 * *
9 * *
10 * Description: *
11 * Implementation of linear neighbors 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::PDEFoamKernelLinN
29\ingroup TMVA
30This PDEFoam kernel estimates a cell value for a given event by
31weighting with cell values of the nearest neighbor cells.
32*/
33
35
36#include "TMVA/PDEFoam.h"
37#include "TMVA/MsgLogger.h"
39#include "TMVA/Types.h"
40
41#include "Rtypes.h"
42
43#include <vector>
44
46
47////////////////////////////////////////////////////////////////////////////////
48/// Default constructor for streamer
49
52{
53}
54
55////////////////////////////////////////////////////////////////////////////////
56/// Copy constructor
57
59 : PDEFoamKernelBase(other)
60{
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Linear neighbors kernel estimator. It returns the cell value
65/// 'cv', corresponding to the event vector 'txvec' (in foam
66/// coordinates) linear weighted by the cell values of the neighbor
67/// cells.
68///
69/// Parameters:
70///
71/// - foam - the pdefoam to search in
72///
73/// - txvec - event vector in foam coordinates [0,1]
74///
75/// - cv - cell value to estimate
76
77Float_t TMVA::PDEFoamKernelLinN::Estimate(PDEFoam *foam, std::vector<Float_t> &txvec, ECellValue cv)
78{
79 if (foam == NULL)
80 Log() << kFATAL << "<PDEFoamKernelLinN::Estimate>: PDEFoam not set!" << Endl;
81
82 return WeightLinNeighbors(foam, txvec, cv, kTRUE);
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// Returns the cell value, corresponding to 'txvec' (foam
87/// coordinates [0,1]), weighted by the neighbor cells via a linear
88/// function.
89///
90/// Parameters:
91/// - foam - the foam to search in
92///
93/// - txvec - event vector, transformed into foam coordinates [0,1]
94///
95/// - cv - cell value to be weighted
96///
97/// - treatEmptyCells - if this option is set to false (default),
98/// it is not checked, wether the cell value or neighbor cell
99/// values are undefined (using foam->CellValueIsUndefined()).
100/// If this option is set to true, than only non-empty neighbor
101/// cells are taken into account for weighting. If the cell
102/// value of the cell, which contains txvec, is empty, than its
103/// value is estimated by the average value of the non-empty
104/// neighbor cells (using GetAverageNeighborsValue()).
105
106Float_t TMVA::PDEFoamKernelLinN::WeightLinNeighbors(PDEFoam *foam, std::vector<Float_t> &txvec, ECellValue cv, Bool_t treatEmptyCells)
107{
108 Float_t result = 0.;
109 UInt_t norm = 0;
110 const Float_t xoffset = 1.e-6;
111
112 if (txvec.size() != UInt_t(foam->GetTotDim()))
113 Log() << kFATAL << "Wrong dimension of event variable!" << Endl;
114
115 // find cell, which contains txvec
116 PDEFoamCell *cell = foam->FindCell(txvec);
117 PDEFoamVect cellSize(foam->GetTotDim());
118 PDEFoamVect cellPosi(foam->GetTotDim());
119 cell->GetHcub(cellPosi, cellSize);
120 // calc value of cell, which contains txvec
121 Float_t cellval = 0;
122 if (!(treatEmptyCells && foam->CellValueIsUndefined(cell)))
123 // cell is not empty -> get cell value
124 cellval = foam->GetCellValue(cell, cv);
125 else
126 // cell is empty -> get average value of non-empty neighbor
127 // cells
128 cellval = GetAverageNeighborsValue(foam, txvec, cv);
129
130 // loop over all dimensions to find neighbor cells
131 for (Int_t dim = 0; dim < foam->GetTotDim(); dim++) {
132 std::vector<Float_t> ntxvec(txvec);
133 Float_t mindist;
134 PDEFoamCell *mindistcell = 0; // cell with minimal distance to txvec
135 // calc minimal distance to neighbor cell
136 mindist = (txvec[dim] - cellPosi[dim]) / cellSize[dim];
137 if (mindist < 0.5) { // left neighbour
138 ntxvec[dim] = cellPosi[dim] - xoffset;
139 mindistcell = foam->FindCell(ntxvec); // left neighbor cell
140 } else { // right neighbour
141 mindist = 1 - mindist;
142 ntxvec[dim] = cellPosi[dim] + cellSize[dim] + xoffset;
143 mindistcell = foam->FindCell(ntxvec); // right neighbor cell
144 }
145 // get cell value of cell, which contains ntxvec
146 Float_t mindistcellval = foam->GetCellValue(mindistcell, cv);
147 // if treatment of empty neighbor cells is deactivated, do
148 // normal weighting
149 if (!(treatEmptyCells && foam->CellValueIsUndefined(mindistcell))) {
150 result += cellval * (0.5 + mindist);
151 result += mindistcellval * (0.5 - mindist);
152 norm++;
153 }
154 }
155 if (norm == 0) return cellval; // all nearest neighbors were empty
156 else return result / norm; // normalisation
157}
158
159////////////////////////////////////////////////////////////////////////////////
160/// This function returns the average value 'cv' of only nearest
161/// neighbor cells. It is used in cases when a cell value is
162/// undefined and the cell value shall be estimated by the
163/// (well-defined) cell values of the neighbor cells.
164///
165/// Parameters:
166/// - foam - the foam to search in
167/// - txvec - event vector, transformed into foam coordinates [0, 1]
168/// - cv - cell value, see definition of ECellValue
169
171 std::vector<Float_t> &txvec,
172 ECellValue cv)
173{
174 const Float_t xoffset = 1.e-6;
175 Float_t norm = 0; // normalisation
176 Float_t result = 0; // return value
177
178 PDEFoamCell *cell = foam->FindCell(txvec); // find corresponding cell
179 PDEFoamVect cellSize(foam->GetTotDim());
180 PDEFoamVect cellPosi(foam->GetTotDim());
181 cell->GetHcub(cellPosi, cellSize); // get cell coordinates
182
183 // loop over all dimensions and find neighbor cells
184 for (Int_t dim = 0; dim < foam->GetTotDim(); dim++) {
185 std::vector<Float_t> ntxvec(txvec);
186 PDEFoamCell* left_cell = 0; // left cell
187 PDEFoamCell* right_cell = 0; // right cell
188
189 // get left cell
190 ntxvec[dim] = cellPosi[dim] - xoffset;
191 left_cell = foam->FindCell(ntxvec);
192 if (!foam->CellValueIsUndefined(left_cell)) {
193 // if left cell is not empty, take its value
194 result += foam->GetCellValue(left_cell, cv);
195 norm++;
196 }
197 // get right cell
198 ntxvec[dim] = cellPosi[dim] + cellSize[dim] + xoffset;
199 right_cell = foam->FindCell(ntxvec);
200 if (!foam->CellValueIsUndefined(right_cell)) {
201 // if right cell is not empty, take its value
202 result += foam->GetCellValue(right_cell, cv);
203 norm++;
204 }
205 }
206 if (norm > 0) result /= norm; // calc average target
207 else result = 0; // return null if all neighbors are empty
208
209 return result;
210}
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define ClassImp(name)
Definition Rtypes.h:382
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
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 with cell values of the nea...
virtual Float_t Estimate(PDEFoam *, std::vector< Float_t > &, ECellValue)
Linear neighbors kernel estimator.
Float_t WeightLinNeighbors(PDEFoam *, std::vector< Float_t > &, ECellValue, Bool_t)
Returns the cell value, corresponding to 'txvec' (foam coordinates [0,1]), weighted by the neighbor c...
Float_t GetAverageNeighborsValue(PDEFoam *, std::vector< Float_t > &, ECellValue)
This function returns the average value 'cv' of only nearest neighbor cells.
PDEFoamKernelLinN()
Default constructor for streamer.
Implementation of PDEFoam.
Definition PDEFoam.h:79
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:1015
virtual Bool_t CellValueIsUndefined(PDEFoamCell *)
Returns true, if the value of the given cell is undefined.
Definition PDEFoam.cxx:989
Int_t GetTotDim() const
Definition PDEFoam.h:197
PDEFoamCell * FindCell(const std::vector< Float_t > &) const
Find cell that contains 'xvec' (in foam coordinates [0,1]).
Definition PDEFoam.cxx:1079
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148