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