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