ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  * Web : http://tmva.sourceforge.net *
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 "assert.h"
35 #include <iomanip>
36 #include <cassert>
37 #include "TCut.h"
38 
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// copy constructor
44 
46  : fValues(),
47  fValuesDynamic(0),
48  fTargets(),
49  fSpectators(),
50  fVariableArrangement(0),
51  fClass(0),
52  fWeight(1.0),
53  fBoostWeight(1.0),
54  fDynamic(kFALSE),
55  fDoNotBoost(kFALSE)
56 {
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// constructor
61 
62 TMVA::Event::Event( const std::vector<Float_t>& ev,
63  const std::vector<Float_t>& tg,
64  UInt_t cls,
65  Double_t weight,
66  Double_t boostweight )
67  : fValues(ev),
68  fValuesDynamic(0),
69  fTargets(tg),
70  fSpectators(0),
71  fVariableArrangement(0),
72  fClass(cls),
73  fWeight(weight),
74  fBoostWeight(boostweight),
75  fDynamic(kFALSE),
76  fDoNotBoost(kFALSE)
77 {
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// constructor
82 
83 TMVA::Event::Event( const std::vector<Float_t>& ev,
84  const std::vector<Float_t>& tg,
85  const std::vector<Float_t>& vi,
86  UInt_t cls,
87  Double_t weight,
88  Double_t boostweight )
89  : fValues(ev),
90  fValuesDynamic(0),
91  fTargets(tg),
92  fSpectators(vi),
93  fVariableArrangement(0),
94  fClass(cls),
95  fWeight(weight),
96  fBoostWeight(boostweight),
97  fDynamic(kFALSE),
98  fDoNotBoost(kFALSE)
99 {
100 }
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 /// constructor
104 
105 TMVA::Event::Event( const std::vector<Float_t>& ev,
106  UInt_t cls,
107  Double_t weight,
108  Double_t boostweight )
109  : fValues(ev),
110  fValuesDynamic(0),
111  fTargets(0),
112  fSpectators(0),
113  fVariableArrangement(0),
114  fClass(cls),
115  fWeight(weight),
116  fBoostWeight(boostweight),
117  fDynamic(kFALSE),
118  fDoNotBoost(kFALSE)
119 {
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// constructor for single events
124 
125 TMVA::Event::Event( const std::vector<Float_t*>*& evdyn, UInt_t nvar )
126  : fValues(nvar),
127  fValuesDynamic(0),
128  fTargets(0),
129  fSpectators(evdyn->size()-nvar),
130  fVariableArrangement(0),
131  fClass(0),
132  fWeight(0),
133  fBoostWeight(0),
134  fDynamic(true),
135  fDoNotBoost(kFALSE)
136 {
137  fValuesDynamic = (std::vector<Float_t*>*) evdyn;
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 /// copy constructor
142 
143 TMVA::Event::Event( const Event& event )
144  : fValues(event.fValues),
145  fValuesDynamic(event.fValuesDynamic),
146  fTargets(event.fTargets),
147  fSpectators(event.fSpectators),
148  fVariableArrangement(event.fVariableArrangement),
149  fClass(event.fClass),
150  fWeight(event.fWeight),
151  fBoostWeight(event.fBoostWeight),
152  fDynamic(event.fDynamic),
153  fDoNotBoost(kFALSE)
154 {
155  if (event.fDynamic){
156  fValues.clear();
157  UInt_t nvar = event.GetNVariables();
158  UInt_t idx=0;
159  std::vector<Float_t*>::iterator itDyn=event.fValuesDynamic->begin(), itDynEnd=event.fValuesDynamic->end();
160  for (; itDyn!=itDynEnd && idx<nvar; ++itDyn){
161  Float_t value=*(*itDyn);
162  fValues.push_back( value );
163  ++idx;
164  }
165  fSpectators.clear();
166  for (; itDyn!=itDynEnd; ++itDyn){
167  Float_t value=*(*itDyn);
168  fSpectators.push_back( value );
169  ++idx;
170  }
171 
174  }
175 }
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 /// Event destructor
179 
181 {
182  delete fValuesDynamic;
183 }
184 ////////////////////////////////////////////////////////////////////////////////
185 /// set the variable arrangement
186 
187 void TMVA::Event::SetVariableArrangement( std::vector<UInt_t>* const m ) const {
188  // mapping from global variable index (the position in the vector)
189  // to the new index in the subset of variables used by the
190  // composite classifier
191  fVariableArrangement = m;
192 }
193 
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 /// copies only the variable values
197 
198 void TMVA::Event::CopyVarValues( const Event& other )
199 {
200  fValues = other.fValues;
201  fTargets = other.fTargets;
202  fSpectators = other.fSpectators;
203  if (other.fDynamic){
204  UInt_t nvar = other.GetNVariables();
205  fValues.clear();
206  UInt_t idx=0;
207  std::vector<Float_t*>::iterator itDyn=other.fValuesDynamic->begin(), itDynEnd=other.fValuesDynamic->end();
208  for (; itDyn!=itDynEnd && idx<nvar; ++itDyn){
209  Float_t value=*(*itDyn);
210  fValues.push_back( value );
211  ++idx;
212  }
213  fSpectators.clear();
214  for (; itDyn!=itDynEnd; ++itDyn){
215  Float_t value=*(*itDyn);
216  fSpectators.push_back( value );
217  ++idx;
218  }
219  }
220  fDynamic = kFALSE;
221  fValuesDynamic = NULL;
222 
223  fClass = other.fClass;
224  fWeight = other.fWeight;
225  fBoostWeight = other.fBoostWeight;
226 }
227 
228 ////////////////////////////////////////////////////////////////////////////////
229 /// return value of i'th variable
230 
232 {
233  Float_t retval;
234  if (fVariableArrangement==0) {
235  retval = fDynamic ? ( *((*fValuesDynamic).at(ivar)) ) : fValues.at(ivar);
236  }
237  else {
238  UInt_t mapIdx = (*fVariableArrangement)[ivar];
239  // std::cout<< fDynamic ;
240  if (fDynamic){
241  // std::cout<< " " << (*fValuesDynamic).size() << " " << fValues.size() << std::endl;
242  retval = *((*fValuesDynamic).at(mapIdx));
243  }
244  else{
245  //retval = fValues.at(ivar);
246  retval = ( mapIdx<fValues.size() ) ? fValues[mapIdx] : fSpectators[mapIdx-fValues.size()];
247  }
248  }
249 
250  return retval;
251 }
252 
253 ////////////////////////////////////////////////////////////////////////////////
254 /// return spectator content
255 
257 {
258  if (fDynamic) return *(fValuesDynamic->at(GetNVariables()+ivar));
259  else return fSpectators.at(ivar);
260 }
261 
262 ////////////////////////////////////////////////////////////////////////////////
263 /// return value vector
264 
265 const std::vector<Float_t>& TMVA::Event::GetValues() const
266 {
267  if (fVariableArrangement==0) {
268 
269  if (fDynamic) {
270  fValues.clear();
271  for (std::vector<Float_t*>::const_iterator it = fValuesDynamic->begin(), itEnd=fValuesDynamic->end()-GetNSpectators();
272  it != itEnd; ++it) {
273  Float_t val = *(*it);
274  fValues.push_back( val );
275  }
276  }
277  }else{
278  UInt_t mapIdx;
279  if (fDynamic) {
280  fValues.clear();
281  for (UInt_t i=0; i< fVariableArrangement->size(); i++){
282  mapIdx = (*fVariableArrangement)[i];
283  fValues.push_back(*((*fValuesDynamic).at(mapIdx)));
284  }
285  } else {
286  // hmm now you have a problem, as you do not want to mess with the original event variables
287  // (change them permanently) ... guess the only way is to add a 'fValuesRearranged' array,
288  // and living with the fact that it 'doubles' the Event size :(
289  fValuesRearranged.clear();
290  for (UInt_t i=0; i< fVariableArrangement->size(); i++){
291  mapIdx = (*fVariableArrangement)[i];
292  fValuesRearranged.push_back(fValues.at(mapIdx));
293  }
294  return fValuesRearranged;
295  }
296  }
297  return fValues;
298 }
299 
300 ////////////////////////////////////////////////////////////////////////////////
301 /// accessor to the number of variables
302 
304 {
305  // if variables have to arranged (as it is the case for the
306  // composite classifier) the number of the variables changes
307  if (fVariableArrangement==0) return fValues.size();
308  else return fVariableArrangement->size();
309 }
310 
311 ////////////////////////////////////////////////////////////////////////////////
312 /// accessor to the number of targets
313 
315 {
316  return fTargets.size();
317 }
318 
319 ////////////////////////////////////////////////////////////////////////////////
320 /// accessor to the number of spectators
321 
323 {
324  // if variables have to arranged (as it is the case for the
325  // composite classifier) the number of the variables changes
326 
327  if (fVariableArrangement==0) return fSpectators.size();
328  else return fValues.size()-fVariableArrangement->size();
329 }
330 
331 
332 ////////////////////////////////////////////////////////////////////////////////
333 /// set variable ivar to val
334 
336 {
337  if ((fDynamic ?( (*fValuesDynamic).size() ) : fValues.size())<=ivar)
338  (fDynamic ?( (*fValuesDynamic).resize(ivar+1) ) : fValues.resize(ivar+1));
339 
340  (fDynamic ?( *(*fValuesDynamic)[ivar] ) : fValues[ivar])=val;
341 }
342 
343 ////////////////////////////////////////////////////////////////////////////////
344 /// print method
345 
346 void TMVA::Event::Print( std::ostream& o ) const
347 {
348  o << *this << std::endl;
349 }
350 
351 ////////////////////////////////////////////////////////////////////////////////
352 /// set the target value (dimension itgt) to value
353 
355 {
356  if (fTargets.size() <= itgt) fTargets.resize( itgt+1 );
357  fTargets.at(itgt) = value;
358 }
359 
360 ////////////////////////////////////////////////////////////////////////////////
361 /// set spectator value (dimension ivar) to value
362 
364 {
365  if (fSpectators.size() <= ivar) fSpectators.resize( ivar+1 );
366  fSpectators.at(ivar) = value;
367 }
368 
369 ////////////////////////////////////////////////////////////////////////////////
370 /// return the event weight - depending on whether the flag
371 /// *IgnoreNegWeightsInTraining* is or not. If it is set AND it is
372 /// used for training, then negetive event weights are set to zero !
373 /// NOTE! For events used in Testing, the ORIGINAL possibly negative
374 /// event weight is used no matter what
375 
377 {
378  return (fgIgnoreNegWeightsInTraining && fgIsTraining && fWeight < 0) ? 0. : fWeight*fBoostWeight;
379 }
380 
381 ////////////////////////////////////////////////////////////////////////////////
382 /// when this static function is called, it sets the flag whether
383 /// events with negative event weight should be ignored in the
384 /// training, or not.
385 
387 {
388  fgIsTraining=b;
389 }
390 ////////////////////////////////////////////////////////////////////////////////
391 /// when this static function is called, it sets the flag whether
392 /// events with negative event weight should be ignored in the
393 /// training, or not.
394 
396 {
397  fgIgnoreNegWeightsInTraining=b;
398 }
399 
400 ////////////////////////////////////////////////////////////////////////////////
401 /// Outputs the data of an event
402 
403 std::ostream& TMVA::operator << ( std::ostream& os, const TMVA::Event& event )
404 {
405  os << "Variables [" << event.fValues.size() << "]:";
406  for (UInt_t ivar=0; ivar<event.fValues.size(); ++ivar)
407  os << " " << std::setw(10) << event.GetValue(ivar);
408  os << ", targets [" << event.fTargets.size() << "]:";
409  for (UInt_t ivar=0; ivar<event.fTargets.size(); ++ivar)
410  os << " " << std::setw(10) << event.GetTarget(ivar);
411  os << ", spectators ["<< event.fSpectators.size() << "]:";
412  for (UInt_t ivar=0; ivar<event.fSpectators.size(); ++ivar)
413  os << " " << std::setw(10) << event.GetSpectator(ivar);
414  os << ", weight: " << event.GetWeight();
415  os << ", class: " << event.GetClass();
416  return os;
417 }
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:395
UInt_t fClass
Definition: Event.h:140
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:386
void SetVariableArrangement(std::vector< UInt_t > *const m) const
set the variable arrangement
Definition: Event.cxx:187
float Float_t
Definition: RtypesCore.h:53
std::vector< Float_t > fValues
Definition: Event.h:132
std::vector< Float_t * > * fValuesDynamic
Definition: Event.h:135
std::vector< Float_t > fSpectators
Definition: Event.h:137
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
void SetTarget(UInt_t itgt, Float_t value)
set the target value (dimension itgt) to value
Definition: Event.cxx:354
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
Definition: Event.cxx:376
Double_t fBoostWeight
Definition: Event.h:142
Float_t GetSpectator(UInt_t ivar) const
return spectator content
Definition: Event.cxx:256
void SetVal(UInt_t ivar, Float_t val)
set variable ivar to val
Definition: Event.cxx:335
void Print(std::ostream &o) const
print method
Definition: Event.cxx:346
void SetSpectator(UInt_t ivar, Float_t value)
set spectator value (dimension ivar) to value
Definition: Event.cxx:363
TClass * fClass
pointer to the foreign object
Double_t fWeight
Definition: Event.h:141
UInt_t GetNVariables() const
accessor to the number of variables
Definition: Event.cxx:303
Bool_t fDynamic
Definition: Event.h:143
UInt_t GetNSpectators() const
accessor to the number of spectators
Definition: Event.cxx:322
void CopyVarValues(const Event &other)
copies only the variable values
Definition: Event.cxx:198
std::vector< Float_t > fTargets
Definition: Event.h:136
static Bool_t fgIsTraining
Definition: Event.h:128
unsigned int UInt_t
Definition: RtypesCore.h:42
TMarker * m
Definition: textangle.C:8
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
print the tree recursinvely using the << operator
Definition: BinaryTree.cxx:157
Event()
copy constructor
Definition: Event.cxx:45
~Event()
Event destructor.
Definition: Event.cxx:180
double Double_t
Definition: RtypesCore.h:55
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Definition: Event.cxx:231
UInt_t GetNTargets() const
accessor to the number of targets
Definition: Event.cxx:314
std::vector< Float_t > & GetValues()
Definition: Event.h:93
#define NULL
Definition: Rtypes.h:82
float value
Definition: math.cpp:443
static Bool_t fgIgnoreNegWeightsInTraining
Definition: Event.h:129