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