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