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