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