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