Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
VariableTransformBase.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Peter Speckmayer,Joerg Stelzer, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : VariableTransformBase *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Pre-transformation of input variables (base class) *
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  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
18  * *
19  * Copyright (c) 2005: *
20  * CERN, Switzerland *
21  * U. of Victoria, Canada *
22  * MPI-K Heidelberg, Germany *
23  * *
24  * Redistribution and use in source and binary forms, with or without *
25  * modification, are permitted according to the terms listed in LICENSE *
26  * (http://tmva.sourceforge.net/LICENSE) *
27  **********************************************************************************/
28 
29 #ifndef ROOT_TMVA_VariableTransformBase
30 #define ROOT_TMVA_VariableTransformBase
31 
32 //////////////////////////////////////////////////////////////////////////
33 // //
34 // VariableTransformBase //
35 // //
36 // Linear interpolation class //
37 // //
38 //////////////////////////////////////////////////////////////////////////
39 
40 #include <vector>
41 
42 #include "TH1.h"
43 #include "TDirectory.h"
44 #include "TString.h"
45 
46 #include "TMVA/Types.h"
47 #include "TMVA/Event.h"
48 #include "TMVA/VariableInfo.h"
49 #include "TMVA/DataSetInfo.h"
50 
51 namespace TMVA {
52 
53  class VariableTransformBase : public TObject {
54 
55  public:
56 
57  typedef std::vector<std::pair<Char_t,UInt_t> > VectorOfCharAndInt;
58  typedef VectorOfCharAndInt::iterator ItVarTypeIdx;
59  typedef VectorOfCharAndInt::const_iterator ItVarTypeIdxConst;
60 
61  VariableTransformBase( DataSetInfo& dsi, Types::EVariableTransform tf, const TString& trfName );
62  virtual ~VariableTransformBase( void );
63 
64  virtual void Initialize() = 0;
65  virtual Bool_t PrepareTransformation (const std::vector<Event*>& ) = 0;
66  virtual const Event* Transform ( const Event* const, Int_t cls ) const = 0;
67  virtual const Event* InverseTransform( const Event* const, Int_t cls ) const = 0;
68 
69  // accessors
70  void SetEnabled ( Bool_t e ) { fEnabled = e; }
71  void SetNormalise( Bool_t n ) { fNormalise = n; }
72  Bool_t IsEnabled() const { return fEnabled; }
73  Bool_t IsCreated() const { return fCreated; }
74  Bool_t IsNormalised() const { return fNormalise; }
75 
76  // variable selection
77  virtual void SelectInput( const TString& inputVariables, Bool_t putIntoVariables = kFALSE );
78  virtual Bool_t GetInput ( const Event* event, std::vector<Float_t>& input, std::vector<Char_t>& mask, Bool_t backTransform = kFALSE ) const;
79  virtual void SetOutput( Event* event, std::vector<Float_t>& output, std::vector<Char_t>& mask, const Event* oldEvent = 0, Bool_t backTransform = kFALSE ) const;
80  virtual void CountVariableTypes( UInt_t& nvars, UInt_t& ntgts, UInt_t& nspcts ) const;
81 
82  void ToggleInputSortOrder( Bool_t sortOrder ) { fSortGet = sortOrder; }
83  void SetOutputDataSetInfo( DataSetInfo* outputDsi ) { fDsiOutput = outputDsi; }
84 
85 
86 
87  void SetUseSignalTransform( Bool_t e=kTRUE) { fUseSignalTransform = e; }
88  Bool_t UseSignalTransform() const { return fUseSignalTransform; }
89 
90  virtual const char* GetName() const { return fTransformName.Data(); }
91  TString GetShortName() const { TString a(fTransformName); a.ReplaceAll("Transform",""); return a; }
92 
93  virtual void WriteTransformationToStream ( std::ostream& o ) const = 0;
94  virtual void ReadTransformationFromStream( std::istream& istr, const TString& classname="" ) = 0;
95 
96  virtual void AttachXMLTo(void* parent) = 0;
97  virtual void ReadFromXML( void* trfnode ) = 0;
98 
99  Types::EVariableTransform GetVariableTransform() const { return fVariableTransform; }
100 
101  // writer of function code
102  virtual void MakeFunction( std::ostream& fout, const TString& fncName, Int_t part,
103  UInt_t trCounter, Int_t cls ) = 0;
104 
105  // provides string vector giving explicit transformation
106  virtual std::vector<TString>* GetTransformationStrings( Int_t cls ) const;
107  virtual void PrintTransformation( std::ostream & ) {}
108 
109  const std::vector<TMVA::VariableInfo>& Variables() const { return fVariables; }
110  const std::vector<TMVA::VariableInfo>& Targets() const { return fTargets; }
111  const std::vector<TMVA::VariableInfo>& Spectators() const { return fSpectators; }
112 
113  MsgLogger& Log() const { return *fLogger; }
114 
115  void SetTMVAVersion(TMVAVersion_t v) { fTMVAVersion = v; }
116 
117  protected:
118 
119  void CalcNorm( const std::vector<const Event*>& );
120 
121  void SetCreated( Bool_t c = kTRUE ) { fCreated = c; }
122  void SetNVariables( UInt_t i ) { fNVars = i; }
123  void SetName( const TString& c ) { fTransformName = c; }
124 
125  UInt_t GetNVariables() const { return fDsi.GetNVariables(); }
126  UInt_t GetNTargets() const { return fDsi.GetNTargets(); }
127  UInt_t GetNSpectators() const { return fDsi.GetNSpectators(); }
128 
129  DataSetInfo& fDsi;
130  DataSetInfo* fDsiOutput;
131 
132  std::vector<TMVA::VariableInfo>& Variables() { return fVariables; }
133  std::vector<TMVA::VariableInfo>& Targets() { return fTargets; }
134  std::vector<TMVA::VariableInfo>& Spectators() { return fSpectators; }
135  Int_t GetNClasses() const { return fDsi.GetNClasses(); }
136 
137 
138  mutable Event* fTransformedEvent; // holds the current transformed event
139  mutable Event* fBackTransformedEvent; // holds the current back-transformed event
140 
141  // variable selection
142  VectorOfCharAndInt fGet; // get variables/targets/spectators
143  VectorOfCharAndInt fPut; // put variables/targets/spectators
144 
145  private:
146 
147  Types::EVariableTransform fVariableTransform; // Decorrelation, PCA, etc.
148 
149  void UpdateNorm( Int_t ivar, Double_t x );
150 
151  Bool_t fUseSignalTransform; // true if transformation bases on signal data
152  Bool_t fEnabled; // has been enabled
153  Bool_t fCreated; // has been created
154  Bool_t fNormalise; // normalise input variables
155  UInt_t fNVars; // number of variables
156  TString fTransformName; // name of transformation
157  std::vector<TMVA::VariableInfo> fVariables; // event variables [saved to weight file]
158  std::vector<TMVA::VariableInfo> fTargets; // event targets [saved to weight file --> TODO ]
159  std::vector<TMVA::VariableInfo> fSpectators; // event spectators [saved to weight file --> TODO ]
160 
161  mutable Bool_t fVariableTypesAreCounted; // true if variable types have been counted already
162  mutable UInt_t fNVariables; // number of variables to be transformed
163  mutable UInt_t fNTargets; // number of targets to be transformed
164  mutable UInt_t fNSpectators; // number of spectators to be transformed
165 
166  Bool_t fSortGet; // if true, sort the variables into the order as defined by the user at the var definition
167  // if false, sort the variables according to the order given for the var transformation
168 
169  protected:
170 
171  TMVAVersion_t fTMVAVersion;
172 
173  mutable MsgLogger* fLogger; //! message logger
174 
175  ClassDef(VariableTransformBase,0); // Base class for variable transformations
176  };
177 
178 } // namespace TMVA
179 
180 #endif