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