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