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