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