Logo ROOT   6.10/09
Reference Guide
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 /*! \class TMVA::PDEFoamMultiTarget
30 \ingroup TMVA
31 This PDEFoam variant is used to estimate multiple targets by
32 creating an event density foam (PDEFoamEvent), which has dimension:
33 
34  dimension = number of variables + number targets
35 
36 This PDEFoam variant stores in every cell the sum of event weights
37 and the sum of the squared event weights. During evaluation for a
38 given event, which has only variables and no targets (number of
39 event variables is smaller than the foam dimension), the targets
40 are estimated by finding all cells, which correspond to this event
41 and calculate the Mean (or Mpv, depending on the ETargetSelection)
42 cell center weighted by the event density in the cell.
43 
44 This PDEFoam variant should be booked together with the
45 PDEFoamEventDensity density estimator, which returns the event
46 weight density at a given phase space point during the foam
47 build-up.
48 */
49 
51 
52 #include "TMVA/MsgLogger.h"
53 #include "TMVA/PDEFoam.h"
54 #include "TMVA/PDEFoamEvent.h"
55 #include "TMVA/Types.h"
56 
57 #include "Rtypes.h"
58 
59 #include <map>
60 
61 class TString;
62 
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Default constructor for streamer, user should not use it.
67 
69 : PDEFoamEvent()
70  , fTargetSelection(kMean)
71 {
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// User constructor
76 ///
77 /// Parameters:
78 ///
79 /// - name - name of PDEFoam object
80 ///
81 /// - ts - target selection method used in
82 /// GetCellValue(const std::map<Int_t, Float_t>& xvec, ECellValue)
83 /// Candidates are: TMVA::kMean, TMVA::kMpv
84 ///
85 /// - TMVA::kMean - The function GetCellValue() finds all cells
86 /// which contain a given event vector 'xvec' and returns the
87 /// mean target (for every target variable in the foam).
88 ///
89 /// - TMVA::kMpv - The function GetCellValue() finds all cells
90 /// which contain a given event vector 'xvec' and returns the
91 /// most probable target (for every target variable in the
92 /// foam), that is the target value which corresponds to the
93 /// cell with the largest event density.
94 
96  : PDEFoamEvent(name)
97  , fTargetSelection(ts)
98 {
99 }
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Copy Constructor NOT IMPLEMENTED (NEVER USED)
103 
105  : PDEFoamEvent(from)
107 {
108  Log() << kFATAL << "COPY CONSTRUCTOR NOT IMPLEMENTED" << Endl;
109 }
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// This function is overridden from PDFEFoam. It returns all
113 /// regression targets (in order), given an untransformed event
114 /// vector 'xvec'. The key of 'xvec' is the dimension and the value
115 /// (Float_t) is the coordinate.
116 ///
117 /// Note: number of foam dimensions = number of variables + number
118 /// of targets
119 ///
120 /// Parameters:
121 /// - xvec - map of event variables (no targets!)
122 /// - cv - cell value to return (ignored!)
123 ///
124 /// Return:
125 /// Targets, ordered by missing dimensions in 'xvec'.
126 /// The size of the returned vector = foam dimension - size of xvec.
127 
128 std::vector<Float_t> TMVA::PDEFoamMultiTarget::GetCellValue(const std::map<Int_t, Float_t>& xvec, ECellValue /*cv*/)
129 {
130  // transform event vector
131  std::map<Int_t, Float_t> txvec; // transformed event vector
132  for (std::map<Int_t, Float_t>::const_iterator it = xvec.begin();
133  it != xvec.end(); ++it) {
134  Float_t coordinate = it->second; // event coordinate
135  Int_t dim = it->first; // dimension
136  // checkt whether coordinate is within foam borders. if not,
137  // push event coordinate into foam
138  if (coordinate <= fXmin[dim])
139  coordinate = fXmin[dim] + std::numeric_limits<float>::epsilon();
140  else if (coordinate >= fXmax[dim])
141  coordinate = fXmax[dim] - std::numeric_limits<float>::epsilon();
142  // transform event
143  txvec.insert(std::pair<Int_t, Float_t>(dim, VarTransform(dim, coordinate)));
144  }
145 
146  // map of targets
147  std::map<Int_t, Float_t> target;
148 
149  // find cells, which fit txvec
150  std::vector<PDEFoamCell*> cells = FindCells(txvec);
151  if (cells.empty()) {
152  // return empty target vector (size = dimension of foam -
153  // number of variables)
154  return std::vector<Float_t>(GetTotDim() - xvec.size(), 0);
155  }
156 
157  // initialize the target map
158  for (Int_t idim = 0; idim < GetTotDim(); ++idim) {
159  // is idim a target dimension, i.e. is idim missing in txvec?
160  if (txvec.find(idim) == txvec.end())
161  target.insert(std::pair<Int_t, Float_t>(idim, 0));
162  }
163 
164  switch (fTargetSelection) {
165  case kMean:
166  CalculateMean(target, cells);
167  break;
168  case kMpv:
169  CalculateMpv(target, cells);
170  break;
171  default:
172  Log() << "<PDEFoamMultiTarget::GetCellValue>: "
173  << "unknown target selection type!" << Endl;
174  break;
175  }
176 
177  // copy targets to result vector
178  std::vector<Float_t> result;
179  result.reserve(target.size());
180  for (std::map<Int_t, Float_t>::const_iterator it = target.begin();
181  it != target.end(); ++it)
182  result.push_back(it->second);
183 
184  return result;
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// This function calculates the most probable target value from a
189 /// given number of cells. The most probable target is defined to
190 /// be the coordinates of the cell which has the biggest event
191 /// density.
192 ///
193 /// Parameters:
194 ///
195 /// - target - map of targets, where the key is the dimension and
196 /// the value is the target value. It is assumed that this map is
197 /// initialized such that there is a map entry for every target.
198 ///
199 /// - cells - vector of PDEFoam cells to pick the most probable
200 /// target from
201 
202 void TMVA::PDEFoamMultiTarget::CalculateMpv(std::map<Int_t, Float_t>& target, const std::vector<PDEFoamCell*>& cells)
203 {
204  Double_t max_dens = 0.0; // maximum cell density
205 
206  // loop over all cells and find cell with maximum event density
207  for (std::vector<PDEFoamCell*>::const_iterator cell_it = cells.begin();
208  cell_it != cells.end(); ++cell_it) {
209 
210  // get event density of cell
211  const Double_t cell_density = GetCellValue(*cell_it, kValueDensity);
212 
213  // has this cell a larger event density?
214  if (cell_density > max_dens) {
215  // get cell position and size
216  PDEFoamVect cellPosi(GetTotDim()), cellSize(GetTotDim());
217  (*cell_it)->GetHcub(cellPosi, cellSize);
218 
219  // save new maximum density
220  max_dens = cell_density;
221 
222  // calculate new target values
223  for (std::map<Int_t, Float_t>::iterator target_it = target.begin();
224  target_it != target.end(); ++target_it) {
225  const Int_t dim = target_it->first; // target dimension
226  target_it->second =
227  VarTransformInvers(dim, cellPosi[dim] + 0.5 * cellSize[dim]);
228  }
229  }
230  }
231 }
232 
233 ////////////////////////////////////////////////////////////////////////////////
234 /// This function calculates the mean target value from a given
235 /// number of cells. As weight the event density of the cell is
236 /// used.
237 ///
238 /// Parameters:
239 ///
240 /// - target - map of targets, where the key is the dimension and
241 /// the value is the target value. It is assumed that this map is
242 /// initialized such that there is a map entry for every target
243 /// with all target values set to zero.
244 ///
245 /// - cells - vector of PDEFoam cells to pick the most probable
246 /// target from
247 
248 void TMVA::PDEFoamMultiTarget::CalculateMean(std::map<Int_t, Float_t>& target, const std::vector<PDEFoamCell*>& cells)
249 {
250  // normalization
251  std::map<Int_t, Float_t> norm;
252 
253  // loop over all cells and find cell with maximum event density
254  for (std::vector<PDEFoamCell*>::const_iterator cell_it = cells.begin();
255  cell_it != cells.end(); ++cell_it) {
256 
257  // get event density of cell
258  const Double_t cell_density = GetCellValue(*cell_it, kValueDensity);
259 
260  // get cell position and size
261  PDEFoamVect cellPosi(GetTotDim()), cellSize(GetTotDim());
262  (*cell_it)->GetHcub(cellPosi, cellSize);
263 
264  // accumulate weighted target values
265  for (std::map<Int_t, Float_t>::iterator target_it = target.begin();
266  target_it != target.end(); ++target_it) {
267  const Int_t dim = target_it->first; // target dimension
268  target_it->second += cell_density *
269  VarTransformInvers(dim, cellPosi[dim] + 0.5 * cellSize[dim]);
270  norm[dim] += cell_density;
271  }
272  }
273 
274  // normalize the targets
275  for (std::map<Int_t, Float_t>::iterator target_it = target.begin();
276  target_it != target.end(); ++target_it) {
277 
278  // get target dimension
279  const Int_t dim = target_it->first;
280 
281  // normalize target in dimension 'dim'
282  if (norm[dim] > std::numeric_limits<Float_t>::epsilon()) {
283  target[dim] /= norm[dim];
284  } else {
285  // normalisation factor is too small -> return approximate
286  // target value
287  target[dim] = (fXmax[dim] - fXmin[dim]) / 2.;
288  }
289  }
290 }
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
float Float_t
Definition: RtypesCore.h:53
This PDEFoam variant is used to estimate multiple targets by creating an event density foam (PDEFoamE...
Float_t VarTransform(Int_t idim, Float_t x) const
Definition: PDEFoam.h:279
std::vector< TMVA::PDEFoamCell * > FindCells(const std::vector< Float_t > &) const
Find all cells, that contain txvec.
Definition: PDEFoam.cxx:1166
ETargetSelection fTargetSelection
Double_t * fXmax
Definition: PDEFoam.h:105
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
virtual void CalculateMpv(std::map< Int_t, Float_t > &, const std::vector< PDEFoamCell *> &)
This function calculates the most probable target value from a given number of cells.
Int_t GetTotDim() const
Definition: PDEFoam.h:195
PDEFoamMultiTarget()
Default constructor for streamer, user should not use it.
This PDEFoam variant stores in every cell the sum of event weights and the sum of the squared event w...
Definition: PDEFoamEvent.h:38
Float_t VarTransformInvers(Int_t idim, Float_t x) const
Definition: PDEFoam.h:296
virtual void CalculateMean(std::map< Int_t, Float_t > &, const std::vector< PDEFoamCell *> &)
This function calculates the mean target value from a given number of cells.
MsgLogger & Log() const
Definition: PDEFoam.h:238
Double_t * fXmin
Definition: PDEFoam.h:104
REAL epsilon
Definition: triangle.c:617
#define ClassImp(name)
Definition: Rtypes.h:336
double Double_t
Definition: RtypesCore.h:55
Abstract ClassifierFactory template that handles arbitrary types.
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