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 * 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
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
64
65////////////////////////////////////////////////////////////////////////////////
66/// Default constructor for streamer, user should not use it.
67
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
97 , fTargetSelection(ts)
98{
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// Copy Constructor NOT IMPLEMENTED (NEVER USED)
103
105 : PDEFoamEvent(from)
106 , fTargetSelection(from.fTargetSelection)
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
128std::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
202void 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
248void 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}
double Double_t
Definition RtypesCore.h:59
float Float_t
Definition RtypesCore.h:57
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
This PDEFoam variant stores in every cell the sum of event weights and the sum of the squared event w...
This PDEFoam variant is used to estimate multiple targets by creating an event density foam (PDEFoamE...
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.
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.
virtual std::vector< Float_t > GetCellValue(const std::map< Int_t, Float_t > &, ECellValue)
This function is overridden from PDFEFoam.
PDEFoamMultiTarget()
Default constructor for streamer, user should not use it.
MsgLogger & Log() const
Definition PDEFoam.h:240
Basic string class.
Definition TString.h:136
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:158