Event.cxx

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: Event.cxx 37986 2011-02-04 21:42:15Z pcanal $   
00002 // Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss
00003 
00004 /**********************************************************************************
00005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
00006  * Package: TMVA                                                                  *
00007  * Class  : Event                                                                 *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      Implementation (see header for description)                               *
00012  *                                                                                *
00013  * Authors (alphabetical):                                                        *
00014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
00015  *      Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland           *
00016  *      Joerg Stelzer   <Joerg.Stelzer@cern.ch>  - CERN, Switzerland              *
00017  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
00018  *                                                                                *
00019  * Copyright (c) 2005:                                                            *
00020  *      CERN, Switzerland                                                         * 
00021  *      U. of Victoria, Canada                                                    * 
00022  *      MPI-K Heidelberg, Germany                                                 * 
00023  *      LAPP, Annecy, France                                                      *
00024  *                                                                                *
00025  * Redistribution and use in source and binary forms, with or without             *
00026  * modification, are permitted according to the terms listed in LICENSE           *
00027  * (http://mva.sourceforge.net/license.txt)                                       *
00028  **********************************************************************************/
00029 
00030 #include "TMVA/Event.h"
00031 #include "TMVA/Tools.h"
00032 #include <iostream>
00033 #include "assert.h"
00034 #include <iomanip>
00035 #include <cassert>
00036 #include "TCut.h"
00037 
00038 //____________________________________________________________
00039 TMVA::Event::Event()
00040    : fValues(),
00041      fValuesDynamic(0),
00042      fTargets(),
00043      fSpectators(),
00044      fVariableArrangement(0),
00045      fClass(0),
00046      fWeight(1.0),
00047      fBoostWeight(1.0),
00048      fDynamic(kFALSE)
00049 {
00050    // copy constructor
00051 }
00052 
00053 //____________________________________________________________
00054 TMVA::Event::Event( const std::vector<Float_t>& ev,
00055                     const std::vector<Float_t>& tg,
00056                     UInt_t cls,
00057                     Double_t weight,
00058                     Double_t boostweight )
00059    : fValues(ev),
00060      fValuesDynamic(0),
00061      fTargets(tg),
00062      fSpectators(0),
00063      fVariableArrangement(0),
00064      fClass(cls),
00065      fWeight(weight),
00066      fBoostWeight(boostweight),
00067      fDynamic(kFALSE)
00068 {
00069    // constructor
00070 }
00071 
00072 //____________________________________________________________
00073 TMVA::Event::Event( const std::vector<Float_t>& ev,
00074                     const std::vector<Float_t>& tg,
00075                     const std::vector<Float_t>& vi,
00076                     UInt_t cls,
00077                     Double_t weight,
00078                     Double_t boostweight )
00079    : fValues(ev),
00080      fValuesDynamic(0),
00081      fTargets(tg),
00082      fSpectators(vi),
00083      fVariableArrangement(0),
00084      fClass(cls),
00085      fWeight(weight),
00086      fBoostWeight(boostweight),
00087      fDynamic(kFALSE)
00088 {
00089    // constructor
00090 }
00091 
00092 //____________________________________________________________
00093 TMVA::Event::Event( const std::vector<Float_t>& ev,
00094                     UInt_t cls,
00095                     Double_t weight,
00096                     Double_t boostweight )
00097    : fValues(ev),
00098      fValuesDynamic(0),
00099      fTargets(0),
00100      fSpectators(0),
00101      fVariableArrangement(0),
00102      fClass(cls),
00103      fWeight(weight),
00104      fBoostWeight(boostweight),
00105      fDynamic(kFALSE)
00106 {
00107    // constructor
00108 }
00109 
00110 //____________________________________________________________
00111 TMVA::Event::Event( const std::vector<Float_t*>*& evdyn, UInt_t nvar )
00112    : fValues(nvar),
00113      fValuesDynamic(0),
00114      fTargets(0),
00115      fSpectators(evdyn->size()-nvar),
00116      fVariableArrangement(0),
00117      fClass(0),
00118      fWeight(0),
00119      fBoostWeight(0),
00120      fDynamic(true)
00121 {
00122 
00123    //std::cout << "CON 2 " << evdyn->size() << std::endl;
00124    // constructor for single events
00125    fValuesDynamic = (std::vector<Float_t*>*) evdyn;
00126 }
00127 
00128 //____________________________________________________________
00129 TMVA::Event::Event( const Event& event ) 
00130    : fValues(event.fValues),
00131      fValuesDynamic(0),
00132      fTargets(event.fTargets),
00133      fSpectators(event.fSpectators),
00134      fVariableArrangement(event.fVariableArrangement),
00135      fClass(event.fClass),
00136      fWeight(event.fWeight),
00137      fBoostWeight(event.fBoostWeight),
00138      fDynamic(event.fDynamic)
00139 {
00140    // copy constructor
00141 }
00142 
00143 //____________________________________________________________
00144 TMVA::Event::~Event()
00145 {
00146    // Event destructor
00147 }
00148 
00149 //____________________________________________________________
00150 void TMVA::Event::ClearDynamicVariables()
00151 {
00152    // clear global variable
00153 //    if (fValuesDynamic != 0) { 
00154 //       fValuesDynamic->clear();
00155 //       delete fValuesDynamic;
00156 //       fValuesDynamic = 0;
00157 //    }
00158 }
00159 
00160 //____________________________________________________________
00161 void TMVA::Event::SetVariableArrangement( std::vector<UInt_t>* const m ) const {
00162    // set the variable arrangement
00163 
00164    // mapping from global variable index (the position in the vector)
00165    // to the new index in the subset of variables used by the
00166    // composite classifier
00167    fVariableArrangement = m;
00168 }
00169 
00170 
00171 
00172 //____________________________________________________________
00173 void TMVA::Event::CopyVarValues( const Event& other )
00174 {
00175    // copies only the variable values
00176    fValues      = other.fValues;
00177    fClass       = other.fClass;
00178    fWeight      = other.fWeight;
00179    fBoostWeight = other.fBoostWeight;
00180 }
00181 
00182 //____________________________________________________________
00183 Float_t TMVA::Event::GetValue( UInt_t ivar ) const
00184 {
00185    // return value of i'th variable
00186    Float_t retval;
00187    if (fVariableArrangement==0) {
00188       //if(fDynamic)
00189       //   std::cout << fValuesDynamic->size() << "   index = " << ivar << std::endl;
00190       retval = fDynamic ?( *(*fValuesDynamic)[ivar] ) : fValues[ivar];
00191    }
00192    else {
00193       UInt_t mapIdx = (*fVariableArrangement)[ivar];
00194       if (fDynamic) {
00195          retval = *(*fValuesDynamic)[mapIdx];
00196       }
00197       else {
00198          retval = ( mapIdx<fValues.size() ) ? fValues[mapIdx] : fSpectators[mapIdx-fValues.size()];
00199       }
00200    }
00201    return retval;
00202 }
00203 
00204 //____________________________________________________________
00205 Float_t TMVA::Event::GetSpectator( UInt_t ivar) const 
00206 {
00207    // return spectator content
00208    if (fDynamic) return *(fValuesDynamic->at(GetNVariables()+ivar));
00209    else          return fSpectators.at(ivar);
00210 }
00211 
00212 //____________________________________________________________
00213 const std::vector<Float_t>& TMVA::Event::GetValues() const
00214 {
00215    // return value vector
00216    if (fVariableArrangement!=0) {
00217       assert(0);
00218    }
00219    if (fDynamic) {
00220 //       if (fValuesDynamic->size()-GetNSpectators() != fValues.size()) {
00221 //          std::cout << "ERROR Event::GetValues() is trying to change the size of the variable vector, exiting ..." << std::endl;
00222 //          assert(0);
00223 //       }
00224       fValues.clear();
00225       for (std::vector<Float_t*>::const_iterator it = fValuesDynamic->begin(); 
00226            it != fValuesDynamic->end()-GetNSpectators(); it++) { 
00227          Float_t val = *(*it); 
00228          fValues.push_back( val ); 
00229       }
00230    }
00231    return fValues;
00232 }
00233 
00234 //____________________________________________________________
00235 UInt_t TMVA::Event::GetNVariables() const 
00236 {
00237    // accessor to the number of variables 
00238 
00239    // if variables have to arranged (as it is the case for the
00240    // composite classifier) the number of the variables changes
00241 
00242    if (fVariableArrangement==0) return fValues.size();
00243    else                         return fVariableArrangement->size();
00244 }
00245 
00246 //____________________________________________________________
00247 UInt_t TMVA::Event::GetNTargets() const 
00248 {
00249    // accessor to the number of targets
00250    return fTargets.size();
00251 }
00252 
00253 //____________________________________________________________
00254 UInt_t TMVA::Event::GetNSpectators() const 
00255 {
00256    // accessor to the number of spectators 
00257 
00258    // if variables have to arranged (as it is the case for the
00259    // composite classifier) the number of the variables changes
00260 
00261    if (fVariableArrangement==0) return fSpectators.size();
00262    else                         return fValues.size()-fVariableArrangement->size();
00263 }
00264 
00265 
00266 //____________________________________________________________
00267 void TMVA::Event::SetVal( UInt_t ivar, Float_t val ) 
00268 {
00269    // set variable ivar to val
00270    if ((fDynamic ?( (*fValuesDynamic).size() ) : fValues.size())<=ivar)
00271       (fDynamic ?( (*fValuesDynamic).resize(ivar+1) ) : fValues.resize(ivar+1));
00272 
00273    (fDynamic ?( *(*fValuesDynamic)[ivar] ) : fValues[ivar])=val;
00274 }
00275 
00276 //____________________________________________________________
00277 void TMVA::Event::Print( std::ostream& o ) const
00278 {
00279    // print method
00280    o << *this << std::endl;
00281 }
00282 
00283 //_____________________________________________________________
00284 void TMVA::Event::SetTarget( UInt_t itgt, Float_t value ) 
00285 { 
00286    // set the target value (dimension itgt) to value
00287 
00288    if (fTargets.size() <= itgt) fTargets.resize( itgt+1 );
00289    fTargets.at(itgt) = value;
00290 }
00291 
00292 //_____________________________________________________________
00293 void TMVA::Event::SetSpectator( UInt_t ivar, Float_t value ) 
00294 { 
00295    // set spectator value (dimension ivar) to value
00296 
00297    if (fSpectators.size() <= ivar) fSpectators.resize( ivar+1 );
00298    fSpectators.at(ivar) = value;
00299 }
00300 
00301 //_______________________________________________________________________
00302 ostream& TMVA::operator << ( ostream& os, const TMVA::Event& event )
00303 { 
00304    // Outputs the data of an event
00305    os << "Variables [" << event.fValues.size() << "]:";
00306    for (UInt_t ivar=0; ivar<event.fValues.size(); ++ivar)
00307       os << " " << std::setw(10) << event.GetValue(ivar);
00308    os << ", targets [" << event.fTargets.size() << "]:";
00309    for (UInt_t ivar=0; ivar<event.fTargets.size(); ++ivar)
00310       os << " " << std::setw(10) << event.GetTarget(ivar);
00311    os << ", spectators ["<< event.fSpectators.size() << "]:";
00312    for (UInt_t ivar=0; ivar<event.fSpectators.size(); ++ivar)
00313       os << " " << std::setw(10) << event.GetSpectator(ivar);
00314    os << ", weight: " << event.GetWeight();
00315    os << ", class: " << event.GetClass();
00316    return os;
00317 }

Generated on Tue Jul 5 15:14:49 2011 for ROOT_528-00b_version by  doxygen 1.5.1