#include "TMVA/Event.h"
#include "TMVA/Tools.h"
#include <iostream>
#include "assert.h"
#include <iomanip>
#include <cassert>
#include "TCut.h"
Bool_t TMVA::Event::fgIsTraining = kFALSE;
Bool_t TMVA::Event::fgIgnoreNegWeightsInTraining = kFALSE;
TMVA::Event::Event()
: fValues(),
fValuesDynamic(0),
fTargets(),
fSpectators(),
fVariableArrangement(0),
fClass(0),
fWeight(1.0),
fBoostWeight(1.0),
fDynamic(kFALSE),
fDoNotBoost(kFALSE)
{
}
TMVA::Event::Event( const std::vector<Float_t>& ev,
const std::vector<Float_t>& tg,
UInt_t cls,
Double_t weight,
Double_t boostweight )
: fValues(ev),
fValuesDynamic(0),
fTargets(tg),
fSpectators(0),
fVariableArrangement(0),
fClass(cls),
fWeight(weight),
fBoostWeight(boostweight),
fDynamic(kFALSE),
fDoNotBoost(kFALSE)
{
}
TMVA::Event::Event( const std::vector<Float_t>& ev,
const std::vector<Float_t>& tg,
const std::vector<Float_t>& vi,
UInt_t cls,
Double_t weight,
Double_t boostweight )
: fValues(ev),
fValuesDynamic(0),
fTargets(tg),
fSpectators(vi),
fVariableArrangement(0),
fClass(cls),
fWeight(weight),
fBoostWeight(boostweight),
fDynamic(kFALSE),
fDoNotBoost(kFALSE)
{
}
TMVA::Event::Event( const std::vector<Float_t>& ev,
UInt_t cls,
Double_t weight,
Double_t boostweight )
: fValues(ev),
fValuesDynamic(0),
fTargets(0),
fSpectators(0),
fVariableArrangement(0),
fClass(cls),
fWeight(weight),
fBoostWeight(boostweight),
fDynamic(kFALSE),
fDoNotBoost(kFALSE)
{
}
TMVA::Event::Event( const std::vector<Float_t*>*& evdyn, UInt_t nvar )
: fValues(nvar),
fValuesDynamic(0),
fTargets(0),
fSpectators(evdyn->size()-nvar),
fVariableArrangement(0),
fClass(0),
fWeight(0),
fBoostWeight(0),
fDynamic(true),
fDoNotBoost(kFALSE)
{
fValuesDynamic = (std::vector<Float_t*>*) evdyn;
}
TMVA::Event::Event( const Event& event )
: fValues(event.fValues),
fValuesDynamic(event.fValuesDynamic),
fTargets(event.fTargets),
fSpectators(event.fSpectators),
fVariableArrangement(event.fVariableArrangement),
fClass(event.fClass),
fWeight(event.fWeight),
fBoostWeight(event.fBoostWeight),
fDynamic(event.fDynamic),
fDoNotBoost(kFALSE)
{
if (event.fDynamic){
fValues.clear();
UInt_t nvar = event.GetNVariables();
UInt_t idx=0;
std::vector<Float_t*>::iterator itDyn=event.fValuesDynamic->begin(), itDynEnd=event.fValuesDynamic->end();
for (; itDyn!=itDynEnd && idx<nvar; ++itDyn){
Float_t value=*(*itDyn);
fValues.push_back( value );
++idx;
}
fSpectators.clear();
for (; itDyn!=itDynEnd; ++itDyn){
Float_t value=*(*itDyn);
fSpectators.push_back( value );
++idx;
}
fDynamic=kFALSE;
fValuesDynamic=NULL;
}
}
TMVA::Event::~Event()
{
}
void TMVA::Event::SetVariableArrangement( std::vector<UInt_t>* const m ) const {
fVariableArrangement = m;
}
void TMVA::Event::CopyVarValues( const Event& other )
{
fValues = other.fValues;
fTargets = other.fTargets;
fSpectators = other.fSpectators;
if (other.fDynamic){
UInt_t nvar = other.GetNVariables();
fValues.clear();
UInt_t idx=0;
std::vector<Float_t*>::iterator itDyn=other.fValuesDynamic->begin(), itDynEnd=other.fValuesDynamic->end();
for (; itDyn!=itDynEnd && idx<nvar; ++itDyn){
Float_t value=*(*itDyn);
fValues.push_back( value );
++idx;
}
fSpectators.clear();
for (; itDyn!=itDynEnd; ++itDyn){
Float_t value=*(*itDyn);
fSpectators.push_back( value );
++idx;
}
}
fDynamic = kFALSE;
fValuesDynamic = NULL;
fClass = other.fClass;
fWeight = other.fWeight;
fBoostWeight = other.fBoostWeight;
}
Float_t TMVA::Event::GetValue( UInt_t ivar ) const
{
Float_t retval;
if (fVariableArrangement==0) {
retval = fDynamic ? ( *((*fValuesDynamic).at(ivar)) ) : fValues.at(ivar);
}
else {
UInt_t mapIdx = (*fVariableArrangement)[ivar];
if (fDynamic){
retval = *((*fValuesDynamic).at(mapIdx));
}
else{
retval = ( mapIdx<fValues.size() ) ? fValues[mapIdx] : fSpectators[mapIdx-fValues.size()];
}
}
return retval;
}
Float_t TMVA::Event::GetSpectator( UInt_t ivar) const
{
if (fDynamic) return *(fValuesDynamic->at(GetNVariables()+ivar));
else return fSpectators.at(ivar);
}
const std::vector<Float_t>& TMVA::Event::GetValues() const
{
if (fVariableArrangement==0) {
if (fDynamic) {
fValues.clear();
for (std::vector<Float_t*>::const_iterator it = fValuesDynamic->begin(), itEnd=fValuesDynamic->end()-GetNSpectators();
it != itEnd; ++it) {
Float_t val = *(*it);
fValues.push_back( val );
}
}
}else{
UInt_t mapIdx;
if (fDynamic) {
fValues.clear();
for (UInt_t i=0; i< fVariableArrangement->size(); i++){
mapIdx = (*fVariableArrangement)[i];
fValues.push_back(*((*fValuesDynamic).at(mapIdx)));
}
} else {
fValuesRearranged.clear();
for (UInt_t i=0; i< fVariableArrangement->size(); i++){
mapIdx = (*fVariableArrangement)[i];
fValuesRearranged.push_back(fValues.at(mapIdx));
}
return fValuesRearranged;
}
}
return fValues;
}
UInt_t TMVA::Event::GetNVariables() const
{
if (fVariableArrangement==0) return fValues.size();
else return fVariableArrangement->size();
}
UInt_t TMVA::Event::GetNTargets() const
{
return fTargets.size();
}
UInt_t TMVA::Event::GetNSpectators() const
{
if (fVariableArrangement==0) return fSpectators.size();
else return fValues.size()-fVariableArrangement->size();
}
void TMVA::Event::SetVal( UInt_t ivar, Float_t val )
{
if ((fDynamic ?( (*fValuesDynamic).size() ) : fValues.size())<=ivar)
(fDynamic ?( (*fValuesDynamic).resize(ivar+1) ) : fValues.resize(ivar+1));
(fDynamic ?( *(*fValuesDynamic)[ivar] ) : fValues[ivar])=val;
}
void TMVA::Event::Print( std::ostream& o ) const
{
o << *this << std::endl;
}
void TMVA::Event::SetTarget( UInt_t itgt, Float_t value )
{
if (fTargets.size() <= itgt) fTargets.resize( itgt+1 );
fTargets.at(itgt) = value;
}
void TMVA::Event::SetSpectator( UInt_t ivar, Float_t value )
{
if (fSpectators.size() <= ivar) fSpectators.resize( ivar+1 );
fSpectators.at(ivar) = value;
}
Double_t TMVA::Event::GetWeight() const
{
return (fgIgnoreNegWeightsInTraining && fgIsTraining && fWeight < 0) ? 0. : fWeight*fBoostWeight;
}
void TMVA::Event::SetIsTraining(Bool_t b)
{
fgIsTraining=b;
}
void TMVA::Event::SetIgnoreNegWeightsInTraining(Bool_t b)
{
fgIgnoreNegWeightsInTraining=b;
}
std::ostream& TMVA::operator << ( std::ostream& os, const TMVA::Event& event )
{
os << "Variables [" << event.fValues.size() << "]:";
for (UInt_t ivar=0; ivar<event.fValues.size(); ++ivar)
os << " " << std::setw(10) << event.GetValue(ivar);
os << ", targets [" << event.fTargets.size() << "]:";
for (UInt_t ivar=0; ivar<event.fTargets.size(); ++ivar)
os << " " << std::setw(10) << event.GetTarget(ivar);
os << ", spectators ["<< event.fSpectators.size() << "]:";
for (UInt_t ivar=0; ivar<event.fSpectators.size(); ++ivar)
os << " " << std::setw(10) << event.GetSpectator(ivar);
os << ", weight: " << event.GetWeight();
os << ", class: " << event.GetClass();
return os;
}