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