Logo ROOT   master
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 <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 
65 TMVA::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 
86 TMVA::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 
108 TMVA::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 
128 TMVA::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 
146 TMVA::Event::Event( const Event& event )
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 
191 void 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 
203 void TMVA::Event::CopyVarValues( const Event& other )
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) return *(fValuesDynamic->at(GetNVariables()+ivar));
264  else return fSpectators.at(ivar);
265 }
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// return value vector
269 
270 const std::vector<Float_t>& TMVA::Event::GetValues() const
271 {
272  if (fVariableArrangement.size()==0) {
273 
274  if (fDynamic) {
275  fValues.clear();
276  for (std::vector<Float_t*>::const_iterator it = fValuesDynamic->begin(), itEnd=fValuesDynamic->end()-GetNSpectators();
277  it != itEnd; ++it) {
278  Float_t val = *(*it);
279  fValues.push_back( val );
280  }
281  }
282  }else{
283  UInt_t mapIdx;
284  if (fDynamic) {
285  fValues.clear();
286  for (UInt_t i=0; i< fVariableArrangement.size(); i++){
287  mapIdx = fVariableArrangement[i];
288  fValues.push_back(*((fValuesDynamic)->at(mapIdx)));
289  }
290  } else {
291  // hmm now you have a problem, as you do not want to mess with the original event variables
292  // (change them permanently) ... guess the only way is to add a 'fValuesRearranged' array,
293  // and living with the fact that it 'doubles' the Event size :(
294  fValuesRearranged.clear();
295  for (UInt_t i=0; i< fVariableArrangement.size(); i++){
296  mapIdx = fVariableArrangement[i];
297  fValuesRearranged.push_back(fValues.at(mapIdx));
298  }
299  return fValuesRearranged;
300  }
301  }
302  return fValues;
303 }
304 
305 ////////////////////////////////////////////////////////////////////////////////
306 /// accessor to the number of variables
307 
309 {
310  // if variables have to arranged (as it is the case for the
311  // composite classifier) the number of the variables changes
312  if (fVariableArrangement.size()==0) return fValues.size();
313  else return fVariableArrangement.size();
314 }
315 
316 ////////////////////////////////////////////////////////////////////////////////
317 /// accessor to the number of targets
318 
320 {
321  return fTargets.size();
322 }
323 
324 ////////////////////////////////////////////////////////////////////////////////
325 /// accessor to the number of spectators
326 
328 {
329  // if variables have to arranged (as it is the case for the
330  // composite classifier) the number of the variables changes
331 
332  if (fVariableArrangement.size()==0) return fSpectators.size();
333  else return fValues.size()-fVariableArrangement.size();
334 }
335 
336 
337 ////////////////////////////////////////////////////////////////////////////////
338 /// set variable ivar to val
339 
341 {
342  if ((fDynamic ?( (*fValuesDynamic).size() ) : fValues.size())<=ivar)
343  (fDynamic ?( (*fValuesDynamic).resize(ivar+1) ) : fValues.resize(ivar+1));
344 
345  (fDynamic ?( *(*fValuesDynamic)[ivar] ) : fValues[ivar])=val;
346 }
347 
348 ////////////////////////////////////////////////////////////////////////////////
349 /// print method
350 
351 void TMVA::Event::Print( std::ostream& o ) const
352 {
353  o << *this << std::endl;
354 }
355 
356 ////////////////////////////////////////////////////////////////////////////////
357 /// set the target value (dimension itgt) to value
358 
360 {
361  if (fTargets.size() <= itgt) fTargets.resize( itgt+1 );
362  fTargets.at(itgt) = value;
363 }
364 
365 ////////////////////////////////////////////////////////////////////////////////
366 /// set spectator value (dimension ivar) to value
367 
369 {
370  if (fSpectators.size() <= ivar) fSpectators.resize( ivar+1 );
371  fSpectators.at(ivar) = value;
372 }
373 
374 ////////////////////////////////////////////////////////////////////////////////
375 /// return the event weight - depending on whether the flag
376 /// *IgnoreNegWeightsInTraining* is or not. If it is set AND it is
377 /// used for training, then negative event weights are set to zero !
378 /// NOTE! For events used in Testing, the ORIGINAL possibly negative
379 /// event weight is used no matter what
380 
382 {
383  return (fgIgnoreNegWeightsInTraining && fgIsTraining && fWeight < 0) ? 0. : fWeight*fBoostWeight;
384 }
385 
386 ////////////////////////////////////////////////////////////////////////////////
387 /// when this static function is called, it sets the flag whether
388 /// events with negative event weight should be ignored in the
389 /// training, or not.
390 
392 {
393  fgIsTraining=b;
394 }
395 ////////////////////////////////////////////////////////////////////////////////
396 /// when this static function is called, it sets the flag whether
397 /// events with negative event weight should be ignored in the
398 /// training, or not.
399 
401 {
402  fgIgnoreNegWeightsInTraining=b;
403 }
404 
405 ////////////////////////////////////////////////////////////////////////////////
406 /// Outputs the data of an event
407 
408 std::ostream& TMVA::operator << ( std::ostream& os, const TMVA::Event& event )
409 {
410  os << "Variables [" << event.fValues.size() << "]:";
411  for (UInt_t ivar=0; ivar<event.fValues.size(); ++ivar)
412  os << " " << std::setw(10) << event.GetValue(ivar);
413  os << ", targets [" << event.fTargets.size() << "]:";
414  for (UInt_t ivar=0; ivar<event.fTargets.size(); ++ivar)
415  os << " " << std::setw(10) << event.GetTarget(ivar);
416  os << ", spectators ["<< event.fSpectators.size() << "]:";
417  for (UInt_t ivar=0; ivar<event.fSpectators.size(); ++ivar)
418  os << " " << std::setw(10) << event.GetSpectator(ivar);
419  os << ", weight: " << event.GetWeight();
420  os << ", class: " << event.GetClass();
421  return os;
422 }
UInt_t fClass
Definition: Event.h:143
auto * m
Definition: textangle.C:8
float Float_t
Definition: RtypesCore.h:55
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:61
static Bool_t fgIgnoreNegWeightsInTraining
Definition: Event.h:132
Double_t fBoostWeight
Definition: Event.h:145
Event()
copy constructor
Definition: Event.cxx:48
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:391
void SetVariableArrangement(std::vector< UInt_t > *const m) const
set the variable arrangement
Definition: Event.cxx:191
Double_t fWeight
Definition: Event.h:144
void SetVal(UInt_t ivar, Float_t val)
set variable ivar to val
Definition: Event.cxx:340
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
Definition: Event.cxx:381
UInt_t GetNTargets() const
accessor to the number of targets
Definition: Event.cxx:319
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:368
UInt_t GetNSpectators() const
accessor to the number of spectators
Definition: Event.cxx:327
static Bool_t fgIsTraining
Definition: Event.h:131
unsigned int UInt_t
Definition: RtypesCore.h:44
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:359
void Print(std::ostream &o) const
print method
Definition: Event.cxx:351
UInt_t GetNVariables() const
accessor to the number of variables
Definition: Event.cxx:308
const Bool_t kFALSE
Definition: RtypesCore.h:90
Float_t GetValue(UInt_t ivar) const
return value of i&#39;th variable
Definition: Event.cxx:236
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:400
void CopyVarValues(const Event &other)
copies only the variable values
Definition: Event.cxx:203
Mother of all ROOT objects.
Definition: TObject.h:37
~Event()
Event destructor.
Definition: Event.cxx:184
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:261