Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Event.cxx
Go to the documentation of this file.
1// @(#)root/tmva $Id$
2// Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss, Jan Therhaag
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : Event *
8 * *
9 * *
10 * Description: *
11 * Implementation (see header for description) *
12 * *
13 * Authors (alphabetical): *
14 * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15 * Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland *
16 * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
17 * Jan Therhaag <Jan.Therhaag@cern.ch> - U of Bonn, Germany *
18 * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
19 * *
20 * Copyright (c) 2005-2011: *
21 * CERN, Switzerland *
22 * U. of Victoria, Canada *
23 * MPI-K Heidelberg, Germany *
24 * U. of Bonn, Germany *
25 * *
26 * Redistribution and use in source and binary forms, with or without *
27 * modification, are permitted according to the terms listed in LICENSE *
28 * (http://mva.sourceforge.net/license.txt) *
29 **********************************************************************************/
30
31#include "TMVA/Event.h"
32#include "TMVA/Tools.h"
33#include <iostream>
34#include <iomanip>
35#include <cassert>
36#include "TCut.h"
37
38/*! \class TMVA::Event
39\ingroup TMVA
40*/
41
44
45////////////////////////////////////////////////////////////////////////////////
46/// copy constructor
47
49 : fValues(),
50 fValuesDynamic(0),
51 fTargets(),
52 fSpectators(),
53 fVariableArrangement(0),
54 fClass(0),
55 fWeight(1.0),
56 fBoostWeight(1.0),
57 fDynamic(kFALSE),
58 fDoNotBoost(kFALSE)
59{
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// constructor
64
65TMVA::Event::Event( const std::vector<Float_t>& ev,
66 const std::vector<Float_t>& tg,
67 UInt_t cls,
68 Double_t weight,
69 Double_t boostweight )
70 : fValues(ev),
71 fValuesDynamic(0),
72 fTargets(tg),
73 fSpectators(0),
74 fVariableArrangement(0),
75 fClass(cls),
76 fWeight(weight),
77 fBoostWeight(boostweight),
78 fDynamic(kFALSE),
79 fDoNotBoost(kFALSE)
80{
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// constructor
85
86TMVA::Event::Event( const std::vector<Float_t>& ev,
87 const std::vector<Float_t>& tg,
88 const std::vector<Float_t>& vi,
89 UInt_t cls,
90 Double_t weight,
91 Double_t boostweight )
92 : fValues(ev),
93 fValuesDynamic(0),
94 fTargets(tg),
95 fSpectators(vi),
96 fVariableArrangement(0),
97 fClass(cls),
98 fWeight(weight),
99 fBoostWeight(boostweight),
100 fDynamic(kFALSE),
101 fDoNotBoost(kFALSE)
102{
103}
104
105////////////////////////////////////////////////////////////////////////////////
106/// constructor
107
108TMVA::Event::Event( const std::vector<Float_t>& ev,
109 UInt_t cls,
110 Double_t weight,
111 Double_t boostweight )
112 : fValues(ev),
113 fValuesDynamic(0),
114 fTargets(0),
115 fSpectators(0),
116 fVariableArrangement(0),
117 fClass(cls),
118 fWeight(weight),
119 fBoostWeight(boostweight),
120 fDynamic(kFALSE),
121 fDoNotBoost(kFALSE)
122{
123}
124
125////////////////////////////////////////////////////////////////////////////////
126/// constructor for single events
127
128TMVA::Event::Event( const std::vector<Float_t*>*& evdyn, UInt_t nvar )
129 : fValues(nvar),
130 fValuesDynamic(0),
131 fTargets(0),
132 fSpectators(evdyn->size()-nvar),
133 fVariableArrangement(0),
134 fClass(0),
135 fWeight(0),
136 fBoostWeight(0),
137 fDynamic(true),
138 fDoNotBoost(kFALSE)
139{
140 fValuesDynamic = (std::vector<Float_t*>*) evdyn;
141}
142
143////////////////////////////////////////////////////////////////////////////////
144/// copy constructor
145
147 : TObject(event),
148 fValues(event.fValues),
149 fValuesDynamic(event.fValuesDynamic),
150 fTargets(event.fTargets),
151 fSpectators(event.fSpectators),
152 fVariableArrangement(event.fVariableArrangement),
153 fClass(event.fClass),
154 fWeight(event.fWeight),
155 fBoostWeight(event.fBoostWeight),
156 fDynamic(event.fDynamic),
157 fDoNotBoost(kFALSE)
158{
159 if (event.fDynamic){
160 fValues.clear();
161 UInt_t nvar = event.GetNVariables();
162 UInt_t idx=0;
163 std::vector<Float_t*>::iterator itDyn=event.fValuesDynamic->begin(), itDynEnd=event.fValuesDynamic->end();
164 for (; itDyn!=itDynEnd && idx<nvar; ++itDyn){
165 Float_t value=*(*itDyn);
166 fValues.push_back( value );
167 ++idx;
168 }
169 fSpectators.clear();
170 for (; itDyn!=itDynEnd; ++itDyn){
171 Float_t value=*(*itDyn);
172 fSpectators.push_back( value );
173 ++idx;
174 }
175
177 fValuesDynamic=NULL;
178}
179}
180
181////////////////////////////////////////////////////////////////////////////////
182/// Event destructor
183
185{
186// delete fValuesDynamic;
187}
188////////////////////////////////////////////////////////////////////////////////
189/// set the variable arrangement
190
191void TMVA::Event::SetVariableArrangement( std::vector<UInt_t>* const m ) const {
192 // mapping from global variable index (the position in the vector)
193 // to the new index in the subset of variables used by the
194 // composite classifier
195 if(!m)fVariableArrangement.clear();
196 else fVariableArrangement = *m;
197}
198
199
200////////////////////////////////////////////////////////////////////////////////
201/// copies only the variable values
202
204{
205 fValues = other.fValues;
206 fTargets = other.fTargets;
207 fSpectators = other.fSpectators;
208 if (other.fDynamic){
209 UInt_t nvar = other.GetNVariables();
210 fValues.clear();
211 UInt_t idx=0;
212 std::vector<Float_t*>::iterator itDyn=other.fValuesDynamic->begin(), itDynEnd=other.fValuesDynamic->end();
213 for (; itDyn!=itDynEnd && idx<nvar; ++itDyn){
214 Float_t value=*(*itDyn);
215 fValues.push_back( value );
216 ++idx;
217 }
218 fSpectators.clear();
219 for (; itDyn!=itDynEnd; ++itDyn){
220 Float_t value=*(*itDyn);
221 fSpectators.push_back( value );
222 ++idx;
223 }
224 }
225 fDynamic = kFALSE;
226 fValuesDynamic = NULL;
227
228 fClass = other.fClass;
229 fWeight = other.fWeight;
230 fBoostWeight = other.fBoostWeight;
231}
232
233////////////////////////////////////////////////////////////////////////////////
234/// return value of i'th variable
235
237{
238 Float_t retval;
239 if (fVariableArrangement.size()==0) {
240 retval = fDynamic ? ( *((fValuesDynamic)->at(ivar)) ) : fValues.at(ivar);
241 }
242 else {
243 UInt_t mapIdx = fVariableArrangement[ivar];
244 // std::cout<< fDynamic ;
245 if (fDynamic){
246 // std::cout<< " " << (*fValuesDynamic).size() << " " << fValues.size() << std::endl;
247 retval = *((fValuesDynamic)->at(mapIdx));
248 }
249 else{
250 //retval = fValues.at(ivar);
251 retval = ( mapIdx<fValues.size() ) ? fValues[mapIdx] : fSpectators[mapIdx-fValues.size()];
252 }
253 }
254
255 return retval;
256}
257
258////////////////////////////////////////////////////////////////////////////////
259/// return spectator content
260
262{
263 if (fDynamic) {
264 if (fSpectatorTypes[ivar] == 'F')
265 return *(fValuesDynamic->at(GetNVariables()+ivar));
266 else if (fSpectatorTypes[ivar] == 'I')
267 return *(reinterpret_cast<int *>(fValuesDynamic->at(GetNVariables() + ivar)));
268 else {
269 throw std::runtime_error("Spectator variable has an invalid type ");
270 }
271 } else
272 return fSpectators.at(ivar);
273}
274
275////////////////////////////////////////////////////////////////////////////////
276/// return value vector
277
278const std::vector<Float_t>& TMVA::Event::GetValues() const
279{
280 if (fVariableArrangement.size()==0) {
281
282 if (fDynamic) {
283 fValues.clear();
284 for (std::vector<Float_t*>::const_iterator it = fValuesDynamic->begin(), itEnd=fValuesDynamic->end()-GetNSpectators();
285 it != itEnd; ++it) {
286 Float_t val = *(*it);
287 fValues.push_back( val );
288 }
289 }
290 }else{
291 UInt_t mapIdx;
292 if (fDynamic) {
293 fValues.clear();
294 for (UInt_t i=0; i< fVariableArrangement.size(); i++){
295 mapIdx = fVariableArrangement[i];
296 fValues.push_back(*((fValuesDynamic)->at(mapIdx)));
297 }
298 } else {
299 // hmm now you have a problem, as you do not want to mess with the original event variables
300 // (change them permanently) ... guess the only way is to add a 'fValuesRearranged' array,
301 // and living with the fact that it 'doubles' the Event size :(
302 fValuesRearranged.clear();
303 for (UInt_t i=0; i< fVariableArrangement.size(); i++){
304 mapIdx = fVariableArrangement[i];
305 fValuesRearranged.push_back(fValues.at(mapIdx));
306 }
307 return fValuesRearranged;
308 }
309 }
310 return fValues;
311}
312
313////////////////////////////////////////////////////////////////////////////////
314/// accessor to the number of variables
315
317{
318 // if variables have to arranged (as it is the case for the
319 // composite classifier) the number of the variables changes
320 if (fVariableArrangement.size()==0) return fValues.size();
321 else return fVariableArrangement.size();
322}
323
324////////////////////////////////////////////////////////////////////////////////
325/// accessor to the number of targets
326
328{
329 return fTargets.size();
330}
331
332////////////////////////////////////////////////////////////////////////////////
333/// accessor to the number of spectators
334
336{
337 // if variables have to arranged (as it is the case for the
338 // composite classifier) the number of the variables changes
339
340 if (fVariableArrangement.size()==0) return fSpectators.size();
341 else return fValues.size()-fVariableArrangement.size();
342}
343
344
345////////////////////////////////////////////////////////////////////////////////
346/// set variable ivar to val
347
349{
350 if ((fDynamic ?( (*fValuesDynamic).size() ) : fValues.size())<=ivar)
351 (fDynamic ?( (*fValuesDynamic).resize(ivar+1) ) : fValues.resize(ivar+1));
352
353 (fDynamic ?( *(*fValuesDynamic)[ivar] ) : fValues[ivar])=val;
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// print method
358
359void TMVA::Event::Print( std::ostream& o ) const
360{
361 o << *this << std::endl;
362}
363
364////////////////////////////////////////////////////////////////////////////////
365/// set the target value (dimension itgt) to value
366
368{
369 if (fTargets.size() <= itgt) fTargets.resize( itgt+1 );
370 fTargets.at(itgt) = value;
371}
372
373////////////////////////////////////////////////////////////////////////////////
374/// set spectator value (dimension ivar) to value
375
377{
378 if (fSpectators.size() <= ivar) fSpectators.resize( ivar+1 );
379 fSpectators.at(ivar) = value;
380}
381
382////////////////////////////////////////////////////////////////////////////////
383/// return the event weight - depending on whether the flag
384/// *IgnoreNegWeightsInTraining* is or not. If it is set AND it is
385/// used for training, then negative event weights are set to zero !
386/// NOTE! For events used in Testing, the ORIGINAL possibly negative
387/// event weight is used no matter what
388
390{
391 return (fgIgnoreNegWeightsInTraining && fgIsTraining && fWeight < 0) ? 0. : fWeight*fBoostWeight;
392}
393
394////////////////////////////////////////////////////////////////////////////////
395/// when this static function is called, it sets the flag whether
396/// events with negative event weight should be ignored in the
397/// training, or not.
398
400{
401 fgIsTraining=b;
402}
403////////////////////////////////////////////////////////////////////////////////
404/// when this static function is called, it sets the flag whether
405/// events with negative event weight should be ignored in the
406/// training, or not.
407
409{
410 fgIgnoreNegWeightsInTraining=b;
411}
412
413////////////////////////////////////////////////////////////////////////////////
414/// Outputs the data of an event
415
416std::ostream& TMVA::operator << ( std::ostream& os, const TMVA::Event& event )
417{
418 os << "Variables [" << event.fValues.size() << "]:";
419 for (UInt_t ivar=0; ivar<event.fValues.size(); ++ivar)
420 os << " " << std::setw(10) << event.GetValue(ivar);
421 os << ", targets [" << event.fTargets.size() << "]:";
422 for (UInt_t ivar=0; ivar<event.fTargets.size(); ++ivar)
423 os << " " << std::setw(10) << event.GetTarget(ivar);
424 os << ", spectators ["<< event.fSpectators.size() << "]:";
425 for (UInt_t ivar=0; ivar<event.fSpectators.size(); ++ivar)
426 os << " " << std::setw(10) << event.GetSpectator(ivar);
427 os << ", weight: " << event.GetWeight();
428 os << ", class: " << event.GetClass();
429 return os;
430}
Cppyy::TCppType_t fClass
#define b(i)
Definition RSha256.hxx:100
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
bool Bool_t
Definition RtypesCore.h:63
unsigned int UInt_t
Definition RtypesCore.h:46
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
std::vector< Float_t > fValues
the event values ; mutable, to be able to copy the dynamic values in there
Definition Event.h:137
static Bool_t fgIsTraining
Definition Event.h:133
Double_t fWeight
event weight (product of global and individual weights)
Definition Event.h:147
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Definition Event.cxx:236
void SetTarget(UInt_t itgt, Float_t value)
set the target value (dimension itgt) to value
Definition Event.cxx:367
std::vector< Float_t > fTargets
target values for regression
Definition Event.h:141
Double_t fBoostWeight
internal weight to be set by boosting algorithm
Definition Event.h:148
Bool_t fDynamic
is set when the dynamic values are taken
Definition Event.h:149
std::vector< Float_t > fSpectators
"visiting" variables not used in MVAs ; mutable, to be able to copy the dynamic values in there
Definition Event.h:142
UInt_t GetNSpectators() const
accessor to the number of spectators
Definition Event.cxx:335
~Event()
Event destructor.
Definition Event.cxx:184
std::vector< Float_t * > * fValuesDynamic
! the event values
Definition Event.h:140
Event()
copy constructor
Definition Event.cxx:48
UInt_t GetNVariables() const
accessor to the number of variables
Definition Event.cxx:316
void CopyVarValues(const Event &other)
copies only the variable values
Definition Event.cxx:203
UInt_t GetNTargets() const
accessor to the number of targets
Definition Event.cxx:327
static Bool_t fgIgnoreNegWeightsInTraining
Definition Event.h:134
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not.
Definition Event.cxx:389
void SetVal(UInt_t ivar, Float_t val)
set variable ivar to val
Definition Event.cxx:348
static void SetIsTraining(Bool_t)
when this static function is called, it sets the flag whether events with negative event weight shoul...
Definition Event.cxx:399
void SetVariableArrangement(std::vector< UInt_t > *const m) const
set the variable arrangement
Definition Event.cxx:191
Float_t GetSpectator(UInt_t ivar) const
return spectator content
Definition Event.cxx:261
void SetSpectator(UInt_t ivar, Float_t value)
set spectator value (dimension ivar) to value
Definition Event.cxx:376
std::vector< Float_t > & GetValues()
Definition Event.h:94
static void SetIgnoreNegWeightsInTraining(Bool_t)
when this static function is called, it sets the flag whether events with negative event weight shoul...
Definition Event.cxx:408
void Print(std::ostream &o) const
print method
Definition Event.cxx:359
UInt_t fClass
class number
Definition Event.h:146
Mother of all ROOT objects.
Definition TObject.h:41
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
TMarker m
Definition textangle.C:8