ROOT logo
// @(#)root/tmva $Id: Event.cxx 27320 2009-02-02 06:40:36Z brun $   
// Author: Andreas Hoecker, Joerg Stelzer, Helge Voss

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Class  : Event                                                                 *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      Implementation (see header for description)                               *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
 *      Joerg Stelzer   <Joerg.Stelzer@cern.ch>  - CERN, Switzerland              *
 *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
 *                                                                                *
 * Copyright (c) 2005:                                                            *
 *      CERN, Switzerland                                                         * 
 *      U. of Victoria, Canada                                                    * 
 *      MPI-K Heidelberg, Germany                                                 * 
 *      LAPP, Annecy, France                                                      *
 *                                                                                *
 * Redistribution and use in source and binary forms, with or without             *
 * modification, are permitted according to the terms listed in LICENSE           *
 * (http://mva.sourceforge.net/license.txt)                                       *
 **********************************************************************************/

//////////////////////////////////////////////////////////////////////////
//
// Event
//
// Storage class for an event. It is used by all TMVA methods
// during the training. Events are collected in Dataset
//
//////////////////////////////////////////////////////////////////////////

#include "TMVA/Event.h"
#include "TMVA/Tools.h"
#include "TTree.h"
#include "TBranch.h"
#include <iostream>
#include <iomanip>

 
Int_t TMVA::Event::fgCount = 0;

//____________________________________________________________
TMVA::Event::Event(const std::vector<VariableInfo>& varinfo, Bool_t AllowExternalLinks) 
   : fVariables(varinfo),
     fVarPtr(new void*[varinfo.size()]), // array to hold pointers to the integer or float array
     fVarPtrF(0),                        // array to hold all integer variables
     fType(0),
     fWeight(0),
     fBoostWeight(1.0),
     fCountI(0),
     fCountF(0)
{
   // constructor

   fgCount++; // static member: counts number of Event instances (check for memory leak)

   fCountF = fVariables.size();

   InitPointers(AllowExternalLinks);
}

TMVA::Event::Event( const Event& event ) 
   : fVariables(event.fVariables),
     fVarPtr(new void*[event.fVariables.size()]), // array to hold pointers to the integer or float array
     fVarPtrF(0),                                 // array to hold all float variables
     fType(event.fType),
     fWeight(event.fWeight),
     fBoostWeight(event.fBoostWeight),
     fCountI(event.fCountI),
     fCountF(event.fCountF)
{
   // copy constructor

   fgCount++; 
   InitPointers(kFALSE); // this constructor is used in the BinarySearchTree

   // where we don't want to have externaly linked variables
   for (UInt_t ivar=0; ivar<GetNVars(); ivar++) {
      *(Float_t*)fVarPtr[ivar] = *(Float_t*)event.fVarPtr[ivar];
   }
}

//____________________________________________________________
TMVA::Event::~Event() 
{
   // Event destructor
   delete[] fVarPtr;
   delete[] fVarPtrF;
   fgCount--;
}
 
void TMVA::Event::InitPointers(bool AllowExternalLink)
{
   // sets the links of fVarPtr to the internal arrays that hold the
   // integer and float variables

   fVarPtrF = new Float_t[fCountF];
   
   UInt_t ivar(0), ivarF(0);
   std::vector<VariableInfo>::const_iterator varIt = fVariables.begin();

   // for each variable,
   for (; varIt != fVariables.end(); varIt++, ivar++) {
      const VariableInfo& var = *varIt;
      // set the void pointer (which are used to access the data) to the proper field
      // if external field is given
      if (AllowExternalLink && var.GetExternalLink()!=0) {
         fVarPtr[ivar] = var.GetExternalLink();
         // or if its type is I(int) or F(float)
      } 
      else {
         // set the void pointer to the float field
         fVarPtr[ivar] = fVarPtrF+ivarF++;
      } 
   }
}

//____________________________________________________________
void TMVA::Event::SetBranchAddresses(TTree *tr)
{
   // sets the branch addresses of the associated
   // tree to the local memory as given by fVarPtr

   fBranches.clear();
   Int_t ivar(0);
   TBranch * br(0);
   std::vector<VariableInfo>::const_iterator varIt;
   for (varIt = fVariables.begin(); varIt != fVariables.end(); varIt++) {
      const VariableInfo& var = *varIt;
      br = tr->GetBranch(var.GetInternalVarName());
      br->SetAddress(fVarPtr[ivar++]);
      fBranches.push_back(br);
   }
   br = tr->GetBranch("type");        br->SetAddress(&fType);        fBranches.push_back(br);
   br = tr->GetBranch("weight");      br->SetAddress(&fWeight);      fBranches.push_back(br);
   br = tr->GetBranch("boostweight"); br->SetAddress(&fBoostWeight); fBranches.push_back(br);
}

//____________________________________________________________
void TMVA::Event::CopyVarValues( const Event& other )
{
   // copies only the variable values
   for (UInt_t ivar=0; ivar<GetNVars(); ivar++)
      SetVal(ivar, other.GetVal(ivar));
   SetType(other.Type());
   SetWeight(other.GetWeight());
   SetBoostWeight(other.GetBoostWeight());
}

//____________________________________________________________
void TMVA::Event::SetVal(UInt_t ivar, Float_t val) 
{
   // set variable ivar to val
   Bool_t isInternallyLinked = (fVarPtr[ivar] >= fVarPtrF && fVarPtr[ivar] < fVarPtrF+fCountF);

   if(isInternallyLinked) {
      fVarPtrF[ivar] = val;
   } else { // external variable, have to go with type
      if(fVariables[ivar].GetVarType()=='F') {
         *(Float_t*)fVarPtr[ivar] = val;
      } else {
         *(Int_t*)fVarPtr[ivar] = (Int_t)val;
      }
   }
}

//____________________________________________________________
Float_t TMVA::Event::GetVal(UInt_t ivar) const
{
   // return value of variable ivar
   Bool_t isInternallyLinked = (fVarPtr[ivar] >= fVarPtrF && fVarPtr[ivar] < fVarPtrF+fCountF);
   if(isInternallyLinked) {
      return fVarPtrF[ivar];
   } else { // external variable, have to go with type
      if(fVariables[ivar].GetVarType()=='F') {
         return *(Float_t*)fVarPtr[ivar];
      } else {
         return *(Int_t*)fVarPtr[ivar];
      }
   }
}

//____________________________________________________________
Float_t TMVA::Event::GetValueNormalized(UInt_t ivar) const 
{
   // returns the value of variable ivar, normalized to [-1,1]
   return gTools().NormVariable(GetVal(ivar),fVariables[ivar].GetMin(),fVariables[ivar].GetMax());
}

//____________________________________________________________
void TMVA::Event::Print(std::ostream& o) const
{
   // print method
   o << fVariables.size() << " variables: ";
   for (UInt_t ivar=0; ivar<fVariables.size(); ivar++)
      o << " " << std::setw(10) << GetVal(ivar) << '(' << fVariables[ivar].GetVarType() << ')';
   o << "  weight = " << GetWeight();
   o << std::setw(10) << (IsSignal()?" signal":" background");
   o << std::endl;
}

//_______________________________________________________________________
ostream& TMVA::operator<<(ostream& os, const TMVA::Event& event)
{ 
   // Outputs the data of an event
   
   event.Print(os);
   return os;
}

//_______________________________________________________________________
ostream& TMVA::operator<<(ostream& os, const TMVA::Event* event)
{
   // Outputs the data of an event
   return os << *event;
}
 Event.cxx:1
 Event.cxx:2
 Event.cxx:3
 Event.cxx:4
 Event.cxx:5
 Event.cxx:6
 Event.cxx:7
 Event.cxx:8
 Event.cxx:9
 Event.cxx:10
 Event.cxx:11
 Event.cxx:12
 Event.cxx:13
 Event.cxx:14
 Event.cxx:15
 Event.cxx:16
 Event.cxx:17
 Event.cxx:18
 Event.cxx:19
 Event.cxx:20
 Event.cxx:21
 Event.cxx:22
 Event.cxx:23
 Event.cxx:24
 Event.cxx:25
 Event.cxx:26
 Event.cxx:27
 Event.cxx:28
 Event.cxx:29
 Event.cxx:30
 Event.cxx:31
 Event.cxx:32
 Event.cxx:33
 Event.cxx:34
 Event.cxx:35
 Event.cxx:36
 Event.cxx:37
 Event.cxx:38
 Event.cxx:39
 Event.cxx:40
 Event.cxx:41
 Event.cxx:42
 Event.cxx:43
 Event.cxx:44
 Event.cxx:45
 Event.cxx:46
 Event.cxx:47
 Event.cxx:48
 Event.cxx:49
 Event.cxx:50
 Event.cxx:51
 Event.cxx:52
 Event.cxx:53
 Event.cxx:54
 Event.cxx:55
 Event.cxx:56
 Event.cxx:57
 Event.cxx:58
 Event.cxx:59
 Event.cxx:60
 Event.cxx:61
 Event.cxx:62
 Event.cxx:63
 Event.cxx:64
 Event.cxx:65
 Event.cxx:66
 Event.cxx:67
 Event.cxx:68
 Event.cxx:69
 Event.cxx:70
 Event.cxx:71
 Event.cxx:72
 Event.cxx:73
 Event.cxx:74
 Event.cxx:75
 Event.cxx:76
 Event.cxx:77
 Event.cxx:78
 Event.cxx:79
 Event.cxx:80
 Event.cxx:81
 Event.cxx:82
 Event.cxx:83
 Event.cxx:84
 Event.cxx:85
 Event.cxx:86
 Event.cxx:87
 Event.cxx:88
 Event.cxx:89
 Event.cxx:90
 Event.cxx:91
 Event.cxx:92
 Event.cxx:93
 Event.cxx:94
 Event.cxx:95
 Event.cxx:96
 Event.cxx:97
 Event.cxx:98
 Event.cxx:99
 Event.cxx:100
 Event.cxx:101
 Event.cxx:102
 Event.cxx:103
 Event.cxx:104
 Event.cxx:105
 Event.cxx:106
 Event.cxx:107
 Event.cxx:108
 Event.cxx:109
 Event.cxx:110
 Event.cxx:111
 Event.cxx:112
 Event.cxx:113
 Event.cxx:114
 Event.cxx:115
 Event.cxx:116
 Event.cxx:117
 Event.cxx:118
 Event.cxx:119
 Event.cxx:120
 Event.cxx:121
 Event.cxx:122
 Event.cxx:123
 Event.cxx:124
 Event.cxx:125
 Event.cxx:126
 Event.cxx:127
 Event.cxx:128
 Event.cxx:129
 Event.cxx:130
 Event.cxx:131
 Event.cxx:132
 Event.cxx:133
 Event.cxx:134
 Event.cxx:135
 Event.cxx:136
 Event.cxx:137
 Event.cxx:138
 Event.cxx:139
 Event.cxx:140
 Event.cxx:141
 Event.cxx:142
 Event.cxx:143
 Event.cxx:144
 Event.cxx:145
 Event.cxx:146
 Event.cxx:147
 Event.cxx:148
 Event.cxx:149
 Event.cxx:150
 Event.cxx:151
 Event.cxx:152
 Event.cxx:153
 Event.cxx:154
 Event.cxx:155
 Event.cxx:156
 Event.cxx:157
 Event.cxx:158
 Event.cxx:159
 Event.cxx:160
 Event.cxx:161
 Event.cxx:162
 Event.cxx:163
 Event.cxx:164
 Event.cxx:165
 Event.cxx:166
 Event.cxx:167
 Event.cxx:168
 Event.cxx:169
 Event.cxx:170
 Event.cxx:171
 Event.cxx:172
 Event.cxx:173
 Event.cxx:174
 Event.cxx:175
 Event.cxx:176
 Event.cxx:177
 Event.cxx:178
 Event.cxx:179
 Event.cxx:180
 Event.cxx:181
 Event.cxx:182
 Event.cxx:183
 Event.cxx:184
 Event.cxx:185
 Event.cxx:186
 Event.cxx:187
 Event.cxx:188
 Event.cxx:189
 Event.cxx:190
 Event.cxx:191
 Event.cxx:192
 Event.cxx:193
 Event.cxx:194
 Event.cxx:195
 Event.cxx:196
 Event.cxx:197
 Event.cxx:198
 Event.cxx:199
 Event.cxx:200
 Event.cxx:201
 Event.cxx:202
 Event.cxx:203
 Event.cxx:204
 Event.cxx:205
 Event.cxx:206
 Event.cxx:207
 Event.cxx:208
 Event.cxx:209
 Event.cxx:210
 Event.cxx:211
 Event.cxx:212
 Event.cxx:213
 Event.cxx:214
 Event.cxx:215
 Event.cxx:216
 Event.cxx:217
 Event.cxx:218
 Event.cxx:219
 Event.cxx:220
 Event.cxx:221
 Event.cxx:222