Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
PDEFoamTarget.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: PDEFoamTarget *
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::PDEFoamTarget
30 \ingroup TMVA
31 This PDEFoam variant stores in every cell the average target
32 fTarget (see the Constructor) as well as the statistical error on
33 the target fTarget. It therefore acts as a target estimator. It
34 should be booked together with the PDEFoamTargetDensity density
35 estimator, which returns the target fTarget density at a given
36 phase space point during the foam build-up.
37 */
38 
39 #include "TMVA/PDEFoamTarget.h"
40 
41 #include "TMVA/Event.h"
42 #include "TMVA/MsgLogger.h"
43 #include "TMVA/PDEFoam.h"
44 #include "TMVA/PDEFoamCell.h"
45 #include "TMVA/PDEFoamKernelBase.h"
46 #include "TMVA/Types.h"
47 
48 #include "TMath.h"
49 
50 #include "Rtypes.h"
51 
52 ClassImp(TMVA::PDEFoamTarget);
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Default constructor for streamer, user should not use it.
56 
57 TMVA::PDEFoamTarget::PDEFoamTarget()
58 : PDEFoam()
59  , fTarget(0)
60 {
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// User constructor
65 ///
66 /// Parameters:
67 ///
68 /// - name - name of PDEFoam object
69 ///
70 /// - target - target number to range-search for
71 
72 TMVA::PDEFoamTarget::PDEFoamTarget(const TString& name, UInt_t target)
73  : PDEFoam(name)
74  , fTarget(target)
75 {
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// Copy Constructor NOT IMPLEMENTED (NEVER USED)
80 
81 TMVA::PDEFoamTarget::PDEFoamTarget(const PDEFoamTarget &from)
82  : PDEFoam(from)
83  , fTarget(from.fTarget)
84 {
85  Log() << kFATAL << "COPY CONSTRUCTOR NOT IMPLEMENTED" << Endl;
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// This function fills an event into the discriminant PDEFoam. The
90 /// weight 'wt' is filled into cell element 0 if the event is of
91 /// class 'fTarget', and filled into cell element 1 otherwise.
92 
93 void TMVA::PDEFoamTarget::FillFoamCells(const Event* ev, Float_t wt)
94 {
95  // find corresponding foam cell
96  std::vector<Float_t> values = ev->GetValues();
97  std::vector<Float_t> tvalues = VarTransform(values);
98  std::vector<Float_t> targets = ev->GetTargets();
99  PDEFoamCell *cell = FindCell(tvalues);
100 
101  // 0. Element: Number of events
102  // 1. Element: Target 0
103  SetCellElement(cell, 0, GetCellElement(cell, 0) + wt);
104  SetCellElement(cell, 1, GetCellElement(cell, 1) + wt * targets.at(fTarget));
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Calculate average cell target in every cell and save them to the
109 /// cell. Cell element 0 will contain the average target and cell
110 /// element 1 will contain the error on the target.
111 
112 void TMVA::PDEFoamTarget::Finalize()
113 {
114  // loop over cells
115  for (Long_t iCell = 0; iCell <= fLastCe; iCell++) {
116  if (!(fCells[iCell]->GetStat()))
117  continue;
118 
119  Double_t n_ev = GetCellElement(fCells[iCell], 0); // get number of events
120  Double_t tar = GetCellElement(fCells[iCell], 1); // get sum of targets
121 
122  if (n_ev > 0) {
123  SetCellElement(fCells[iCell], 0, tar / n_ev); // set average target
124  SetCellElement(fCells[iCell], 1, tar / TMath::Sqrt(n_ev)); // set error on average target
125  } else {
126  SetCellElement(fCells[iCell], 0, 0.0); // set mean target
127  SetCellElement(fCells[iCell], 1, -1); // set mean target error
128  }
129  }
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Returns true, if the target error equals -1, as set in
134 /// Finalize() in case of no events in the cell
135 
136 Bool_t TMVA::PDEFoamTarget::CellValueIsUndefined(PDEFoamCell* cell)
137 {
138  return GetCellValue(cell, kValueError) == -1;
139 }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// This function finds the cell, which corresponds to the given
143 /// untransformed event vector 'xvec' and return its value, which is
144 /// given by the parameter 'cv'.
145 ///
146 /// If cv == kValue, it is checked wether the cell value is
147 /// undefined. If this is the case, then the mean of the neighbor's
148 /// target values is returned, using GetAverageNeighborsValue().
149 
150 Float_t TMVA::PDEFoamTarget::GetCellValue(const std::vector<Float_t> &xvec, ECellValue cv, PDEFoamKernelBase *kernel)
151 {
152  std::vector<Float_t> txvec(VarTransform(xvec));
153  PDEFoamCell *cell = FindCell(txvec);
154 
155  if (!CellValueIsUndefined(cell)) {
156  // cell is not empty
157  if (kernel == NULL)
158  return GetCellValue(cell, cv);
159  else
160  return kernel->Estimate(this, txvec, cv);
161  } else
162  // cell is empty -> calc average target of neighbor cells
163  return GetAverageNeighborsValue(txvec, kValue);
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// This function returns the average value 'cv' of only nearest
168 /// neighbor cells. It is used in cases, where empty cells shall
169 /// not be evaluated.
170 ///
171 /// Parameters:
172 /// - txvec - event vector, transformed into foam coordinates [0, 1]
173 /// - cv - cell value, see definition of ECellValue
174 
175 Float_t TMVA::PDEFoamTarget::GetAverageNeighborsValue(std::vector<Float_t> &txvec,
176  ECellValue cv)
177 {
178  const Float_t xoffset = 1.e-6;
179  Float_t norm = 0; // normalisation
180  Float_t result = 0; // return value
181 
182  PDEFoamCell *cell = FindCell(txvec); // find corresponding cell
183  PDEFoamVect cellSize(GetTotDim());
184  PDEFoamVect cellPosi(GetTotDim());
185  cell->GetHcub(cellPosi, cellSize); // get cell coordinates
186 
187  // loop over all dimensions and find neighbor cells
188  for (Int_t dim = 0; dim < GetTotDim(); dim++) {
189  std::vector<Float_t> ntxvec(txvec);
190  PDEFoamCell* left_cell = 0; // left cell
191  PDEFoamCell* right_cell = 0; // right cell
192 
193  // get left cell
194  ntxvec[dim] = cellPosi[dim] - xoffset;
195  left_cell = FindCell(ntxvec);
196  if (!CellValueIsUndefined(left_cell)) {
197  // if left cell is not empty, take its value
198  result += GetCellValue(left_cell, cv);
199  norm++;
200  }
201  // get right cell
202  ntxvec[dim] = cellPosi[dim] + cellSize[dim] + xoffset;
203  right_cell = FindCell(ntxvec);
204  if (!CellValueIsUndefined(right_cell)) {
205  // if right cell is not empty, take its value
206  result += GetCellValue(right_cell, cv);
207  norm++;
208  }
209  }
210  if (norm > 0) result /= norm; // calc average target
211  else result = 0; // return null if all neighbors are empty
212 
213  return result;
214 }