Loading [MathJax]/extensions/tex2jax.js
ROOT  6.06/09
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PDEFoamMultiTarget.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Tancredi Carli, Dominik Dannheim, Alexander Voigt
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Classes: PDEFoamMultiTarget *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation. *
12  * *
13  * Authors (alphabetical): *
14  * Tancredi Carli - CERN, Switzerland *
15  * Dominik Dannheim - CERN, Switzerland *
16  * S. Jadach - Institute of Nuclear Physics, Cracow, Poland *
17  * Alexander Voigt - TU Dresden, Germany *
18  * Peter Speckmayer - CERN, Switzerland *
19  * *
20  * Copyright (c) 2008, 2010: *
21  * CERN, Switzerland *
22  * MPI-K Heidelberg, Germany *
23  * *
24  * Redistribution and use in source and binary forms, with or without *
25  * modification, are permitted according to the terms listed in LICENSE *
26  * (http://tmva.sourceforge.net/LICENSE) *
27  **********************************************************************************/
28 
29 //_____________________________________________________________________
30 //
31 // PDEFoamMultiTarget
32 //
33 // This PDEFoam variant is used to estimate multiple targets by
34 // creating an event density foam (PDEFoamEvent), which has dimension:
35 //
36 // dimension = number of variables + number targets
37 //
38 // This PDEFoam variant stores in every cell the sum of event weights
39 // and the sum of the squared event weights. During evaluation for a
40 // given event, which has only variables and no targets (number of
41 // event variables is smaller than the foam dimension), the targets
42 // are estimated by finding all cells, which correspond to this event
43 // and calculate the Mean (or Mpv, depending on the ETargetSelection)
44 // cell center weighted by the event density in the cell.
45 //
46 // This PDEFoam variant should be booked together with the
47 // PDEFoamEventDensity density estimator, which returns the event
48 // weight density at a given phase space point during the foam
49 // build-up.
50 //
51 //_____________________________________________________________________
52 
53 #ifndef ROOT_TMVA_PDEFoamMultiTarget
55 #endif
56 
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Default constructor for streamer, user should not use it.
61 
62 TMVA::PDEFoamMultiTarget::PDEFoamMultiTarget()
63  : PDEFoamEvent()
64  , fTargetSelection(kMean)
65 {
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// User constructor
70 ///
71 /// Parameters:
72 ///
73 /// - name - name of PDEFoam object
74 ///
75 /// - ts - target selection method used in
76 /// GetCellValue(const std::map<Int_t, Float_t>& xvec, ECellValue)
77 /// Cadidates are: TMVA::kMean, TMVA::kMpv
78 ///
79 /// - TMVA::kMean - The function GetCellValue() finds all cells
80 /// which contain a given event vector 'xvec' and returns the
81 /// mean target (for every target variable in the foam).
82 ///
83 /// - TMVA::kMpv - The function GetCellValue() finds all cells
84 /// which contain a given event vector 'xvec' and returns the
85 /// most probable target (for every target variable in the
86 /// foam), that is the target value which corresponds to the
87 /// cell with the largest event density.
88 
90  : PDEFoamEvent(name)
91  , fTargetSelection(ts)
92 {
93 }
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Copy Constructor NOT IMPLEMENTED (NEVER USED)
97 
99  : PDEFoamEvent(from)
100  , fTargetSelection(from.fTargetSelection)
101 {
102  Log() << kFATAL << "COPY CONSTRUCTOR NOT IMPLEMENTED" << Endl;
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// This function is overridden from PDFEFoam. It returns all
107 /// regression targets (in order), given an untransformed event
108 /// vector 'xvec'. The key of 'xvec' is the dimension and the value
109 /// (Float_t) is the coordinate.
110 ///
111 /// Note: number of foam dimensions = number of variables + number
112 /// of targets
113 ///
114 /// Parameters:
115 /// - xvec - map of event variables (no targets!)
116 /// - cv - cell value to return (ignored!)
117 ///
118 /// Return:
119 /// Targets, ordered by missing dimensions in 'xvec'.
120 /// The size of the returned vector = foam dimension - size of xvec.
121 
122 std::vector<Float_t> TMVA::PDEFoamMultiTarget::GetCellValue(const std::map<Int_t, Float_t>& xvec, ECellValue /*cv*/)
123 {
124  // transform event vector
125  std::map<Int_t, Float_t> txvec; // transformed event vector
126  for (std::map<Int_t, Float_t>::const_iterator it = xvec.begin();
127  it != xvec.end(); ++it) {
128  Float_t coordinate = it->second; // event coordinate
129  Int_t dim = it->first; // dimension
130  // checkt whether coordinate is within foam borders. if not,
131  // push event coordinate into foam
132  if (coordinate <= fXmin[dim])
133  coordinate = fXmin[dim] + std::numeric_limits<float>::epsilon();
134  else if (coordinate >= fXmax[dim])
135  coordinate = fXmax[dim] - std::numeric_limits<float>::epsilon();
136  // transform event
137  txvec.insert(std::pair<Int_t, Float_t>(dim, VarTransform(dim, coordinate)));
138  }
139 
140  // map of targets
141  std::map<Int_t, Float_t> target;
142 
143  // find cells, which fit txvec
144  std::vector<PDEFoamCell*> cells = FindCells(txvec);
145  if (cells.empty()) {
146  // return empty target vector (size = dimension of foam -
147  // number of variables)
148  return std::vector<Float_t>(GetTotDim() - xvec.size(), 0);
149  }
150 
151  // initialize the target map
152  for (Int_t idim = 0; idim < GetTotDim(); ++idim) {
153  // is idim a target dimension, i.e. is idim missing in txvec?
154  if (txvec.find(idim) == txvec.end())
155  target.insert(std::pair<Int_t, Float_t>(idim, 0));
156  }
157 
158  switch (fTargetSelection) {
159  case kMean:
160  CalculateMean(target, cells);
161  break;
162  case kMpv:
163  CalculateMpv(target, cells);
164  break;
165  default:
166  Log() << "<PDEFoamMultiTarget::GetCellValue>: "
167  << "unknown target selection type!" << Endl;
168  break;
169  }
170 
171  // copy targets to result vector
172  std::vector<Float_t> result;
173  result.reserve(target.size());
174  for (std::map<Int_t, Float_t>::const_iterator it = target.begin();
175  it != target.end(); ++it)
176  result.push_back(it->second);
177 
178  return result;
179 }
180 
181 void TMVA::PDEFoamMultiTarget::CalculateMpv(std::map<Int_t, Float_t>& target, const std::vector<PDEFoamCell*>& cells)
182 {
183  // This function calculates the most probable target value from a
184  // given number of cells. The most probable target is defined to
185  // be the coordinates of the cell which has the biggest event
186  // density.
187  //
188  // Parameters:
189  //
190  // - target - map of targets, where the key is the dimension and
191  // the value is the target value. It is assumed that this map is
192  // initialized such that there is a map entry for every target.
193  //
194  // - cells - vector of PDEFoam cells to pick the most probable
195  // target from
196 
197  Double_t max_dens = 0.0; // maximum cell density
198 
199  // loop over all cells and find cell with maximum event density
200  for (std::vector<PDEFoamCell*>::const_iterator cell_it = cells.begin();
201  cell_it != cells.end(); ++cell_it) {
202 
203  // get event density of cell
204  const Double_t cell_density = GetCellValue(*cell_it, kValueDensity);
205 
206  // has this cell a larger event density?
207  if (cell_density > max_dens) {
208  // get cell position and size
209  PDEFoamVect cellPosi(GetTotDim()), cellSize(GetTotDim());
210  (*cell_it)->GetHcub(cellPosi, cellSize);
211 
212  // save new maximum density
213  max_dens = cell_density;
214 
215  // calculate new target values
216  for (std::map<Int_t, Float_t>::iterator target_it = target.begin();
217  target_it != target.end(); ++target_it) {
218  const Int_t dim = target_it->first; // target dimension
219  target_it->second =
220  VarTransformInvers(dim, cellPosi[dim] + 0.5 * cellSize[dim]);
221  }
222  }
223  }
224 }
225 
226 void TMVA::PDEFoamMultiTarget::CalculateMean(std::map<Int_t, Float_t>& target, const std::vector<PDEFoamCell*>& cells)
227 {
228  // This function calculates the mean target value from a given
229  // number of cells. As weight the event density of the cell is
230  // used.
231  //
232  // Parameters:
233  //
234  // - target - map of targets, where the key is the dimension and
235  // the value is the target value. It is assumed that this map is
236  // initialized such that there is a map entry for every target
237  // with all target values set to zero.
238  //
239  // - cells - vector of PDEFoam cells to pick the most probable
240  // target from
241 
242  // normalization
243  std::map<Int_t, Float_t> norm;
244 
245  // loop over all cells and find cell with maximum event density
246  for (std::vector<PDEFoamCell*>::const_iterator cell_it = cells.begin();
247  cell_it != cells.end(); ++cell_it) {
248 
249  // get event density of cell
250  const Double_t cell_density = GetCellValue(*cell_it, kValueDensity);
251 
252  // get cell position and size
253  PDEFoamVect cellPosi(GetTotDim()), cellSize(GetTotDim());
254  (*cell_it)->GetHcub(cellPosi, cellSize);
255 
256  // accumulate weighted target values
257  for (std::map<Int_t, Float_t>::iterator target_it = target.begin();
258  target_it != target.end(); ++target_it) {
259  const Int_t dim = target_it->first; // target dimension
260  target_it->second += cell_density *
261  VarTransformInvers(dim, cellPosi[dim] + 0.5 * cellSize[dim]);
262  norm[dim] += cell_density;
263  }
264  }
265 
266  // normalize the targets
267  for (std::map<Int_t, Float_t>::iterator target_it = target.begin();
268  target_it != target.end(); ++target_it) {
269 
270  // get target dimension
271  const Int_t dim = target_it->first;
272 
273  // normalize target in dimension 'dim'
274  if (norm[dim] > std::numeric_limits<Float_t>::epsilon()) {
275  target[dim] /= norm[dim];
276  } else {
277  // normalisation factor is too small -> return approximate
278  // target value
279  target[dim] = (fXmax[dim] - fXmin[dim]) / 2.;
280  }
281  }
282 }
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
float Float_t
Definition: RtypesCore.h:53
MsgLogger & Log() const
Definition: PDEFoam.h:265
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
virtual void CalculateMean(std::map< Int_t, Float_t > &, const std::vector< PDEFoamCell * > &)
ClassImp(TMVA::PDEFoamMultiTarget) TMVA
Default constructor for streamer, user should not use it.
REAL epsilon
Definition: triangle.c:617
double Double_t
Definition: RtypesCore.h:55
ECellValue
Definition: PDEFoam.h:85
#define name(a, b)
Definition: linkTestLib0.cpp:5
Abstract ClassifierFactory template that handles arbitrary types.
virtual void CalculateMpv(std::map< Int_t, Float_t > &, const std::vector< PDEFoamCell * > &)
double result[121]
virtual std::vector< Float_t > GetCellValue(const std::map< Int_t, Float_t > &, ECellValue)
This function is overridden from PDFEFoam.
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
Definition: math.cpp:60